Front-end for a made-up language as part of a compilers course.
cpl.py
- Driver programlexer.py
- Reads the textual source-code and converts it into a stream of tokens described in tokens.pyparser.py
- Parses variable declarations and builds and AST out of the statements in the code. Also does semantic analysis.codegen.py
- Divides the AST into basic-blocks, maps IR instructions into the back-end's instructions and finally flattens the instructions into a single sequence.quad.py
- Contains conversions between IR instructions into Quad instructions.
CPL | Quad |
---|---|
/* Finding minimum between two numbers */
a, b: float;
{
input(a);
input(b);
if (a < b)
output(a);
else
output(b);
} |
|
/* Sum a variadic list of integers */
N, num, sum : int;
{
sum = 0;
while (N > 0) {
input(num);
sum = sum + num;
N = N - 1;
}
output(sum);
} |
|
/* Tell if an integer is prime */
N, p, limit, result : int;
{
result = 1;
if (N < 2)
result = 0;
p = 2;
limit = N / 2;
while (p < limit) {
if ((N / p) * p == N) {
result = 0;
break;
}
p = p + 1;
}
output(result);
} |
|
/*
* Simple Calculator
* Accepts two floats and outputs a float if successful.
* On error, the output is an integer. Error values:
* 1: Invalid operation
* 2: Division by zero
*/
/* Input numbers */
A, B : float;
/*
* Desired operation:
* - Addition=0
* - Subtraction=1
* - Multiplication=2
* - Divison=3
*/
operation : int;
{
input(A);
input(B);
input(operation);
switch (operation) {
default:
output(1);
break;
case 0:
output(A + B);
break;
case 1:
output(A - B);
break;
case 2:
output(A * B);
break;
case 3:
/* B is a float so the immediate 0 is
* implicitly casted to a float
*/
if (B == 0)
output(2);
else
output(A / B);
break;
}
} |
|