summaryrefslogtreecommitdiffstats
path: root/code/simpleSableCCCalulator
diff options
context:
space:
mode:
Diffstat (limited to 'code/simpleSableCCCalulator')
-rw-r--r--code/simpleSableCCCalulator/examples/maths2.txt2
-rw-r--r--code/simpleSableCCCalulator/examples/maths4.txt2
-rw-r--r--code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java26
-rw-r--r--code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java17
-rw-r--r--code/simpleSableCCCalulator/sableCCCalculator/Translation.java58
5 files changed, 62 insertions, 43 deletions
diff --git a/code/simpleSableCCCalulator/examples/maths2.txt b/code/simpleSableCCCalulator/examples/maths2.txt
index 1f1d63e..313296b 100644
--- a/code/simpleSableCCCalulator/examples/maths2.txt
+++ b/code/simpleSableCCCalulator/examples/maths2.txt
@@ -1 +1 @@
-sin(45 * 3) / 3 \ No newline at end of file
+sin(45 * 2) / 3
diff --git a/code/simpleSableCCCalulator/examples/maths4.txt b/code/simpleSableCCCalulator/examples/maths4.txt
index 5b3e67d..a922b77 100644
--- a/code/simpleSableCCCalulator/examples/maths4.txt
+++ b/code/simpleSableCCCalulator/examples/maths4.txt
@@ -1 +1 @@
-2 + 3.1
+2 + 2
diff --git a/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java b/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java
index c1046ee..b49c0d1 100644
--- a/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java
+++ b/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java
@@ -1,29 +1,31 @@
package sableCCCalculator;
+import sableCCCalculator.SymbolTable.SymbolTableIndex;
import sableCCCalculator.types.*;
import java.util.Stack;
-public class ProgramStack<T extends Type> extends Stack<T> {
+public class ProgramStack extends Stack<SymbolTableIndex> {
- public String toString() {
+ public String toString(SymbolTable table) {
String out = "Stack is now: [";
for (int i = 0; i < this.size(); i++) {
// String theStr = this.elementAt(i).toString();
// out += String.format("%s, ", theStr.substring(0, theStr.length() - 1));
- out += String.format("%s, ", this.elementAt(i).toString());
+ out += String.format("%s, ", table.get(this.elementAt(i)));
}
return out.substring(0, out.length() - 2) + "]";
}
public static void main(String[] args) {
- ProgramStack<Type> myStack = new ProgramStack<>();
- myStack.add(new Int(2));
- myStack.add(new Int(4));
- myStack.add(new Int(6));
- myStack.add(new Int(0));
- myStack.add(new Int(1));
- myStack.add(new Decimal(24601.10642));
+ ProgramStack myStack = new ProgramStack();
+ SymbolTable table = new SymbolTable();
+ myStack.add(table.addConstant(new Int(2)));
+ myStack.add(table.addConstant(new Int(4)));
+ myStack.add(table.addConstant(new Int(6)));
+ myStack.add(table.addConstant(new Int(0)));
+ myStack.add(table.addConstant(new Int(1)));
+ myStack.add(table.addConstant(new Decimal(24601.10642)));
- System.out.println(myStack.pop().getText());
- System.out.println(myStack);
+ System.out.println(table.get(myStack.pop()));
+ System.out.println(myStack.toString(table));
}
}
diff --git a/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java b/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java
index 435e48d..992e873 100644
--- a/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java
+++ b/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java
@@ -6,7 +6,8 @@ public class SymbolTable {
public interface SymbolTableIndex {}
- public abstract class Name implements SymbolTableIndex {
+ public abstract class Name implements SymbolTableIndex {
+ // can be used for functions too hopefully one day...
protected String name;
String getName() {
@@ -42,7 +43,21 @@ public class SymbolTable {
return index;
}
+ public void updateVariable(Type newItem, SymbolTableIndex index) {
+ theSymbolTable.replace(index, newItem);
+ }
+
public Type get(SymbolTableIndex index) {
return theSymbolTable.get(index);
}
+
+ public static void main(String[] args) {
+ SymbolTable symbolTable = new SymbolTable();
+ symbolTable.addConstant(new Int(3));
+ SymbolTableIndex i_var = symbolTable.addVariable(new Int(0), "i");
+ System.out.println(symbolTable.get(i_var));
+ symbolTable.updateVariable(symbolTable.get(i_var).add(new Int(1)), i_var);
+ System.out.println(symbolTable.get(i_var));
+ }
}
+ \ No newline at end of file
diff --git a/code/simpleSableCCCalulator/sableCCCalculator/Translation.java b/code/simpleSableCCCalulator/sableCCCalculator/Translation.java
index a673edb..737ecbd 100644
--- a/code/simpleSableCCCalulator/sableCCCalculator/Translation.java
+++ b/code/simpleSableCCCalulator/sableCCCalculator/Translation.java
@@ -1,88 +1,90 @@
package sableCCCalculator;
+import sableCCCalculator.SymbolTable.SymbolTableIndex;
import sableCCCalculator.analysis.*;
import sableCCCalculator.types.*;
import sableCCCalculator.node.*;
class Translation extends DepthFirstAdapter
{
- private ProgramStack<Type> programStack = new ProgramStack<>();
+ private ProgramStack programStack = new ProgramStack();
+ private SymbolTable symbolTable = new SymbolTable();
public void caseTNumber(TNumber node)
{
System.out.println("Pushing " + Integer.parseInt(node.getText()) + " to stack");
- programStack.push(new Int(node.getText()));
- System.out.println(programStack);
+ programStack.push(symbolTable.addConstant(new Int(node.getText())));
+ System.out.println(programStack.toString(symbolTable));
}
public void caseTDouble(TDouble node)
{
System.out.println("Pushing a double: " + Double.parseDouble(node.getText()));
- programStack.push(new Decimal(node.getText()));
- System.out.println(programStack);
+ programStack.push(symbolTable.addConstant(new Decimal(node.getText())));
+ System.out.println(programStack.toString(symbolTable));
}
public void outASineTerm(ASineTerm node)
{
- Double num = Double.parseDouble(programStack.pop().getText());
+ Double num = Double.parseDouble(symbolTable.get(programStack.pop()).toString());
System.out.println("Popped " + num);
Double out = Math.sin(Math.toRadians(num));
- programStack.push(new Decimal(out));
+ programStack.push(symbolTable.addConstant(new Decimal(out)));
System.out.println("Pushed sin(" + num + ") = " + out + " to stack");
- System.out.println(programStack);
+ System.out.println(programStack.toString(symbolTable));
}
public void outAPlusExpr(APlusExpr node)
{
- Type op2 = programStack.pop();
- Type op1 = programStack.pop();
+ Type op2 = symbolTable.get(programStack.pop());
+ Type op1 = symbolTable.get(programStack.pop());
System.out.println("Popped " + op1 + " and " + op2 + " from stack");
Type out = op1.add(op2);
- programStack.push(out);
+ programStack.push(symbolTable.addConstant(out));
System.out.println("Pushed " + op1 + "+" + op2 + "=" + out + " to stack");
- System.out.println(programStack);
+ System.out.println(programStack.toString(symbolTable));
}
public void outAMinusExpr(AMinusExpr node)
{
- Type op2 = programStack.pop();
- Type op1 = programStack.pop();
+ Type op2 = symbolTable.get(programStack.pop());
+ Type op1 = symbolTable.get(programStack.pop());
System.out.println("Popped " + op1 + " and " + op2 + " from stack");
Type out = op1.sub(op2);
- programStack.push(out);
+ programStack.push(symbolTable.addConstant(out));
System.out.println("Pushed " + op1 + "-" + op2 + "=" + out + " to stack");
- System.out.println(programStack);
+ System.out.println(programStack.toString(symbolTable));
}
public void outAMultFactor(AMultFactor node)
{
- Type op2 = programStack.pop();
- Type op1 = programStack.pop();
+ Type op2 = symbolTable.get(programStack.pop());
+ Type op1 = symbolTable.get(programStack.pop());
System.out.println("Popped " + op1 + " and " + op2 + " from stack");
Type out = op1.mult(op2);
- programStack.push(out);
+ programStack.push(symbolTable.addConstant(out));
System.out.println("Pushed " + op1 + "*" + op2 + "=" + out + " to stack");
- System.out.println(programStack);
+ System.out.println(programStack.toString(symbolTable));
}
public void outADivFactor(ADivFactor node)
{
- Type op2 = programStack.pop();
- Type op1 = programStack.pop();
+ Type op2 = symbolTable.get(programStack.pop());
+ Type op1 = symbolTable.get(programStack.pop());
System.out.println("Popped " + op1 + " and " + op2 + " from stack");
Type out = op1.div(op2);
- programStack.push(out);
+ programStack.push(symbolTable.addConstant(out));
System.out.println("Pushed " + op1 + "/" + op2 + "=" + out + " to stack");
- System.out.println(programStack);
+ System.out.println(programStack.toString(symbolTable));
}
public void outAModFactor(AModFactor node)
{
- Type op2 = programStack.pop();
- Type op1 = programStack.pop();
+ Type op2 = symbolTable.get(programStack.pop());
+ Type op1 = symbolTable.get(programStack.pop());
System.out.println("Popped " + op1 + " and " + op2 + " from stack");
Type out = op1.mod(op2);
- programStack.push(out);
+ programStack.push(symbolTable.addConstant(out));
System.out.println("Pushed " + op1 + "%" + op2 + "=" + out + " to stack");
- System.out.println(programStack);
+ System.out.println(programStack.toString(symbolTable));
}
}