A small educational compiler written in Go.
It reads a tiny Lisp-like input and converts it into a JavaScript-like function call output.
Example:
Input: (mix 4 (blend 9 1))
Output: mix(4, blend(9, 1));
- compiler.go: Main compiler implementation and demo run in main()
- compiler_test.go: Unit tests and benchmark for tokenizer/parser/compiler
The compiler runs in four steps:
- Tokenizer
- Converts source text into tokens such as paren, name, and number.
- Parser
- Converts tokens into an AST (abstract syntax tree).
- Transformer
- Converts the first AST shape into a JS-style AST shape.
- Code Generator
- Converts the transformed AST into the final output string.
From this folder:
go run compiler.go
Run all tests with:
go test ./...
- Basic compiler pipeline design
- Recursive descent style parsing
- AST traversal and transformation
- Code generation from structured trees
- Add string literals
- Add better parser error messages for missing parentheses
- Add support for uppercase letters in identifiers