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); } }