(* binary operations *) datatype BOp = Plus | Minus | Multiply | Divide (* unary operations: Inverse is the function that divides 1 by its argument, Inverse 2 = 0.5 Ln is the natural logarithm on your calculator *) datatype UOp = Inverse | Square | SquareRoot | Sin | Cos | Tan | Ln (* expressions: Pi is the constant 3.1415926... *) datatype Expression = Pi | Number of real | Binary of Expression * BOp * Expression | Unary of UOp * ExpressionUsing the grammar, the abstract parse tree for the expression
3+4
is represented in ML as Binary(Number(3.0), Plus,
Number(4.0))
:
interpret
which takes an Expression
and evaluates it to a real number.
For example:
- interpret(Binary(Number(3.0), Plus, Number(4.0))); val it = 7.0 : realTo access the mathematical functions in ML, use
Math.sin,
Math.cos
, etc.
datatype
declaration
of Expression
by adding the following two lines:
| Define of string * Expression * Expression | Variable of stringThe intention is that a
Define
gives a name to a
subexpression that can be reused later. For example,
Define ("x", Number 3.0, Binary(Variable "x", Plus, Variable "x"))should evaluate to
6
. Extend your interpreter to handle
the additional two cases.
sabry@cs.uoregon.edu