함수형 언어/Erlang

erlang string to term or expr

manywaypark 2015. 4. 7. 16:41

string to term:

ErlTerms = "{a,b,c,d}.".

{ok,Tokens,_} = erl_scan:string(ErlTerms).

{ok,Term} = erl_parse:parse_term(Tokens).

Term -> {a,b,c,d}.


string to expression (and eval):

StrTerm = "fun() -> result_atom end.".

{ok,Tokens,_} = erl_scan:string(StrTerm).

{ok,[Expr]} = erl_parse:parse_exprs(Tokens).

erl_eval:expr(Expr,[]).

resulted in: {value,#Fun,[]}


refs:

https://github.com/skruger/Surrogate/issues/3

http://stackoverflow.com/questions/1974236/string-to-abstract-syntax-tree

http://stackoverflow.com/questions/2008777/convert-a-string-into-a-fun


happy hackin'