This project is a compiler for a programming language capable of:
- Integer-type Variables
- Basic Arithmetic (addition, subtraction, multiplication, integer division)
- Boolean Logic Operators (less-than, greater-than, not)
- Conditional Statements (if, switch)
- Iterative Statements (while, for)
- Outputting variable values to stdout
This compiler takes programs written in a text file as input via redirection, and provides any output to stdout.
ex.) $ ./a.out < program.txt
A program written in the compiler-specific language is composed of three sections (in order):
- A Variable Declaration Section
- A Body Section
- An Input Section
Variables are declared at the top of a file (before the body section) with the following syntax:
form: a, b;
This section is enclosed in curly-brackets.
The body section can consist of the following statements:
form 1 - initialization from input section: input <variable name>;
note: inputs are assigned to variables in the order in which they are provided in the inputs section.
ex.) input G;
G will equal the int-value x if the input section consists of "x, w
" (where x
is an integer, and w
is a potentially empty list of integers delimited by commas) at execution-time.
form 2 - explicit initialization: input <integer literal>;
ex.) G = 5;
G will equal 5 at execution-time.
form: <var> = <var or literal> <operator> <var or literal>;
ex.) G = 1 + 2; \\ G now holds the value of 3
ex.) sum = sum - 1; \\ sum has been decremented by 1
ex.) growth = original * 2; \\ growth now holds double the amount of original
ex.) x = 7 / 2; \\ x now holds the value of 3
if form: IF <variable or name> <boolean operator> <variable or name> { <statements> }
ex.) IF G < 0 { ... } \\ enters IF block if the value in G is negative
ex.) IF answerToUniverse <> 42 { ... } \\ enters IF block if answerToUniverse does not equal 42
while form: WHILE <variable or name> <boolean operator> <variable or name> { <statements> }
ex.) WHILE i > 0 { ... } \\ enters and loops through WHILE block while the value in i is greater than zero
switch form: SWITCH <variable or name> { CASE x: { ... } CASE y: { ... } ... } \\ where x and y are integer literals
ex.)
SWITCH a {
CASE 1: { output a; }
CASE 2: { a = a - 50; }
CASE 3: { output b; }
}
form: output <variable>;
ex.) output a; \\ outputs the value stored in a
This section is a line at the bottom of the program that consists of a non-empty list of integers. There must be a sufficient number of integers specified in this line for all INPUT statements (There cannot be more INPUT statements than there are user inputs).
ex.) -3 1 4 1 5 9
Program_1.txt
i, j;
{
input i;
i = 42 ;
j = i + 1;
output i;
output j;
}
1 2
Terminal output
$ ./a.out < Program_1.txt
42 43
Program_2.txt
j, i, minusfour, k ;
{
input i;
input j;
minusfour = 0-4;
WHILE i > 0 {
j = 3;
IF j > 1 {
WHILE j > 0 {
k = i*j;
output k;
j = j-1;
}
}
IF j > 0 {
WHILE j > 0 {
k = i*j;
output k;
j = j-1;
}
}
i = i-1;
}
input minusfour;
WHILE i > minusfour {
output i;
i = i - 1;
}
i = i+1;
j = j+1;
output i;
output j;
}
4 3 4 4 5 2 6
Terminal Output
$ ./a.out < Program_1.txt
12 8 4 9 6 3 6 4 2 3 2 1 1 1