MCPcopy Index your code
hub / github.com/DQNEO/babygo

github.com/DQNEO/babygo @v0.4.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.4.1 ↗ · + Follow
990 symbols 3,351 edges 42 files 92 documented · 9%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Babygo, a go compiler made from scratch

Test

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 , 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 for this project.

$ docker pull dqneo/ubuntu-compiler-go
$ ./docker-run

Usage

Currently we are changing CLI design. This section will be updated later

Hello world

# 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

# 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:

$ 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

Extension points exported contracts — how you extend this code

Decl (Interface)
Pseudo interface for *ast.Decl *GenDecl | *FuncDecl [81 implementers]
lib/ast/ast.go
Type (Interface)
Type emulates go/types.Type [11 implementers]
internal/types/types.go
MetaStmt (Interface)
(no doc) [81 implementers]
internal/ir/ir.go
Expr (Interface)
(no doc) [81 implementers]
t/test.go
Writer (Interface)
(no doc) [2 implementers]
src/io/io.go
Expr (Interface)
(no doc) [81 implementers]
lib/ast/ast.go
MetaExpr (Interface)
(no doc) [24 implementers]
internal/ir/ir.go
WriteCloser (Interface)
(no doc) [2 implementers]
t/test.go

Core symbols most depended-on inside this repo

printf
called by 381
internal/codegen/codegen.go
writeln
called by 358
t/another.go
panic
called by 141
src/runtime/runtime.go
Itoa
called by 124
lib/strconv/strconv.go
write
called by 85
t/another.go
Printf
called by 82
lib/fmt/fmt.go
Pos
called by 82
internal/ir/ir.go
emitExpr
called by 78
internal/codegen/codegen.go

Shape

Function 539
Method 281
Struct 143
Interface 13
TypeAlias 13
Class 1

Languages

Go99%
C1%

Modules by API surface

t/test.go191 symbols
internal/ir/ir.go123 symbols
internal/codegen/codegen.go100 symbols
lib/ast/ast.go97 symbols
internal/sema/sema.go91 symbols
lib/parser/parser.go77 symbols
internal/types/types.go55 symbols
src/runtime/runtime.go41 symbols
src/os/os.go24 symbols
internal/builder/builder.go21 symbols
lib/fmt/fmt.go20 symbols
src/syscall/syscall.go17 symbols

For agents

$ claude mcp add babygo \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact