summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-04-24 18:43:16 -0600
committerbd-912 <bdunahu@colostate.edu>2024-04-24 18:43:16 -0600
commitb9c7f34926bfec0b7704b09865b74cb42c66ab6d (patch)
tree60e94eed2b64092eeff60060a88e80c2b740ddf0
parent5fb49263868a96ce42fdf40202ce0ff2fc507dd8 (diff)
Int arrays + Referencing Locals FIXED
-rw-r--r--boil/library/BoilVisitor.java174
1 files changed, 126 insertions, 48 deletions
diff --git a/boil/library/BoilVisitor.java b/boil/library/BoilVisitor.java
index 2542cc0..43360b9 100644
--- a/boil/library/BoilVisitor.java
+++ b/boil/library/BoilVisitor.java
@@ -24,6 +24,42 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
return this.vapor;
}
+ public int getVarIndex(ClassInstance cls, TypeInstance type) {
+ /**
+ * Returns the index of the attribute in the class, or a negative number
+ * if it is not included.
+ */
+ int attr_index = -1;
+ if (cls != null &&
+ ((attr_index = cls.getLocals().indexOf(type)*4) >= 0)) {
+ attr_index += cls.getMethods().size() * 4;
+ }
+ return attr_index;
+ }
+
+ public String memoryReadFilter(String rhs) {
+ /**
+ * when a method recieves an id of either an alias
+ * or an untranslated variable name, this method will
+ * automatically create a memory store var.
+ */
+ String lhs = this.tf.alias(this.getUniqueID());
+ int attr_index = 0;
+ TypeInstance t;
+ if ((t = this.symt.getType(rhs)) != null) {
+ // memory store
+ ClassInstance cur = this.symt.getClass(this.symt.getActive(TypeEnum.classname));
+ attr_index = getVarIndex(cur, t);
+ rhs = String.format("[this+%d]", attr_index);
+ }
+
+ this.addVapor(String.format(" %s = %s\n",
+ lhs,
+ rhs));
+
+ return lhs;
+ }
+
private void addVapor(String str) {
MinimalLogger.info(String.format("Adding \"%s\"",
str));
@@ -298,7 +334,7 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
n.f7.accept(this, argu);
n.f8.accept(this, argu);
- String ret = n.f10.accept(this, argu);
+ String ret = this.memoryReadFilter(n.f10.accept(this, argu));
String retID = this.getUniqueID();
this.addVapor(String.format(" %s = %s\n ret %s\n\n",
this.tf.alias(retID),
@@ -480,6 +516,7 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
*/
public String visit(AssignmentStatement n, String argu) {
String _ret=null;
+ String id = n.f0.f0.tokenImage;
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
///////////////////////////////////////////////////////////////
@@ -487,10 +524,18 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
String expr = n.f2.accept(this, argu);
int attr_index = 0;
- if (expr.contains("functable") // is this an allocation... :)
- ) {
+ TypeInstance t;
+ if (expr.contains("functable")) {
+ // an allocation
lhs = String.format("[%s+%d]", lhs, attr_index);
}
+ if ((t = this.symt.getType(lhs)) != null) {
+ // memory store
+ ClassInstance cur = this.symt.getClass(this.symt.getActive(TypeEnum.classname));
+ attr_index = getVarIndex(cur, t);
+ lhs = String.format("[this+%d]", attr_index);
+ }
+
this.addVapor(String.format(" %s = %s\n",
lhs,
@@ -515,12 +560,17 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
///////////////////////////////////////////////////////////////
- String lhs = n.f0.accept(this, argu);
- int index = Integer.parseInt(n.f2.accept(this, argu)) * 4;
+ String arrID = this.tf.alias(this.getUniqueID());
+ String arr = this.memoryReadFilter(n.f0.accept(this, argu));
+ this.addVapor(String.format(" %s = %s\n",
+ arrID,
+ arr));
+
+ int index = Integer.parseInt(n.f2.accept(this, argu)) * 4 + 4;
String expr = n.f5.accept(this, argu);
this.addVapor(String.format(" [%s+%d] = %s\n",
- lhs,
+ arrID,
index,
expr));
///////////////////////////////////////////////////////////////
@@ -585,10 +635,10 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
n.getClass().getSimpleName()));
///////////////////////////////////////////////////////////////
String whileID = this.getUniqueID();
- String cond = n.f2.accept(this, argu);
this.addVapor(String.format("while%s_test:\n",
whileID));
+ String cond = n.f2.accept(this, argu);
String condID = this.getUniqueID();
this.addVapor(String.format(" %s = %s\n",
@@ -651,6 +701,7 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
* | MessageSend()
* | PrimaryExpression()
*/
+ // Expressions return a TYPE alias which is equal to the expression!
public String visit(Expression n, String argu) {
String _ret="";
MinimalLogger.info(String.format("-> %s",
@@ -669,16 +720,19 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
* f1 -> "&&"
* f2 -> PrimaryExpression()
*/
+ // Expressions return a TYPE alias which is equal to the expression!
public String visit(AndExpression n, String argu) {
String _ret="";
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
+ _ret += this.tf.alias(this.getUniqueID());
///////////////////////////////////////////////////////////////
String oper1 = n.f0.accept(this, argu);
String oper2 = n.f2.accept(this, argu);
- _ret += String.format("Eq(%s %s)",
- oper1,
- oper2);
+ this.addVapor(String.format(" %s = Eq(%s %s)\n",
+ _ret,
+ oper1,
+ oper2));
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s with %s",
n.getClass().getSimpleName(),
@@ -691,16 +745,19 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
* f1 -> "<"
* f2 -> PrimaryExpression()
*/
+ // Expressions return a TYPE alias which is equal to the expression!
public String visit(CompareExpression n, String argu) {
String _ret="";
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
+ _ret += this.tf.alias(this.getUniqueID());
///////////////////////////////////////////////////////////////
String oper1 = n.f0.accept(this, argu);
String oper2 = n.f2.accept(this, argu);
- _ret += String.format("LtS(%s %s)",
- oper1,
- oper2);
+ this.addVapor(String.format(" %s = LtS(%s %s)\n",
+ _ret,
+ oper1,
+ oper2));
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s with %s",
n.getClass().getSimpleName(),
@@ -713,16 +770,19 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
* f1 -> "+"
* f2 -> PrimaryExpression()
*/
+ // Expressions return a TYPE alias which is equal to the expression!
public String visit(PlusExpression n, String argu) {
String _ret="";
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
+ _ret += this.tf.alias(this.getUniqueID());
///////////////////////////////////////////////////////////////
- String oper1 = n.f0.accept(this, argu);
- String oper2 = n.f2.accept(this, argu);
- _ret += String.format("Add(%s %s)",
- oper1,
- oper2);
+ String oper1 = this.memoryReadFilter(n.f0.accept(this, argu));
+ String oper2 = this.memoryReadFilter(n.f2.accept(this, argu));
+ this.addVapor(String.format(" %s = Add(%s %s)\n",
+ _ret,
+ oper1,
+ oper2));
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s with %s",
n.getClass().getSimpleName(),
@@ -735,16 +795,19 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
* f1 -> "-"
* f2 -> PrimaryExpression()
*/
+ // Expressions return a TYPE alias which is equal to the expression!
public String visit(MinusExpression n, String argu) {
String _ret="";
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
+ _ret += this.tf.alias(this.getUniqueID());
///////////////////////////////////////////////////////////////
- String oper1 = n.f0.accept(this, argu);
- String oper2 = n.f2.accept(this, argu);
- _ret += String.format("Sub(%s %s)",
- oper1,
- oper2);
+ String oper1 = this.memoryReadFilter(n.f0.accept(this, argu));
+ String oper2 = this.memoryReadFilter(n.f2.accept(this, argu));
+ this.addVapor(String.format(" %s = Sub(%s %s)\n",
+ _ret,
+ oper1,
+ oper2));
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s with %s",
n.getClass().getSimpleName(),
@@ -757,16 +820,19 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
* f1 -> "*"
* f2 -> PrimaryExpression()
*/
+ // Expressions return a TYPE alias which is equal to the expression!
public String visit(TimesExpression n, String argu) {
String _ret="";
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
+ _ret += this.tf.alias(this.getUniqueID());
///////////////////////////////////////////////////////////////
- String oper1 = n.f0.accept(this, argu);
- String oper2 = n.f2.accept(this, argu);
- _ret += String.format("MulS(%s %s)",
- oper1,
- oper2);
+ String oper1 = this.memoryReadFilter(n.f0.accept(this, argu));
+ String oper2 = this.memoryReadFilter(n.f2.accept(this, argu));
+ this.addVapor(String.format(" %s = MulS(%s %s)\n",
+ _ret,
+ oper1,
+ oper2));
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s with %s",
n.getClass().getSimpleName(),
@@ -780,16 +846,23 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
* f2 -> PrimaryExpression()
* f3 -> "]"
*/
+ // Expressions return a TYPE alias which is equal to the expression!
public String visit(ArrayLookup n, String argu) {
String _ret="";
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
+ _ret += this.tf.alias(this.getUniqueID());
///////////////////////////////////////////////////////////////
- String arr = n.f0.accept(this, argu);
- int index = Integer.parseInt(n.f2.accept(this, argu)) * 4;
- _ret += String.format("[%s+%d]",
- arr,
- index);
+ String arrID = this.tf.alias(this.getUniqueID());
+ String arr = this.memoryReadFilter(n.f0.accept(this, argu));
+ int index = Integer.parseInt(n.f2.accept(this, argu)) * 4 + 4;
+ this.addVapor(String.format(" %s = %s\n",
+ arrID,
+ arr));
+ this.addVapor(String.format(" %s = [%s+%d]\n",
+ _ret,
+ arrID,
+ index));
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s with %s",
n.getClass().getSimpleName(),
@@ -800,16 +873,23 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
/**
* f0 -> PrimaryExpression()
* f1 -> "."
- * f2 -> "length"
+ * f2 -> "length"l
*/
+ // Expressions return a TYPE alias which is equal to the expression!
public String visit(ArrayLength n, String argu) {
String _ret="";
MinimalLogger.info(String.format("-> %s",
n.getClass().getSimpleName()));
+ _ret += this.tf.alias(this.getUniqueID());
///////////////////////////////////////////////////////////////
- String arr = n.f0.accept(this, argu);
- _ret += String.format("[%s+0]",
- arr);
+ String arrID = this.tf.alias(this.getUniqueID());
+ String arr = this.memoryReadFilter(n.f0.accept(this, argu));
+ this.addVapor(String.format(" %s = %s\n",
+ arrID,
+ arr));
+ this.addVapor(String.format(" %s = [%s+0]\n",
+ _ret,
+ arrID));
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s with %s",
n.getClass().getSimpleName(),
@@ -825,6 +905,7 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
* f4 -> ( ExpressionList() )?
* f5 -> ")"
*/
+ // Expressions return a TYPE alias which is equal to the expression!
public String visit(MessageSend n, String argu) {
String _ret="";
MinimalLogger.info(String.format("-> %s",
@@ -1039,28 +1120,25 @@ public class BoilVisitor extends GJDepthFirst<String,String> {
*/
public String visit(Identifier n, String argu) {
String _ret="";
- String id = n.f0.tokenImage;
- MinimalLogger.info(String.format("-> %s (%s)",
+ String id = n.f0.tokenImage;
+ MinimalLogger.info(String.format("-> %s (%s)",
n.getClass().getSimpleName(),
id));
///////////////////////////////////////////////////////////////
ClassInstance cur = this.symt.getClass(this.symt.getActive(TypeEnum.classname));
- TypeInstance t = this.symt.getType(id);
+ TypeInstance t = this.symt.getType(id);
if (cur.getLocals().contains(t)) {
- MinimalLogger.info(String.format("Getting the index for local variable %s",
- id));
- // calculate the attr index!
- int attr_index = (cur.getMethods().size() * 4);
- attr_index += cur.getLocals().indexOf(t);
- _ret += String.format("[this+%d]",
- attr_index);
+ MinimalLogger.info(String.format("Identifier found a class variable %s",
+ id));
+ // handle elsewhere :(
+ _ret += n.f0.tokenImage;
} else {
_ret += this.tf.alias(id);
}
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<- %s (%s) with %s",
n.getClass().getSimpleName(),
- id,
+ id,
_ret));
return _ret;
}