RosettaCodeData/Task/Runtime-evaluation-In-an-environment/Erlang/runtime-evaluation-in-an-environment.erl
Ingy döt Net 776bba907c Sync
2013-10-27 22:24:23 +00:00

21 lines
682 B
Erlang

-module( runtime_evaluation ).
-export( [evaluate_form/2, form_from_string/1, task/0] ).
evaluate_form( Form, {Variable_name, Value} ) ->
Bindings = erl_eval:add_binding( Variable_name, Value, erl_eval:new_bindings() ),
{value, Evaluation, _} = erl_eval:expr( Form, Bindings ),
Evaluation.
form_from_string( String ) ->
{ok, Tokens, _} = erl_scan:string( String ),
{ok, [Form]} = erl_parse:parse_exprs( Tokens ),
Form.
task() ->
Form = form_from_string( "X." ),
Variable1 = evaluate_form( Form, {'X', 1} ),
io:fwrite( "~p~n", [Variable1] ),
Variable2 = evaluate_form( Form, {'X', 2} ),
io:fwrite( "~p~n", [Variable2] ),
io:fwrite( "~p~n", [Variable2 - Variable1] ).