Skip to content

Latest commit

 

History

History
39 lines (24 loc) · 1.09 KB

README.md

File metadata and controls

39 lines (24 loc) · 1.09 KB

tiny-c

A C compiler in javascript

tiny-c is a hobby compiler for a small subset of C 😊 This implementation of 8 queens is an example of what tiny-c can handle today. For more examples and features, check out the Tests directory

Usage

node cc.js <source file>

Features

Generates x86-32 assembly in GAS syntax, which is then assembled and linked by GCC.

Has support for:

  • Integers and Integer Arrays only
  • ~, -, !
  • +, -, *, /, &&, ||, <, >, >=, <=, ==, !=
  • All ops have precedence as defined by the C standard
  • Local and Global variables
  • For loops
  • If Else conditionals and the ?: operator
  • C scoping
  • Functions

Implementation

Lexer

The lexer is implemented in clex.js. It uses simple regex matching to tokenize the source file

Parser

The parser is implemented in cpars.js. It uses recursive descent for everything.

ASM Generator

The code generator is implemented in cgen.js. It takes is the AST generated by the parser, traverses and generates assembly code.