My attempt at writing a compiler for the PL/0 programming language by Niklaus Wirth.
Initially, the aim was to write the entire compiler from scratch. While working on it, the focus slowly shifted to writing an LLVM frontend.
See GRAMMAR.md for details on the language spec.
- Recursive descent parser returns AST
- AST implemented with nodes having a list of nodes as children (CLRS 10.4)
- Symbol table is an unordered linked list
- I/O uses wrapper functions written in C. This makes it easier than handling variadic functions (which now clang can handle for us). These wrappers are implemented in examples/io.c
$ make
$ ./pl0c <file_name>.pl0
pl0coutputs a.llfile containing LLVM IR corresponding to the source.- Use
llcto get an object file andclangto get an executable. - If the source uses
printand/orscanstatements, you'll need compile io.c and link with it.
io.c contains wrappers with the following signatures:
void print64(int64_t)
int64_t scan64()
examples/build.sh automates all of this. Usage described below.
# get corresponding exectable named <filename>
$ ./build.sh <filename>.pl0
# get executable along with <filename>.ll containing LLVM IR
$ ./build.sh -emit-llvm <filename>.pl0
Demo : running examples
$ make
$ cd examples
$ ./build.sh -emit-llvm fibonacci.pl0 # get executable named fibonacci and fibonacci.ll (contains IR)
$ ./fibonacci
10
55
$
$ ./build.sh natural_recurse.pl0 # get executable named natural_recurse
$ ./natural_recurse
5
1
2
3
4
5
$
All pl0c source code is licensed under the terms of the MIT License.