From 5677447c250dcf0c970fe7ce4ac51f47bf6d6da0 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Sat, 11 May 2024 15:32:13 -0600 Subject: Changed SymbolTable to throw and exception when searching for missing entries --- heat/HeatVisitor.java | 32 ----------------- misc/MinimalSimpleFormatter.java | 2 +- output/negative/ExtendMissing-error.java | 12 +++++++ output/negative/ExtendMissing.java | 12 ------- st/SymbolTable.java | 60 ++++++++++++++++---------------- 5 files changed, 43 insertions(+), 75 deletions(-) create mode 100644 output/negative/ExtendMissing-error.java delete mode 100644 output/negative/ExtendMissing.java diff --git a/heat/HeatVisitor.java b/heat/HeatVisitor.java index 2e830c5..ae3d150 100644 --- a/heat/HeatVisitor.java +++ b/heat/HeatVisitor.java @@ -329,9 +329,6 @@ public class HeatVisitor extends GJDepthFirst> if (n.f0.which == 3) { String id = ((Identifier) n.f0.choice).f0.tokenImage; ClassInstance c = this.symt.getClass(id); - if (c == null) - throw new TypecheckException(String.format("%s was never declared!", - id)); _ret = new TypeBundle(TypeEnum.classname, c); } else _ret = n.f0.accept(this, argu); @@ -444,12 +441,6 @@ public class HeatVisitor extends GJDepthFirst> n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeInstance t = this.symt.getType(id); - if (t == null) - t = this.symt.getTypeAttr(id); - if (t == null) - throw new TypecheckException(String.format("%s found that %s was never declared!", - n.getClass().getSimpleName(), - id)); TypeBundle tb = new TypeBundle(t.getType(), t.getClassInstance()); _ret = n.f2.accept(this, argu); @@ -485,12 +476,6 @@ public class HeatVisitor extends GJDepthFirst> n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeInstance t = this.symt.getType(id); - if (t == null) - t = this.symt.getTypeAttr(id); - if (t == null) - throw new TypecheckException(String.format("%s found that %s was never declared!", - n.getClass().getSimpleName(), - id)); if (t.getType() != TypeEnum.intarray) throw new TypecheckException(String.format("%s called on %s, a non-array!", n.getClass().getSimpleName(), @@ -783,12 +768,6 @@ public class HeatVisitor extends GJDepthFirst> case 3: MinimalLogger.info(String.format("Message send found IDENTIFIER")); t = this.symt.getType(((Identifier) n.f0.f0.choice).f0.tokenImage); - if (t == null) - t = this.symt.getTypeAttr(((Identifier) n.f0.f0.choice).f0.tokenImage); - if (t == null) - throw new TypecheckException(String.format("%s found that %s was never declared!", - n.getClass().getSimpleName(), - ((Identifier) n.f0.f0.choice).f0.tokenImage)); // HOW TO CALL? break; case 4: MinimalLogger.info(String.format("Message send found THIS")); @@ -819,13 +798,7 @@ public class HeatVisitor extends GJDepthFirst> n.getClass().getSimpleName())); MethodInstance m = this.symt.getMethod(n.f2.f0.tokenImage, t.getClassInstance()); - if (m == null) - throw new TypecheckException(String.format("%s called a method not part of %s!", - n.getClass().getSimpleName(), - t.getName())); - MinimalLogger.severe(String.format("Class instance was: %s", t.getClassInstance())); - MinimalLogger.severe(String.format("M was: %s with args %s", m, m.getParentClass())); ArrayList actual = new ArrayList(); ArrayList expected = new ArrayList(); n.f4.accept(this, actual); @@ -978,11 +951,6 @@ public class HeatVisitor extends GJDepthFirst> n.getClass().getSimpleName())); /////////////////////////////////////////////////////////////// TypeInstance t = this.symt.getType(n.f0.tokenImage); - if (t == null) - t = this.symt.getTypeAttr(n.f0.tokenImage); - if (t == null) - throw new TypecheckException(String.format("%s was never declared!", - n.f0.tokenImage)); _ret = new TypeBundle(t.getType(), t.getClassInstance()); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s with %s", diff --git a/misc/MinimalSimpleFormatter.java b/misc/MinimalSimpleFormatter.java index 741863d..a325185 100644 --- a/misc/MinimalSimpleFormatter.java +++ b/misc/MinimalSimpleFormatter.java @@ -7,7 +7,7 @@ import java.util.logging.SimpleFormatter; public class MinimalSimpleFormatter extends SimpleFormatter { @Override public String format(LogRecord record) { - if (false) + if (true) return record.getLevel() + ": " + formatMessage(record) + "\n"; else return ""; diff --git a/output/negative/ExtendMissing-error.java b/output/negative/ExtendMissing-error.java new file mode 100644 index 0000000..72a487a --- /dev/null +++ b/output/negative/ExtendMissing-error.java @@ -0,0 +1,12 @@ +class ExtendMissing { + public static void main(String[] a){ + System.out.println(new Test().start()); + } +} + +class Test extends NotPresent { //TE + + public int start(){ + return 0; + } +} diff --git a/output/negative/ExtendMissing.java b/output/negative/ExtendMissing.java deleted file mode 100644 index 72a487a..0000000 --- a/output/negative/ExtendMissing.java +++ /dev/null @@ -1,12 +0,0 @@ -class ExtendMissing { - public static void main(String[] a){ - System.out.println(new Test().start()); - } -} - -class Test extends NotPresent { //TE - - public int start(){ - return 0; - } -} diff --git a/st/SymbolTable.java b/st/SymbolTable.java index fa196e3..0793a6a 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -52,10 +52,6 @@ public class SymbolTable { ClassInstance cls = (ClassInstance) this.active.get(TypeEnum.classname); ClassInstance ext = this.getClass(arg); - if (ext == null) - throw new TypecheckException(String.format("There is no %s to extend!", - arg)); - MinimalLogger.info(String.format("%s found to extend %s", cls.getName(), ext.getName())); @@ -176,37 +172,41 @@ public class SymbolTable { } public TypeInstance getType(String name) { - TokenKey id = new TokenKey(name, - TypeEnum.integer, - (ClassInstance) this.getActive(TypeEnum.classname), - (MethodInstance) this.getActive(TypeEnum.method)); + /** + * Extracts a 'type' (non-method,class) symbol from the + * symbol table, with proper scoping. Throws a typecheck error if missing. + */ + TokenKey id; + // local var + id = new TokenKey(name, + TypeEnum.integer, + (ClassInstance) this.getActive(TypeEnum.classname), + (MethodInstance) this.getActive(TypeEnum.method)); AbstractInstance symbol; TypeInstance ret = ((symbol = this.symt.get(id)) != null && symbol instanceof TypeInstance) ? (TypeInstance) symbol : null; if (ret == null) - MinimalLogger.severe(String.format("getType returning null for missing alias %s!", - id)); - return ret; - } - - public TypeInstance getTypeAttr(String name) { - TokenKey id = new TokenKey(name, - TypeEnum.integer, - (ClassInstance) this.getActive(TypeEnum.classname), - null); - AbstractInstance symbol; - TypeInstance ret = ((symbol = this.symt.get(id)) != - null && symbol instanceof TypeInstance) ? + // class var + id = new TokenKey(name, + TypeEnum.integer, + (ClassInstance) this.getActive(TypeEnum.classname), + null); + ret = ((symbol = this.symt.get(id)) != + null && symbol instanceof TypeInstance) ? (TypeInstance) symbol : null; if (ret == null) - MinimalLogger.severe(String.format("getType returning null for missing alias %s!", - id)); + throw new TypecheckException(String.format("No type symbol with name %s was found in %s, %s!", + name, this.getActive(TypeEnum.classname), + this.getActive(TypeEnum.method))); return ret; } - public MethodInstance getMethod(String name) { + /** + * Extracts a 'method' (non-type,class) symbol from the + * symbol table, with proper scoping. Throws a typecheck error if missing. + */ TokenKey id = new TokenKey(name, TypeEnum.method, (ClassInstance) this.getActive(TypeEnum.classname), @@ -216,8 +216,8 @@ public class SymbolTable { null && symbol instanceof MethodInstance) ? (MethodInstance) symbol : null; if (ret == null) - MinimalLogger.severe(String.format("getMethod returning null for missing alias %s!", - id)); + throw new TypecheckException(String.format("No method symbol with name %s was found in %s!", + name, this.getActive(TypeEnum.classname))); return ret; } @@ -231,8 +231,8 @@ public class SymbolTable { null && symbol instanceof MethodInstance) ? (MethodInstance) symbol : null; if (ret == null) - MinimalLogger.severe(String.format("getMethod returning null for missing alias %s!", - id)); + throw new TypecheckException(String.format("No method symbol with name %s was found in %s!", + name, c)); return ret; } @@ -246,8 +246,8 @@ public class SymbolTable { null && symbol instanceof ClassInstance) ? (ClassInstance) symbol : null; if (ret == null) - MinimalLogger.severe(String.format("getClass returning null for missing alias %s!", - id)); + throw new TypecheckException(String.format("No class symbol with name %s was found!", + name)); return ret; } -- cgit v1.2.3