Intitial commit of second assessed coursework.
This commit is contained in:
commit
ecbc9b750d
12 changed files with 864 additions and 0 deletions
188
task1/asts.java
Normal file
188
task1/asts.java
Normal file
|
@ -0,0 +1,188 @@
|
|||
// Do not modify the code below except for replacing the "..."! Don't
|
||||
// add anything (including "public" declarations), don't remove
|
||||
// anything (including "public" declarations). Don't wrap it in a
|
||||
// package, don't make it an innner class of some other class. If
|
||||
// your IDE suggsts to change anything below, ignore your IDE. You are
|
||||
// welcome to add new classes! Please put them into separate files.
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class Program { // Used in Tasks 1, 2, 3.
|
||||
// Leaves in the accumulator what running the first declaration leaves in the accumulator.
|
||||
public List <Declaration> decls;
|
||||
public Program ( List <Declaration> _decls ) {
|
||||
assert ( _decls.size () > 0 ); // ensures that we have at least one declaration the
|
||||
// first element in decls is the the procedure executed
|
||||
// at startup. We assume that this initial procedure takes
|
||||
// 0 arguments. NOTE: the entry procedure does NOT have
|
||||
// have to be called "main". It can have ANY valid identifier
|
||||
// as name.
|
||||
assert ( _decls.get ( 0 ).numOfArgs == 0 ); // ensures that the first declaration takes
|
||||
// 0 arguments.
|
||||
decls = _decls; } }
|
||||
|
||||
|
||||
class Declaration { // Used in Tasks 1, 2, 3.
|
||||
public String id;
|
||||
public int numOfArgs; // Used for computing the size of the AR.
|
||||
public Exp body;
|
||||
|
||||
public Declaration ( String _id, int _numOfargs, Exp _body ) {
|
||||
assert ( _numOfargs >= 0 );
|
||||
id = _id;
|
||||
numOfArgs = _numOfargs;
|
||||
body = _body; } }
|
||||
|
||||
|
||||
abstract class Exp {} // Used in Tasks 1, 2, 3.
|
||||
|
||||
class IntLiteral extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Leaves n in the accumulator.
|
||||
public int n;
|
||||
IntLiteral ( int _n ) { n = _n; } }
|
||||
|
||||
class Variable extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Leaves the content of the variables x in the accumulator. Note
|
||||
// that the integer x represents the OFFSET of the variable relative
|
||||
// to the encompassing procedure declaration (which is used by the
|
||||
// code generator to compute, at compile time, the place, relative
|
||||
// to the frame pointer, where the variable is stored at
|
||||
// run-time). It does NOT hold the values the variable takes at
|
||||
// run-time. E.g. in
|
||||
//
|
||||
// def f ( a, b, c, d ) = ...
|
||||
//
|
||||
// the variable a has offset 1, b has offset 2, c has 3 and d has
|
||||
// 4. There are no other offsets for this procedure.
|
||||
public int x;
|
||||
public Variable ( int _x ) {
|
||||
assert ( _x > 0 ); // ensures that 1 is the smallest possible offset.
|
||||
x = _x; } }
|
||||
|
||||
class If extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// If the comparison is true, executes the thenBody.
|
||||
// Otherwise the elseBody is executed.
|
||||
public Exp l;
|
||||
public Comp comp;
|
||||
public Exp r;
|
||||
public Exp thenBody;
|
||||
public Exp elseBody;
|
||||
public If ( Exp _l, Comp _comp, Exp _r, Exp _thenBody, Exp _elseBody ) {
|
||||
l = _l;
|
||||
comp = _comp;
|
||||
r = _r;
|
||||
thenBody = _thenBody;
|
||||
elseBody = _elseBody; } }
|
||||
|
||||
class Binexp extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Evaluates l, then r, and applies binop to both.
|
||||
public Exp l;
|
||||
public Binop binop;
|
||||
public Exp r;
|
||||
public Binexp ( Exp _l, Binop _binop, Exp _r ) {
|
||||
l = _l;
|
||||
binop = _binop;
|
||||
r = _r; } }
|
||||
|
||||
class Invoke extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Evaluates the arguments right-to-left. Then calls the procedure name with
|
||||
// these arguments.
|
||||
public String name;
|
||||
public List<Exp> args;
|
||||
public Invoke ( String _name, List<Exp> _args ) {
|
||||
name = _name;
|
||||
args = _args; } }
|
||||
|
||||
class While extends Exp { // Used in Tasks 2, 3.
|
||||
// Implements the standard while-loop,
|
||||
// Can return anything in accumulator
|
||||
public Exp l;
|
||||
public Comp comp;
|
||||
public Exp r;
|
||||
public Exp body;
|
||||
public While ( Exp _l, Comp _comp, Exp _r, Exp _body ) {
|
||||
l = _l;
|
||||
comp = _comp;
|
||||
r = _r;
|
||||
body = _body; } }
|
||||
|
||||
class RepeatUntil extends Exp { // Used in Tasks 2, 3.
|
||||
// Implements the standard repeat-unitl loop,
|
||||
// Can return anything in accumulator.
|
||||
public Exp body;
|
||||
public Exp l;
|
||||
public Comp comp;
|
||||
public Exp r;
|
||||
public RepeatUntil ( Exp _body, Exp _l, Comp _comp, Exp _r ) {
|
||||
body = _body;
|
||||
l = _l;
|
||||
comp = _comp;
|
||||
r = _r; } }
|
||||
|
||||
class Assign extends Exp { // Used in Tasks 2, 3.
|
||||
// Evaluates the right-hand side e, and stores the result
|
||||
// Can return anything in accumulator.
|
||||
public int x;
|
||||
public Exp e;
|
||||
public Assign ( int _x, Exp _e ) {
|
||||
assert ( _x > 0 );
|
||||
x = _x;
|
||||
e = _e; } }
|
||||
|
||||
class Seq extends Exp { // Used in Tasks 2, 3.
|
||||
// Runs l first, then r.
|
||||
// Returns the result of r in the accumulator.
|
||||
public Exp l;
|
||||
public Exp r;
|
||||
public Seq ( Exp _l, Exp _r ) {
|
||||
l = _l;
|
||||
r = _r; } }
|
||||
|
||||
class Skip extends Exp { // Used in Tasks 2, 3.
|
||||
// Does nothing.
|
||||
// Can return anything in accumulator.
|
||||
public Skip () {} }
|
||||
|
||||
class Break extends Exp { // Used in Task 3 only.
|
||||
// Exits the nearest enclosing loop. Leaves
|
||||
// Can return anything in accumulator.
|
||||
public Break () {} }
|
||||
|
||||
class Continue extends Exp { // Used in Task 3 only.
|
||||
// Abandons the current iteration of the nearest enclosing
|
||||
// loop, jumping straight to the checking of the conditional.
|
||||
// Can return anything in accumulator.
|
||||
public Continue () {} }
|
||||
|
||||
|
||||
abstract class Comp {} // Used in Tasks 1, 2, 3.
|
||||
|
||||
class Less extends Comp { // Used in Tasks 2, 3.
|
||||
public Less () {} }
|
||||
|
||||
class LessEq extends Comp { // Used in Tasks 2, 3.
|
||||
public LessEq () {} }
|
||||
|
||||
class Equals extends Comp { // Used in Tasks 1, 2, 3.
|
||||
public Equals () {} }
|
||||
|
||||
class Greater extends Comp { // Used in Tasks 2, 3.
|
||||
public Greater () {} }
|
||||
|
||||
class GreaterEq extends Comp { // Used in Tasks 2, 3.
|
||||
public GreaterEq () {} }
|
||||
|
||||
|
||||
abstract class Binop {}
|
||||
|
||||
class Plus extends Binop { // Used in Tasks 1, 2, 3.
|
||||
public Plus () {} }
|
||||
|
||||
class Minus extends Binop { // Used in Tasks 1, 2, 3.
|
||||
public Minus () {} }
|
||||
|
||||
class Times extends Binop { // Used in Tasks 2, 3.
|
||||
public Times () {} }
|
||||
|
||||
class Div extends Binop { // Used in Task 3 only.
|
||||
public Div () {} }
|
14
task1/basic.java
Normal file
14
task1/basic.java
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Do not modify the code below except for replacing the "..."! Don't
|
||||
// add anything (including "public" declarations), don't remove
|
||||
// anything (including "public" declarations). Don't wrap it in a
|
||||
// package, don't make it an innner class of some other class. If
|
||||
// your IDE suggsts to change anything below, ignore your IDE. You are
|
||||
// welcome to add new classes! Please put them into separate files.
|
||||
|
||||
class CodegenException extends Exception {
|
||||
public String msg;
|
||||
public CodegenException ( String _msg ) { msg = _msg; } }
|
||||
|
||||
interface Codegen {
|
||||
public String codegen ( Program p ) throws CodegenException; }
|
||||
|
9
task1/codegen1.java
Normal file
9
task1/codegen1.java
Normal file
|
@ -0,0 +1,9 @@
|
|||
// Do not modify the code below except for replacing the "..."! Don't
|
||||
// add anything (including "public" declarations), don't remove
|
||||
// anything (including "public" declarations). Don't wrap it in a
|
||||
// package, don't make it an innner class of some other class. If
|
||||
// your IDE suggsts to change anything below, ignore your IDE. You are
|
||||
// welcome to add new classes! Please put them into separate files.
|
||||
|
||||
class Task1 {
|
||||
public static Codegen create () throws CodegenException { ... } }
|
77
task1/tester-skeleton-task1.java
Normal file
77
task1/tester-skeleton-task1.java
Normal file
|
@ -0,0 +1,77 @@
|
|||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
class DoesItCompile {
|
||||
public void f () {
|
||||
try {
|
||||
Codegen codegen = Task1.create ();
|
||||
|
||||
Declaration declFirst = new Declaration ( "first",
|
||||
0,
|
||||
new IntLiteral ( 0 ) );
|
||||
Declaration decl = new Declaration ( "f",
|
||||
3,
|
||||
new If ( new Variable ( 1 ),
|
||||
new Less (),
|
||||
new Variable ( 2 ),
|
||||
new Variable( 3 ),
|
||||
new IntLiteral ( 0 ) ) );
|
||||
Program p = new Program ( new ArrayList<Declaration> ( asList ( declFirst, decl ) ) );
|
||||
String result1 = codegen.codegen ( p );
|
||||
CodegenException codegenException = new CodegenException ( "msg" );
|
||||
String tmp = decl.id;
|
||||
int n = decl.numOfArgs;
|
||||
Exp exp = decl.body;
|
||||
IntLiteral intLiteral = new IntLiteral ( 5 );
|
||||
n = intLiteral.n;
|
||||
Variable variable = new Variable ( 7 );
|
||||
n = variable.x;
|
||||
Comp comp = new Equals ();
|
||||
If ifAST = new If ( intLiteral, comp, variable, intLiteral, variable );
|
||||
exp = ifAST.l;
|
||||
comp = ifAST.comp;
|
||||
exp = ifAST.r;
|
||||
exp = ifAST.thenBody;
|
||||
exp = ifAST.elseBody;
|
||||
Binop binop = new Plus ();
|
||||
Binexp binexp = new Binexp ( variable, binop, intLiteral );
|
||||
exp = binexp.l;
|
||||
comp = ifAST.comp;
|
||||
exp = binexp.r;
|
||||
Invoke invoke = new Invoke ( "name", new ArrayList<Exp> () );
|
||||
tmp = invoke.name;
|
||||
List<Exp> l = invoke.args;
|
||||
While whileAST = new While ( intLiteral, comp, variable, binexp );
|
||||
exp = whileAST.l;
|
||||
comp = whileAST.comp;
|
||||
exp = whileAST.r;
|
||||
exp = whileAST.body;
|
||||
RepeatUntil repeatAST = new RepeatUntil ( binexp, intLiteral, comp, variable );
|
||||
exp = repeatAST.l;
|
||||
comp = repeatAST.comp;
|
||||
exp = repeatAST.r;
|
||||
exp = repeatAST.body;
|
||||
Assign assign = new Assign ( 7, exp );
|
||||
int x = assign.x;
|
||||
exp = assign.e;
|
||||
Seq seq = new Seq ( exp, exp );
|
||||
exp = seq.l;
|
||||
exp = seq.r;
|
||||
// Break breakAST = new Break ();
|
||||
// Continue continueAST = new Continue ();
|
||||
comp = new Less ();
|
||||
comp = new LessEq ();
|
||||
comp = new Equals ();
|
||||
comp = new Greater ();
|
||||
comp = new GreaterEq ();
|
||||
binop = new Plus ();
|
||||
binop = new Minus ();
|
||||
binop = new Times ();
|
||||
binop = new Div ();
|
||||
}
|
||||
catch ( CodegenException e ) {
|
||||
String tmp = e.msg; } }
|
||||
|
||||
}
|
||||
|
188
task2/asts.java
Normal file
188
task2/asts.java
Normal file
|
@ -0,0 +1,188 @@
|
|||
// Do not modify the code below except for replacing the "..."! Don't
|
||||
// add anything (including "public" declarations), don't remove
|
||||
// anything (including "public" declarations). Don't wrap it in a
|
||||
// package, don't make it an innner class of some other class. If
|
||||
// your IDE suggsts to change anything below, ignore your IDE. You are
|
||||
// welcome to add new classes! Please put them into separate files.
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class Program { // Used in Tasks 1, 2, 3.
|
||||
// Leaves in the accumulator what running the first declaration leaves in the accumulator.
|
||||
public List <Declaration> decls;
|
||||
public Program ( List <Declaration> _decls ) {
|
||||
assert ( _decls.size () > 0 ); // ensures that we have at least one declaration the
|
||||
// first element in decls is the the procedure executed
|
||||
// at startup. We assume that this initial procedure takes
|
||||
// 0 arguments. NOTE: the entry procedure does NOT have
|
||||
// have to be called "main". It can have ANY valid identifier
|
||||
// as name.
|
||||
assert ( _decls.get ( 0 ).numOfArgs == 0 ); // ensures that the first declaration takes
|
||||
// 0 arguments.
|
||||
decls = _decls; } }
|
||||
|
||||
|
||||
class Declaration { // Used in Tasks 1, 2, 3.
|
||||
public String id;
|
||||
public int numOfArgs; // Used for computing the size of the AR.
|
||||
public Exp body;
|
||||
|
||||
public Declaration ( String _id, int _numOfargs, Exp _body ) {
|
||||
assert ( _numOfargs >= 0 );
|
||||
id = _id;
|
||||
numOfArgs = _numOfargs;
|
||||
body = _body; } }
|
||||
|
||||
|
||||
abstract class Exp {} // Used in Tasks 1, 2, 3.
|
||||
|
||||
class IntLiteral extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Leaves n in the accumulator.
|
||||
public int n;
|
||||
IntLiteral ( int _n ) { n = _n; } }
|
||||
|
||||
class Variable extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Leaves the content of the variables x in the accumulator. Note
|
||||
// that the integer x represents the OFFSET of the variable relative
|
||||
// to the encompassing procedure declaration (which is used by the
|
||||
// code generator to compute, at compile time, the place, relative
|
||||
// to the frame pointer, where the variable is stored at
|
||||
// run-time). It does NOT hold the values the variable takes at
|
||||
// run-time. E.g. in
|
||||
//
|
||||
// def f ( a, b, c, d ) = ...
|
||||
//
|
||||
// the variable a has offset 1, b has offset 2, c has 3 and d has
|
||||
// 4. There are no other offsets for this procedure.
|
||||
public int x;
|
||||
public Variable ( int _x ) {
|
||||
assert ( _x > 0 ); // ensures that 1 is the smallest possible offset.
|
||||
x = _x; } }
|
||||
|
||||
class If extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// If the comparison is true, executes the thenBody.
|
||||
// Otherwise the elseBody is executed.
|
||||
public Exp l;
|
||||
public Comp comp;
|
||||
public Exp r;
|
||||
public Exp thenBody;
|
||||
public Exp elseBody;
|
||||
public If ( Exp _l, Comp _comp, Exp _r, Exp _thenBody, Exp _elseBody ) {
|
||||
l = _l;
|
||||
comp = _comp;
|
||||
r = _r;
|
||||
thenBody = _thenBody;
|
||||
elseBody = _elseBody; } }
|
||||
|
||||
class Binexp extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Evaluates l, then r, and applies binop to both.
|
||||
public Exp l;
|
||||
public Binop binop;
|
||||
public Exp r;
|
||||
public Binexp ( Exp _l, Binop _binop, Exp _r ) {
|
||||
l = _l;
|
||||
binop = _binop;
|
||||
r = _r; } }
|
||||
|
||||
class Invoke extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Evaluates the arguments right-to-left. Then calls the procedure name with
|
||||
// these arguments.
|
||||
public String name;
|
||||
public List<Exp> args;
|
||||
public Invoke ( String _name, List<Exp> _args ) {
|
||||
name = _name;
|
||||
args = _args; } }
|
||||
|
||||
class While extends Exp { // Used in Tasks 2, 3.
|
||||
// Implements the standard while-loop,
|
||||
// Can return anything in accumulator
|
||||
public Exp l;
|
||||
public Comp comp;
|
||||
public Exp r;
|
||||
public Exp body;
|
||||
public While ( Exp _l, Comp _comp, Exp _r, Exp _body ) {
|
||||
l = _l;
|
||||
comp = _comp;
|
||||
r = _r;
|
||||
body = _body; } }
|
||||
|
||||
class RepeatUntil extends Exp { // Used in Tasks 2, 3.
|
||||
// Implements the standard repeat-unitl loop,
|
||||
// Can return anything in accumulator.
|
||||
public Exp body;
|
||||
public Exp l;
|
||||
public Comp comp;
|
||||
public Exp r;
|
||||
public RepeatUntil ( Exp _body, Exp _l, Comp _comp, Exp _r ) {
|
||||
body = _body;
|
||||
l = _l;
|
||||
comp = _comp;
|
||||
r = _r; } }
|
||||
|
||||
class Assign extends Exp { // Used in Tasks 2, 3.
|
||||
// Evaluates the right-hand side e, and stores the result
|
||||
// Can return anything in accumulator.
|
||||
public int x;
|
||||
public Exp e;
|
||||
public Assign ( int _x, Exp _e ) {
|
||||
assert ( _x > 0 );
|
||||
x = _x;
|
||||
e = _e; } }
|
||||
|
||||
class Seq extends Exp { // Used in Tasks 2, 3.
|
||||
// Runs l first, then r.
|
||||
// Returns the result of r in the accumulator.
|
||||
public Exp l;
|
||||
public Exp r;
|
||||
public Seq ( Exp _l, Exp _r ) {
|
||||
l = _l;
|
||||
r = _r; } }
|
||||
|
||||
class Skip extends Exp { // Used in Tasks 2, 3.
|
||||
// Does nothing.
|
||||
// Can return anything in accumulator.
|
||||
public Skip () {} }
|
||||
|
||||
class Break extends Exp { // Used in Task 3 only.
|
||||
// Exits the nearest enclosing loop. Leaves
|
||||
// Can return anything in accumulator.
|
||||
public Break () {} }
|
||||
|
||||
class Continue extends Exp { // Used in Task 3 only.
|
||||
// Abandons the current iteration of the nearest enclosing
|
||||
// loop, jumping straight to the checking of the conditional.
|
||||
// Can return anything in accumulator.
|
||||
public Continue () {} }
|
||||
|
||||
|
||||
abstract class Comp {} // Used in Tasks 1, 2, 3.
|
||||
|
||||
class Less extends Comp { // Used in Tasks 2, 3.
|
||||
public Less () {} }
|
||||
|
||||
class LessEq extends Comp { // Used in Tasks 2, 3.
|
||||
public LessEq () {} }
|
||||
|
||||
class Equals extends Comp { // Used in Tasks 1, 2, 3.
|
||||
public Equals () {} }
|
||||
|
||||
class Greater extends Comp { // Used in Tasks 2, 3.
|
||||
public Greater () {} }
|
||||
|
||||
class GreaterEq extends Comp { // Used in Tasks 2, 3.
|
||||
public GreaterEq () {} }
|
||||
|
||||
|
||||
abstract class Binop {}
|
||||
|
||||
class Plus extends Binop { // Used in Tasks 1, 2, 3.
|
||||
public Plus () {} }
|
||||
|
||||
class Minus extends Binop { // Used in Tasks 1, 2, 3.
|
||||
public Minus () {} }
|
||||
|
||||
class Times extends Binop { // Used in Tasks 2, 3.
|
||||
public Times () {} }
|
||||
|
||||
class Div extends Binop { // Used in Task 3 only.
|
||||
public Div () {} }
|
14
task2/basic.java
Normal file
14
task2/basic.java
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Do not modify the code below except for replacing the "..."! Don't
|
||||
// add anything (including "public" declarations), don't remove
|
||||
// anything (including "public" declarations). Don't wrap it in a
|
||||
// package, don't make it an innner class of some other class. If
|
||||
// your IDE suggsts to change anything below, ignore your IDE. You are
|
||||
// welcome to add new classes! Please put them into separate files.
|
||||
|
||||
class CodegenException extends Exception {
|
||||
public String msg;
|
||||
public CodegenException ( String _msg ) { msg = _msg; } }
|
||||
|
||||
interface Codegen {
|
||||
public String codegen ( Program p ) throws CodegenException; }
|
||||
|
9
task2/codegen2.java
Normal file
9
task2/codegen2.java
Normal file
|
@ -0,0 +1,9 @@
|
|||
// Do not modify the code below except for replacing the "..."! Don't
|
||||
// add anything (including "public" declarations), don't remove
|
||||
// anything (including "public" declarations). Don't wrap it in a
|
||||
// package, don't make it an innner class of some other class. If
|
||||
// your IDE suggsts to change anything below, ignore your IDE. You are
|
||||
// welcome to add new classes! Please put them into separate files.
|
||||
|
||||
class Task2 {
|
||||
public static Codegen create () throws CodegenException { ... } }
|
77
task2/tester-skeleton-task2.java
Normal file
77
task2/tester-skeleton-task2.java
Normal file
|
@ -0,0 +1,77 @@
|
|||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
class DoesItCompile {
|
||||
public void f () {
|
||||
try {
|
||||
Codegen codegen = Task2.create ();
|
||||
|
||||
Declaration declFirst = new Declaration ( "first",
|
||||
0,
|
||||
new IntLiteral ( 0 ) );
|
||||
Declaration decl = new Declaration ( "f",
|
||||
3,
|
||||
new If ( new Variable ( 1 ),
|
||||
new Less (),
|
||||
new Variable ( 2 ),
|
||||
new Variable( 3 ),
|
||||
new IntLiteral ( 0 ) ) );
|
||||
Program p = new Program ( new ArrayList<Declaration> ( asList ( declFirst, decl ) ) );
|
||||
String result1 = codegen.codegen ( p );
|
||||
CodegenException codegenException = new CodegenException ( "msg" );
|
||||
String tmp = decl.id;
|
||||
int n = decl.numOfArgs;
|
||||
Exp exp = decl.body;
|
||||
IntLiteral intLiteral = new IntLiteral ( 5 );
|
||||
n = intLiteral.n;
|
||||
Variable variable = new Variable ( 7 );
|
||||
n = variable.x;
|
||||
Comp comp = new Equals ();
|
||||
If ifAST = new If ( intLiteral, comp, variable, intLiteral, variable );
|
||||
exp = ifAST.l;
|
||||
comp = ifAST.comp;
|
||||
exp = ifAST.r;
|
||||
exp = ifAST.thenBody;
|
||||
exp = ifAST.elseBody;
|
||||
Binop binop = new Plus ();
|
||||
Binexp binexp = new Binexp ( variable, binop, intLiteral );
|
||||
exp = binexp.l;
|
||||
comp = ifAST.comp;
|
||||
exp = binexp.r;
|
||||
Invoke invoke = new Invoke ( "name", new ArrayList<Exp> () );
|
||||
tmp = invoke.name;
|
||||
List<Exp> l = invoke.args;
|
||||
While whileAST = new While ( intLiteral, comp, variable, binexp );
|
||||
exp = whileAST.l;
|
||||
comp = whileAST.comp;
|
||||
exp = whileAST.r;
|
||||
exp = whileAST.body;
|
||||
RepeatUntil repeatAST = new RepeatUntil ( binexp, intLiteral, comp, variable );
|
||||
exp = repeatAST.l;
|
||||
comp = repeatAST.comp;
|
||||
exp = repeatAST.r;
|
||||
exp = repeatAST.body;
|
||||
Assign assign = new Assign ( 7, exp );
|
||||
int x = assign.x;
|
||||
exp = assign.e;
|
||||
Seq seq = new Seq ( exp, exp );
|
||||
exp = seq.l;
|
||||
exp = seq.r;
|
||||
// Break breakAST = new Break ();
|
||||
// Continue continueAST = new Continue ();
|
||||
comp = new Less ();
|
||||
comp = new LessEq ();
|
||||
comp = new Equals ();
|
||||
comp = new Greater ();
|
||||
comp = new GreaterEq ();
|
||||
binop = new Plus ();
|
||||
binop = new Minus ();
|
||||
binop = new Times ();
|
||||
binop = new Div ();
|
||||
}
|
||||
catch ( CodegenException e ) {
|
||||
String tmp = e.msg; } }
|
||||
|
||||
}
|
||||
|
188
task3/asts.java
Normal file
188
task3/asts.java
Normal file
|
@ -0,0 +1,188 @@
|
|||
// Do not modify the code below except for replacing the "..."! Don't
|
||||
// add anything (including "public" declarations), don't remove
|
||||
// anything (including "public" declarations). Don't wrap it in a
|
||||
// package, don't make it an innner class of some other class. If
|
||||
// your IDE suggsts to change anything below, ignore your IDE. You are
|
||||
// welcome to add new classes! Please put them into separate files.
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class Program { // Used in Tasks 1, 2, 3.
|
||||
// Leaves in the accumulator what running the first declaration leaves in the accumulator.
|
||||
public List <Declaration> decls;
|
||||
public Program ( List <Declaration> _decls ) {
|
||||
assert ( _decls.size () > 0 ); // ensures that we have at least one declaration the
|
||||
// first element in decls is the the procedure executed
|
||||
// at startup. We assume that this initial procedure takes
|
||||
// 0 arguments. NOTE: the entry procedure does NOT have
|
||||
// have to be called "main". It can have ANY valid identifier
|
||||
// as name.
|
||||
assert ( _decls.get ( 0 ).numOfArgs == 0 ); // ensures that the first declaration takes
|
||||
// 0 arguments.
|
||||
decls = _decls; } }
|
||||
|
||||
|
||||
class Declaration { // Used in Tasks 1, 2, 3.
|
||||
public String id;
|
||||
public int numOfArgs; // Used for computing the size of the AR.
|
||||
public Exp body;
|
||||
|
||||
public Declaration ( String _id, int _numOfargs, Exp _body ) {
|
||||
assert ( _numOfargs >= 0 );
|
||||
id = _id;
|
||||
numOfArgs = _numOfargs;
|
||||
body = _body; } }
|
||||
|
||||
|
||||
abstract class Exp {} // Used in Tasks 1, 2, 3.
|
||||
|
||||
class IntLiteral extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Leaves n in the accumulator.
|
||||
public int n;
|
||||
IntLiteral ( int _n ) { n = _n; } }
|
||||
|
||||
class Variable extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Leaves the content of the variables x in the accumulator. Note
|
||||
// that the integer x represents the OFFSET of the variable relative
|
||||
// to the encompassing procedure declaration (which is used by the
|
||||
// code generator to compute, at compile time, the place, relative
|
||||
// to the frame pointer, where the variable is stored at
|
||||
// run-time). It does NOT hold the values the variable takes at
|
||||
// run-time. E.g. in
|
||||
//
|
||||
// def f ( a, b, c, d ) = ...
|
||||
//
|
||||
// the variable a has offset 1, b has offset 2, c has 3 and d has
|
||||
// 4. There are no other offsets for this procedure.
|
||||
public int x;
|
||||
public Variable ( int _x ) {
|
||||
assert ( _x > 0 ); // ensures that 1 is the smallest possible offset.
|
||||
x = _x; } }
|
||||
|
||||
class If extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// If the comparison is true, executes the thenBody.
|
||||
// Otherwise the elseBody is executed.
|
||||
public Exp l;
|
||||
public Comp comp;
|
||||
public Exp r;
|
||||
public Exp thenBody;
|
||||
public Exp elseBody;
|
||||
public If ( Exp _l, Comp _comp, Exp _r, Exp _thenBody, Exp _elseBody ) {
|
||||
l = _l;
|
||||
comp = _comp;
|
||||
r = _r;
|
||||
thenBody = _thenBody;
|
||||
elseBody = _elseBody; } }
|
||||
|
||||
class Binexp extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Evaluates l, then r, and applies binop to both.
|
||||
public Exp l;
|
||||
public Binop binop;
|
||||
public Exp r;
|
||||
public Binexp ( Exp _l, Binop _binop, Exp _r ) {
|
||||
l = _l;
|
||||
binop = _binop;
|
||||
r = _r; } }
|
||||
|
||||
class Invoke extends Exp { // Used in Tasks 1, 2, 3.
|
||||
// Evaluates the arguments right-to-left. Then calls the procedure name with
|
||||
// these arguments.
|
||||
public String name;
|
||||
public List<Exp> args;
|
||||
public Invoke ( String _name, List<Exp> _args ) {
|
||||
name = _name;
|
||||
args = _args; } }
|
||||
|
||||
class While extends Exp { // Used in Tasks 2, 3.
|
||||
// Implements the standard while-loop,
|
||||
// Can return anything in accumulator
|
||||
public Exp l;
|
||||
public Comp comp;
|
||||
public Exp r;
|
||||
public Exp body;
|
||||
public While ( Exp _l, Comp _comp, Exp _r, Exp _body ) {
|
||||
l = _l;
|
||||
comp = _comp;
|
||||
r = _r;
|
||||
body = _body; } }
|
||||
|
||||
class RepeatUntil extends Exp { // Used in Tasks 2, 3.
|
||||
// Implements the standard repeat-unitl loop,
|
||||
// Can return anything in accumulator.
|
||||
public Exp body;
|
||||
public Exp l;
|
||||
public Comp comp;
|
||||
public Exp r;
|
||||
public RepeatUntil ( Exp _body, Exp _l, Comp _comp, Exp _r ) {
|
||||
body = _body;
|
||||
l = _l;
|
||||
comp = _comp;
|
||||
r = _r; } }
|
||||
|
||||
class Assign extends Exp { // Used in Tasks 2, 3.
|
||||
// Evaluates the right-hand side e, and stores the result
|
||||
// Can return anything in accumulator.
|
||||
public int x;
|
||||
public Exp e;
|
||||
public Assign ( int _x, Exp _e ) {
|
||||
assert ( _x > 0 );
|
||||
x = _x;
|
||||
e = _e; } }
|
||||
|
||||
class Seq extends Exp { // Used in Tasks 2, 3.
|
||||
// Runs l first, then r.
|
||||
// Returns the result of r in the accumulator.
|
||||
public Exp l;
|
||||
public Exp r;
|
||||
public Seq ( Exp _l, Exp _r ) {
|
||||
l = _l;
|
||||
r = _r; } }
|
||||
|
||||
class Skip extends Exp { // Used in Tasks 2, 3.
|
||||
// Does nothing.
|
||||
// Can return anything in accumulator.
|
||||
public Skip () {} }
|
||||
|
||||
class Break extends Exp { // Used in Task 3 only.
|
||||
// Exits the nearest enclosing loop. Leaves
|
||||
// Can return anything in accumulator.
|
||||
public Break () {} }
|
||||
|
||||
class Continue extends Exp { // Used in Task 3 only.
|
||||
// Abandons the current iteration of the nearest enclosing
|
||||
// loop, jumping straight to the checking of the conditional.
|
||||
// Can return anything in accumulator.
|
||||
public Continue () {} }
|
||||
|
||||
|
||||
abstract class Comp {} // Used in Tasks 1, 2, 3.
|
||||
|
||||
class Less extends Comp { // Used in Tasks 2, 3.
|
||||
public Less () {} }
|
||||
|
||||
class LessEq extends Comp { // Used in Tasks 2, 3.
|
||||
public LessEq () {} }
|
||||
|
||||
class Equals extends Comp { // Used in Tasks 1, 2, 3.
|
||||
public Equals () {} }
|
||||
|
||||
class Greater extends Comp { // Used in Tasks 2, 3.
|
||||
public Greater () {} }
|
||||
|
||||
class GreaterEq extends Comp { // Used in Tasks 2, 3.
|
||||
public GreaterEq () {} }
|
||||
|
||||
|
||||
abstract class Binop {}
|
||||
|
||||
class Plus extends Binop { // Used in Tasks 1, 2, 3.
|
||||
public Plus () {} }
|
||||
|
||||
class Minus extends Binop { // Used in Tasks 1, 2, 3.
|
||||
public Minus () {} }
|
||||
|
||||
class Times extends Binop { // Used in Tasks 2, 3.
|
||||
public Times () {} }
|
||||
|
||||
class Div extends Binop { // Used in Task 3 only.
|
||||
public Div () {} }
|
14
task3/basic.java
Normal file
14
task3/basic.java
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Do not modify the code below except for replacing the "..."! Don't
|
||||
// add anything (including "public" declarations), don't remove
|
||||
// anything (including "public" declarations). Don't wrap it in a
|
||||
// package, don't make it an innner class of some other class. If
|
||||
// your IDE suggsts to change anything below, ignore your IDE. You are
|
||||
// welcome to add new classes! Please put them into separate files.
|
||||
|
||||
class CodegenException extends Exception {
|
||||
public String msg;
|
||||
public CodegenException ( String _msg ) { msg = _msg; } }
|
||||
|
||||
interface Codegen {
|
||||
public String codegen ( Program p ) throws CodegenException; }
|
||||
|
9
task3/codegen3.java
Normal file
9
task3/codegen3.java
Normal file
|
@ -0,0 +1,9 @@
|
|||
// Do not modify the code below except for replacing the "..."! Don't
|
||||
// add anything (including "public" declarations), don't remove
|
||||
// anything (including "public" declarations). Don't wrap it in a
|
||||
// package, don't make it an innner class of some other class. If
|
||||
// your IDE suggsts to change anything below, ignore your IDE. You are
|
||||
// welcome to add new classes! Please put them into separate files.
|
||||
|
||||
class Task3 {
|
||||
public static Codegen create () throws CodegenException { ... } }
|
77
task3/tester-skeleton-task3.java
Normal file
77
task3/tester-skeleton-task3.java
Normal file
|
@ -0,0 +1,77 @@
|
|||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
class DoesItCompile {
|
||||
public void f () {
|
||||
try {
|
||||
Codegen codegen = Task3.create ();
|
||||
|
||||
Declaration declFirst = new Declaration ( "first",
|
||||
0,
|
||||
new IntLiteral ( 0 ) );
|
||||
Declaration decl = new Declaration ( "f",
|
||||
3,
|
||||
new If ( new Variable ( 1 ),
|
||||
new Less (),
|
||||
new Variable ( 2 ),
|
||||
new Variable( 3 ),
|
||||
new IntLiteral ( 0 ) ) );
|
||||
Program p = new Program ( new ArrayList<Declaration> ( asList ( declFirst, decl ) ) );
|
||||
String result1 = codegen.codegen ( p );
|
||||
CodegenException codegenException = new CodegenException ( "msg" );
|
||||
String tmp = decl.id;
|
||||
int n = decl.numOfArgs;
|
||||
Exp exp = decl.body;
|
||||
IntLiteral intLiteral = new IntLiteral ( 5 );
|
||||
n = intLiteral.n;
|
||||
Variable variable = new Variable ( 7 );
|
||||
n = variable.x;
|
||||
Comp comp = new Equals ();
|
||||
If ifAST = new If ( intLiteral, comp, variable, intLiteral, variable );
|
||||
exp = ifAST.l;
|
||||
comp = ifAST.comp;
|
||||
exp = ifAST.r;
|
||||
exp = ifAST.thenBody;
|
||||
exp = ifAST.elseBody;
|
||||
Binop binop = new Plus ();
|
||||
Binexp binexp = new Binexp ( variable, binop, intLiteral );
|
||||
exp = binexp.l;
|
||||
comp = ifAST.comp;
|
||||
exp = binexp.r;
|
||||
Invoke invoke = new Invoke ( "name", new ArrayList<Exp> () );
|
||||
tmp = invoke.name;
|
||||
List<Exp> l = invoke.args;
|
||||
While whileAST = new While ( intLiteral, comp, variable, binexp );
|
||||
exp = whileAST.l;
|
||||
comp = whileAST.comp;
|
||||
exp = whileAST.r;
|
||||
exp = whileAST.body;
|
||||
RepeatUntil repeatAST = new RepeatUntil ( binexp, intLiteral, comp, variable );
|
||||
exp = repeatAST.l;
|
||||
comp = repeatAST.comp;
|
||||
exp = repeatAST.r;
|
||||
exp = repeatAST.body;
|
||||
Assign assign = new Assign ( 7, exp );
|
||||
int x = assign.x;
|
||||
exp = assign.e;
|
||||
Seq seq = new Seq ( exp, exp );
|
||||
exp = seq.l;
|
||||
exp = seq.r;
|
||||
// Break breakAST = new Break ();
|
||||
// Continue continueAST = new Continue ();
|
||||
comp = new Less ();
|
||||
comp = new LessEq ();
|
||||
comp = new Equals ();
|
||||
comp = new Greater ();
|
||||
comp = new GreaterEq ();
|
||||
binop = new Plus ();
|
||||
binop = new Minus ();
|
||||
binop = new Times ();
|
||||
binop = new Div ();
|
||||
}
|
||||
catch ( CodegenException e ) {
|
||||
String tmp = e.msg; } }
|
||||
|
||||
}
|
||||
|
Loading…
Reference in a new issue