Homework 2

This commit is contained in:
Carlos Galindo 2020-01-15 22:30:09 +01:00
parent 12f678a924
commit bf60a078d7
Signed by: kauron
GPG key ID: 83E68706DEE119A3
64 changed files with 4786 additions and 1185 deletions

View file

@ -1,13 +1,13 @@
package cd.ir;
import cd.util.Pair;
import cd.util.debug.AstOneLine;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import cd.util.Pair;
import cd.util.debug.AstOneLine;
public abstract class Ast {
/**
@ -17,7 +17,6 @@ public abstract class Ast {
* <p><b>Note:</b> this list may contain null pointers!
*/
public final List<Ast> rwChildren;
final String VAR_LABEL_FORMAT = "var_%s";
protected Ast(int fixedCount) {
if (fixedCount == -1)
@ -69,27 +68,17 @@ public abstract class Ast {
/** Base class for all expressions */
public static abstract class Expr extends Ast {
int needed = -1;
protected Expr(int fixedCount) {
super(fixedCount);
}
@Override
public <R, A> R accept(AstVisitor<R, A> visitor, A arg) {
return this.accept((ExprVisitor<R, A>) visitor, 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);
/**
* Registers needed to perform the operation represented by this Expr node
* If not already run, the cost is visiting all subnodes, else the cost is constant
* @return Minimum number of registers needed
*/
public abstract int registersNeeded();
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) {
return item;
@ -108,21 +97,7 @@ public abstract class Ast {
setLeft(left);
setRight(right);
}
@Override
public int registersNeeded() {
if (needed != -1)
return needed;
int leftNeed = left().registersNeeded();
int rightNeed = right().registersNeeded();
if (leftNeed > rightNeed)
return needed = leftNeed;
else if (leftNeed < rightNeed)
return needed = rightNeed;
else
return needed = ++rightNeed;
}
public Expr left() { return (Expr) this.rwChildren.get(0); }
public void setLeft(Expr node) { this.rwChildren.set(0, node); }
@ -138,12 +113,7 @@ public abstract class Ast {
assert arg != null;
setArg(arg);
}
@Override
public int registersNeeded() {
return arg().registersNeeded();
}
public Expr arg() { return (Expr) this.rwChildren.get(0); }
public void setArg(Expr node) { this.rwChildren.set(0, node); }
@ -154,11 +124,6 @@ public abstract class Ast {
public LeafExpr() {
super(0);
}
@Override
public int registersNeeded() {
return 1;
}
}
/** Represents {@code this}, the current object */
@ -374,7 +339,7 @@ public abstract class Ast {
public static class Var extends LeafExpr {
public String name;
/**
* Use this constructor to build an instance of this AST
* in the parser.
@ -382,11 +347,6 @@ public abstract class Ast {
public Var(String name) {
this.name = name;
}
public String getLabel() {
return String.format(VAR_LABEL_FORMAT, name);
}
@Override
public <R, A> R accept(ExprVisitor<R, A> visitor, A arg) {
return visitor.var(this, arg);
@ -448,12 +408,7 @@ public abstract class Ast {
public <R, A> R accept(ExprVisitor<R, A> visitor, A arg) {
return visitor.methodCall(this, arg);
}
@Override
public int registersNeeded() {
throw new RuntimeException("Not implemented");
}
}
// _________________________________________________________________
@ -643,10 +598,6 @@ public abstract class Ast {
this.name = name;
}
public String getLabel() {
return String.format(VAR_LABEL_FORMAT, name);
}
@Override
public <R, A> R accept(AstVisitor<R, A> visitor, A arg) {
return visitor.varDecl(this, arg);