A minimal compiler for the SPICY programming language, written in OCaml. It parses, type-checks, and compiles SPICY source code to LLVM IR.
- Language support: Integers, booleans, strings, functions, if/elseif/else, for/while/do-while loops, variables, and printing.
- Type Checking: Basic type inference and checking for expressions and statements.
- LLVM IR Generation: Emits IR for execution with LLVM toolchain.
- OCaml Toolchain: Uses Menhir for parsing and OCamlex for lexing.
- Test Scripts: Example programs and LLVM output provided in the
tests/directory.
fun add(x, y) {
x + y
}
let a = 3
let b = 4
print add(a, b)
if true {
print "This is true"
} elseif false {
print "This won't run"
} else {
print "Fallback"
}
for i in 0..2 {
print i
}Supported features:
- Variables:
let a = 10 - Functions:
fun f(x, y) { ... } - Conditionals:
if {...} elseif {...} else {...} - Loops:
for,while,do-while - Basic types:
Int,Bool,String - Print statement
- OCaml (4.x)
- Menhir
- OCamlex
- LLVM (for IR execution and further processing)
- GCC (for final machine code, optional)
Make sure you are in the project root, then run:
makeor for Windows MinGW64:
make -f Makefile_win_mingw64This will:
- Build the SPICY compiler frontend (
spicyc) - Generate LLVM IR from the test SPICY file
- Optionally compile and run the generated IR
ast.ml # Abstract syntax tree definitions
sast.ml # Annotated (typed) AST
lexer.mll # Lexer (OCamlex)
parser.mly # Parser (Menhir)
semant.ml # Semantic analysis (type checking)
codegen.ml # LLVM IR generation
main.ml # Compiler entry point
tests/ # Example programs and LLVM IR outputs
Makefile* # Build scripts
grammar.txt # Informal language grammar
Several example SPICY programs are in the tests/ directory. You can run all test cases with:
cd tests
./run_tests.sh- Built for educational purposes.
- Written in OCaml.
MIT License (or specify your own)