diff options
author | jwansek <eddie.atten.ea29@gmail.com> | 2021-10-18 16:40:56 +0100 |
---|---|---|
committer | jwansek <eddie.atten.ea29@gmail.com> | 2021-10-18 16:40:56 +0100 |
commit | e09b0bb865bbb0087c46b4acd90b759f14dfa824 (patch) | |
tree | 824f93a769d5ae3b38b4f6b3597347b78478e69c /code/simpleSableCCCalulator/sableCCCalculator/Translation.java | |
parent | 3c3706a8957f27d7bcb553eff6ded1c6dc76fa24 (diff) | |
download | esotericFORTRAN-e09b0bb865bbb0087c46b4acd90b759f14dfa824.tar.gz esotericFORTRAN-e09b0bb865bbb0087c46b4acd90b759f14dfa824.zip |
added sableCC calculator
Diffstat (limited to 'code/simpleSableCCCalulator/sableCCCalculator/Translation.java')
-rw-r--r-- | code/simpleSableCCCalulator/sableCCCalculator/Translation.java | 132 |
1 files changed, 132 insertions, 0 deletions
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); + } +} |