From c28a1cc9d206bdde41a621b018c01980b3c8a617 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Thu, 25 Apr 2024 12:58:10 -0600 Subject: Rewrote Symbol Table to be more context aware and avoid collisions --- st/AbstractInstance.java | 13 +-- st/ClassInstance.java | 6 ++ st/MethodInstance.java | 19 +++- st/SymTableBottomUp.java | 238 ----------------------------------------------- st/SymTableClasses.java | 116 +++++++++++++++++++++++ st/SymTableMethods.java | 169 +++++++++++++++++++++++++++++++++ st/SymTableTopDown.java | 193 -------------------------------------- st/SymTableVars.java | 218 +++++++++++++++++++++++++++++++++++++++++++ st/SymbolTable.java | 30 ++++-- st/TokenKey.java | 17 ++-- st/TypeInstance.java | 41 +++++++- 11 files changed, 599 insertions(+), 461 deletions(-) delete mode 100644 st/SymTableBottomUp.java create mode 100644 st/SymTableClasses.java create mode 100644 st/SymTableMethods.java delete mode 100644 st/SymTableTopDown.java create mode 100644 st/SymTableVars.java (limited to 'st') diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java index cdeef8e..a7052a1 100644 --- a/st/AbstractInstance.java +++ b/st/AbstractInstance.java @@ -5,7 +5,6 @@ import java.util.ArrayList; public abstract class AbstractInstance { protected String name; // the literal name of the declaration protected TypeEnum type; // the type of the declaration - protected ClassInstance cls; // the surrounding class public AbstractInstance(String name, TypeEnum type) { this.type = type; @@ -16,18 +15,12 @@ public abstract class AbstractInstance { return this.name; } - public boolean equals(AbstractInstance other) { - return this.name == other.getName() && - this.type == this.type; - } + @Override public abstract boolean equals(Object other); @Override public int hashCode() { return this.name.hashCode(); } - public void addClassInstance(ClassInstance cls) { - this.cls = cls; - } public String getName() { return this.name; @@ -37,8 +30,4 @@ public abstract class AbstractInstance { return this.type; } - public ClassInstance getClassInstance() { - return this.cls; - } - } diff --git a/st/ClassInstance.java b/st/ClassInstance.java index e93d3bd..8394a2c 100644 --- a/st/ClassInstance.java +++ b/st/ClassInstance.java @@ -13,6 +13,12 @@ public class ClassInstance extends AbstractInstance { this.mtds = new ArrayList<>(); } + public boolean equals(Object other) { + ClassInstance o; + return (other instanceof ClassInstance && + ((o = (ClassInstance) other).getName() == this.getName())); + } + public ClassInstance getExtend() { /** * Returns the parent class, or diff --git a/st/MethodInstance.java b/st/MethodInstance.java index 6cf6ccc..c2f978b 100644 --- a/st/MethodInstance.java +++ b/st/MethodInstance.java @@ -5,15 +5,24 @@ import java.util.ArrayList; public class MethodInstance extends AbstractInstance { private ArrayList args; // the list of arguments private ArrayList lvars; // the list of local variables + protected ClassInstance par_cls; // the surrounding class private TypeEnum rtrn; // the returned type - public MethodInstance(String name, TypeEnum rtrn) { + public MethodInstance(String name, TypeEnum rtrn, ClassInstance par_cls) { super(name, TypeEnum.method); this.lvars = new ArrayList<>(); this.args = new ArrayList<>(); + this.par_cls = par_cls; this.rtrn = rtrn; } + public boolean equals(Object other) { + MethodInstance o; + return (other instanceof MethodInstance && + ((o = (MethodInstance) other).getName() == this.getName() && + o.getParentClass() == this.getParentClass())); + } + public ArrayList getArguments() { return this.args; } @@ -35,4 +44,12 @@ public class MethodInstance extends AbstractInstance { this.lvars.add(lvar); } + public void addParentClass(ClassInstance par_cls) { + this.par_cls = par_cls; + } + + public ClassInstance getParentClass() { + return this.par_cls; + } + } diff --git a/st/SymTableBottomUp.java b/st/SymTableBottomUp.java deleted file mode 100644 index 72a4575..0000000 --- a/st/SymTableBottomUp.java +++ /dev/null @@ -1,238 +0,0 @@ -package st; - -import syntaxtree.*; -import visitor.*; -import java.util.*; -import misc.*; - -/** - * Performs a bottom-up preliminary visit through the AST - * initializing all Instances and placing them in the passed ST - */ -public class SymTableBottomUp extends GJDepthFirst { - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "{" - * f3 -> "public" - * f4 -> "static" - * f5 -> "void" - * f6 -> "main" - * f7 -> "(" - * f8 -> "String" - * f9 -> "[" - * f10 -> "]" - * f11 -> Identifier() - * f12 -> ")" - * f13 -> "{" - * f14 -> ( VarDeclaration() )* - * f15 -> ( Statement() )* - * f16 -> "}" - * f17 -> "}" - */ - public R visit(MainClass n, SymbolTable symt) { - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - n.f6.accept(this, symt); - n.f7.accept(this, symt); - n.f8.accept(this, symt); - n.f9.accept(this, symt); - n.f10.accept(this, symt); - n.f11.accept(this, symt); - n.f12.accept(this, symt); - n.f13.accept(this, symt); - n.f14.accept(this, symt); - n.f15.accept(this, symt); - n.f16.accept(this, symt); - n.f17.accept(this, symt); - - - TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0); - ClassInstance instance = new ClassInstance(id.getName()); - symt.put(id, instance); - - id = new TokenKey(n.f6.tokenImage, n.f6.beginLine); - MethodInstance main = new MethodInstance(id.getName(), TypeEnum.ERROR); - symt.put(id, main); - - - return null; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "{" - * f3 -> ( VarDeclaration() )* - * f4 -> ( MethodDeclaration() )* - * f5 -> "}" - */ - public R visit(ClassDeclaration n, SymbolTable symt) { - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - - - TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0); - ClassInstance instance = new ClassInstance(id.getName()); - symt.put(id, instance); - - - return null; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "extends" - * f3 -> Identifier() - * f4 -> "{" - * f5 -> ( VarDeclaration() )* - * f6 -> ( MethodDeclaration() )* - * f7 -> "}" - */ - public R visit(ClassExtendsDeclaration n, SymbolTable symt) { - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - n.f6.accept(this, symt); - n.f7.accept(this, symt); - - - TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0); - ClassInstance instance = new ClassInstance(id.getName()); - symt.put(id, instance); - - - return null; - } - - /** - * f0 -> Type() - * f1 -> Identifier() - * f2 -> ";" - */ - public R visit(VarDeclaration n, SymbolTable symt) { - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - - TokenKey id = new TokenKey(n.f1.f0.tokenImage, n.f1.f0.beginLine); - TypeEnum rtrn = TypeEnum.ERROR; - switch (n.f0.f0.which) { - case 0: - rtrn = TypeEnum.intarray; break; - case 1: - rtrn = TypeEnum.bool; break; - case 2: - rtrn = TypeEnum.integer; break; - case 3: - rtrn = TypeEnum.classname; break; - default: - MinimalLogger.severe("Unsupported case"); - } - - TypeInstance instance = new TypeInstance(id.getName(), rtrn); - symt.put(id, instance); - - - return null; - } - - /** - * f0 -> "public" - * f1 -> Type() - * f2 -> Identifier() - * f3 -> "(" - * f4 -> ( FormalParameterList() )? - * f5 -> ")" - * f6 -> "{" - * f7 -> ( VarDeclaration() )* - * f8 -> ( Statement() )* - * f9 -> "return" - * f10 -> Expression() - * f11 -> ";" - * f12 -> "}" - */ - public R visit(MethodDeclaration n, SymbolTable symt) { - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - n.f6.accept(this, symt); - n.f7.accept(this, symt); - n.f8.accept(this, symt); - n.f9.accept(this, symt); - n.f10.accept(this, symt); - n.f11.accept(this, symt); - n.f12.accept(this, symt); - - - - TokenKey id = new TokenKey(n.f2.f0.tokenImage, n.f2.f0.beginLine); - TypeEnum rtrn = TypeEnum.ERROR; - switch (n.f1.f0.which) { - case 0: - rtrn = TypeEnum.intarray; break; - case 1: - rtrn = TypeEnum.bool; break; - case 2: - rtrn = TypeEnum.integer; break; - case 3: - rtrn = TypeEnum.classname; break; - default: - MinimalLogger.severe("Unsupported case"); - } - - MethodInstance instance = new MethodInstance(id.getName(), rtrn); - symt.put(id, instance); - - - return null; - } - - /** - * f0 -> Type() - * f1 -> Identifier() - */ - public R visit(FormalParameter n, SymbolTable symt) { - n.f0.accept(this, symt); - n.f1.accept(this, symt); - - - TokenKey id = new TokenKey(n.f1.f0.tokenImage, n.f1.f0.beginLine); - TypeEnum rtrn = TypeEnum.ERROR; - switch (n.f0.f0.which) { - case 0: - rtrn = TypeEnum.intarray; break; - case 1: - rtrn = TypeEnum.bool; break; - case 2: - rtrn = TypeEnum.integer; break; - case 3: - rtrn = TypeEnum.classname; break; - default: - MinimalLogger.severe("Unsupported case"); - } - - TypeInstance instance = new TypeInstance(id.getName(), rtrn); - symt.put(id, instance); - - - return null; - } - -} diff --git a/st/SymTableClasses.java b/st/SymTableClasses.java new file mode 100644 index 0000000..3c0b761 --- /dev/null +++ b/st/SymTableClasses.java @@ -0,0 +1,116 @@ +package st; + +import syntaxtree.*; +import visitor.*; +import java.util.*; +import misc.*; + +/** + * Performs a bottom-up preliminary visit through the AST + * initializing all ClassInstances and placing them in the passed ST + */ +public class SymTableClasses extends GJDepthFirst { + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> "public" + * f4 -> "static" + * f5 -> "void" + * f6 -> "main" + * f7 -> "(" + * f8 -> "String" + * f9 -> "[" + * f10 -> "]" + * f11 -> Identifier() + * f12 -> ")" + * f13 -> "{" + * f14 -> ( VarDeclaration() )* + * f15 -> ( Statement() )* + * f16 -> "}" + * f17 -> "}" + */ + public R visit(MainClass n, SymbolTable symt) { + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + n.f8.accept(this, symt); + n.f9.accept(this, symt); + n.f10.accept(this, symt); + n.f11.accept(this, symt); + n.f12.accept(this, symt); + n.f13.accept(this, symt); + n.f14.accept(this, symt); + n.f15.accept(this, symt); + n.f16.accept(this, symt); + n.f17.accept(this, symt); + + + TokenKey id = new TokenKey(n.f1.f0.tokenImage, null, null); + ClassInstance instance = new ClassInstance(id.getName()); + symt.put(id, instance); + + return null; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> ( VarDeclaration() )* + * f4 -> ( MethodDeclaration() )* + * f5 -> "}" + */ + public R visit(ClassDeclaration n, SymbolTable symt) { + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + + + TokenKey id = new TokenKey(n.f1.f0.tokenImage, null, null); + ClassInstance instance = new ClassInstance(id.getName()); + symt.put(id, instance); + + + return null; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "extends" + * f3 -> Identifier() + * f4 -> "{" + * f5 -> ( VarDeclaration() )* + * f6 -> ( MethodDeclaration() )* + * f7 -> "}" + */ + public R visit(ClassExtendsDeclaration n, SymbolTable symt) { + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + + + TokenKey id = new TokenKey(n.f1.f0.tokenImage, null, null); + ClassInstance instance = new ClassInstance(id.getName()); + symt.put(id, instance); + + + return null; + } + +} diff --git a/st/SymTableMethods.java b/st/SymTableMethods.java new file mode 100644 index 0000000..6dc7d68 --- /dev/null +++ b/st/SymTableMethods.java @@ -0,0 +1,169 @@ +package st; + +import syntaxtree.*; +import visitor.*; +import java.util.*; +import misc.*; + +/** + * Performs a bottom-up preliminary visit through the AST + * initializing all Instances and placing them in the passed ST + */ +public class SymTableMethods extends GJDepthFirst { + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> "public" + * f4 -> "static" + * f5 -> "void" + * f6 -> "main" + * f7 -> "(" + * f8 -> "String" + * f9 -> "[" + * f10 -> "]" + * f11 -> Identifier() + * f12 -> ")" + * f13 -> "{" + * f14 -> ( VarDeclaration() )* + * f15 -> ( Statement() )* + * f16 -> "}" + * f17 -> "}" + */ + public R visit(MainClass n, SymbolTable symt) { + String act = n.f1.f0.tokenImage; + symt.setActive(TypeEnum.classname, symt.getClass(act)); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + n.f8.accept(this, symt); + n.f9.accept(this, symt); + n.f10.accept(this, symt); + n.f11.accept(this, symt); + n.f12.accept(this, symt); + n.f13.accept(this, symt); + n.f14.accept(this, symt); + n.f15.accept(this, symt); + n.f16.accept(this, symt); + n.f17.accept(this, symt); + + TokenKey id = new TokenKey(n.f6.tokenImage, (ClassInstance) symt.getActive(TypeEnum.classname), null); + MethodInstance main = new MethodInstance(id.getName(), TypeEnum.ERROR, (ClassInstance) symt.getActive(TypeEnum.classname)); + symt.put(id, main); + + + symt.removeActive(TypeEnum.classname); + return null; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> ( VarDeclaration() )* + * f4 -> ( MethodDeclaration() )* + * f5 -> "}" + */ + public R visit(ClassDeclaration n, SymbolTable symt) { + String act = n.f1.f0.tokenImage; + symt.setActive(TypeEnum.classname, symt.getClass(act)); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + + + symt.removeActive(TypeEnum.classname); + return null; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "extends" + * f3 -> Identifier() + * f4 -> "{" + * f5 -> ( VarDeclaration() )* + * f6 -> ( MethodDeclaration() )* + * f7 -> "}" + */ + public R visit(ClassExtendsDeclaration n, SymbolTable symt) { + String act = n.f1.f0.tokenImage; + symt.setActive(TypeEnum.classname, symt.getClass(act)); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + + + symt.removeActive(TypeEnum.classname); + return null; + } + + /** + * f0 -> "public" + * f1 -> Type() + * f2 -> Identifier() + * f3 -> "(" + * f4 -> ( FormalParameterList() )? + * f5 -> ")" + * f6 -> "{" + * f7 -> ( VarDeclaration() )* + * f8 -> ( Statement() )* + * f9 -> "return" + * f10 -> Expression() + * f11 -> ";" + * f12 -> "}" + */ + public R visit(MethodDeclaration n, SymbolTable symt) { + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + n.f8.accept(this, symt); + n.f9.accept(this, symt); + n.f10.accept(this, symt); + n.f11.accept(this, symt); + n.f12.accept(this, symt); + + + + TokenKey id = new TokenKey(n.f2.f0.tokenImage, (ClassInstance) symt.getActive(TypeEnum.classname), null); + TypeEnum rtrn = TypeEnum.ERROR; + switch (n.f1.f0.which) { + case 0: + rtrn = TypeEnum.intarray; break; + case 1: + rtrn = TypeEnum.bool; break; + case 2: + rtrn = TypeEnum.integer; break; + case 3: + rtrn = TypeEnum.classname; break; + default: + MinimalLogger.severe("Unsupported case"); + } + + MethodInstance instance = new MethodInstance(id.getName(), rtrn, (ClassInstance) symt.getActive(TypeEnum.classname)); + symt.put(id, instance); + + + return null; + } + +} diff --git a/st/SymTableTopDown.java b/st/SymTableTopDown.java deleted file mode 100644 index 5bfd971..0000000 --- a/st/SymTableTopDown.java +++ /dev/null @@ -1,193 +0,0 @@ -package st; - -import syntaxtree.*; -import visitor.*; -import java.util.*; -import misc.*; - -/** - * Performs a top-down final visit through the AST adding all local - * methods, attributes, extension, and scoping information to the ST - */ -public class SymTableTopDown extends GJDepthFirst { - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "{" - * f3 -> "public" - * f4 -> "static" - * f5 -> "void" - * f6 -> "main" - * f7 -> "(" - * f8 -> "String" - * f9 -> "[" - * f10 -> "]" - * f11 -> Identifier() - * f12 -> ")" - * f13 -> "{" - * f14 -> ( VarDeclaration() )* - * f15 -> ( Statement() )* - * f16 -> "}" - * f17 -> "}" - */ - public R visit(MainClass n, SymbolTable symt) { - TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0); - symt.setActive(TypeEnum.classname, symt.getClass(id)); - TokenKey main = new TokenKey(n.f6.tokenImage, n.f6.beginLine); - symt.setActive(TypeEnum.method, symt.getMethod(main)); - symt.addMethod(main); - - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - n.f6.accept(this, symt); - n.f7.accept(this, symt); - n.f8.accept(this, symt); - n.f9.accept(this, symt); - n.f10.accept(this, symt); - n.f11.accept(this, symt); - n.f12.accept(this, symt); - n.f13.accept(this, symt); - n.f14.accept(this, symt); - n.f15.accept(this, symt); - n.f16.accept(this, symt); - n.f17.accept(this, symt); - - symt.removeActive(TypeEnum.method); - return null; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "{" - * f3 -> ( VarDeclaration() )* - * f4 -> ( MethodDeclaration() )* - * f5 -> "}" - */ - public R visit(ClassDeclaration n, SymbolTable symt) { - TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0); - symt.setActive(TypeEnum.classname, symt.getClass(id)); - - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - return null; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "extends" - * f3 -> Identifier() - * f4 -> "{" - * f5 -> ( VarDeclaration() )* - * f6 -> ( MethodDeclaration() )* - * f7 -> "}" - */ - public R visit(ClassExtendsDeclaration n, SymbolTable symt) { - TokenKey id = new TokenKey(n.f1.f0.tokenImage, 0); - symt.setActive(TypeEnum.classname, symt.getClass(id)); - TokenKey ext = new TokenKey(n.f3.f0.tokenImage, 0); - symt.setExtend(ext); - - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - n.f6.accept(this, symt); - n.f7.accept(this, symt); - return null; - } - - /** - * f0 -> Type() - * f1 -> Identifier() - * f2 -> ";" - */ - public R visit(VarDeclaration n, SymbolTable symt) { - String cls = (n.f0.f0.which == 3) ? - ((Identifier) n.f0.f0.choice).f0.tokenImage : - null; - - TokenKey id = new TokenKey(n.f1.f0.tokenImage, n.f1.f0.beginLine); - TypeInstance me = symt.getType(id); - symt.addLocal(id); - symt.addClassInstance(me, cls); - - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - return null; - } - - /** - * f0 -> "public" - * f1 -> Type() - * f2 -> Identifier() - * f3 -> "(" - * f4 -> ( FormalParameterList() )? - * f5 -> ")" - * f6 -> "{" - * f7 -> ( VarDeclaration() )* - * f8 -> ( Statement() )* - * f9 -> "return" - * f10 -> Expression() - * f11 -> ";" - * f12 -> "}" - */ - public R visit(MethodDeclaration n, SymbolTable symt) { - ClassInstance cls = (ClassInstance) symt.getActive(TypeEnum.classname); - - TokenKey id = new TokenKey(n.f2.f0.tokenImage, n.f2.f0.beginLine); - MethodInstance me = symt.getMethod(id); - symt.setActive(TypeEnum.method, me); - symt.addMethod(id); - symt.addClassInstance(me, cls.getName()); - - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - n.f6.accept(this, symt); - n.f7.accept(this, symt); - n.f8.accept(this, symt); - n.f9.accept(this, symt); - n.f10.accept(this, symt); - n.f11.accept(this, symt); - n.f12.accept(this, symt); - symt.removeActive(TypeEnum.method); - return null; - } - - /** - * f0 -> Type() - * f1 -> Identifier() - */ - public R visit(FormalParameter n, SymbolTable symt) { - TokenKey id = new TokenKey(n.f1.f0.tokenImage, n.f1.f0.beginLine); - symt.addParameter(id); - - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - return null; - } - -} diff --git a/st/SymTableVars.java b/st/SymTableVars.java new file mode 100644 index 0000000..f356a8c --- /dev/null +++ b/st/SymTableVars.java @@ -0,0 +1,218 @@ +package st; + +import syntaxtree.*; +import visitor.*; +import java.util.*; +import misc.*; + +/** + * Performs a bottom-up preliminary visit through the AST + * initializing all Instances and placing them in the passed ST + */ +public class SymTableVars extends GJDepthFirst { + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> "public" + * f4 -> "static" + * f5 -> "void" + * f6 -> "main" + * f7 -> "(" + * f8 -> "String" + * f9 -> "[" + * f10 -> "]" + * f11 -> Identifier() + * f12 -> ")" + * f13 -> "{" + * f14 -> ( VarDeclaration() )* + * f15 -> ( Statement() )* + * f16 -> "}" + * f17 -> "}" + */ + public R visit(MainClass n, SymbolTable symt) { + String id = n.f1.f0.tokenImage; + symt.setActive(TypeEnum.classname, symt.getClass(id)); + id = n.f6.tokenImage; + symt.setActive(TypeEnum.method, symt.getMethod(id)); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + n.f8.accept(this, symt); + n.f9.accept(this, symt); + n.f10.accept(this, symt); + n.f11.accept(this, symt); + n.f12.accept(this, symt); + n.f13.accept(this, symt); + n.f14.accept(this, symt); + n.f15.accept(this, symt); + n.f16.accept(this, symt); + n.f17.accept(this, symt); + + symt.addMethod(id); + symt.removeActive(TypeEnum.method); + symt.removeActive(TypeEnum.classname); + return null; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> ( VarDeclaration() )* + * f4 -> ( MethodDeclaration() )* + * f5 -> "}" + */ + public R visit(ClassDeclaration n, SymbolTable symt) { + String id = n.f1.f0.tokenImage; + symt.setActive(TypeEnum.classname, symt.getClass(id)); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + + symt.removeActive(TypeEnum.classname); + return null; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "extends" + * f3 -> Identifier() + * f4 -> "{" + * f5 -> ( VarDeclaration() )* + * f6 -> ( MethodDeclaration() )* + * f7 -> "}" + */ + public R visit(ClassExtendsDeclaration n, SymbolTable symt) { + String id = n.f1.f0.tokenImage; + symt.setActive(TypeEnum.classname, symt.getClass(id)); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + + symt.removeActive(TypeEnum.classname); + return null; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + * f2 -> ";" + */ + public R visit(VarDeclaration n, SymbolTable symt) { + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + TokenKey id = new TokenKey(n.f1.f0.tokenImage, + (ClassInstance) symt.getActive(TypeEnum.classname), + (MethodInstance) symt.getActive(TypeEnum.method)); + TypeEnum rtrn = TypeEnum.ERROR; + switch (n.f0.f0.which) { + case 0: + rtrn = TypeEnum.intarray; break; + case 1: + rtrn = TypeEnum.bool; break; + case 2: + rtrn = TypeEnum.integer; break; + case 3: + rtrn = TypeEnum.classname; break; + default: + MinimalLogger.severe("Unsupported case"); + } + + TypeInstance instance = new TypeInstance(id.getName(), rtrn, (MethodInstance) symt.getActive(TypeEnum.method), + (ClassInstance) symt.getActive(TypeEnum.classname)); + symt.put(id, instance); + symt.addLocal(id.getName()); + + return null; + } + + /** + * f0 -> "public" + * f1 -> Type() + * f2 -> Identifier() + * f3 -> "(" + * f4 -> ( FormalParameterList() )? + * f5 -> ")" + * f6 -> "{" + * f7 -> ( VarDeclaration() )* + * f8 -> ( Statement() )* + * f9 -> "return" + * f10 -> Expression() + * f11 -> ";" + * f12 -> "}" + */ + public R visit(MethodDeclaration n, SymbolTable symt) { + String id = n.f2.f0.tokenImage; + symt.setActive(TypeEnum.method, symt.getMethod(id)); + symt.addMethod(id); + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + n.f8.accept(this, symt); + n.f9.accept(this, symt); + n.f10.accept(this, symt); + n.f11.accept(this, symt); + n.f12.accept(this, symt); + + + symt.removeActive(TypeEnum.method); + return null; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + */ + public R visit(FormalParameter n, SymbolTable symt) { + n.f0.accept(this, symt); + n.f1.accept(this, symt); + TokenKey id = new TokenKey(n.f1.f0.tokenImage, + (ClassInstance) symt.getActive(TypeEnum.classname), + (MethodInstance) symt.getActive(TypeEnum.method)); + + TypeEnum rtrn = TypeEnum.ERROR; + switch (n.f0.f0.which) { + case 0: + rtrn = TypeEnum.intarray; break; + case 1: + rtrn = TypeEnum.bool; break; + case 2: + rtrn = TypeEnum.integer; break; + case 3: + rtrn = TypeEnum.classname; break; + default: + MinimalLogger.severe("Unsupported case"); + } + + TypeInstance instance = new TypeInstance(id.getName(), rtrn, (MethodInstance) symt.getActive(TypeEnum.method), + (ClassInstance) symt.getActive(TypeEnum.classname)); + symt.put(id, instance); + symt.addParameter(id.getName()); + + + return null; + } + +} diff --git a/st/SymbolTable.java b/st/SymbolTable.java index dcc1f5b..a8a6dcf 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -42,7 +42,7 @@ public class SymbolTable { /** * Methods intended to be used during the second pass */ - public void setExtend(TokenKey arg) { + public void setExtend(String arg) { ClassInstance cls = (ClassInstance) this.active.get(TypeEnum.classname); ClassInstance ext = this.getClass(arg); @@ -64,7 +64,7 @@ public class SymbolTable { } } - public void addLocal(TokenKey lvar) { + public void addLocal(String lvar) { TypeInstance var = this.getType(lvar); AbstractInstance par; if (this.active.get(TypeEnum.method) != null) { // we are in a method @@ -81,7 +81,7 @@ public class SymbolTable { par.getName(), par.getType())); } - public void addMethod(TokenKey mtd) { + public void addMethod(String mtd) { ClassInstance cls = (ClassInstance) this.active.get(TypeEnum.classname); MethodInstance lmtd = this.getMethod(mtd); @@ -91,7 +91,7 @@ public class SymbolTable { lmtd.getName(), cls.getName())); } - public void addParameter(TokenKey arg) { + public void addParameter(String arg) { MethodInstance mtd = (MethodInstance) this.active.get(TypeEnum.method); TypeInstance para = this.getType(arg); @@ -99,11 +99,14 @@ public class SymbolTable { MinimalLogger.info(String.format("Added %s as a parameter of %s", para.getName(), mtd.getName())); + MinimalLogger.info(String.format("Added %s as a localvar of %s", + para.getName(), mtd.getName())); + } - public void addClassInstance(AbstractInstance t, String c) { + public void addClassInstance(TypeInstance t, String c) { ClassInstance cls = (c != null) ? - this.getClass(new TokenKey(c, 0)) : + this.getClass(c) : null; t.addClassInstance(cls); @@ -128,7 +131,10 @@ public class SymbolTable { this.active.remove(type); } - public TypeInstance getType(TokenKey id) { + public TypeInstance getType(String name) { + TokenKey id = new TokenKey(name, + (ClassInstance) this.getActive(TypeEnum.classname), + (MethodInstance) this.getActive(TypeEnum.method)); AbstractInstance symbol; TypeInstance ret = ((symbol = this.symt.get(id)) != null && symbol instanceof TypeInstance) ? @@ -139,7 +145,10 @@ public class SymbolTable { return ret; } - public MethodInstance getMethod(TokenKey id) { + public MethodInstance getMethod(String name) { + TokenKey id = new TokenKey(name, + (ClassInstance) this.getActive(TypeEnum.classname), + null); AbstractInstance symbol; MethodInstance ret = ((symbol = this.symt.get(id)) != null && symbol instanceof MethodInstance) ? @@ -150,7 +159,10 @@ public class SymbolTable { return ret; } - public ClassInstance getClass(TokenKey id) { + public ClassInstance getClass(String name) { + TokenKey id = new TokenKey(name, + null, + null); AbstractInstance symbol; ClassInstance ret = ((symbol = this.symt.get(id)) != null && symbol instanceof ClassInstance) ? diff --git a/st/TokenKey.java b/st/TokenKey.java index 2319917..54086c5 100644 --- a/st/TokenKey.java +++ b/st/TokenKey.java @@ -8,18 +8,20 @@ package st; public class TokenKey { private String name; - private int beginLine; + private AbstractInstance cls; // null for classes + private AbstractInstance mtd; // null for classes, methods - public TokenKey(String name, int beginLine) { - // classes CANNOT collide, so CALLEES ARE EXPECTED TO USE ZERO! + public TokenKey(String name, AbstractInstance cls, AbstractInstance mtd) { this.name = name; - this.beginLine = beginLine; + this.cls = cls; + this.mtd = mtd; } @Override public String toString() { - return String.format("%s (%d)", + return String.format("%s (%s,%s)", this.name, - this.beginLine); + this.mtd, + this.cls); } @@ -28,7 +30,8 @@ public class TokenKey { TokenKey o; if (other instanceof TokenKey && (o = (TokenKey) other).name == this.name && - o.beginLine == this.beginLine) { + o.cls == this.cls && + o.mtd == this.mtd) { ret = true; } return ret; diff --git a/st/TypeInstance.java b/st/TypeInstance.java index 88b73f3..e43c5b3 100644 --- a/st/TypeInstance.java +++ b/st/TypeInstance.java @@ -2,8 +2,23 @@ package st; public class TypeInstance extends AbstractInstance { - public TypeInstance(String name, TypeEnum type) { + protected ClassInstance par_cls; // the surrounding class + protected MethodInstance par_mtd; // the surrounding method + protected ClassInstance cls; // the class instance + + + public TypeInstance(String name, TypeEnum type, MethodInstance par_mtd, ClassInstance par_cls) { super(name, type); + this.par_cls = par_cls; + this.par_mtd = par_mtd; + } + + @Override public boolean equals(Object other) { + TypeInstance o; + return (other instanceof TypeInstance && + ((o = (TypeInstance) other).getName() == this.getName() && + o.getParentClass() == this.getParentClass() && + o.getParentMethod() == this.getParentMethod())); } public int getSize() { @@ -30,4 +45,28 @@ public class TypeInstance extends AbstractInstance { return type != TypeEnum.ERROR; } + public void addClassInstance(ClassInstance cls) { + this.cls = cls; + } + + public ClassInstance getClassInstance() { + return this.cls; + } + + public void addParentClass(ClassInstance par_cls) { + this.par_cls = par_cls; + } + + public ClassInstance getParentClass() { + return this.par_cls; + } + + public void addParentMethod(MethodInstance par_mtd) { + this.par_mtd = par_mtd; + } + + public MethodInstance getParentMethod() { + return this.par_mtd; + } + } -- cgit v1.2.3