Full Circle C (fcc) is a compiler for a limited subset of the C language, written in Python. It was a homework assignment by prof. Dra***ici (full name omitted because he probably doesn't want to be associated in any way with this monstruosity) for a course in Compiler Architecture.
It compiles code for a custom stack based virtual machine (included). While the lexer and code generator are decent, reading the parser can be considered torture under certain jurisdictions.
- most C operators
int
,float
andchar
data types- multiple recursion
if
/else
branchingfor
andwhile
loops
import fcc
tokens = fcc.lex("""
int gcd(int a, int b) {
/* computes gcd(a, b) using Euclid's algorithm */
while(a != b) {
if(a > b)
a -= b;
else
b -= a;
}
return a;
}
void main() {
/* Since it doesn't have a stdlib, the backtick (`) operator
may be used to print arithmetic results to the standard output */
`gcd(48, 36);
}
""")
ast = fcc.parse(tokens)
bytecode = fcc.compile(ast)
fcc.run(bytecode, stack=16384)
To paraphrase a famous politician, "I choose to write a C interpreter on top of a Python interpreter written in C. Not because it's easy, but because it's hard".
Also, because I felt like I've been criticizing other compilers and languages for far too long without so much as a thought about the challenges their developers face.
While this is MOST DEFINITELY NOT something that you want to use in any ACTUAL project, I've learned a bunch from it (mostly how NOT to do things), and maybe someone else will too.
Whatever dude, I don't care. It was homework.