summaryrefslogtreecommitdiffstats
path: root/code/simpleSableCCCalulator/sableCCCalculator
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2021-10-18 16:40:56 +0100
committerjwansek <eddie.atten.ea29@gmail.com>2021-10-18 16:40:56 +0100
commite09b0bb865bbb0087c46b4acd90b759f14dfa824 (patch)
tree824f93a769d5ae3b38b4f6b3597347b78478e69c /code/simpleSableCCCalulator/sableCCCalculator
parent3c3706a8957f27d7bcb553eff6ded1c6dc76fa24 (diff)
downloadesotericFORTRAN-e09b0bb865bbb0087c46b4acd90b759f14dfa824.tar.gz
esotericFORTRAN-e09b0bb865bbb0087c46b4acd90b759f14dfa824.zip
added sableCC calculator
Diffstat (limited to 'code/simpleSableCCCalulator/sableCCCalculator')
-rw-r--r--code/simpleSableCCCalulator/sableCCCalculator/Compiler.java27
-rw-r--r--code/simpleSableCCCalulator/sableCCCalculator/ProgramStack.java28
-rw-r--r--code/simpleSableCCCalulator/sableCCCalculator/Translation.java132
3 files changed, 187 insertions, 0 deletions
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<T extends Token> extends Stack<T> {
+
+ 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<Token> 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<Token> 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);
+ }
+}