Sums of Products

The SOP processor executes SOP expressions.

class Processor {
  public Double execute(Expression exp) {
     return exp.execute();
  }
}

The SOP language currently has three types of expressions: numbers, sums, and products.

expression ::= number | sum | product

A sum is the token "sum" followed by a list of 2 or more expressions:

sum(a, b, …)

A product is the token "mul" followed by a list of 2 or more expressions:

mul(a, b, …)

A number has the format num(x) where x is any Java Double:

num(-3.14)

Here are a few examples of SOP expressions and their values:

num(5.1) // = 5.1

sum(num(3.0), num(4.0), num(5.0)) // = 12

mul(num(2.0), sum(num(3.0), num(4.0), num(5.0))) // = 24

Design, implement, and test the SOP expression hierarchy.

Notes

·       Here’s a start on a test harness. Add more tests: TestSOP.java.

·       My constructors for Sum and Product use Java’s varargs feature:

Sum(Expression operand ...) { ... }

For example

new Sum(a, b, c)

gathers the inputs into an array [a, b, c].