github.com/DQNEO/babygo@v0.0.3/README.md (about) 1 # Babygo, a go compiler made from scratch 2 3  4 5 Babygo is a small and simple go compiler. (Smallest and simplest in the world, I believe.) 6 It is made from scratch and can compile itself. 7 8 * No dependency to any libraries. Standard libraries and calling of system calls are home made. 9 * Lexer, parser and code generator are hand written. 10 * Emit assemble code which resutls in a single static binary. 11 12 It depends only on `as` as an assembler and `ld` as a linker. 13 14 It is composed of only a few files. 15 16 * main.go - the main compiler 17 * parser.go - parser 18 * scanner.go - scanner(or lexer) 19 * src/ - internal packages 20 * lib/ - libraries 21 22 # Design 23 24 ## Lexer, Parser and AST 25 The design and logic of ast, lexer and parser are borrowed (or should I say "stolen") from `go/ast`, `go/scanner` and `go/parser`. 26 27 ## Code generator 28 The design of code generator is borrowed from [chibicc](https://github.com/rui314/chibicc) , a C compiler. 29 30 ## Remaining parts (Semantic analysis, Type management etc.) 31 This is purely my design :) 32 33 # Environment 34 35 It supports x86-64 Linux only. 36 37 If you are not using Linux, you can use [a dedicated docker image](https://hub.docker.com/r/dqneo/ubuntu-build-essential/tags) for this project. 38 39 ```termiinal 40 $ docker pull dqneo/ubuntu-build-essential:go 41 $ ./docker-run 42 ``` 43 44 # Usage 45 46 ## Hello world 47 48 ```terminal 49 # Build babygo 50 $ go build -o babygo *.go 51 52 # Compile the hello world program by babygo 53 $ ./babygo example/hello.go 54 55 # Assemble and link 56 $ as -o hello.o /tmp/*.s 57 $ ld -o hello hello.o 58 59 # Run hello world 60 $ ./hello 61 hello world! 62 ``` 63 64 ## How to do self hosting 65 66 ```terminal 67 # Build babygo (1st generation) 68 $ go build -o babygo *.go 69 70 # Build babygo by babygo (2nd generation) 71 $ rm /tmp/*.s 72 $ ./babygo *.go 73 $ as -o babygo2.o /tmp/*.s 74 $ ld -o babygo2 babygo2.o # 2nd generation compiler 75 76 # You can generate babygo3 (3rd generation), babygo4, and so on... 77 $ rm /tmp/*.s 78 $ ./babygo2 *.go 79 $ as -o babygo3.o /tmp/*.s 80 $ ld -o babygo3 babygo3.o # 3rd generation compiler 81 ``` 82 83 ## Test 84 85 ```terminal 86 $ make test 87 ``` 88 89 # Reference 90 91 * https://golang.org/ref/spec The Go Programming Language Specification 92 * https://golang.org/pkg/go/parser/ go/parser 93 * https://sourceware.org/binutils/docs/as/ GNU assembler 94 * https://github.com/rui314/chibicc C compiler 95 * https://github.com/DQNEO/minigo Go compiler (my previous work) 96 97 98 # License 99 100 MIT 101 102 # Author 103 104 [@DQNEO](https://twitter.com/DQNEO)