From 024cddfa95cdeed073dd4eebcd8edd96b1d93955 Mon Sep 17 00:00:00 2001 From: Apoorva Ranade Date: Sun, 25 Apr 2021 22:15:20 +0530 Subject: [PATCH] Analyze if expr and stmt --- src/main/java/chocopy/pa3/CodeGenImpl.java | 54 ++++++++++++++-------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index 9a264a7..7e3a7ef 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -68,7 +68,7 @@ public class CodeGenImpl extends CodeGenBase { private final Label errorDiv = new Label("error.Div"); /** Index out of bounds. */ private final Label errorOob = new Label("error.OOB"); - + /** * Emits the top level of the program. * @@ -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,38 +241,52 @@ 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(IfExpr node) { + public Void analyze(BinaryExpr node) { + elseBlock = generateLocalLabel(); + return null; + } + + @Override + 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) { @@ -276,7 +295,6 @@ public class CodeGenImpl extends CodeGenBase { } - @Override public Void analyze(ListExpr node) { // statement