An LLVM enabled compiler written in C
The name is inspired from Euler's totient function which is also called Euler's phi function.
Watch my devlogs on YouTube here!
- Working lexer for all future syntax
- Correct parsing for mathematical expressions
- Generate LLVM IR from parser
- Variable declaration
- Function parsing and implementation
- Variables are function scoped
- Global variables
- Function calls
- Implicit return function declarations
- Variable reassignment
- Variable increment operators
- Implement a print function
- Implement strings
- Implement "if statements"
- Implement "for loops" and "while loops"
- Include a standard library (especially math)
- Allow custom types
- Infer return type for implicit return functions
func get_two(): int => 2;
func add_five(a: int): int => a + 5;
func main() {
int two = get_two();
return add_five(two); // should return 7
}
func get_name(): string => "phi";
// it would be cool if I could remove the : int and just infer the return type
func add_five(a: int): int => a + 5;
func main() {
string name = get_name();
int result = add_five(5);
print(name);
print(result);
}
You will need llvm installed along with coreutils on MacOS.
I forget how to install llvm correctly, it may just be brew install llvm
CoreUtils is brew install coreutils
If you need any help with the commands you can always used
make helpto see all the commands.
The project uses a Makefile to build and test the compiler. Here are the available commands:
makeormake all- Builds the main compiler executable (bin/mycompiler)make bin/mycompiler- Explicitly builds the main compiler
To compile a single .phi file with the main compiler:
make # First build the compiler
./bin/mycompiler input.phimake test-all- Builds all test executables (lexer, parser, etc.)make test-lexer- Builds the lexer test executable and runs all lexer testsmake test-parser- Builds the parser test executable and runs all parser testsmake bin/test-lexer- Only builds the lexer test executable (without running tests)make bin/test-parser- Only builds the parser test executable (without running tests)
To test the lexer or parser on a specific file:
make test-lexer # Build and run all lexer tests
./bin/test-lexer input.phi # Run lexer on a single filemake clean- Removes all compiled objects, binaries, and test output filesmake clean-tests- Only removes test output files (.outand.difffiles)
The test system works by:
- Running your compiler component on
.phifiles intest-cases/ - Comparing the output with expected results in
.ansfiles - Showing differences in
.difffiles if tests fail
Test files are organized in subdirectories like test-cases/lexer/ and test-cases/parser/.