summaryrefslogtreecommitdiff
path: root/submit/ast/CompoundStatement.java
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-04-23 00:24:42 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-04-23 00:24:42 -0600
commita1c15f046183373baf5deb66e77188e656806fb7 (patch)
treefc1a7f584e67dc583d335f09d5271a45ff7d9df4 /submit/ast/CompoundStatement.java
parent5f28f80c4e25a56cd444914c2f0b3da5e7fdb088 (diff)
downloadcminus-a1c15f046183373baf5deb66e77188e656806fb7.tar.gz
cminus-a1c15f046183373baf5deb66e77188e656806fb7.zip
squash all the thingsHEADmain
Diffstat (limited to 'submit/ast/CompoundStatement.java')
-rw-r--r--submit/ast/CompoundStatement.java31
1 files changed, 30 insertions, 1 deletions
diff --git a/submit/ast/CompoundStatement.java b/submit/ast/CompoundStatement.java
index bd5628a..e8d9256 100644
--- a/submit/ast/CompoundStatement.java
+++ b/submit/ast/CompoundStatement.java
@@ -5,6 +5,9 @@
package submit.ast;
import java.util.List;
+import submit.MIPSResult;
+import submit.RegisterAllocator;
+import submit.SymbolTable;
/**
*
@@ -13,11 +16,16 @@ import java.util.List;
public class CompoundStatement extends AbstractNode implements Statement {
private final List<Statement> statements;
+ private SymbolTable symbolTable;
- public CompoundStatement(List<Statement> statements) {
+ public CompoundStatement(List<Statement> statements,
+ SymbolTable symbolTable) {
this.statements = statements;
+ this.symbolTable = symbolTable;
}
+ public SymbolTable getSymbolTable() { return symbolTable; }
+
@Override
public void toCminus(StringBuilder builder, String prefix) {
builder.append(prefix).append("{\n");
@@ -27,4 +35,25 @@ public class CompoundStatement extends AbstractNode implements Statement {
builder.append(prefix).append("}\n");
}
+ @Override
+ public MIPSResult toMIPS(StringBuilder code, StringBuilder data,
+ SymbolTable symbolTable,
+ RegisterAllocator regAllocator) {
+ code.append("# Entering a new scope.\n");
+ code.append("# Symbols in symbol table:\n");
+ for (String name : this.symbolTable.symbolNames())
+ code.append(
+ String.format("# %s : %d\n", name, this.symbolTable.offsetOf(name)));
+
+ code.append("# Update the stack pointer.\n");
+ code.append(String.format("addi $sp $sp %d\n", symbolTable.getOffset()));
+
+ for (Statement statement : statements)
+ statement.toMIPS(code, data, this.symbolTable, regAllocator);
+
+ code.append("# Exit scope.\n");
+ code.append(String.format("addi $sp $sp %s\n", -symbolTable.getOffset()));
+
+ return MIPSResult.createVoidResult();
+ }
}