From e09b0bb865bbb0087c46b4acd90b759f14dfa824 Mon Sep 17 00:00:00 2001 From: jwansek Date: Mon, 18 Oct 2021 16:40:56 +0100 Subject: added sableCC calculator --- .../sableCCCalculator/Compiler.java | 27 +++++ .../sableCCCalculator/ProgramStack.java | 28 +++++ .../sableCCCalculator/Translation.java | 132 +++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 code/simpleSableCCCalulator/sableCCCalculator/Compiler.java create mode 100644 code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java create mode 100644 code/simpleSableCCCalulator/sableCCCalculator/Translation.java (limited to 'code/simpleSableCCCalulator/sableCCCalculator') diff --git a/code/simpleSableCCCalulator/sableCCCalculator/Compiler.java b/code/simpleSableCCCalulator/sableCCCalculator/Compiler.java new file mode 100644 index 0000000..7430cfe --- /dev/null +++ b/code/simpleSableCCCalulator/sableCCCalculator/Compiler.java @@ -0,0 +1,27 @@ +package sableCCCalculator; +import sableCCCalculator.parser.*; +import sableCCCalculator.lexer.*; +import sableCCCalculator.node.*; +import java.io.*; + +public class Compiler +{ + public static void main(String[] args) + { + try + { + System.out.println("Using source file: " + args[0]); + // Create a Parser instance. + Parser p = new Parser(new Lexer(new PushbackReader(new InputStreamReader(new FileInputStream(args[0])), 1024))); + // Parse the input. + Start tree = p.parse(); + // Apply the translation. + tree.apply(new Translation()); + System.out.println(""); + } + catch(Exception e) + { + System.out.println(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java b/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java new file mode 100644 index 0000000..1875e57 --- /dev/null +++ b/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java @@ -0,0 +1,28 @@ +package sableCCCalculator; +import sableCCCalculator.node.*; +import java.util.Stack; + +public class ProgramStack extends Stack { + + public String toString() { + 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)); + } + return out.substring(0, out.length() - 2) + "]"; + } + + public static void main(String[] args) { + ProgramStack myStack = new ProgramStack<>(); + myStack.add(new TNumber("2")); + myStack.add(new TNumber("4")); + myStack.add(new TNumber("6")); + myStack.add(new TNumber("0")); + myStack.add(new TNumber("1")); + myStack.add(new TDouble("24601.10642")); + + System.out.println(myStack.pop().getText()); + System.out.println(myStack); + } +} diff --git a/code/simpleSableCCCalulator/sableCCCalculator/Translation.java b/code/simpleSableCCCalulator/sableCCCalculator/Translation.java new file mode 100644 index 0000000..d8fd74d --- /dev/null +++ b/code/simpleSableCCCalulator/sableCCCalculator/Translation.java @@ -0,0 +1,132 @@ +package sableCCCalculator; +import sableCCCalculator.analysis.*; +import sableCCCalculator.node.*; + +class Translation extends DepthFirstAdapter +{ + private ProgramStack programStack = new ProgramStack<>(); + + public void caseTNumber(TNumber node) + { + System.out.println("Pushing " + Integer.parseInt(node.getText()) + " to stack"); + programStack.push(node); + System.out.println(programStack); + } + + public void caseTDouble(TDouble node) + { + System.out.println("Pushing a double: " + Double.parseDouble(node.getText())); + programStack.push(node); + System.out.println(programStack); + } + + public void outASineTerm(ASineTerm node) + { + Double num = Double.parseDouble(programStack.pop().getText()); + System.out.println("Popped " + num); + Double out = Math.sin(Math.toRadians(num)); + programStack.push(new TDouble(String.format("%f", out))); + System.out.println("Pushed sin(" + num + ") = " + out + " to stack"); + System.out.println(programStack); + } + + public void outAPlusExpr(APlusExpr node) + { + Double num2 = Double.parseDouble(programStack.pop().getText()); + Double num1 = Double.parseDouble(programStack.pop().getText()); + System.out.println("Popped " + num1 + " and " + num2 + " from stack"); + Double out = num1 + num2; + if ((out % 1) == 0) + { + // the output is an integer, change types to save memory + programStack.push(new TNumber(String.format("%d", out.intValue()))); + } + else + { + programStack.push(new TDouble(String.format("%f", out))); + } + + System.out.println("Pushed " + num1 + "+" + num2 + "=" + out + " to stack"); + System.out.println(programStack); + } + + public void outAMinusExpr(AMinusExpr node) + { + Double num2 = Double.parseDouble(programStack.pop().getText()); + Double num1 = Double.parseDouble(programStack.pop().getText()); + System.out.println("Popped " + num1 + " and " + num2 + " from stack"); + Double out = num1 - num2; + if ((out % 1) == 0) + { + // the output is an integer, change types to save memory + programStack.push(new TNumber(String.format("%d", out.intValue()))); + } + else + { + programStack.push(new TDouble(String.format("%f", out))); + } + + System.out.println("Pushed " + num1 + "-" + num2 + "=" + out + " to stack"); + System.out.println(programStack); + } + + public void outAMultFactor(AMultFactor node) + { + Double num2 = Double.parseDouble(programStack.pop().getText()); + Double num1 = Double.parseDouble(programStack.pop().getText()); + System.out.println("Popped " + num1 + " and " + num2 + " from stack"); + Double out = num1 * num2; + if ((out % 1) == 0) + { + // the output is an integer, change types to save memory + programStack.push(new TNumber(String.format("%d", out.intValue()))); + } + else + { + programStack.push(new TDouble(String.format("%f", out))); + } + + System.out.println("Pushed " + num1 + "*" + num2 + "=" + out + " to stack"); + System.out.println(programStack); + } + + public void outADivFactor(ADivFactor node) + { + Double num2 = Double.parseDouble(programStack.pop().getText()); + Double num1 = Double.parseDouble(programStack.pop().getText()); + System.out.println("Popped " + num1 + " and " + num2 + " from stack"); + Double out = num1 / num2; + if ((out % 1) == 0) + { + // the output is an integer, change types to save memory + programStack.push(new TNumber(String.format("%d", out.intValue()))); + } + else + { + programStack.push(new TDouble(String.format("%f", out))); + } + + System.out.println("Pushed " + num1 + "/" + num2 + "=" + out + " to stack"); + System.out.println(programStack); + } + + public void outAModFactor(AModFactor node) + { + Double num2 = Double.parseDouble(programStack.pop().getText()); + Double num1 = Double.parseDouble(programStack.pop().getText()); + System.out.println("Popped " + num1 + " and " + num2 + " from stack"); + Double out = num1 % num2; + if ((out % 1) == 0) + { + // the output is an integer, change types to save memory + programStack.push(new TNumber(String.format("%d", out.intValue()))); + } + else + { + programStack.push(new TDouble(String.format("%f", out))); + } + + System.out.println("Pushed " + num1 + "%" + num2 + "=" + out + " to stack"); + System.out.println(programStack); + } +} -- cgit v1.2.3 From 8f50e4d189bad9dbd180fd945881950769c3a989 Mon Sep 17 00:00:00 2001 From: jwansek Date: Sun, 24 Oct 2021 14:07:29 +0100 Subject: added the start of a type system --- .../sableCCCalculator/ProgramStack.java | 23 +++-- .../sableCCCalculator/Translation.java | 114 +++++++-------------- .../sableCCCalculator/types/Decimal.java | 57 +++++++++++ .../sableCCCalculator/types/Int.java | 60 +++++++++++ .../sableCCCalculator/types/Type.java | 22 ++++ 5 files changed, 186 insertions(+), 90 deletions(-) create mode 100644 code/simpleSableCCCalulator/sableCCCalculator/types/Decimal.java create mode 100644 code/simpleSableCCCalulator/sableCCCalculator/types/Int.java create mode 100644 code/simpleSableCCCalulator/sableCCCalculator/types/Type.java (limited to 'code/simpleSableCCCalulator/sableCCCalculator') diff --git a/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java b/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java index 1875e57..c1046ee 100644 --- a/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java +++ b/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java @@ -1,26 +1,27 @@ package sableCCCalculator; -import sableCCCalculator.node.*; +import sableCCCalculator.types.*; import java.util.Stack; -public class ProgramStack extends Stack { +public class ProgramStack extends Stack { public String toString() { 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)); + // String theStr = this.elementAt(i).toString(); + // out += String.format("%s, ", theStr.substring(0, theStr.length() - 1)); + out += String.format("%s, ", this.elementAt(i).toString()); } return out.substring(0, out.length() - 2) + "]"; } public static void main(String[] args) { - ProgramStack myStack = new ProgramStack<>(); - myStack.add(new TNumber("2")); - myStack.add(new TNumber("4")); - myStack.add(new TNumber("6")); - myStack.add(new TNumber("0")); - myStack.add(new TNumber("1")); - myStack.add(new TDouble("24601.10642")); + ProgramStack 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)); System.out.println(myStack.pop().getText()); System.out.println(myStack); diff --git a/code/simpleSableCCCalulator/sableCCCalculator/Translation.java b/code/simpleSableCCCalulator/sableCCCalculator/Translation.java index d8fd74d..a673edb 100644 --- a/code/simpleSableCCCalulator/sableCCCalculator/Translation.java +++ b/code/simpleSableCCCalulator/sableCCCalculator/Translation.java @@ -1,22 +1,23 @@ package sableCCCalculator; import sableCCCalculator.analysis.*; +import sableCCCalculator.types.*; import sableCCCalculator.node.*; class Translation extends DepthFirstAdapter { - private ProgramStack programStack = new ProgramStack<>(); + private ProgramStack programStack = new ProgramStack<>(); public void caseTNumber(TNumber node) { System.out.println("Pushing " + Integer.parseInt(node.getText()) + " to stack"); - programStack.push(node); + programStack.push(new Int(node.getText())); System.out.println(programStack); } public void caseTDouble(TDouble node) { System.out.println("Pushing a double: " + Double.parseDouble(node.getText())); - programStack.push(node); + programStack.push(new Decimal(node.getText())); System.out.println(programStack); } @@ -25,108 +26,63 @@ class Translation extends DepthFirstAdapter Double num = Double.parseDouble(programStack.pop().getText()); System.out.println("Popped " + num); Double out = Math.sin(Math.toRadians(num)); - programStack.push(new TDouble(String.format("%f", out))); + programStack.push(new Decimal(out)); System.out.println("Pushed sin(" + num + ") = " + out + " to stack"); System.out.println(programStack); } public void outAPlusExpr(APlusExpr node) { - Double num2 = Double.parseDouble(programStack.pop().getText()); - Double num1 = Double.parseDouble(programStack.pop().getText()); - System.out.println("Popped " + num1 + " and " + num2 + " from stack"); - Double out = num1 + num2; - if ((out % 1) == 0) - { - // the output is an integer, change types to save memory - programStack.push(new TNumber(String.format("%d", out.intValue()))); - } - else - { - programStack.push(new TDouble(String.format("%f", out))); - } - - System.out.println("Pushed " + num1 + "+" + num2 + "=" + out + " to stack"); + Type op2 = programStack.pop(); + Type op1 = programStack.pop(); + System.out.println("Popped " + op1 + " and " + op2 + " from stack"); + Type out = op1.add(op2); + programStack.push(out); + System.out.println("Pushed " + op1 + "+" + op2 + "=" + out + " to stack"); System.out.println(programStack); } public void outAMinusExpr(AMinusExpr node) { - Double num2 = Double.parseDouble(programStack.pop().getText()); - Double num1 = Double.parseDouble(programStack.pop().getText()); - System.out.println("Popped " + num1 + " and " + num2 + " from stack"); - Double out = num1 - num2; - if ((out % 1) == 0) - { - // the output is an integer, change types to save memory - programStack.push(new TNumber(String.format("%d", out.intValue()))); - } - else - { - programStack.push(new TDouble(String.format("%f", out))); - } - - System.out.println("Pushed " + num1 + "-" + num2 + "=" + out + " to stack"); + Type op2 = programStack.pop(); + Type op1 = programStack.pop(); + System.out.println("Popped " + op1 + " and " + op2 + " from stack"); + Type out = op1.sub(op2); + programStack.push(out); + System.out.println("Pushed " + op1 + "-" + op2 + "=" + out + " to stack"); System.out.println(programStack); } public void outAMultFactor(AMultFactor node) { - Double num2 = Double.parseDouble(programStack.pop().getText()); - Double num1 = Double.parseDouble(programStack.pop().getText()); - System.out.println("Popped " + num1 + " and " + num2 + " from stack"); - Double out = num1 * num2; - if ((out % 1) == 0) - { - // the output is an integer, change types to save memory - programStack.push(new TNumber(String.format("%d", out.intValue()))); - } - else - { - programStack.push(new TDouble(String.format("%f", out))); - } - - System.out.println("Pushed " + num1 + "*" + num2 + "=" + out + " to stack"); + Type op2 = programStack.pop(); + Type op1 = programStack.pop(); + System.out.println("Popped " + op1 + " and " + op2 + " from stack"); + Type out = op1.mult(op2); + programStack.push(out); + System.out.println("Pushed " + op1 + "*" + op2 + "=" + out + " to stack"); System.out.println(programStack); } public void outADivFactor(ADivFactor node) { - Double num2 = Double.parseDouble(programStack.pop().getText()); - Double num1 = Double.parseDouble(programStack.pop().getText()); - System.out.println("Popped " + num1 + " and " + num2 + " from stack"); - Double out = num1 / num2; - if ((out % 1) == 0) - { - // the output is an integer, change types to save memory - programStack.push(new TNumber(String.format("%d", out.intValue()))); - } - else - { - programStack.push(new TDouble(String.format("%f", out))); - } - - System.out.println("Pushed " + num1 + "/" + num2 + "=" + out + " to stack"); + Type op2 = programStack.pop(); + Type op1 = programStack.pop(); + System.out.println("Popped " + op1 + " and " + op2 + " from stack"); + Type out = op1.div(op2); + programStack.push(out); + System.out.println("Pushed " + op1 + "/" + op2 + "=" + out + " to stack"); System.out.println(programStack); } public void outAModFactor(AModFactor node) { - Double num2 = Double.parseDouble(programStack.pop().getText()); - Double num1 = Double.parseDouble(programStack.pop().getText()); - System.out.println("Popped " + num1 + " and " + num2 + " from stack"); - Double out = num1 % num2; - if ((out % 1) == 0) - { - // the output is an integer, change types to save memory - programStack.push(new TNumber(String.format("%d", out.intValue()))); - } - else - { - programStack.push(new TDouble(String.format("%f", out))); - } - - System.out.println("Pushed " + num1 + "%" + num2 + "=" + out + " to stack"); + Type op2 = programStack.pop(); + Type op1 = programStack.pop(); + System.out.println("Popped " + op1 + " and " + op2 + " from stack"); + Type out = op1.mod(op2); + programStack.push(out); + System.out.println("Pushed " + op1 + "%" + op2 + "=" + out + " to stack"); System.out.println(programStack); } } diff --git a/code/simpleSableCCCalulator/sableCCCalculator/types/Decimal.java b/code/simpleSableCCCalulator/sableCCCalculator/types/Decimal.java new file mode 100644 index 0000000..5836ff1 --- /dev/null +++ b/code/simpleSableCCCalulator/sableCCCalculator/types/Decimal.java @@ -0,0 +1,57 @@ +package sableCCCalculator.types; + +public class Decimal extends Type { + + public Decimal(Double toDecimal) { + javaObject = (Double)toDecimal; + } + + public Decimal(String toDecimal) { + javaObject = (Double)Double.parseDouble(toDecimal); + } + + public Decimal add(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Decimal")) { + return new Decimal((Double)this.javaObject + (double)toAdd.javaObject); + } else { + return new Decimal((Double)this.javaObject + Double.parseDouble(String.format("%d", (int)toAdd.javaObject))); + } + } + + public Decimal sub(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Decimal")) { + return new Decimal((Double)this.javaObject - (double)toAdd.javaObject); + } else { + return new Decimal((Double)this.javaObject - Double.parseDouble(String.format("%d", (int)toAdd.javaObject))); + } + } + + public Decimal mult(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Decimal")) { + return new Decimal((Double)this.javaObject * (double)toAdd.javaObject); + } else { + return new Decimal((Double)this.javaObject * Double.parseDouble(String.format("%d", (int)toAdd.javaObject))); + } + } + + public Decimal div(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Decimal")) { + return new Decimal((Double)this.javaObject / (double)toAdd.javaObject); + } else { + return new Decimal((Double)this.javaObject / Double.parseDouble(String.format("%d", (int)toAdd.javaObject))); + } + } + + public Decimal mod(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Decimal")) { + return new Decimal((Double)this.javaObject % (double)toAdd.javaObject); + } else { + return new Decimal((Double)this.javaObject % Double.parseDouble(String.format("%d", (int)toAdd.javaObject))); + } + } + + public static void main(String[] args) { + Decimal aDec = new Decimal(3.1); + System.out.println(aDec.sub(new Int(2))); + } +} diff --git a/code/simpleSableCCCalulator/sableCCCalculator/types/Int.java b/code/simpleSableCCCalulator/sableCCCalculator/types/Int.java new file mode 100644 index 0000000..1ccd20e --- /dev/null +++ b/code/simpleSableCCCalulator/sableCCCalculator/types/Int.java @@ -0,0 +1,60 @@ +package sableCCCalculator.types; + +public class Int extends Type { + + public Int(int toInt) { + javaObject = (Integer)toInt; + } + + public Int(String toInt) { + javaObject = (Integer)Integer.parseInt(toInt); + } + + public Type add(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Int")) { + return new Int((int)this.javaObject + (int)toAdd.javaObject); + } else { + return new Decimal(Double.parseDouble(String.format("%d", (int)this.javaObject)) + (Double)toAdd.javaObject); + } + } + + public Type sub(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Int")) { + return new Int((int)this.javaObject - (int)toAdd.javaObject); + } else { + return new Decimal(Double.parseDouble(String.format("%d", (int)this.javaObject)) - (Double)toAdd.javaObject); + } + } + + public Type mult(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Int")) { + return new Int((int)this.javaObject * (int)toAdd.javaObject); + } else { + return new Decimal(Double.parseDouble(String.format("%d", (int)this.javaObject)) * (Double)toAdd.javaObject); + } + } + + public Type div(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Int")) { + return new Int((int)this.javaObject / (int)toAdd.javaObject); + } else { + return new Decimal(Double.parseDouble(String.format("%d", (int)this.javaObject)) / (Double)toAdd.javaObject); + } + } + + public Type mod(Type toAdd) { + if (toAdd.getClass().getSimpleName().equals("Int")) { + return new Int((int)this.javaObject % (int)toAdd.javaObject); + } else { + return new Decimal(Double.parseDouble(String.format("%d", (int)this.javaObject)) % (Double)toAdd.javaObject); + } + } + + public static void main(String[] args) { + Int int1 = new Int(3); + System.out.println(int1.add(new Int(4))); + + Int int2 = new Int(3); + System.out.println(int2.mult(new Decimal(2.2))); + } +} diff --git a/code/simpleSableCCCalulator/sableCCCalculator/types/Type.java b/code/simpleSableCCCalulator/sableCCCalculator/types/Type.java new file mode 100644 index 0000000..359f2b8 --- /dev/null +++ b/code/simpleSableCCCalulator/sableCCCalculator/types/Type.java @@ -0,0 +1,22 @@ +package sableCCCalculator.types; + +// not happy with the amount of polymorphism +// using generics would be better but idk how that'd work in this context... +public abstract class Type { + + protected Object javaObject; + + public abstract Type add(Type toAdd); + public abstract Type sub(Type toSub); + public abstract Type mult(Type toMult); + public abstract Type div(Type toDiv); + public abstract Type mod(Type toMod); + + public String toString() { + return javaObject.toString(); + } + + public String getText() { + return this.toString(); + } +} \ No newline at end of file -- cgit v1.2.3 From 814c115d8accedc07e42d218bfdab639148fa836 Mon Sep 17 00:00:00 2001 From: jwansek Date: Sun, 24 Oct 2021 15:59:49 +0100 Subject: started work on the symbol table, preparing for variables --- .../sableCCCalculator/SymbolTable.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java (limited to 'code/simpleSableCCCalulator/sableCCCalculator') diff --git a/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java b/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java new file mode 100644 index 0000000..435e48d --- /dev/null +++ b/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java @@ -0,0 +1,48 @@ +package sableCCCalculator; +import java.util.HashMap; +import sableCCCalculator.types.*; + +public class SymbolTable { + + public interface SymbolTableIndex {} + + public abstract class Name implements SymbolTableIndex { + protected String name; + + String getName() { + return this.name; + } + } + + public class Constant implements SymbolTableIndex { + int index; + + public Constant(int index) { + this.index = index; + } + } + + public class Variable extends Name { + public Variable(String name) { + this.name = name; + } + } + + private HashMap theSymbolTable = new HashMap<>(); + + public SymbolTableIndex addConstant(Type item) { + SymbolTableIndex index = new Constant(item.hashCode()); + theSymbolTable.put(index, item); + return index; + } + + public SymbolTableIndex addVariable(Type item, String name) { + SymbolTableIndex index = new Variable(name); + theSymbolTable.put(index, item); + return index; + } + + public Type get(SymbolTableIndex index) { + return theSymbolTable.get(index); + } +} -- cgit v1.2.3 From aa3a6929584eaa63cafb73353a62effd75d4dd8c Mon Sep 17 00:00:00 2001 From: jwansek Date: Mon, 25 Oct 2021 15:20:30 +0100 Subject: added using symbol table in stack --- .../sableCCCalculator/ProgramStack.java | 26 +++++----- .../sableCCCalculator/SymbolTable.java | 17 ++++++- .../sableCCCalculator/Translation.java | 58 +++++++++++----------- 3 files changed, 60 insertions(+), 41 deletions(-) (limited to 'code/simpleSableCCCalulator/sableCCCalculator') 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 extends Stack { +public class ProgramStack extends Stack { - 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 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 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)); } } -- cgit v1.2.3