A minimal Go implementation aiming for self-hosting capability.
petitgo is a small-scale Go language implementation that aims to eventually compile itself (self-hosting). Starting with minimal features, we incrementally add functionality following a test-driven development approach.
- Phase 1: Minimal Calculator - Four arithmetic operations with operator precedence
- Phase 2: Variables and Statements - Variable declarations, assignments, and basic statements
- Phase 3: Control Flow - if/else statements, for loops, break/continue
- Phase 4: Functions - Function definitions, calls, parameters, and return values
- Phase 5: Type System - Basic types (int, string, bool), type checking, and type inference
- Phase 8: Native Compiler - Direct ARM64 assembly generation (no Go dependency)
- Advanced Features:
- Switch statements with case matching
- Struct definitions and field access
- Basic slice operations (len, append, indexing)
- Comments (line and block comments)
- Increment/decrement operators (++, --)
- Compound assignment operators (+=, -=, *=, /=)
- Complete for loops (init; condition; update)
- Phase 9: Self-hosting - Compiling petitgo with petitgo itself
git clone https://github.com/yuya-takeyama/petitgo.git
cd petitgo
go build -o petitgo
./petitgo
Example REPL session:
> 2 + 3 * 4
14
> x := 10
> x + 5
15
> if x > 5 { print("big") }
big
Create a .pg
file:
// fibonacci.pg
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
}
func main() {
for i := 0; i < 10; i++ {
println(fibonacci(i))
}
}
Compile and run:
# Direct execution
./petitgo run fibonacci.pg
# Compile to native binary
./petitgo build fibonacci.pg
./fibonacci
# View AST as JSON
./petitgo ast fibonacci.pg
# Generate ARM64 assembly
./petitgo asm fibonacci.pg
See the examples/ directory for various sample programs:
hello.pg
- Simple hello worldcalculator.pg
- Arithmetic operationsfibonacci.pg
- Recursive fibonacci implementationswitch_demo.pg
- Switch statement usagestruct_field_test.pg
- Struct field access- And more...
petitgo consists of several packages:
token/
- Token definitions for lexical analysisscanner/
- Lexical analyzer (tokenizer)ast/
- Abstract Syntax Tree node definitionsparser/
- Syntax analyzer (parser)eval/
- Expression evaluator with type systemasmgen/
- ARM64 assembly code generatorrepl/
- Read-Eval-Print Loop implementation
- Source Code (.pg files)
- Scanner → Tokens
- Parser → Abstract Syntax Tree (AST)
- ASM Generator → ARM64 Assembly
- System Assembler (as) → Object File
- System Linker (clang) → Native Executable
No Go compiler dependency for the final binary!
int
- Integer numbersstring
- String literals with escape sequencesbool
- Boolean values (true/false)struct
- Structure types with fields
- Arithmetic:
+
,-
,*
,/
- Comparison:
==
,!=
,<
,>
,<=
,>=
- Assignment:
:=
,=
- Compound:
+=
,-=
,*=
,/=
- Increment/Decrement:
++
,--
if
/else
statementsfor
loops (condition-only and full form)switch
/case
statementsbreak
/continue
return
statements
- Function definitions with parameters and return types
- Recursive function calls
- Built-in functions:
println()
,len()
,append()
- Struct field access (
obj.field
) - Basic slice operations
- Line comments (
//
) and block comments (/* */
) - Variable reassignment and compound operators
This project follows Test-Driven Development (TDD) principles and uses a Pull Request based workflow.
# Run all tests
go test ./...
# Run specific package tests
go test ./parser
go test ./eval
- Create feature branch:
yuya-takeyama/feat/FEATURE_NAME
- Implement with TDD approach
- Create draft Pull Request:
gh pr create --draft
- Complete implementation and tests
- Remove draft status for review
- Phase 6: Enhanced standard library functions
- Phase 7: Package system and imports
- Phase 9: Self-hosting capability
See docs/ROADMAP.md for detailed development phases.
Contributions are welcome! Please read our development guide in CLAUDE.md for detailed instructions on:
- Test-Driven Development approach
- Code style guidelines
- Pull Request workflow
- Commit message conventions
MIT License - see LICENSE file for details.
- Inspired by the Go programming language
- Built with Test-Driven Development principles
- Aims for eventual self-hosting capability