diff options
Diffstat (limited to 'st')
-rw-r--r-- | st/ClassInstance.java | 26 | ||||
-rw-r--r-- | st/SymbolTable.java | 57 |
2 files changed, 63 insertions, 20 deletions
diff --git a/st/ClassInstance.java b/st/ClassInstance.java index d96d3d5..a794f96 100644 --- a/st/ClassInstance.java +++ b/st/ClassInstance.java @@ -1,6 +1,7 @@ package st; import java.util.ArrayList; +import misc.*; public class ClassInstance extends AbstractInstance { private ClassInstance ext; // the name of the extended class (null if none) @@ -15,8 +16,29 @@ public class ClassInstance extends AbstractInstance { @Override public boolean equals(Object other) { ClassInstance o; - return (other instanceof ClassInstance && - ((o = (ClassInstance) other).getName() == this.getName())); + if (other instanceof ClassInstance) + o = (ClassInstance) other; + else + return false; + + if (o.getName().equals(this.getName())) + return true; + else + MinimalLogger.info(String.format("I (%s) do not have the same name as %s... Checking if I extend...", + this.getName(), o.getName())); + + if (this.getExtend() == null) { + MinimalLogger.info(String.format("I (%s) do not extend anything!", this.getName())); + return false; + } + + ClassInstance te = this.getExtend(); + if (te.equals(o)) { + MinimalLogger.info(String.format("I (%s) extends %s!", this.getName(), o.getName())); + return true; + } + MinimalLogger.info(String.format("I (%s) do not extend %s! I extend %s instead.", this.getName(), o.getName(), te.getName())); + return false; } public ClassInstance getExtend() { diff --git a/st/SymbolTable.java b/st/SymbolTable.java index fa6bdf2..ca38295 100644 --- a/st/SymbolTable.java +++ b/st/SymbolTable.java @@ -56,27 +56,48 @@ public class SymbolTable { cls.getName(), ext.getName())); cls.setExtend(ext); + TokenKey k; for (TypeInstance t : ext.getLocals()) { - MinimalLogger.info(String.format("Added %s (%s) as a local var of %s (%s)", - t.getName(), t.getType(), - cls.getName(), cls.getType())); - cls.addLocal(t); - this.symt.put(new TokenKey(t.getName(), - TypeEnum.integer, - cls, - null), - t); + k = new TokenKey(t.getName(), TypeEnum.integer, + cls, null); + if (this.symt.get(k) == null) { + MinimalLogger.info(String.format("Added %s (%s) as a local var of %s (%s)", + t.getName(), t.getType(), + cls.getName(), cls.getType())); + cls.addLocal(t); + this.symt.put(k, t); + } else { MinimalLogger.info(String.format("%s found to be overridden in %s.", + t.getName(), cls.getName())); } } for (MethodInstance m : ext.getMethods()) { - MinimalLogger.info(String.format("Added %s (%s) as a method of %s (%s)", - m.getName(), m.getType(), - cls.getName(), cls.getType())); - this.symt.put(new TokenKey(m.getName(), - TypeEnum.method, - cls, - null), - m); - cls.addMethod(m); + k = new TokenKey(m.getName(), TypeEnum.method, + cls, null); + if (this.symt.get(k) == null) { + MinimalLogger.info(String.format("Added %s (%s) as a method of %s (%s)", + m.getName(), m.getType(), + cls.getName(), cls.getType())); + this.symt.put(k, m); + cls.addMethod(m); + } else { + MethodInstance exist = (MethodInstance) this.symt.get(k); + ArrayList<TypeInstance> expected = exist.getArguments(); + ArrayList<TypeInstance> actual = m.getArguments(); + if (expected.size() != actual.size() || + !exist.getReturn().equals(m.getReturn())) + throw new TypecheckException(String.format("SymbolTable found that %s is overwritten in %s!", + m.getName(), + cls.getName())); + for (int i = 0; i < actual.size(); ++i) { + if (expected.get(i).getClassInstance() != actual.get(i).getClassInstance()) { + throw new TypecheckException(String.format("SymbolTable found that %s is overwritten in %s!", + m.getName(), + cls.getName())); + } + } + + MinimalLogger.info(String.format("%s found to be overridden in %s.", + m.getName(), cls.getName())); + } } } |