diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-17 12:22:59 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-17 12:22:59 -0600 |
commit | df976c9bac104d44c8e7b446235ad73e956b5415 (patch) | |
tree | 332a55b1f9c5359be01efa88467d5839289809c7 /boil/library | |
parent | c14f3676114df656e5c6c07b49351c74496aaf04 (diff) |
TypeFactory now maps "strings" to temp alias
Diffstat (limited to 'boil/library')
-rw-r--r-- | boil/library/BoilSimp.java | 130 | ||||
-rw-r--r-- | boil/library/TypeFactory.java | 5 |
2 files changed, 66 insertions, 69 deletions
diff --git a/boil/library/BoilSimp.java b/boil/library/BoilSimp.java index 1de93d1..561bbcd 100644 --- a/boil/library/BoilSimp.java +++ b/boil/library/BoilSimp.java @@ -6,16 +6,18 @@ import st.*; import misc.*; import java.util.*; -public class BoilSimp extends GJDepthFirst<String,String> { +public class BoilSimp extends GJDepthFirst<String,TypeInstance> { private String vapor; // the collected vapor program private TypeFactory tf; // the shared type generator + private int id; private SymbolTable symt; public BoilSimp(SymbolTable symt) { this.symt = symt; this.vapor = ""; this.tf = new TypeFactory(); + this.id = 0; } public String getVapor() { @@ -25,7 +27,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { // // Auto class visitors--probably don't need to be overridden. // - public String visit(NodeList n, String args) { + public String visit(NodeList n, TypeInstance args) { String mod = ""; int _count=0; for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { @@ -35,7 +37,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { return mod; } - public String visit(NodeListOptional n, String args) { + public String visit(NodeListOptional n, TypeInstance args) { String mod = ""; if ( n.present() ) { int _count=0; @@ -49,14 +51,14 @@ public class BoilSimp extends GJDepthFirst<String,String> { return ""; } - public String visit(NodeOptional n, String args) { + public String visit(NodeOptional n, TypeInstance args) { if ( n.present() ) return n.node.accept(this,args); else return ""; } - public String visit(NodeSequence n, String args) { + public String visit(NodeSequence n, TypeInstance args) { String mod = ""; int _count=0; for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { @@ -66,7 +68,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { return mod; } - public String visit(NodeToken n, String args) { return ""; } + public String visit(NodeToken n, TypeInstance args) { return ""; } // // User-generated visitor methods below @@ -77,7 +79,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> ( TypeDeclaration() )* * f2 -> <EOF> */ - public String visit(Goal n, String args) { + public String visit(Goal n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -105,7 +107,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f16 -> "}" * f17 -> "}" */ - public String visit(MainClass n, String args) { + public String visit(MainClass n, TypeInstance args) { String id = n.f1.f0.tokenImage; this.symt.setActive(TypeEnum.classname, id); @@ -145,7 +147,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f0 -> ClassDeclaration() * | ClassExtendsDeclaration() */ - public String visit(TypeDeclaration n, String args) { + public String visit(TypeDeclaration n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); return mod; @@ -159,7 +161,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ - public String visit(ClassDeclaration n, String args) { + public String visit(ClassDeclaration n, TypeInstance args) { String id = n.f1.f0.tokenImage; this.symt.setActive(TypeEnum.classname, id); String mod = ""; @@ -189,7 +191,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ - public String visit(ClassExtendsDeclaration n, String args) { + public String visit(ClassExtendsDeclaration n, TypeInstance args) { String id = n.f1.f0.tokenImage; this.symt.setActive(TypeEnum.classname, id); String mod = ""; @@ -210,7 +212,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> Identifier() * f2 -> ";" */ - public String visit(VarDeclaration n, String args) { + public String visit(VarDeclaration n, TypeInstance args) { String mod = ""; String cls = n.f0.accept(this, args); @@ -218,10 +220,10 @@ public class BoilSimp extends GJDepthFirst<String,String> { TypeInstance t = this.symt.getType(id); this.vapor += String.format(" %s = HeapAllocZ(%d)\n", - this.tf.alias(t), + this.tf.alias(t.getName()), t.getSize()); this.vapor += String.format(" if0 %s goto :error\n", - this.tf.alias(this.symt.getType(id))); + this.tf.alias(this.symt.getType(id).getName())); n.f2.accept(this, args); return mod; } @@ -241,7 +243,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f11 -> ";" * f12 -> "}" */ - public String visit(MethodDeclaration n, String args) { + public String visit(MethodDeclaration n, TypeInstance args) { String id = n.f2.f0.tokenImage; this.symt.setActive(TypeEnum.method, id); this.tf.reset(); @@ -262,21 +264,20 @@ public class BoilSimp extends GJDepthFirst<String,String> { .getClassInstance() .getLocals()) { this.vapor += String.format(" %s = this\n", - this.tf.alias(attr)); + this.tf.alias(attr.getName())); } n.f7.accept(this, args); n.f8.accept(this, args); n.f9.accept(this, args); - String ret = n.f10.accept(this, args); // FIXME + String ret = n.f10.accept(this, args); n.f11.accept(this, args); n.f12.accept(this, args); - TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); this.vapor += String.format(" %s = %s\n ret %s\n\n", - this.tf.alias(tp1), + this.tf.alias(Integer.toString(this.id++)), ret, - this.tf.alias(tp1)); + this.tf.alias(Integer.toString(this.id-1))); this.symt.removeActive(TypeEnum.method); return mod; @@ -286,7 +287,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f0 -> FormalParameter() * f1 -> ( FormalParameterRest() )* */ - public String visit(FormalParameterList n, String args) { + public String visit(FormalParameterList n, TypeInstance args) { String mod = ""; String arg = n.f0.accept(this, args); if (arg != null) @@ -299,7 +300,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f0 -> Type() * f1 -> Identifier() */ - public String visit(FormalParameter n, String args) { + public String visit(FormalParameter n, TypeInstance args) { String mod = ""; mod += n.f0.accept(this, args); mod += n.f1.accept(this, args); @@ -310,7 +311,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f0 -> "," * f1 -> FormalParameter() */ - public String visit(FormalParameterRest n, String args) { + public String visit(FormalParameterRest n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); String arg = n.f1.accept(this, args); @@ -325,7 +326,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * | IntegerType() * | Identifier() */ - public String visit(Type n, String args) { + public String visit(Type n, TypeInstance args) { String mod = ""; mod += n.f0.accept(this, args); return mod; @@ -336,7 +337,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> "[" * f2 -> "]" */ - public String visit(ArrayType n, String args) { + public String visit(ArrayType n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -347,7 +348,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { /** * f0 -> "boolean" */ - public String visit(BooleanType n, String args) { + public String visit(BooleanType n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); return mod; @@ -356,7 +357,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { /** * f0 -> "int" */ - public String visit(IntegerType n, String args) { + public String visit(IntegerType n, TypeInstance args) { String mod = ""; n.f0.accept(this, args);; return mod; @@ -370,7 +371,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * | WhileStatement() * | PrintStatement() */ - public String visit(Statement n, String args) { + public String visit(Statement n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); return mod; @@ -381,7 +382,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> ( Statement() )* * f2 -> "}" */ - public String visit(Block n, String args) { + public String visit(Block n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -395,7 +396,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f2 -> Expression() * f3 -> ";" */ - public String visit(AssignmentStatement n, String args) { + public String visit(AssignmentStatement n, TypeInstance args) { String mod = ""; String id = n.f0.accept(this, args); @@ -422,7 +423,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { } // if (t == null ) { // anonymous? - // t = new TypeInstance("tp1", TypeEnum.classname); // FIXME maybe this is wrong for arrays? + // t = new TypeInstance(Integer.toString(this.id++), TypeEnum.classname); // FIXME maybe this is wrong for arrays? // t.addClassInstance(this.symt.getClass(cls)); // } @@ -436,7 +437,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f5 -> Expression() * f6 -> ";" */ - public String visit(ArrayAssignmentStatement n, String args) { + public String visit(ArrayAssignmentStatement n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -457,7 +458,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f5 -> "else" * f6 -> Statement() */ - public String visit(IfStatement n, String args) { + public String visit(IfStatement n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -476,7 +477,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f3 -> ")" * f4 -> Statement() */ - public String visit(WhileStatement n, String args) { + public String visit(WhileStatement n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -493,17 +494,16 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f3 -> ")" * f4 -> ";" */ - public String visit(PrintStatement n, String args) { + public String visit(PrintStatement n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); String expr = n.f2.accept(this, args); n.f3.accept(this, args); n.f4.accept(this, args); - TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); vapor += String.format(" %s = %s\n", - this.tf.alias(tp1), + this.tf.alias(Integer.toString(this.id++)), expr); vapor += String.format(" PrintIntS(%s)\n", @@ -522,7 +522,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * | MessageSend() * | PrimaryExpression() */ - public String visit(Expression n, String args) { + public String visit(Expression n, TypeInstance args) { String mod = ""; mod += n.f0.accept(this, args); return mod; @@ -533,7 +533,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> "&&" * f2 -> PrimaryExpression() */ - public String visit(AndExpression n, String args) { + public String visit(AndExpression n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -546,7 +546,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> "<" * f2 -> PrimaryExpression() */ - public String visit(CompareExpression n, String args) { + public String visit(CompareExpression n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -559,7 +559,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> "+" * f2 -> PrimaryExpression() */ - public String visit(PlusExpression n, String args) { + public String visit(PlusExpression n, TypeInstance args) { String mod = ""; String oper1 = n.f0.accept(this, args); n.f1.accept(this, args); @@ -577,7 +577,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> "-" * f2 -> PrimaryExpression() */ - public String visit(MinusExpression n, String args) { + public String visit(MinusExpression n, TypeInstance args) { String mod = ""; String oper1 = n.f0.accept(this, args); n.f1.accept(this, args); @@ -596,7 +596,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> "*" * f2 -> PrimaryExpression() */ - public String visit(TimesExpression n, String args) { + public String visit(TimesExpression n, TypeInstance args) { String mod = ""; String oper1 = n.f0.accept(this, args); n.f1.accept(this, args); @@ -616,7 +616,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f2 -> PrimaryExpression() * f3 -> "]" */ - public String visit(ArrayLookup n, String args) { + public String visit(ArrayLookup n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -630,7 +630,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> "." * f2 -> "length" */ - public String visit(ArrayLength n, String args) { + public String visit(ArrayLength n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -646,13 +646,13 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f4 -> ( ExpressionList() )? * f5 -> ")" */ - public String visit(MessageSend n, String args) { + public String visit(MessageSend n, TypeInstance args) { String mod = ""; String id = n.f0.accept(this, args); n.f1.accept(this, args); String id2 = n.f2.accept(this, args); - TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); // TypeFactory likes to know who it's renting to - TypeInstance tp2 = new TypeInstance("tp2", TypeEnum.ERROR); + String tp1 = Integer.toString(this.id++); // TypeFactory likes to know who it's renting to + String tp2 = "tp2"; TypeInstance cur = this.symt.getType(id); @@ -664,7 +664,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { this.vapor += String.format(" %s = [%s+%d]\n", this.tf.alias(tp1), - this.tf.alias(cur), + this.tf.alias(cur.getName()), 0); this.vapor += String.format(" %s = [%s+%d]\n", @@ -678,7 +678,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { mod += String.format("call %s(%s", this.tf.alias(tp2), - this.tf.alias(cur)); + this.tf.alias(cur.getName())); mod += String.format(" %s)", this.tf.retrieveRecentList(this.symt.getMethod(id2) @@ -692,13 +692,12 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f0 -> Expression() * f1 -> ( ExpressionRest() )* */ - public String visit(ExpressionList n, String args) { + public String visit(ExpressionList n, TypeInstance args) { String mod = ""; String rhs = n.f0.accept(this, args); - TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); mod += String.format(" %s = %s\n", - this.tf.alias(tp1), + this.tf.alias(Integer.toString(this.id++)), rhs); mod += n.f1.accept(this, args); @@ -710,14 +709,13 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f0 -> "," * f1 -> Expression() */ - public String visit(ExpressionRest n, String args) { + public String visit(ExpressionRest n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); String rhs = n.f1.accept(this, args); - TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR); mod += String.format(" %s = %s\n", - this.tf.alias(tp1), + this.tf.alias(Integer.toString(this.id++)), rhs); return mod; @@ -734,7 +732,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * | NotExpression() * | BracketExpression() */ - public String visit(PrimaryExpression n, String args) { + public String visit(PrimaryExpression n, TypeInstance args) { String mod = ""; mod += n.f0.accept(this, args); return mod; @@ -743,7 +741,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { /** * f0 -> <INTEGER_LITERAL> */ - public String visit(IntegerLiteral n, String args) { + public String visit(IntegerLiteral n, TypeInstance args) { String mod = ""; mod += n.f0.tokenImage; return mod; @@ -752,7 +750,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { /** * f0 -> "true" */ - public String visit(TrueLiteral n, String args) { + public String visit(TrueLiteral n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); return mod; @@ -761,7 +759,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { /** * f0 -> "false" */ - public String visit(FalseLiteral n, String args) { + public String visit(FalseLiteral n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); return mod; @@ -770,7 +768,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { /** * f0 -> <IDENTIFIER> */ - public String visit(Identifier n, String args) { + public String visit(Identifier n, TypeInstance args) { String mod = ""; mod += n.f0.tokenImage; return mod; @@ -779,7 +777,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { /** * f0 -> "this" */ - public String visit(ThisExpression n, String args) { + public String visit(ThisExpression n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); return mod; @@ -792,7 +790,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f3 -> Expression() * f4 -> "]" */ - public String visit(ArrayAllocationExpression n, String args) { + public String visit(ArrayAllocationExpression n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -808,7 +806,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f2 -> "(" * f3 -> ")" */ - public String visit(AllocationExpression n, String args) { + public String visit(AllocationExpression n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); String cls = n.f1.accept(this, args); @@ -825,7 +823,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f0 -> "!" * f1 -> Expression() */ - public String visit(NotExpression n, String args) { + public String visit(NotExpression n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); @@ -837,7 +835,7 @@ public class BoilSimp extends GJDepthFirst<String,String> { * f1 -> Expression() * f2 -> ")" */ - public String visit(BracketExpression n, String args) { + public String visit(BracketExpression n, TypeInstance args) { String mod = ""; n.f0.accept(this, args); n.f1.accept(this, args); diff --git a/boil/library/TypeFactory.java b/boil/library/TypeFactory.java index be77f63..8c64101 100644 --- a/boil/library/TypeFactory.java +++ b/boil/library/TypeFactory.java @@ -1,19 +1,18 @@ package boil.library; import java.util.HashMap; -import st.TypeInstance; public class TypeFactory { private int type_num; - private HashMap<TypeInstance,String> map; + private HashMap<String,String> map; public void reset() { this.type_num = 0; this.map = new HashMap<>(); } - public String alias(TypeInstance t) { + public String alias(String t) { /** * Given a TypeInstance, return the designated * vapor alias. If the alias does not exist, create it. |