Added list expr analyze

master
Apoorva Ranade 4 years ago
parent 1fb6f3f085
commit 530f3ce6ea

@ -130,7 +130,7 @@ public class CodeGenImpl extends CodeGenBase
} }
/** An analyzer that encapsulates code generation for statements. */ /** An analyzer that encapsulates code generation for statements. */
private class StmtAnalyzer extends AbstractNodeAnalyzer<Void> private class StmtAnalyzer extends AbstractNodeAnalyzer<Register>
{ {
/* /*
* The symbol table has all the info you need to determine what a given * The symbol table has all the info you need to determine what a given
@ -191,7 +191,7 @@ public class CodeGenImpl extends CodeGenBase
// FIXME: Example of statement. // FIXME: Example of statement.
@Override @Override
public Void analyze(ReturnStmt stmt) public Register analyze(ReturnStmt stmt)
{ {
// FIXME: Here, we emit an instruction that does nothing. Clearly, // FIXME: Here, we emit an instruction that does nothing. Clearly,
// this is wrong, and you'll have to fix it. // this is wrong, and you'll have to fix it.
@ -202,38 +202,38 @@ public class CodeGenImpl extends CodeGenBase
} }
@Override @Override
public Void analyze(NoneLiteral node) public Register analyze(NoneLiteral node)
{ {
backend.emitMV(Register.A0, Register.ZERO, "Load none"); backend.emitMV(Register.A0, Register.ZERO, "Load none");
return null; return Register.A0;
} }
@Override @Override
public Void analyze(StringLiteral node) public Register analyze(StringLiteral node)
{ {
Label l = constants.getStrConstant(node.value); Label l = constants.getStrConstant(node.value);
backend.emitLA(Register.A0, l, "Load string literal"); backend.emitLA(Register.A0, l, "Load string literal");
return null; return Register.A0;
} }
@Override @Override
public Void analyze(IntegerLiteral node) public Register analyze(IntegerLiteral node)
{ {
backend.emitLI(Register.A0, node.value, "Load integer literal "+node.value); backend.emitLI(Register.A0, node.value, "Load integer literal "+node.value);
return null; return Register.A0;
} }
@Override @Override
public Void analyze(BooleanLiteral node) public Register analyze(BooleanLiteral node)
{ {
if(node.value==true) if(node.value==true)
backend.emitLI(Register.A0, 1, "Load boolean literal: true "); backend.emitLI(Register.A0, 1, "Load boolean literal: true ");
else else
backend.emitLI(Register.A0, 0, "Load boolean literal: false "); backend.emitLI(Register.A0, 0, "Load boolean literal: false ");
return null; return Register.A0;
} }
@Override @Override
public Void analyze(AssignStmt node) public Register analyze(AssignStmt node)
{ {
Type t = node.value.getInferredType(); Type t = node.value.getInferredType();
if(t.isSpecialType()) if(t.isSpecialType() || t.isListType())
{ {
node.value.dispatch(this); node.value.dispatch(this);
if (sym.getParent() == null) if (sym.getParent() == null)
@ -250,7 +250,7 @@ public class CodeGenImpl extends CodeGenBase
{ {
StackVarInfo svi = (StackVarInfo) sym.get(((Identifier)target).name); StackVarInfo svi = (StackVarInfo) sym.get(((Identifier)target).name);
int loc = offsetMap.get(svi); int loc = offsetMap.get(svi);
backend.emitSW(Register.A0, Register.FP, loc*4, "Load local variable: "+svi.getVarName()); backend.emitSW(Register.A0, Register.FP, -loc*4, "Load local variable: "+svi.getVarName());
} }
} }
} }
@ -258,17 +258,17 @@ public class CodeGenImpl extends CodeGenBase
{//TODO: Object Assignment {//TODO: Object Assignment
} }
return null; return Register.A0;
} }
@Override @Override
public Void analyze(ExprStmt node) public Register analyze(ExprStmt node)
{ {
node.expr.dispatch(this); node.expr.dispatch(this);
return null; return null;
} }
@Override @Override
public Void analyze(IfExpr node) public Register analyze(IfExpr node)
{ {
node.condition.dispatch(this); node.condition.dispatch(this);
Label ln = generateLocalLabel(); Label ln = generateLocalLabel();
@ -280,7 +280,7 @@ public class CodeGenImpl extends CodeGenBase
return null; return null;
} }
@Override @Override
public Void analyze(IfStmt node) public Register analyze(IfStmt node)
{ {
node.condition.dispatch(this); node.condition.dispatch(this);
Label ln = generateLocalLabel(); Label ln = generateLocalLabel();
@ -294,14 +294,14 @@ public class CodeGenImpl extends CodeGenBase
return null; return null;
} }
@Override @Override
public Void analyze(BinaryExpr node) public Register analyze(BinaryExpr node)
{ {
node.left.dispatch(this); node.left.dispatch(this);
backend.emitSW(Register.A0, Register.FP, offset*4, "Push on stack slot "+offset); backend.emitSW(Register.A0, Register.FP, -offset*4, "Push on stack slot "+offset);
offset++; offset++;
node.right.dispatch(this); node.right.dispatch(this);
offset--; offset--;
backend.emitLW(Register.T0, Register.FP, (offset)*4, "Pop stack slot "+offset); backend.emitLW(Register.T0, Register.FP, -offset*4, "Pop stack slot "+offset);
// Arithmetic Operators // Arithmetic Operators
if(node.operator.equals("+")) if(node.operator.equals("+"))
backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation"); backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation");
@ -339,7 +339,7 @@ public class CodeGenImpl extends CodeGenBase
return null; return null;
} }
@Override @Override
public Void analyze(UnaryExpr node) public Register analyze(UnaryExpr node)
{ {
node.operand.dispatch(this); node.operand.dispatch(this);
if(node.operator.equals("-")) if(node.operator.equals("-"))
@ -347,11 +347,11 @@ public class CodeGenImpl extends CodeGenBase
backend.emitLI(Register.T0, -1, "Set value of Registr T0 to -1"); backend.emitLI(Register.T0, -1, "Set value of Registr T0 to -1");
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Multiply by -1"); backend.emitMUL(Register.A0, Register.A0, Register.T0, "Multiply by -1");
} }
return null; return Register.A0;
} }
@Override @Override
public Void analyze(Identifier node) public Register analyze(Identifier node)
{ {
if (sym.getParent() == null) if (sym.getParent() == null)
{ {
@ -362,27 +362,27 @@ public class CodeGenImpl extends CodeGenBase
{ {
StackVarInfo svi = (StackVarInfo) sym.get(node.name); StackVarInfo svi = (StackVarInfo) sym.get(node.name);
int loc = offsetMap.get(svi); int loc = offsetMap.get(svi);
backend.emitLW(Register.A0, Register.FP, loc*4, "Load local variable: "+svi.getVarName()); backend.emitLW(Register.A0, Register.FP, -loc*4, "Load local variable: "+svi.getVarName());
} }
return null; return null;
} }
@Override @Override
public Void analyze(VarDef node) public Register analyze(VarDef node)
{ {
StackVarInfo svi = (StackVarInfo) sym.get(node.var.identifier.name); StackVarInfo svi = (StackVarInfo) sym.get(node.var.identifier.name);
node.value.dispatch(this); node.value.dispatch(this);
backend.emitSW(Register.A0, Register.FP, offset*4, "Store variable "+node.var.identifier.name+" value in Stack"); backend.emitSW(Register.A0, Register.FP, -offset*4, "Store variable "+node.var.identifier.name+" value in Stack");
offsetMap.put(svi, offset); offsetMap.put(svi, offset);
offset++; offset++;
return null; return null;
} }
@Override @Override
public Void analyze(TypedVar node) public Register analyze(TypedVar node)
{ {
return null; return null;
} }
@Override @Override
public Void analyze(WhileStmt node) public Register analyze(WhileStmt node)
{ {
Label startLoop = generateLocalLabel(); Label startLoop = generateLocalLabel();
backend.emitLocalLabel(startLoop, "Beginning of while loop"); backend.emitLocalLabel(startLoop, "Beginning of while loop");
@ -395,29 +395,56 @@ public class CodeGenImpl extends CodeGenBase
return null; return null;
} }
@Override @Override
public Void analyze(ForStmt node) { public Register analyze(ListExpr node) {
// control flow int l = node.elements.size();
return defaultAction(node); int i = l;
for(Expr exp:node.elements)
{
Register r = exp.dispatch(this);
backend.emitSW(r,Register.FP,-offset*4,"Push argument "+i+" from last.");
offset++;
i--;
}
backend.emitLI(Register.A0, l, "Pass list length");
backend.emitSW(Register.A0, Register.FP, -offset*4, "Push argument "+i+" from last.");
offset++;
backend.emitADDI(Register.SP, Register.SP, -offset*4, "Set SP to last argument.");
//TODO: Store reference to variable
return Register.A0;
} }
@Override @Override
public Void analyze(IndexExpr node) { public Register analyze(ForStmt node) {
// statement System.out.println(node);
return defaultAction(node); /*
node.
Label startLoop = generateLocalLabel();
backend.emitLocalLabel(startLoop, "Beginning of while loop");
node.condition.dispatch(this);
Label endLoop = elseBlock;
for(Stmt stmt:node.body)
stmt.dispatch(this);
backend.emitJ(startLoop, "Jump to beginning of loop");
backend.emitLocalLabel(endLoop, "End of while loop");*/
return null;
} }
@Override @Override
public Void analyze(ListExpr node) { public Register analyze(IndexExpr node) {
// statement System.out.println(node);
return defaultAction(node); return defaultAction(node);
} }
@Override @Override
public Void analyze(ListType node) { public Register analyze(ListType node) {
// statement System.out.println(node);
return defaultAction(node); return defaultAction(node);
} }

Loading…
Cancel
Save