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.
The design and logic of ast, lexer and parser are borrowed (or should I say "stolen") from go/ast
, go/scanner
and go/parser
.
The design of code generator is borrowed from chibicc , a C compiler.
This is purely my design :)
It supports x86-64 Linux only.
If you are not using Linux, you can use a dedicated docker image for this project.
$ docker pull dqneo/ubuntu-compiler-go
$ ./docker-run
Currently we are changing CLI design. This section will be updated later
# Build babygo
$ go build -o babygo
# Build hello world by babygo
$ ./babygo build -o hello ./example/hello
# Run hello world
$ ./hello
hello world!
# 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
Run this in the docker container:
$ make test
- 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)
MIT