Skip to content

A compiler for the C0 language (C language simplified) made in Haskell.

License

Notifications You must be signed in to change notification settings

carlahnr/c0_compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C0 Compiler

Haskell

About

A basic compiler that reads C0 language 1 code and generates a code in Assembly for MIPS architecture.

Commands accepted

The following commands and block of commands are accepted by this simple C0 compiler:

  • Basic types (int, bool) and constants such as true, false and integer numbers.
  • String type
  • Comments in line or block multiline: // or /* multiline comment */
  • Arithmetic expressions: +, -, *, / and %
  • Variable declarations, and variable simple attributions of value: variable_name = expression
  • Comparison operators: ==, !=, <, <=, >, >=
  • Conditional executors:
    if(expression)
      //single line instruction or instruction block
    
    if(expression)
      //single line instruction or instruction block
    else
      //single line instruction or instruction block
    
  • Instruction blocks:
    {
      //multiple lines of instructions
    }
    
  • Cycles for or while:
    while(expression)
      //single line instruction or instruction block
    
  • Function definitions with argument parameters and possible return of a value
  • Functions for IO of integers: scan_int(), print_int()
  • Printing function for string: print_str()
  • Flux control: break and continue
  • Logical operators with short-circuit evaluation: !, && and ||

Development toolchain

  • Haskell
  • Cabal for building the application.
  • Alex for generating lexical analysers.
  • Happy for parsing generation.

Executing

Run the commands:

  • cabal build
  • cabal run < tests/input.c0

Examples

File saved as input.c0 into /tests directory:

int factorial(int n) {
  if (n == 0) 
    return 1;
  return n * factorial(n-1);
}

int main() {
  print_int(factorial(scan_int()));
}

Contributors

Footnotes

  1. A simplified version of C language created for teaching programming created by University Carnegie Mellon. See C0 Reference Guide.

About

A compiler for the C0 language (C language simplified) made in Haskell.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published