Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dqneo/babygo
babygo👶 is a small Go compiler made from scratch, which can compile itself. It's going to be the smallest and simplest go compiler in the world.
https://github.com/dqneo/babygo
ast compiler go golang lexer linux parser x86-64
Last synced: 7 days ago
JSON representation
babygo👶 is a small Go compiler made from scratch, which can compile itself. It's going to be the smallest and simplest go compiler in the world.
- Host: GitHub
- URL: https://github.com/dqneo/babygo
- Owner: DQNEO
- License: mit
- Created: 2020-03-29T11:48:07.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-25T13:42:58.000Z (6 months ago)
- Last Synced: 2024-10-14T22:41:10.563Z (22 days ago)
- Topics: ast, compiler, go, golang, lexer, linux, parser, x86-64
- Language: Go
- Homepage:
- Size: 4.99 MB
- Stars: 294
- Watchers: 10
- Forks: 20
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Babygo, a go compiler made from scratch
![Test](https://github.com/DQNEO/babygo/workflows/Test/badge.svg)
Babygo is a small and simple go compiler. (Smallest and simplest in the world, I believe.)
It is made from scratch and can compile itself.* No dependency to any libraries. Standard libraries and calling of system calls are home made.
* Lexer, parser and code generator are handwritten.
* Emit assemble code which results in a single static binary.It depends only on `as` as an assembler and `ld` as a linker.
# Design
## Lexer, Parser and AST
The design and logic of ast, lexer and parser are borrowed (or should I say "stolen") from `go/ast`, `go/scanner` and `go/parser`.## Code generator
The design of code generator is borrowed from [chibicc](https://github.com/rui314/chibicc) , a C compiler.## Remaining parts (Semantic analysis, Type management etc.)
This is purely my design :)# Environment
It supports x86-64 Linux only.
If you are not using Linux, you can use [a dedicated docker image](https://hub.docker.com/r/dqneo/ubuntu-compiler-go) for this project.
```termiinal
$ docker pull dqneo/ubuntu-compiler-go
$ ./docker-run
```# Usage
**Currently we are changing CLI design. This section will be updated later**
## Hello world
```terminal
# Build babygo
$ go build -o babygo# Build hello world by babygo
$ ./babygo build -o hello ./example/hello# Run hello world
$ ./hello
hello world!
```## How to do self hosting
```terminal
# Build babygo (1st generation)
$ go build -o babygo# Build babygo (2nd generation) by babygo 1gen
$ ./babygo build -o babygo2 ./# Build babygo (3rd generation) by babygo 2gen
$ ./babygo2 build -o ./babygo3 ./# Check if babygo2 and babygo3 are identical
$ diff ./babygo2 ./babygo3
```## Test
Run this in the docker container:
```terminal
$ make test
```# Reference
* https://golang.org/ref/spec The Go Programming Language Specification
* https://golang.org/pkg/go/parser/ go/parser
* https://sourceware.org/binutils/docs/as/ GNU assembler
* https://github.com/rui314/chibicc C compiler
* https://github.com/DQNEO/minigo Go compiler (my previous work)# License
MIT
# Author
[@DQNEO](https://twitter.com/DQNEO)