這可以通過使用erl_scan,erl_parse,erl_eval來實現,如link中所述。 問題是變量綁定。要知道變量綁定,當它在表達式中使用(更困難時,它是一個有趣的內部)
1> Expr="Awesome = 41 + 41.".
"Awesome = 41 + 41."
2> {ok, Tokens, _} = erl_scan:string(Expr).
{ok,[{var,1,'Awesome'},
{'=',1},
{integer,1,41},
{'+',1},
{integer,1,41},
{dot,1}],
1}
3> {ok, [Form]} = erl_parse:parse_exprs(Tokens).
{ok,[{match,1,
{var,1,'Awesome'},
{op,1,'+',{integer,1,41},{integer,1,41}}}]}
4> {value, Value, Binding} = erl_eval:expr(Form, []).
{value,82,[{'Awesome',82}]}
當用戶可以發送迴響應82
瀏覽器中輸入
Awesome = 41 + 41.
您還需要維護表達式的狀態。這就是應該保留的綁定可以使用如下:
5> FunStr ="io:format(\"Awesome = ~p\", [Awesome]).".
"io:format(\"Awesome = ~p\", [Awesome])."
6> {ok, Tokens1, _} = erl_scan:string(FunStr).
{ok,[{atom,1,io},
{':',1},
{atom,1,format},
{'(',1},
{string,1,"Awesome = ~p"},
{',',1},
{'[',1},
{var,1,'Awesome'},
{']',1},
{')',1},
{dot,1}],
1}
7> {ok, [Form1]} = erl_parse:parse_exprs(Tokens1).
{ok,[{call,1,
{remote,1,{atom,1,io},{atom,1,format}},
[{string,1,"Awesome = ~p"},
{cons,1,{var,1,'Awesome'},{nil,1}}]}]}
8> {value, Fun1, _} = erl_eval:expr(Form, Binding).
{value,82,[{'Awesome',82}]}
9> {value, V, _} = erl_eval:expr(Form1, Binding).
Awesome = 82{value,ok,[{'Awesome',82}]}
綁定列表應該增長到會話結束。