Skip to content

Rax-x/PL-0-Compiler

Repository files navigation

PL/0 Compiler

This is a compiler for the programming language PL/0 introduced by Niklaus Wirth in the book, Algorithms + Data Structures = Programs, as an example of how to construct a compiler. This language is a minimal Pascal, has a really simple grammar and supports all basic features of a programming language, like: constants, variables, procedures, control flow and so on. Because of its simplicity, I consider it a good starting point to learn how build compilers with LLVM as backend. For more details about this language check out the Wikipedia article.

Important

The focus of this project is not to design a good and fast compiler, but just to learn how use the LLVM to develop compilers backends. So all the components of this compiler (lexer, parsers, error reporting, etc.) are really basic.

Example

const MAX = 100;
var start;

procedure countFromStartToMax;
begin
  if start < MAX then
    while start < MAX do
      start := start + 1
end;

procedure recursiveDecrement;
begin
  if start > 0 then
  begin
    start := start - 1;
    call recursiveDecrement
  end
end;

begin
  ?start;
  call countFromStartToMax;
  !start;

  call recursiveDecrement;
  !start
end.

Warning

Only global variables are initialized to 0.

How generate the executable?

Run ./pl0 myprogram.pl0 and simply execute the executable ./myprogram

TODO

  • Code refactor (the codegen code is a little bit unredable).
  • Generate the object file with the same name of the input file.
  • Invoke the clang or gcc internally.
  • Initialize even local variables to zero.
  • Make expressions printable with ! operator.

Resources