8.3 編寫健壯的應用程式-分離計算部分
阿新 • • 發佈:2018-12-23
-module(c_shell). -export([start/0, eval/2]). start() -> process_flag(trap_exit, true), go(). go() -> eval(io:parse_exprs('-> ')), go(). eval({form, Exprs}) -> Id = spawn_link(c_shell, eval, [self(), Exprs]), receive {value, Res, _} -> io:format("Result: ~w~n", [Res]), receive {'EXIT', Id, _ } -> true end; {'EXIT', Id, Reason} -> io:format("Error: ~w!~n", [Reason]) end; eval(_) -> io:format("Syntax Error!~n", []). eval(Id, Exprs) -> Id ! eval:exprs(Exprs, []).