summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-04-15 15:19:54 -0600
committerbd-912 <bdunahu@colostate.edu>2024-04-15 15:19:54 -0600
commit04e9109f112f4cb7317416c2e31806f42663cb45 (patch)
tree9ae99f2bc5d5fbb41d731792b1d22a30f2e642b3
parent056690e0e60d4d0ce046c171691aa02a7071eeae (diff)
Allow MethodInstance to keep track of parent class
-rw-r--r--boil/library/BoilSimp.java35
-rw-r--r--boil/library/TypeFactory.java2
-rw-r--r--st/AbstractInstance.java9
-rw-r--r--st/SymTableTopDown.java7
-rw-r--r--st/SymbolTable.java5
-rw-r--r--st/TypeInstance.java10
6 files changed, 40 insertions, 28 deletions
diff --git a/boil/library/BoilSimp.java b/boil/library/BoilSimp.java
index 9e8d40a..fe54052 100644
--- a/boil/library/BoilSimp.java
+++ b/boil/library/BoilSimp.java
@@ -243,6 +243,15 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> {
mod += String.format("%s)\n", args);
mod += n.f5.accept(this, symt);
mod += n.f6.accept(this, symt);
+
+ // spill out all class attributes, if they're not used, who cares
+ for (TypeInstance attr : symt.getMethod(id)
+ .getClassInstance()
+ .getLocals()) {
+ mod += String.format(" %s = this\n",
+ this.tf.alias(attr));
+ }
+
mod += n.f7.accept(this, symt);
mod += n.f8.accept(this, symt);
mod += n.f9.accept(this, symt);
@@ -451,9 +460,14 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> {
String mod = "";
mod += n.f0.accept(this, symt);
mod += n.f1.accept(this, symt);
- mod += n.f2.accept(this, symt);
+ String expr = n.f2.accept(this, symt);
mod += n.f3.accept(this, symt);
mod += n.f4.accept(this, symt);
+ TypeInstance tp1 = new TypeInstance("tp1", TypeEnum.ERROR);
+
+ // mod += String.format(" %s = %s\n",
+ // this.tf.alias(tp1),
+ // expr);
mod += String.format(" PrintIntS(%s)\n",
this.tf.retrieveRecentList(1));
@@ -622,19 +636,14 @@ public class BoilSimp extends GJDepthFirst<String,SymbolTable> {
mod += n.f4.accept(this, symt);
mod += n.f5.accept(this, symt);
- String call = String.format("call %s(%s",
- this.tf.alias(tp2),
- this.tf.alias(cur));
-
- call += String.format(" %s)",
- this.tf.retrieveRecentList(symt.getMethod(id2)
- .getArguments()
- .size()));
+ mod += String.format("call %s(%s",
+ this.tf.alias(tp2),
+ this.tf.alias(cur));
- TypeInstance tp3 = new TypeInstance("tp3", TypeEnum.ERROR);
- mod += String.format(" %s = %s\n",
- this.tf.alias(tp3),
- call);
+ mod += String.format(" %s)",
+ this.tf.retrieveRecentList(symt.getMethod(id2)
+ .getArguments()
+ .size()));
return mod;
}
diff --git a/boil/library/TypeFactory.java b/boil/library/TypeFactory.java
index 432fc0f..be77f63 100644
--- a/boil/library/TypeFactory.java
+++ b/boil/library/TypeFactory.java
@@ -35,7 +35,7 @@ public class TypeFactory {
String rtn = "";
rtn += String.format("t.%d",
type_num-x);
- for (int i = type_num-(x+1); i < type_num; ++i) {
+ for (int i = type_num-(x-1); i < type_num; ++i) {
rtn += String.format(" t.%d",
i);
}
diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java
index d236044..7214e6a 100644
--- a/st/AbstractInstance.java
+++ b/st/AbstractInstance.java
@@ -6,6 +6,7 @@ public abstract class AbstractInstance {
protected String name; // the literal name of the declaration
protected TypeEnum type; // the type of the declaration
protected ArrayList<AbstractInstance> scope; // the scope where the instance is valid
+ protected ClassInstance cls; // the surrounding class
public AbstractInstance(String name, TypeEnum type) {
this.type = type;
@@ -41,6 +42,10 @@ public abstract class AbstractInstance {
}
}
+ protected void addClassInstance(ClassInstance cls) {
+ this.cls = cls;
+ }
+
public String getName() {
return this.name;
}
@@ -53,4 +58,8 @@ public abstract class AbstractInstance {
return this.scope;
}
+ public ClassInstance getClassInstance() {
+ return this.cls;
+ }
+
}
diff --git a/st/SymTableTopDown.java b/st/SymTableTopDown.java
index df136aa..566752e 100644
--- a/st/SymTableTopDown.java
+++ b/st/SymTableTopDown.java
@@ -121,8 +121,9 @@ public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
null;
String id = n.f1.f0.tokenImage;
+ TypeInstance me = symt.getType(id);
symt.addLocal(id);
- symt.addClassInstance(id, cls);
+ symt.addClassInstance(me, cls);
n.f0.accept(this, symt);
@@ -147,9 +148,13 @@ public class SymTableTopDown<R> extends GJDepthFirst<R,SymbolTable> {
* f12 -> "}"
*/
public R visit(MethodDeclaration n, SymbolTable symt) {
+ String cls = symt.getActive(TypeEnum.classname);
+
String id = n.f2.f0.tokenImage;
+ MethodInstance me = symt.getMethod(id);
symt.setActive(TypeEnum.method, id);
symt.addMethod(id);
+ symt.addClassInstance(me, cls);
n.f0.accept(this, symt);
diff --git a/st/SymbolTable.java b/st/SymbolTable.java
index 93d3af9..de45a58 100644
--- a/st/SymbolTable.java
+++ b/st/SymbolTable.java
@@ -80,13 +80,12 @@ public class SymbolTable {
mtd.addArgument(para); // also adds to local vars
}
- public void addClassInstance(String t, String c) {
- TypeInstance type = this.getType(t);
+ public void addClassInstance(AbstractInstance t, String c) {
ClassInstance cls = (c != null) ?
this.getClass(c) :
null;
- type.addClassInstance(cls);
+ t.addClassInstance(cls);
}
diff --git a/st/TypeInstance.java b/st/TypeInstance.java
index 65311a8..88b73f3 100644
--- a/st/TypeInstance.java
+++ b/st/TypeInstance.java
@@ -2,25 +2,15 @@ package st;
public class TypeInstance extends AbstractInstance {
- ClassInstance cls;
-
public TypeInstance(String name, TypeEnum type) {
super(name, type);
}
- public void addClassInstance(ClassInstance cls) {
- this.cls = cls;
- }
-
public int getSize() {
return (this.cls != null) ?
cls.getSize() : 4;
}
- public ClassInstance getClassInstance() {
- return this.cls;
- }
-
public boolean sameType(TypeInstance other) {
/**
* Given a TypeInstance object other,