summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-04-26 19:36:16 -0600
committerbd-912 <bdunahu@colostate.edu>2024-04-26 19:36:16 -0600
commite2d7b563cd22b92604e07dad9bf6bcf0393a46ca (patch)
tree539f081c254d31afe9574fa42f5629971d8a9eb5
parent42512ab2cfd16422909263475b044f40b3829707 (diff)
HeatVisitor MessageSend anonymous, observe Factorial + others pass
-rw-r--r--Typecheck.java27
-rw-r--r--boil/BoilVisitor.java4
-rw-r--r--heat/HeatVisitor.java33
-rw-r--r--st/SymbolTable.java6
-rwxr-xr-xtest.sh15
5 files changed, 55 insertions, 30 deletions
diff --git a/Typecheck.java b/Typecheck.java
index 098defe..12e74f1 100644
--- a/Typecheck.java
+++ b/Typecheck.java
@@ -20,22 +20,23 @@ public class Typecheck {
// Build the symbol table. Top-down visitor, inherits from
// GJDepthFirst<R,A>. R=Void, A=Integer.
- SymbolTable symt = new SymbolTable();
- MinimalLogger.info("Populating classes...");
- root.accept(new SymTableClasses<Void>(), symt);
- MinimalLogger.info("Populating methods...");
- root.accept(new SymTableMethods<Void>(), symt);
- MinimalLogger.info("Populating variables...");
- root.accept(new SymTableVars<Void>(), symt);
- MinimalLogger.info("Populating extensions...");
- root.accept(new SymTableExtend<Void>(), symt);
- MinimalLogger.info(symt.toString());
-
- HeatVisitor hv = new HeatVisitor(symt);
-
try {
+
+ SymbolTable symt = new SymbolTable();
+ MinimalLogger.info("Populating classes...");
+ root.accept(new SymTableClasses<Void>(), symt);
+ MinimalLogger.info("Populating methods...");
+ root.accept(new SymTableMethods<Void>(), symt);
+ MinimalLogger.info("Populating variables...");
+ root.accept(new SymTableVars<Void>(), symt);
+ MinimalLogger.info("Populating extensions...");
+ root.accept(new SymTableExtend<Void>(), symt);
+ MinimalLogger.info(symt.toString());
+
+ HeatVisitor hv = new HeatVisitor(symt);
root.accept(hv, null);
System.out.println("Program type checked successfully");
+
} catch (TypecheckException e) {
System.out.println("Type error");
MinimalLogger.severe(String.format("Reason: %s",
diff --git a/boil/BoilVisitor.java b/boil/BoilVisitor.java
index 4ba6ea2..0d1d721 100644
--- a/boil/BoilVisitor.java
+++ b/boil/BoilVisitor.java
@@ -969,7 +969,7 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
MinimalLogger.info("Message send found ANONYMOUS");
// expand the entire object out!
rhs = n.f0.accept(this, argu);
- t = new TypeInstance(rhs, TypeEnum.ERROR,
+ t = new TypeInstance(rhs, null,
(MethodInstance) this.symt.getActive(TypeEnum.method),
(ClassInstance) this.symt.getActive(TypeEnum.classname));
t.addClassInstance(this.recentClass);
@@ -981,7 +981,7 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
MinimalLogger.info("Message send found BRACKET");
rhs = n.f0.accept(this, argu);
ClassInstance cls = this.recentMethod.getReturn();
- t = new TypeInstance(this.getUniqueID(), TypeEnum.ERROR,
+ t = new TypeInstance(this.getUniqueID(), null,
(MethodInstance) this.symt.getActive(TypeEnum.method),
(ClassInstance) this.symt.getActive(TypeEnum.classname));
this.addVapor(String.format(" %s = %s\n",
diff --git a/heat/HeatVisitor.java b/heat/HeatVisitor.java
index ba739c9..584eaec 100644
--- a/heat/HeatVisitor.java
+++ b/heat/HeatVisitor.java
@@ -208,7 +208,7 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
n.getClass().getSimpleName(),
id));
///////////////////////////////////////////////////////////////
- _ret = n.f0.accept(this, argu);
+ // f0.accept(this, argu);
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s (%s)",
n.getClass().getSimpleName(),
@@ -428,10 +428,13 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
///////////////////////////////////////////////////////////////
- TypeBundle tb = new TypeBundle(this.symt.getType(id).getType(), null);
- if (tb.getType() == null)
+ TypeInstance t = this.symt.getType(id);
+ if (t == null)
+ t = this.symt.getTypeAttr(id);
+ if (t.getType() == null)
throw new TypecheckException(String.format("%s glowers at symbol table!",
n.getClass().getSimpleName()));
+ TypeBundle tb = new TypeBundle(t.getType(), null);
// Fixme---add subtyping?
_ret = n.f2.accept(this, argu);
@@ -587,7 +590,7 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
///////////////////////////////////////////////////////////////
TypeBundle tb = n.f0.accept(this, argu);
_ret = n.f2.accept(this, argu);
- if (!tb.isBool() || _ret.isBool())
+ if (!tb.isBool() || !_ret.isBool())
throw new TypecheckException(String.format("%s tried with something other than an Boolean!",
n.getClass().getSimpleName()));
///////////////////////////////////////////////////////////////
@@ -609,7 +612,7 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
///////////////////////////////////////////////////////////////
TypeBundle tb = n.f0.accept(this, argu);
_ret = n.f2.accept(this, argu);
- if (!tb.isInt() || _ret.isInt())
+ if (!tb.isInt() || !_ret.isInt())
throw new TypecheckException(String.format("%s tried with something other than an integer!",
n.getClass().getSimpleName()));
_ret = new TypeBundle(TypeEnum.bool, null);
@@ -632,7 +635,7 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
///////////////////////////////////////////////////////////////
TypeBundle tb = n.f0.accept(this, argu);
_ret = n.f2.accept(this, argu);
- if (!tb.isInt() || _ret.isInt())
+ if (!tb.isInt() || !_ret.isInt())
throw new TypecheckException(String.format("%s tried with something other than an integer!",
n.getClass().getSimpleName()));
///////////////////////////////////////////////////////////////
@@ -654,7 +657,7 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
///////////////////////////////////////////////////////////////
TypeBundle tb = n.f0.accept(this, argu);
_ret = n.f2.accept(this, argu);
- if (!tb.isInt() || _ret.isInt())
+ if (!tb.isInt() || !_ret.isInt())
throw new TypecheckException(String.format("%s tried with something other than an integer!",
n.getClass().getSimpleName()));
///////////////////////////////////////////////////////////////
@@ -676,7 +679,7 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
///////////////////////////////////////////////////////////////
TypeBundle tb = n.f0.accept(this, argu);
_ret = n.f2.accept(this, argu);
- if (!tb.isInt() || _ret.isInt())
+ if (!tb.isInt() || !_ret.isInt())
throw new TypecheckException(String.format("%s tried with something other than an integer!",
n.getClass().getSimpleName()));
///////////////////////////////////////////////////////////////
@@ -759,6 +762,7 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
case 6:
MinimalLogger.info(String.format("Message send found ANONYMOUS"));
t = new TypeInstance("null",null,null,null);
+ t.addClassInstance((ClassInstance) this.symt.getClass(((AllocationExpression) (n.f0.f0.choice)).f1.f0.tokenImage));
break;
case 8:
MinimalLogger.info(String.format("Message send found BRACKET"));
@@ -914,9 +918,8 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
///////////////////////////////////////////////////////////////
- if (true)
- throw new TypecheckException("Identifier says: stop calling me!");
- n.f0.accept(this, argu);
+ TypeInstance t = this.symt.getType(n.f0.tokenImage);
+ _ret = new TypeBundle(t.getType(), t.getClassInstance());
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s with %s",
n.getClass().getSimpleName(),
@@ -972,10 +975,12 @@ public class HeatVisitor extends GJDepthFirst<TypeBundle,String> {
*/
public TypeBundle visit(AllocationExpression n, String argu) {
TypeBundle _ret=null;
- MinimalLogger.info(String.format("-> %s",
- n.getClass().getSimpleName()));
+ String id = n.f1.f0.tokenImage;
+ MinimalLogger.info(String.format("-> %s (%s)",
+ n.getClass().getSimpleName(),
+ id));
///////////////////////////////////////////////////////////////
- n.f1.accept(this, argu);
+ _ret = new TypeBundle(TypeEnum.classname, this.symt.getClass(id));
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s with %s",
n.getClass().getSimpleName(),
diff --git a/st/SymbolTable.java b/st/SymbolTable.java
index 90640c6..15d33a0 100644
--- a/st/SymbolTable.java
+++ b/st/SymbolTable.java
@@ -2,6 +2,7 @@ package st;
import java.util.*;
import misc.*;
+import heat.TypecheckException;
/**
* Class which provides methods for interacting with and managing
@@ -30,11 +31,16 @@ public class SymbolTable {
/**
* Methods intended to be used during the first pass
+ *
+ * Will always throw a typecheck error when symbols are not distinct.
*/
public void put(TokenKey id, AbstractInstance symbol) {
MinimalLogger.info(String.format("Inserting %s -> %s",
id,
symbol.getType()));
+ if (this.symt.get(id) != null)
+ throw new TypecheckException(String.format("SymbolTable tried to place %s twice!",
+ id.toString()));
this.symt.put(id, symbol);
}
diff --git a/test.sh b/test.sh
index a8c60bd..a27b321 100755
--- a/test.sh
+++ b/test.sh
@@ -5,19 +5,32 @@
# tests for a given phase. #
##################################################
+function java() {
+ expected=$(bash runner.sh java "$1.java")
+}
+
+function match() {
+ [[ "$1" == *"error"* ]] && expected="Type error" ||
+ expected="Program type checked successfully"
+}
+
testdir="./output"
case "$1" in
"heat")
ext="java"
+ comp="match"
;;
"boil")
ext="java"
+ comp="java"
;;
"vaporize")
ext="vapor"
+ comp="java"
;;
"condense")
ext="vaporm"
+ comp="java"
;;
*)
echo "usage $0 [heat|boil|vaporize|condense]"
@@ -31,7 +44,7 @@ for file in $testdir/*.$ext; do
[ -f "$file" ] || break
base=${file%.*}
echo -n "Processing file: $base "
- expected=$(bash runner.sh java "${base}.java")
+ $comp $base
actual=$(bash runner.sh $1 $file 2>/dev/null)
[[ $expected == $actual ]] && echo 'PASSED' || echo -e "FAILED"
done