Package comp

This package contains classes for compiling Cebollita C-- programs.

See:
          Description

Class Summary
ArgList An argument list is a list of expressions that are to be evaluated as actual parameters to a function call.
ArrayRefNode An array reference expression.
ArrayType  
Assembler An object that knows how to spit out (MIPS) assembly code.
AssignNode Assignment statement.
BinExprNode Binary expression node.
BlockNode A sequence of statements.
CallNode A function call.
CharType  
ExprNode Basic superclass for all expression nodes.
FuncDefNode A function definition.
IdNode Basic identifier expression.
IfNode A conditional.
IntType  
Module This is really the toplevel container node.
Node The superclass of all AST Nodes.
ParmList Represents a formal parameter list belonging to a function definition.
PointerType  
RegBank Manages a set of registers for use during code generation.
ReturnNode Return a value from a function.
Scc Entry point to the C-- compiler.
Scope Manages a lexical scope.
StringValNode String literal expression node.
Type Represents a type in our language.
TypeFactory The typefactory takes a string representation of a type and produces type objects.
ValNode Integer literal expression node.
VarDecl Represents a variable declaration.
VarDef Represents a variable definition in a given Scope.
WhileNode A while loop.
 

Package comp Description

This package contains classes for compiling Cebollita C-- programs. The compiler spits out MIPS assembly code.

Running the compiler

Please see the tools section for the definitive guide to using this tool.
  java comp.parser file.c
It will create a file called file.s as well as a file called out.html which is an HTML representation of the parse tree.
  java comp.parser --version
Will print out the version of the toolset.

Cebollita C-- Language Overview

C-- is a restricted subset of C. The language is similar to C, although it has a weaker type system (if that can be possible), and a restricted set of built-in types, operators, control constructs. Its goal is to be simple enough that a student can understand/modify the compiler, while illustrating the mapping of high level language features to assembly language. It should, however, also be expressive enough to write reasonably interesting benchmark programs.

Below are the major features/bugs/differences from C:

Comments
Only // single line comments are accepted.
Control Constructs
Only if and while are supported. The body clauses must be surrounded by curly braces, even if there is one statement. The following is not legal:
if (x == 10)          // syntax error!
  y = x;

while (x != 0)        // syntax error!
  x = x - 1; 
The above would need to be rewritten as:
if (x == 10) {
  y = x;
}

while (x != 0) {
  x = x - 1; 
}
Note that the commonly used dangling form of if-statement is illegal in C-- syntax:
if (foo) {
  // do one thing
}
else if (bar) {
  // do another thing
}
else {
  // do yet another thing
}
Datatypes
The only supported types are: char, int, char*, int*, arrays of chars, and arrays of ints. Typedefs and composite types (structures) are not allowed.
Global variables
Are supported. A variable that is defined (globally) in another file, must be declared extern in a file that wishes to reference it (as in regular C programs).
Literal Data
Only integers and strings may be expressed as literal data in C--. Integers may be signed decimal or hexidecimal (eg. 0x0000FFA1). Strings are (as in C) text surrounded by double quotes ("foobar"). \n, \t, \0 are valid escape characters in strings. Strings are allocated globally, and are of type char*.
main() {
  char* msg = "hello there\n";
  printString(msg);
  printInt(strlen(msg));
}
Functions
All functions are assumed to return values of type int. Function definitions do not require a return type. In fact, if one is given, it will be ignored. Actual parameters are not type checked against formal parameters.

Function calls are not checked -- that is, the number and types of arguments do not need to match the number and type of parameters. For this reason, while external functions are allowed, they do not (should not) need to be declared as extern.

To return a value from a function, the reserved word return should be used.

Operators
The following operators are supported: +, -, *, /, %, <<, >>, |, &, ^, ==, !=, >=, <=, >, <, [] (array reference).

NO OTHER OPERATORS (eg. ++, --, ^, &, |, etc) are supported.

Reserved words
The following words are reserved: if, else, while, return, extern.
A Simple Program
fact(int n) {
  int result = 1;
  while (n != 0) {
    result = result * n;
    n = n - 1;
  }  
  return result;
}

main() {
  int n = 9;
  int result = fact(n);
  printString("The factorial is: ");
  printInt(result);
}
The above program would need to be linked against a library that provides the I/O functions printString and printInt.