Homework 3
This commit is contained in:
parent
bf60a078d7
commit
0afc86ceeb
129 changed files with 3163 additions and 4316 deletions
|
@ -5,6 +5,10 @@ import java.util.Arrays;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import cd.ir.Symbol.ClassSymbol;
|
||||
import cd.ir.Symbol.MethodSymbol;
|
||||
import cd.ir.Symbol.TypeSymbol;
|
||||
import cd.ir.Symbol.VariableSymbol;
|
||||
import cd.util.Pair;
|
||||
import cd.util.debug.AstOneLine;
|
||||
|
||||
|
@ -56,7 +60,7 @@ public abstract class Ast {
|
|||
|
||||
/** Convenient debugging printout */
|
||||
@Override
|
||||
public String toString() {
|
||||
public String toString() {
|
||||
return String.format(
|
||||
"(%s)@%x",
|
||||
AstOneLine.toString(this),
|
||||
|
@ -73,14 +77,20 @@ public abstract class Ast {
|
|||
super(fixedCount);
|
||||
}
|
||||
|
||||
/** Type that this expression will evaluate to (computed in semantic phase). */
|
||||
public TypeSymbol type;
|
||||
|
||||
@Override
|
||||
public <R,A> R accept(AstVisitor<R, A> visitor, A arg) {
|
||||
public <R,A> R accept(AstVisitor<R, A> visitor, A arg) {
|
||||
return this.accept((ExprVisitor<R,A>)visitor, arg);
|
||||
}
|
||||
public abstract <R,A> R accept(ExprVisitor<R, A> visitor, A arg);
|
||||
|
||||
/** Copies any non-AST fields. */
|
||||
protected <E extends Expr> E postCopy(E item) {
|
||||
{
|
||||
item.type = type;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
@ -140,23 +150,23 @@ public abstract class Ast {
|
|||
* such as "1+2" or "3*4" */
|
||||
public static class BinaryOp extends LeftRightExpr {
|
||||
|
||||
public static enum BOp {
|
||||
|
||||
B_TIMES("*"),
|
||||
B_DIV("/"),
|
||||
B_MOD("%"),
|
||||
B_PLUS("+"),
|
||||
B_MINUS("-"),
|
||||
B_AND("&&"),
|
||||
B_OR("||"),
|
||||
B_EQUAL("=="),
|
||||
B_NOT_EQUAL("!="),
|
||||
B_LESS_THAN("<"),
|
||||
B_LESS_OR_EQUAL("<="),
|
||||
B_GREATER_THAN(">"),
|
||||
B_GREATER_OR_EQUAL(">=");
|
||||
|
||||
public String repr;
|
||||
public static enum BOp {
|
||||
|
||||
B_TIMES("*"),
|
||||
B_DIV("/"),
|
||||
B_MOD("%"),
|
||||
B_PLUS("+"),
|
||||
B_MINUS("-"),
|
||||
B_AND("&&"),
|
||||
B_OR("||"),
|
||||
B_EQUAL("=="),
|
||||
B_NOT_EQUAL("!="),
|
||||
B_LESS_THAN("<"),
|
||||
B_LESS_OR_EQUAL("<="),
|
||||
B_GREATER_THAN(">"),
|
||||
B_GREATER_OR_EQUAL(">=");
|
||||
|
||||
public String repr;
|
||||
private BOp(String repr) { this.repr = repr; }
|
||||
|
||||
/**
|
||||
|
@ -167,26 +177,26 @@ public abstract class Ast {
|
|||
* operator.
|
||||
*/
|
||||
public boolean isCommutative() {
|
||||
switch(this) {
|
||||
case B_PLUS:
|
||||
case B_TIMES:
|
||||
case B_AND:
|
||||
case B_OR:
|
||||
case B_EQUAL:
|
||||
case B_NOT_EQUAL:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
switch(this) {
|
||||
case B_PLUS:
|
||||
case B_TIMES:
|
||||
case B_AND:
|
||||
case B_OR:
|
||||
case B_EQUAL:
|
||||
case B_NOT_EQUAL:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public BOp operator;
|
||||
|
||||
public BinaryOp(Expr left, BOp operator, Expr right) {
|
||||
super(left, right);
|
||||
this.operator = operator;
|
||||
}
|
||||
};
|
||||
|
||||
public BOp operator;
|
||||
|
||||
public BinaryOp(Expr left, BOp operator, Expr right) {
|
||||
super(left, right);
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, A> R accept(ExprVisitor<R, A> visitor, A arg) {
|
||||
|
@ -210,6 +220,12 @@ public abstract class Ast {
|
|||
return visitor.cast(this, arg);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <E extends Expr> E postCopy(E item) {
|
||||
((Cast)item).type = type;
|
||||
return super.postCopy(item);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class IntConst extends LeafExpr {
|
||||
|
@ -253,6 +269,8 @@ public abstract class Ast {
|
|||
|
||||
public final String fieldName;
|
||||
|
||||
public VariableSymbol sym;
|
||||
|
||||
public Field(Expr arg, String fieldName) {
|
||||
super(arg);
|
||||
assert arg != null && fieldName != null;
|
||||
|
@ -264,6 +282,12 @@ public abstract class Ast {
|
|||
return visitor.field(this, arg);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <E extends Expr> E postCopy(E item) {
|
||||
((Field)item).sym = sym;
|
||||
return super.postCopy(item);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Index extends LeftRightExpr {
|
||||
|
@ -314,21 +338,21 @@ public abstract class Ast {
|
|||
|
||||
public static class UnaryOp extends ArgExpr {
|
||||
|
||||
public static enum UOp {
|
||||
U_PLUS("+"),
|
||||
U_MINUS("-"),
|
||||
U_BOOL_NOT("!");
|
||||
public String repr;
|
||||
public static enum UOp {
|
||||
U_PLUS("+"),
|
||||
U_MINUS("-"),
|
||||
U_BOOL_NOT("!");
|
||||
public String repr;
|
||||
private UOp(String repr) { this.repr = repr; }
|
||||
};
|
||||
|
||||
public final UOp operator;
|
||||
|
||||
public UnaryOp(UOp operator, Expr arg) {
|
||||
};
|
||||
|
||||
public final UOp operator;
|
||||
|
||||
public UnaryOp(UOp operator, Expr arg) {
|
||||
super(arg);
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <R, A> R accept(ExprVisitor<R, A> visitor, A arg) {
|
||||
return visitor.unaryOp(this, arg);
|
||||
|
@ -340,6 +364,8 @@ public abstract class Ast {
|
|||
|
||||
public String name;
|
||||
|
||||
public VariableSymbol sym;
|
||||
|
||||
/**
|
||||
* Use this constructor to build an instance of this AST
|
||||
* in the parser.
|
||||
|
@ -351,6 +377,29 @@ public abstract class Ast {
|
|||
public <R, A> R accept(ExprVisitor<R, A> visitor, A arg) {
|
||||
return visitor.var(this, arg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this static function to build an instance after the
|
||||
* semantic phase; it fills in the {@link #type} and {@link #sym}
|
||||
* fields.
|
||||
*/
|
||||
public static Var withSym(VariableSymbol sym) {
|
||||
Var v = new Var(sym.name);
|
||||
v.sym = sym;
|
||||
v.type = sym.type;
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <E extends Expr> E postCopy(E item) {
|
||||
((Var)item).sym = sym;
|
||||
return super.postCopy(item);
|
||||
}
|
||||
|
||||
public void setSymbol(VariableSymbol variableSymbol) {
|
||||
sym = variableSymbol;
|
||||
name = sym.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -367,6 +416,8 @@ public abstract class Ast {
|
|||
|
||||
public String methodName;
|
||||
|
||||
public MethodSymbol sym;
|
||||
|
||||
public MethodCallExpr(Expr rcvr, String methodName, List<Expr> arguments) {
|
||||
super(-1);
|
||||
assert rcvr != null && methodName != null && arguments != null;
|
||||
|
@ -588,6 +639,8 @@ public abstract class Ast {
|
|||
|
||||
public String type;
|
||||
public String name;
|
||||
public VariableSymbol sym;
|
||||
|
||||
public VarDecl(String type, String name) {
|
||||
this(0, type, name);
|
||||
}
|
||||
|
@ -631,6 +684,7 @@ public abstract class Ast {
|
|||
public String name;
|
||||
public List<String> argumentTypes;
|
||||
public List<String> argumentNames;
|
||||
public MethodSymbol sym;
|
||||
public MethodDecl(
|
||||
String returnType,
|
||||
String name,
|
||||
|
@ -672,6 +726,8 @@ public abstract class Ast {
|
|||
|
||||
public String name;
|
||||
public String superClass;
|
||||
public ClassSymbol sym;
|
||||
|
||||
public ClassDecl(
|
||||
String name,
|
||||
String superClass,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue