Made to learn about compilers and programming languages, Koala is heavily inspired by Bob Nystrom's Crafting Interpreters.
The project includes:
- A recursive descent parser for syntactic analysis.
- The visitor pattern for evaluating the abstract syntax tree (AST).
- Runtime support for expressions, statements, variables, and function calls.
- Error reporting with line and column highlights (Rust-style formatting).
- Language: C++
- Build System: CMake, Make
- Arithmetic expressions with operator precedence
- C-style single and multi-line comments
- Variable declarations and assignments
- Block-scoped environments
- Conditional statements (
if
,else
) - Loops (
for
,while
) - Functions and function calls
- Return statements
- Runtime error reporting with file name and line/column tracing
The project is far from done, these are some things off the top of my head that I would like the language to have:
- Better C++ (my experience with C++ has been limited to DSA problems - this lack of knowledge has led to a lot of hacky and unsafe things which I would like to revisit)
- Ternary operator and switch statement (I overuse the ternary operator in every language I touch, it’d be a shame if my own didn’t support it)
- Syntactic Sugar: (support for
else if (elif?)
,do-while
, etc. to make the language feel nicer to write) - stdlib functions (user input, print function, random number generator etc.)
- Closures (currently functions work without closures)
- Support for Object-Oriented Programming (classes and inheritance)
Currently it's quite jarring to write .kol
files. Since I've been fascinated by LSP, treesitter and related technologies in the past, I'd also like to have:
- Integration with Tree-sitter for syntax highlighting and smart indentation
- This is done! You can check it out here: hsnavihS/tree-sitter-koala
- LSP Support to enable editor features like autocomplete, go-to-definition, error detection etc.
Wouldn't have been able to come even halfway without these reources:
- Crafting Interpreters by Bob Nystrom
- Writing A C Compiler by Nora Sandler
- Making A Compiler - YouTube Playlist
To build the project, run from the root:
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B ./build
cmake --build ./build
Or alternatively, you can use the script:
./scripts/build.sh
Make sure to make the script executable (first time use):
chmod +x ./scripts/build.sh
This will generate a binary called koala
in the build directory, which can be invoked in one of two ways:
To drop into a REPL:
./koala
To provide an input file:
./koala ../tests/fibonacci.kol
I haven't looked into writing tests for the interpreter itself, and I mostly won't. Currently I have two scripts that I use to validate my changes:
generateoutput.sh
: iterates over all Koala programs present in the tests/ directory and generates outputs for them in an output directory within tests.runtests.sh
: run all test files with the current binary and compare the diff with outputs generated bygenerateoutputs.sh
.