From e7e8d7751690c80cd2dd681f548d069d053cf23e Mon Sep 17 00:00:00 2001 From: bd-912 Date: Thu, 25 Apr 2024 13:52:13 -0600 Subject: Fix first bugs in BoilVisitor for new SymbolTable --- J2V.java | 9 +++++---- boil/library/BoilVisitor.java | 33 ++++++++++++++++++--------------- st/SymTableVars.java | 6 ++++-- st/SymbolTable.java | 23 +++++++++++++++++++++-- st/TokenKey.java | 9 +++++---- 5 files changed, 53 insertions(+), 27 deletions(-) diff --git a/J2V.java b/J2V.java index 16f26df..bc64f83 100644 --- a/J2V.java +++ b/J2V.java @@ -27,12 +27,13 @@ public class J2V { root.accept(new SymTableMethods(), symt); MinimalLogger.info("Populating variables..."); root.accept(new SymTableVars(), symt); + MinimalLogger.info(symt.toString()); - // BoilVisitor vp = new BoilVisitor(symt); - // root.accept(vp, null); + BoilVisitor vp = new BoilVisitor(symt); + root.accept(vp, null); - // MinimalLogger.info("==================================================="); - // System.out.println(vp.getVapor()); + MinimalLogger.info("==================================================="); + System.out.println(vp.getVapor()); } diff --git a/boil/library/BoilVisitor.java b/boil/library/BoilVisitor.java index 00d7dc7..cbbb434 100644 --- a/boil/library/BoilVisitor.java +++ b/boil/library/BoilVisitor.java @@ -37,7 +37,7 @@ public class BoilVisitor extends GJDepthFirst { return attr_index; } - public String memoryReadFilter(TokenKey rhs) { + public String memoryReadFilter(String rhs) { /** * when a method recieves an id of either an alias * or an untranslated variable name, this method will @@ -174,11 +174,11 @@ public class BoilVisitor extends GJDepthFirst { String _ret=null; this.tf.reset(); String id = n.f1.f0.tokenImage; - this.symt.setActive(TypeEnum.classname, id); - this.symt.setActive(TypeEnum.method, "main"); MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); + this.symt.setActive(TypeEnum.classname, symt.getClass(id)); + this.symt.setActive(TypeEnum.method, symt.getMethod(n.f6.tokenImage)); /////////////////////////////////////////////////////////////// this.addVapor("func Main()\n"); n.f1.accept(this, argu); @@ -188,6 +188,7 @@ public class BoilVisitor extends GJDepthFirst { this.addVapor(" ret\n\n"); /////////////////////////////////////////////////////////////// this.symt.removeActive(TypeEnum.method); + symt.removeActive(TypeEnum.classname); MinimalLogger.info(String.format("<- %s (%s)", n.getClass().getSimpleName(), id)); @@ -222,10 +223,10 @@ public class BoilVisitor extends GJDepthFirst { public String visit(ClassDeclaration n, String argu) { String _ret=null; String id = n.f1.f0.tokenImage; - this.symt.setActive(TypeEnum.classname, id); MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); + this.symt.setActive(TypeEnum.classname, symt.getClass(id)); /////////////////////////////////////////////////////////////// this.addVapor(String.format("const functable_%s\n", id)); for (MethodInstance mtd : this.symt.getClass(id).getMethods()) { @@ -255,10 +256,10 @@ public class BoilVisitor extends GJDepthFirst { public String visit(ClassExtendsDeclaration n, String argu) { String _ret=null; String id = n.f1.f0.tokenImage; - this.symt.setActive(TypeEnum.classname, id); MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); + this.symt.setActive(TypeEnum.classname, symt.getClass(id)); /////////////////////////////////////////////////////////////// this.addVapor(String.format("const functable_%s\n", id)); for (MethodInstance mtd : this.symt.getClass(id).getMethods()) { @@ -320,10 +321,10 @@ public class BoilVisitor extends GJDepthFirst { String _ret=null; this.tf.reset(); String id = n.f2.f0.tokenImage; - this.symt.setActive(TypeEnum.method, id); MinimalLogger.info(String.format("-> %s (%s)", n.getClass().getSimpleName(), id)); + this.symt.setActive(TypeEnum.method, symt.getMethod(id)); /////////////////////////////////////////////////////////////// // n.f1.accept(this, argu); this.addVapor(String.format("func %s_%s(this ", @@ -531,7 +532,7 @@ public class BoilVisitor extends GJDepthFirst { } if ((t = this.symt.getType(lhs)) != null) { // memory store - ClassInstance cur = this.symt.getClass(this.symt.getActive(TypeEnum.classname)); + ClassInstance cur = (ClassInstance) this.symt.getActive(TypeEnum.classname); attr_index = getVarIndex(cur, t); lhs = String.format("[this+%d]", attr_index); } @@ -873,7 +874,7 @@ public class BoilVisitor extends GJDepthFirst { /** * f0 -> PrimaryExpression() * f1 -> "." - * f2 -> "length"l + * f2 -> "length" */ // Expressions return a TYPE alias which is equal to the expression! public String visit(ArrayLength n, String argu) { @@ -925,10 +926,10 @@ public class BoilVisitor extends GJDepthFirst { // we'll do everything here and exit the function MinimalLogger.info("Message send found THIS"); rhs = n.f0.accept(this, argu); - ClassInstance cur = this.symt.getClass(this.symt.getActive(TypeEnum.classname)); + ClassInstance cur = (ClassInstance) this.symt.getActive(TypeEnum.classname); MinimalLogger.info("Calculating method index to call..."); int mtdIndex = cur.getMethods() - .indexOf(this.symt.getMethod(mtd)) * 4; + .indexOf(this.symt.getMethod(mtd, cur)) * 4; this.addVapor(n.f4.accept(this, argu)); this.addVapor(String.format(" %s = [%s+%d]\n", @@ -939,7 +940,7 @@ public class BoilVisitor extends GJDepthFirst { _ret, rhs)); this.addVapor(String.format(" %s)\n", - this.tf.retrieveRecentList(this.symt.getMethod(mtd) + this.tf.retrieveRecentList(this.symt.getMethod(mtd, cur) .getArguments() .size()))); return _ret; @@ -947,7 +948,9 @@ public class BoilVisitor extends GJDepthFirst { MinimalLogger.info("Message send found ANONYMOUS"); // expand the entire object out! rhs = n.f0.accept(this, argu); - t = new TypeInstance(this.getUniqueID(), TypeEnum.ERROR); + t = new TypeInstance(this.getUniqueID(), TypeEnum.ERROR, + (MethodInstance) this.symt.getActive(TypeEnum.method), + (ClassInstance) this.symt.getActive(TypeEnum.classname)); t.addClassInstance(this.symt.getClass(rhs.substring(rhs.indexOf('_')+1))); this.addVapor(String.format(" %s = HeapAllocZ(%d)\n", @@ -977,7 +980,7 @@ public class BoilVisitor extends GJDepthFirst { MinimalLogger.severe("t: " + t); MinimalLogger.severe("t.getClassInstance() " + t.getClassInstance()); int mtdIndex = t.getClassInstance().getMethods() - .indexOf(this.symt.getMethod(mtd)) * 4; + .indexOf(this.symt.getMethod(mtd, t.getClassInstance())) * 4; this.vapor += String.format(" %s = [%s+%d]\n", tp2, @@ -992,7 +995,7 @@ public class BoilVisitor extends GJDepthFirst { this.tf.alias(t.getName()))); this.addVapor(String.format(" %s)\n", - this.tf.retrieveRecentList(this.symt.getMethod(mtd) + this.tf.retrieveRecentList(this.symt.getMethod(mtd, t.getClassInstance()) .getArguments() .size()))); /////////////////////////////////////////////////////////////// @@ -1125,7 +1128,7 @@ public class BoilVisitor extends GJDepthFirst { n.getClass().getSimpleName(), id)); /////////////////////////////////////////////////////////////// - ClassInstance cur = this.symt.getClass(this.symt.getActive(TypeEnum.classname)); + ClassInstance cur = (ClassInstance) this.symt.getActive(TypeEnum.classname); TypeInstance t = this.symt.getType(id); if (cur.getLocals().contains(t)) { MinimalLogger.info(String.format("Identifier found a class variable %s", diff --git a/st/SymTableVars.java b/st/SymTableVars.java index f356a8c..36f02fc 100644 --- a/st/SymTableVars.java +++ b/st/SymTableVars.java @@ -122,6 +122,7 @@ public class SymTableVars extends GJDepthFirst { (ClassInstance) symt.getActive(TypeEnum.classname), (MethodInstance) symt.getActive(TypeEnum.method)); TypeEnum rtrn = TypeEnum.ERROR; + String cls = null; switch (n.f0.f0.which) { case 0: rtrn = TypeEnum.intarray; break; @@ -130,7 +131,8 @@ public class SymTableVars extends GJDepthFirst { case 2: rtrn = TypeEnum.integer; break; case 3: - rtrn = TypeEnum.classname; break; + rtrn = TypeEnum.classname; + cls = ((Identifier) n.f0.f0.choice).f0.tokenImage; break; default: MinimalLogger.severe("Unsupported case"); } @@ -139,7 +141,7 @@ public class SymTableVars extends GJDepthFirst { (ClassInstance) symt.getActive(TypeEnum.classname)); symt.put(id, instance); symt.addLocal(id.getName()); - + symt.addClassInstance(instance, cls); return null; } diff --git a/st/SymbolTable.java b/st/SymbolTable.java index a8a6dcf..ed1506b 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -109,6 +109,9 @@ public class SymbolTable { this.getClass(c) : null; + if (cls != null) + MinimalLogger.info(String.format("%s is an instance of class %s", + t.getName(), c)); t.addClassInstance(cls); } @@ -159,6 +162,20 @@ public class SymbolTable { return ret; } + public MethodInstance getMethod(String name, ClassInstance c) { + TokenKey id = new TokenKey(name, + c, + null); + AbstractInstance symbol; + MethodInstance ret = ((symbol = this.symt.get(id)) != + null && symbol instanceof MethodInstance) ? + (MethodInstance) symbol : null; + if (ret == null) + MinimalLogger.severe(String.format("getMethod returning null for missing alias %s!", + id)); + return ret; + } + public ClassInstance getClass(String name) { TokenKey id = new TokenKey(name, null, @@ -170,11 +187,13 @@ public class SymbolTable { if (ret == null) MinimalLogger.severe(String.format("getClass returning null for missing alias %s!", id)); - return ret; + MinimalLogger.severe(String.format("It was: %s", + this.symt.get(id))); + return ret; } public AbstractInstance getActive(TypeEnum type) { - return this.active.get(type); + return this.active.get(type); } } diff --git a/st/TokenKey.java b/st/TokenKey.java index 54086c5..941c83f 100644 --- a/st/TokenKey.java +++ b/st/TokenKey.java @@ -29,11 +29,12 @@ public class TokenKey { boolean ret = false; TokenKey o; if (other instanceof TokenKey && - (o = (TokenKey) other).name == this.name && - o.cls == this.cls && - o.mtd == this.mtd) { + (o = (TokenKey) other).name.equals(this.name) && + ((o.cls == null && this.cls == null) || + (o.cls != null && o.cls.equals(this.cls))) && + ((o.mtd == null && this.mtd == null) || + (o.mtd != null && o.mtd.equals(this.mtd)))) ret = true; - } return ret; } -- cgit v1.2.3