Analyze if expr and stmt

master
Apoorva Ranade 4 years ago
parent 2f0acdbbf5
commit 024cddfa95

@ -158,6 +158,11 @@ public class CodeGenImpl extends CodeGenBase {
/** The descriptor for the current function, or null at the top level. */
private final FuncInfo funcInfo;
/** Label of code that exits from block. */
protected Label elseBlock;
// Variable to store offset from frame pointer to identify next
// empty space on stack frame to store variable
private final int offset = 12;
/**
* An analyzer for the function described by FUNCINFO0, which is null for the
* top level.
@ -236,39 +241,53 @@ public class CodeGenImpl extends CodeGenBase {
return null;
}
@Override
public Void analyze(BinaryExpr node) {
return null;
}
@Override
public Void analyze(ExprStmt node) {
node.expr.dispatch(this);
return null;
}
@Override
public Void analyze(ForStmt node) {
// control flow
return defaultAction(node);
public Void analyze(IfExpr node) {
node.condition.dispatch(this);
Label ln = generateLocalLabel();
node.thenExpr.dispatch(this);
backend.emitJ(ln, "Jump to end of if expression");
backend.emitLocalLabel(elseBlock, "Else part of if expression");
node.elseExpr.dispatch(this);
backend.emitLocalLabel(ln, "End of if expression");
return null;
}
@Override
public Void analyze(Identifier node) {
// statement
return defaultAction(node);
public Void analyze(IfStmt node) {
node.condition.dispatch(this);
Label ln = generateLocalLabel();
for(Stmt s:node.thenBody)
s.dispatch(this);
backend.emitJ(ln, "Jump to end of if statement");
backend.emitLocalLabel(elseBlock, "Else part of if statement");
for(Stmt s:node.elseBody)
s.dispatch(this);
backend.emitLocalLabel(ln, "End of if statement");
return null;
}
@Override
public Void analyze(BinaryExpr node) {
elseBlock = generateLocalLabel();
return null;
}
@Override
public Void analyze(IfExpr node) {
public Void analyze(ForStmt node) {
// control flow
return defaultAction(node);
}
@Override
public Void analyze(IfStmt node) {
// control flow
public Void analyze(Identifier node) {
// statement
return defaultAction(node);
}
@Override
public Void analyze(IndexExpr node) {
// statement
@ -276,7 +295,6 @@ public class CodeGenImpl extends CodeGenBase {
}
@Override
public Void analyze(ListExpr node) {
// statement

Loading…
Cancel
Save