Skip to content

Instantly share code, notes, and snippets.

@DavideCanton
Created June 7, 2013 15:06
Show Gist options
  • Select an option

  • Save DavideCanton/5729932 to your computer and use it in GitHub Desktop.

Select an option

Save DavideCanton/5729932 to your computer and use it in GitHub Desktop.
expr(n(T1, O, T2)) --> term(T1), addop(O), expr(T2).
expr(T1) --> term(T1).
term(n(F1, O, F2)) --> fact(F1), mulop(O), term(F2).
term(F1) --> fact(F1).
fact(l(I)) --> [I], {integer(I)}.
fact(E) --> ['('], expr(E), [')'].
addop(add) --> [+].
addop(sub) --> [-].
mulop(mul) --> [*].
mulop(div) --> [/].
eval(n(T1, add, T2), V) :- eval(T1, V1), eval(T2, V2), V is V1 + V2.
eval(n(T1, sub, T2), V) :- eval(T1, V1), eval(T2, V2), V is V1 - V2.
eval(n(F1, mul, F2), V) :- eval(F1, V1), eval(F2, V2), V is V1 * V2.
eval(n(F1, div, F2), V) :- eval(F1, V1), eval(F2, V2), V is V1 / V2.
eval(l(V), V).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment