Next: Possible extensions (optional)
Up: Writing a MOOL compiler
Previous: Static check of a MOOL
The last step in writing a MOOL compiler
is the translation to a C program.
Observe that the translation of at least the following MOOL constructs
is trivial, since they are very similar in C:
-
ArithmeticExpression ,
-
BooleanExpression,
-
Alternative,
- WhileLoop,
-
MethodCall.
The only non trivial translations will be those of the following MOOL constructs.
-
ClassDeclaration
- which will be replaced by a struct and one function
per
NewDeclaration or
MethodDeclaration.
-
NewDeclaration
- which will have a returned type in C (i.e. the suitable struct)
-
MethodDeclaration
- which will have the return type int.
-
Assignment
- for which we need to say how we manage objects. A possible and reasonable
solution is to use a pointer to a struct.
We give some precisions for the translation of assignments.
Let us consider a checked assignment
LeftValue :=
RightValue
In the translated C code the LeftValue will be
- a variable of type int if LeftValue is an id and
RightValue
has type int
- a field of a strcut if LeftValue is an
AttributeCall
- a pointer to a strcut if LeftValue is an id and
RightValue
refers to an object.
In the latter case, the assignment means simply that the object
referred by
RightValue is pointed to by the id of LeftValue.
Hence this is just a pointer assignment, no new object is created.
Next: Possible extensions (optional)
Up: Writing a MOOL compiler
Previous: Static check of a MOOL
Marc Moreno Maza
2004-12-01