A basic compiler that reads C0 language 1 code and generates a code in Assembly for MIPS architecture.
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
orwhile
: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
andcontinue
- Logical operators with short-circuit evaluation:
!
,&&
and||
- Haskell
- Cabal for building the application.
- Alex for generating lexical analysers.
- Happy for parsing generation.
Run the commands:
cabal build
cabal run < tests/input.c0
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()));
}
- carlahnr
- Rui Santos
Footnotes
-
A simplified version of C language created for teaching programming created by University Carnegie Mellon. See C0 Reference Guide. ↩