Skip to content

tamaroning/ironcc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ironcc

A toy C compiler written in Rust which uses LLVM as backend.
ironcc is aiming to suppport C99, and C11.

Install

$ git clone https://github.com/tamaroning/ironcc.git
$ cd ironcc
$ cargo build

Usage

$ ironcc <file path>

To show the usage and version, run:

$ ironcc

Status

ironcc supports the following functions:

  • function definition
  • local variable declaration
  • return statement
  • assignment
  • types (int and pointer)
  • control syntax (if, else, for)
  • numerical literal
  • binary operations (+, -, *, /)
  • comparison operations (==, !=, <, >, <=, >=)
  • unary operations (+, -)

Syntax

program = top-level*

top-level = func-def

func-def = declspec declarator "{" compound-stmt

stmt = "return" expr ";"
        | "if" "(" expr ")" stmt ("else" stmt)?
        | "for" "(" expr-stmt expr? ";" expr? ")" stmt
        | "while" "(" expr ")" stmt
        | "{" compound-stmt
        | expr-stmt

compound-stmt = (declaration | stmt)* "}"

declaration = declspec (declarator ("=" expr)? ("," declarator ("=" expr)?)*)? ";"

declspec = "int"

declarator = "*"* <ident> type-suffix

type-suffix = "(" func-params
        |"[" <num> "]"
        | ε

func-params = (param ("," param)*)? ")"

param = declspec declarator

type-suffix = "(" func-params no
        | "[" <num> "]" type-suffix
        | ε

expr-stmt = expr? ";"

expr = assign

assign = equality ("=" assign)?

equality = relational ("=="|"!=" relational)*

relational = add (("<"|">"|"<="|">=") add)*

add = mul (("+"|"-") mul)*

mul = unary (("*"|"/") unary)*

unary = ("+" | "-" | "*" | "&") unary
        | postfix

postfix = primary ("[" expr "]")*

primary = "(" expr ")"
        | "sizeof" unary
        | <ident> func-args?
        | <num>

func-call = <ident> "(" (assign ("," assign)*)? ")"

<XXX> means token.

Todo

  • support arithmetic operations of pointers
  • support multi-[] operator (like a[3][4], b[0][1][3])

About

a toy C compiler written in Rust (llvm_sys) [new]

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published