public void appendOutputString(ExpressionProgram prog, int myIndex, StringBuffer buffer) { // The ExpressionCommand occurs in the program prog at the index indicated by myIndex. // Add a print string representation of the sub-expression represented by this command // (including any previous commands in the program that generate data used by this // command). int upper = prog.extent(myIndex - 1); buffer.append("sum("); buffer.append(sumVar.getName()); buffer.append(", "); prog.appendOutputString(myIndex - 1 - upper, buffer); buffer.append(", "); prog.appendOutputString(myIndex - 1, buffer); buffer.append(", "); buffer.append(sumExpr.toString()); buffer.append(")"); }
public void apply(StackOfDouble stack, Cases cases) { // This routine is called when an ExpressionCommand object is encountered during // the evaluation of an ExpressionProgram. The stack may contain results of // previous commands in the program. In this case, the command is a summation // and the stack contains the upper and lower limits summation limits. double upper = Math.round(stack.pop()) + 0.1; // Get summation limits. double lower = Math.round(stack.pop()); if (Double.isNaN(upper) && Double.isNaN(lower) || upper - lower > 1000000) stack.push(Double.NaN); double sum = 0; for (double x = lower; x <= upper; x++) { // Compute the sum. sumVar.setVal(x); sum += sumExpr.getVal(); } stack.push(sum); // Leave the sum on the stack. }