• Stars
    star
    252
  • Rank 161,312 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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.

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 hand written.
  • Emit assemble code which resutls in a single static binary.

It depends only on as as an assembler and ld as a linker.

It is composed of only a few files.

  • main.go - the main compiler
  • parser.go - parser
  • scanner.go - scanner(or lexer)
  • src/ - internal packages
  • lib/ - libraries

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-build-essential:go
$ ./docker-run

Usage

Hello world

# Build babygo
$ go build -o babygo *.go

# Compile the hello world program by babygo
$ ./babygo example/hello.go

# Assemble and link
$ as -o hello.o /tmp/*.s
$ ld -o hello hello.o

# Run hello world
$ ./hello
hello world!

How to do self hosting

# Build babygo (1st generation)
$ go build -o babygo *.go

# Build babygo by babygo (2nd generation)
$ rm /tmp/*.s
$ ./babygo *.go
$ as -o babygo2.o /tmp/*.s
$ ld -o babygo2 babygo2.o # 2nd generation compiler

# You can generate babygo3 (3rd generation), babygo4, and so on...
$ rm /tmp/*.s
$ ./babygo2 *.go
$ as -o babygo3.o /tmp/*.s
$ ld -o babygo3 babygo3.o # 3rd generation compiler

Test

$ make test

Reference

License

MIT

Author

@DQNEO

More Repositories

1

minigo

minigo🐥is a small Go compiler made from scratch. It can compile itself.
Go
515
star
2

CppTetris

Tetris made within an hour
C++
108
star
3

gojvm

JVM implementation by Go
Go
96
star
4

HowToWriteACompiler

How to write a compiler from scratch in 30 minutes
Go
83
star
5

8cc.go

C compiler written in Go
C
71
star
6

goas

port of GNU Assembler written in go
Assembly
61
star
7

minigit

Reinvention of git
C
25
star
8

Amazon-S3-Thin

A thin, lightweight, low-level Amazon S3 client
Perl
21
star
9

go-build-bash

"go build" implementation by bash script
Shell
21
star
10

Renshu

練習用です。ご自由にforkしてPull Requestを送ってください。↓ 「Git/Github超入門:猿でもできるPull Request」
14
star
11

dotfiles

my dot files (.gitconfig, .emacs, .zshrc, .screenrc ..)
Emacs Lisp
13
star
12

php-skeleton

a skeleton package for modern PHP (>= 7.1)
PHP
11
star
13

php-HelloWorldEnterpriseEdition

HelloWorld Enterprise Edition
PHP
9
star
14

apple-dictionary-parser

Tools for extracting and parsing data from MacOS's dictionary files
Go
6
star
15

go-samples

sample code of golang
Go
6
star
16

ansible-role-php7

6
star
17

Vagrantfile-php7

Vagrantfiles to build and run php7
Shell
6
star
18

gospec-analyzer

Natural language processing of Go Language Specification
HTML
6
star
19

rsolo

rsolo is a simple shell-script which runs chef-solo on remote hosts.
Shell
6
star
20

gospec

Word Wise Go Spec
Makefile
4
star
21

vagrant-chef-remi-php55

Vagrantfile and chef cookbook to install php5.5 by remi-php55 repository
Ruby
3
star
22

c-ltsview

LTSV viewer by C language
C
3
star
23

MakeTutorial

Tutorial of Gnu Makefile
C
3
star
24

splatoon3-random

Splatoon3 random weapon selector
JavaScript
3
star
25

movabletype2hugo

a convert tool for migration from MovableType to Hugo
Perl
3
star
26

Yukiwikimini-on-Ethnam

Yet another implementation of Yukiwikimini
PHP
2
star
27

Kaomoji

Japanese Kaomoji Collection( ˘ω˘)
2
star
28

CTetris

Tetris in C language
C
2
star
29

php-fluent-simplelogger

yet another php library for Fluentd
PHP
2
star
30

ansible-example

Shell
2
star
31

Amazon-S3-FastUploader

a CPAN module
Perl
2
star
32

php-mini

php micro framework
PHP
1
star
33

xrea_cgi

Perl
1
star
34

cwork

C言語入門
C
1
star
35

ethfony

Ethfony is a marriage of Ethna + Symfony
PHP
1
star
36

S3Signer

A PHP library for AmazonS3 direct uploading
JavaScript
1
star
37

knife-solo-renshu

knife soloの練習プロジェクト
Ruby
1
star
38

docker-perlimagemagick

Makefile
1
star
39

ansible-role-nginx

1
star
40

c-curl

sample code of http client using curl
C
1
star
41

cookbook-zsh

Chef cookbook to install zsh from source
Ruby
1
star
42

mt_post

MTにXMLRPCで投稿するスクリプト集
Perl
1
star
43

oxgame

○✗ Game
JavaScript
1
star
44

mt_writer

はてだラ(はてなダイアリライータ)のMT版
CSS
1
star
45

smarty-delimiter-converter

PHP
1
star
46

perl-yaml-tiny-test

YAML::TinyのOSごとの挙動を確認するテスト
Perl
1
star
47

mydiary

Perl製のシンプルなブログシステム
Perl
1
star
48

Assembly

My Assembly sandbox
Assembly
1
star
49

memo

DQNEO's memo
PHP
1
star
50

cookbook-remi

Installs/Configures yum remi and remi-php56 repository
Ruby
1
star
51

voa-reader

JavaScript
1
star
52

go-webapp-example

Go
1
star
53

yourdotfiles

yourdotfiles gathers all your dotfiles.
Ruby
1
star
54

cookbook-tokyotyrant

A Chef CookBook to install TokyoTyrant and TokyoCabinet
Shell
1
star
55

php

PHPの雑多なコード集
PHP
1
star
56

docker-ubuntu

Makefile
1
star