Erlang Central

String Eval

Revision as of 21:49, 18 August 2006 by Cyberlync (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


You want to evaluate Erlang code stored in a string.


Use erl_scan:string to convert the string into a list of tokens, then use erl_parse:parse_exprs to generate the Erlang intermediate representation, then finally use erl_eval:exprs to generate the final output:

1> {ok,A,B} = erl_scan:string("A = 1 + 2.").
2> A.
3> {C,D} = erl_parse:parse_exprs(A).
4> D.
5> erl_eval:exprs(D,[]).

Now, this is an admittedly baroque way to determine the value of 1 + 2, but it does give you interesting access to the inner workings of the Erlang interpreter.