From 33359862f5455dc7003ebbe5357c611298042cee Mon Sep 17 00:00:00 2001 From: "chris.sutcliffe" Date: Mon, 29 Nov 2021 16:46:56 +0000 Subject: clean repo (remove code dir, add UI dir) --- code/simpleSableCCCalulator/Makefile | 13 ---- code/simpleSableCCCalulator/README.md | 22 ------ code/simpleSableCCCalulator/examples/maths.txt | 1 - code/simpleSableCCCalulator/examples/maths2.txt | 1 - code/simpleSableCCCalulator/examples/maths3.txt | 1 - code/simpleSableCCCalulator/examples/maths4.txt | 1 - .../sableCCCalculator/Compiler.java | 27 ------- .../sableCCCalculator/ProgramStack.java | 31 -------- .../sableCCCalculator/SymbolTable.java | 63 --------------- .../sableCCCalculator/Translation.java | 89 ---------------------- .../sableCCCalculator/types/Decimal.java | 57 -------------- .../sableCCCalculator/types/Int.java | 60 --------------- .../sableCCCalculator/types/Type.java | 22 ------ 13 files changed, 388 deletions(-) delete mode 100644 code/simpleSableCCCalulator/Makefile delete mode 100644 code/simpleSableCCCalulator/README.md delete mode 100644 code/simpleSableCCCalulator/examples/maths.txt delete mode 100644 code/simpleSableCCCalulator/examples/maths2.txt delete mode 100644 code/simpleSableCCCalulator/examples/maths3.txt delete mode 100644 code/simpleSableCCCalulator/examples/maths4.txt delete mode 100644 code/simpleSableCCCalulator/sableCCCalculator/Compiler.java delete mode 100644 code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java delete mode 100644 code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java delete mode 100644 code/simpleSableCCCalulator/sableCCCalculator/Translation.java delete mode 100644 code/simpleSableCCCalulator/sableCCCalculator/types/Decimal.java delete mode 100644 code/simpleSableCCCalulator/sableCCCalculator/types/Int.java delete mode 100644 code/simpleSableCCCalulator/sableCCCalculator/types/Type.java (limited to 'code/simpleSableCCCalulator') diff --git a/code/simpleSableCCCalulator/Makefile b/code/simpleSableCCCalulator/Makefile deleted file mode 100644 index 477422b..0000000 --- a/code/simpleSableCCCalulator/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -all: - java -jar ../../../sablecc-3.7/lib/sablecc.jar sableCCCalculator.grammar - javac sableCCCalculator/*.java - javac sableCCCalculator/types/*.java - - -clean: - rm -vf sableCCCalculator/*.class - rm -vf sableCCCalculator/types/*class - rm -rfv sableCCCalculator/analysis/ - rm -rfv sableCCCalculator/lexer/ - rm -rfv sableCCCalculator/node/ - rm -rvf sableCCCalculator/parser/ diff --git a/code/simpleSableCCCalulator/README.md b/code/simpleSableCCCalulator/README.md deleted file mode 100644 index 4878685..0000000 --- a/code/simpleSableCCCalulator/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# simpleSableCCCalculator - -sableCC is a too used to parse .grammar files (containing a BNF, and lexer information) -into a lexer and parser. We can do a depth first traversal of the produced abstract syntax tree -to parse in the correct order. This is in the file `Translation.java`. - -You produce a lexer and parser by running the sablecc .jar file you can download [here](http://downloads.sourceforge.net/sablecc/sablecc-3.7.zip). Then run it with the first argument as the grammar file: - -`java -jar sablecc-3.7/lib/sablecc.jar sableCCCalculator.grammar` - -(changing the paths as appropriate). The produced java files are not included in git since they're unnessicary. We compile the compiler, program stack and translator: - -`javac sableCCCalculator/*.java` - -I setup a makefil that can be used. First make sure that sablecc is extracted in the directory below `EsotericProject` and just run `make`. - -Then we can run the program. For now it only works by reading files. There are some example maths questions in the examples folder: - -`java sableCCCalculator.Compiler examples/maths.txt` - - - diff --git a/code/simpleSableCCCalulator/examples/maths.txt b/code/simpleSableCCCalulator/examples/maths.txt deleted file mode 100644 index 36726f5..0000000 --- a/code/simpleSableCCCalulator/examples/maths.txt +++ /dev/null @@ -1 +0,0 @@ -(36/2 + 45.2) * 3 \ No newline at end of file diff --git a/code/simpleSableCCCalulator/examples/maths2.txt b/code/simpleSableCCCalulator/examples/maths2.txt deleted file mode 100644 index 313296b..0000000 --- a/code/simpleSableCCCalulator/examples/maths2.txt +++ /dev/null @@ -1 +0,0 @@ -sin(45 * 2) / 3 diff --git a/code/simpleSableCCCalulator/examples/maths3.txt b/code/simpleSableCCCalulator/examples/maths3.txt deleted file mode 100644 index e092af1..0000000 --- a/code/simpleSableCCCalulator/examples/maths3.txt +++ /dev/null @@ -1 +0,0 @@ -3-1+2 \ No newline at end of file diff --git a/code/simpleSableCCCalulator/examples/maths4.txt b/code/simpleSableCCCalulator/examples/maths4.txt deleted file mode 100644 index a922b77..0000000 --- a/code/simpleSableCCCalulator/examples/maths4.txt +++ /dev/null @@ -1 +0,0 @@ -2 + 2 diff --git a/code/simpleSableCCCalulator/sableCCCalculator/Compiler.java b/code/simpleSableCCCalulator/sableCCCalculator/Compiler.java deleted file mode 100644 index 7430cfe..0000000 --- a/code/simpleSableCCCalulator/sableCCCalculator/Compiler.java +++ /dev/null @@ -1,27 +0,0 @@ -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 deleted file mode 100644 index b49c0d1..0000000 --- a/code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java +++ /dev/null @@ -1,31 +0,0 @@ -package sableCCCalculator; -import sableCCCalculator.SymbolTable.SymbolTableIndex; -import sableCCCalculator.types.*; -import java.util.Stack; - -public class ProgramStack extends Stack { - - 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, ", table.get(this.elementAt(i))); - } - return out.substring(0, out.length() - 2) + "]"; - } - - public static void main(String[] args) { - 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(table.get(myStack.pop())); - System.out.println(myStack.toString(table)); - } -} diff --git a/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java b/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java deleted file mode 100644 index 992e873..0000000 --- a/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java +++ /dev/null @@ -1,63 +0,0 @@ -package sableCCCalculator; -import java.util.HashMap; -import sableCCCalculator.types.*; - -public class SymbolTable { - - public interface SymbolTableIndex {} - - public abstract class Name implements SymbolTableIndex { - // can be used for functions too hopefully one day... - 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 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 deleted file mode 100644 index 43041c4..0000000 --- a/code/simpleSableCCCalulator/sableCCCalculator/Translation.java +++ /dev/null @@ -1,89 +0,0 @@ -package sableCCCalculator; -import sableCCCalculator.analysis.*; -import sableCCCalculator.types.*; -import sableCCCalculator.node.*; - -class Translation extends DepthFirstAdapter -{ - 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(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(symbolTable.addConstant(new Decimal(node.getText()))); - System.out.println(programStack.toString(symbolTable)); - } - - public void outASineTerm(ASineTerm node) - { - Double num = Double.parseDouble(symbolTable.get(programStack.pop()).toString()); - System.out.println("Popped " + num); - Double out = Math.sin(Math.toRadians(num)); - programStack.push(symbolTable.addConstant(new Decimal(out))); - System.out.println("Pushed sin(" + num + ") = " + out + " to stack"); - System.out.println(programStack.toString(symbolTable)); - } - - public void outAPlusExpr(APlusExpr node) - { - 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(symbolTable.addConstant(out)); - System.out.println("Pushed " + op1 + "+" + op2 + "=" + out + " to stack"); - System.out.println(programStack.toString(symbolTable)); - } - - public void outAMinusExpr(AMinusExpr node) - { - 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(symbolTable.addConstant(out)); - System.out.println("Pushed " + op1 + "-" + op2 + "=" + out + " to stack"); - System.out.println(programStack.toString(symbolTable)); - } - - public void outAMultFactor(AMultFactor node) - { - 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(symbolTable.addConstant(out)); - System.out.println("Pushed " + op1 + "*" + op2 + "=" + out + " to stack"); - System.out.println(programStack.toString(symbolTable)); - } - - public void outADivFactor(ADivFactor node) - { - 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(symbolTable.addConstant(out)); - System.out.println("Pushed " + op1 + "/" + op2 + "=" + out + " to stack"); - System.out.println(programStack.toString(symbolTable)); - } - - public void outAModFactor(AModFactor node) - { - 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(symbolTable.addConstant(out)); - System.out.println("Pushed " + op1 + "%" + op2 + "=" + out + " to stack"); - System.out.println(programStack.toString(symbolTable)); - } -} diff --git a/code/simpleSableCCCalulator/sableCCCalculator/types/Decimal.java b/code/simpleSableCCCalulator/sableCCCalculator/types/Decimal.java deleted file mode 100644 index 5836ff1..0000000 --- a/code/simpleSableCCCalulator/sableCCCalculator/types/Decimal.java +++ /dev/null @@ -1,57 +0,0 @@ -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 deleted file mode 100644 index 1ccd20e..0000000 --- a/code/simpleSableCCCalulator/sableCCCalculator/types/Int.java +++ /dev/null @@ -1,60 +0,0 @@ -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 deleted file mode 100644 index 359f2b8..0000000 --- a/code/simpleSableCCCalulator/sableCCCalculator/types/Type.java +++ /dev/null @@ -1,22 +0,0 @@ -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