So we adapt the evaluator to set and maintain this counter, as so:
-module(eval_s).
-export([eval/1]).
-export([answer/0, error/0]).
% evaluator with state
eval({{con, A}, X}) ->
{A, X};
eval({{dv, T, U}, X}) ->
{A, Y} = eval({T, X}),
{B, Z} = eval({U, Y}),
{A div B, Z + 1}.
answer() ->
{dv, {dv, {con, 1972}, {con, 2}}, {con, 23}}.
error() ->
{dv, {con, 1}, {con, 0}}.
The counter is passed through each evaluation and on the line where we do the division we add one to it.
Then when we evaluate Answer we also pass a starting value zero for the counter: the result contains the answer 42 and the final value of the counter, 2.
1> l(eval_s).
{module,eval_s}
2> eval_s:eval({eval_s:answer(), 0}).
{42,2}
3> eval_s:eval({eval_s:error(), 0}).
** exception error: an error occurred when evaluating an arithmetic expression
in function eval_s:eval/1 (c:/Users/polly/Erlang/eval_s.erl, line 12)
Evaluating Error still gives you an exception of course.
No comments:
Post a Comment