The code is derived from my compiler lab code in school. Since the lab only covers some basic topics of a compiler, and my implementation is dirty and naive, I want to keep polishing my code and present my work here.
Some significant limitations are shown below:
- No declaration, all functions should be defined before called;
- No global variables (parsed but cannot generate code);
- No preprocessor;
- Basic type only consists of int (float is parsed but cannot generate code);
- All local variables should be defined at the beginning of a function;
- Struct is passed by reference, and returning a struct is dangerous.
The top level of a program should look like this:
// one-line comment
/**
* multi-line comment
* can be /* nested */
* ignore everything after //
* like // */
* like // /*
* like // *//*
* like // /*_*/
*/ // This is the end of this multi-line commet!
// int read()
// write(int x)
// are provided functions to support input and output
int echo(int e)
{
write(e);
return read();
}
struct A {
int a;
int b;
};
int main()
{
/* Local definitions, like follows */
int foo;
int bar = 3;
int vec[2];
struct A a;
/* Statements, like follows */
if (/* Expression */) {
/* Statements */
}
else if (/* Expression */) {
/* Statements */
}
else {
/* Statements */
}
while (/* Expression */) {
/* Statements */
}
// the three parts of for-loop cannot be omitted currently
for (foo = 0; foo < 10; foo = foo + 1) {
write(foo);
}
/* Expressions */
foo = bar; // Assignment
foo + 1; // Binary operation: +, -, *, /
foo < bar; // Comparing operation: <, <=, >, >=, ==, !=
!foo; // Unary operation: -, !
a.a // field access
(foo);
foo = echo(vec[0]); // Function call, array access
return 0;
}