Skip to content

za-creature/fcc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fcc

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.

Supported features

  • most C operators
  • int, float and char data types
  • multiple recursion
  • if / else branching
  • for and while loops

Usage

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)

Why?

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.

License

Whatever dude, I don't care. It was homework.

About

A compiler for a subset of C, written in Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published