I am trying to create a function that takes an expression and evaluates it. Expressions can contain the following operations:
- Integers - described by a tuple
int(N), whereNis an integer. - Addition - described by a tuple
add(X Y), where bothXandYare arithmetic expressions. - Multiplication - described by a tuple
mul(X Y), where bothXandYare arithmetic expressions. - Variables - described by a tuple
var(A), whereAis an atom giving the variable name - An environment - described by a record
env(a:5 b:5), whereaandbare variables with values of 5.
For example: {Eval add(var(a) mul(int(3) var(b))) env(a:5 b:5)}. Which should evaluate to 20.
So far, I have implemented integers, addition, and multiplication. But I'm not really sure where to start for the variables and the environment.
My current code:
fun {Eval X}
case X of int(N) then N
[] add(X Y) then {Eval X} + {Eval Y}
[] mul(X Y) then {Eval X} * {Eval Y}
end
end