[erlang-questions] Programmatic interface to the shell

Fred Hebert mononcqc@REDACTED
Mon Aug 10 19:14:39 CEST 2015


On 08/10, Joe Armstrong wrote:
>Do you have any idea how to dig out the error message so it's exactly 
>the same?
>
>I'm asking because I'm writing a tutorial in a "markdown like" language
>and I want to automate production of examples :-)
>

Probably would need to gather the Erlang stacktrace and raise it there 
too with erlang:get_stacktrace() and erlang:raise(T,R,Stacktrace) so 
that the shell evaluators  (with lib:format_stacktrace) can handle it 
right:

    Eval = fun EvalLoop(Bindings) ->
        receive
            {cmd, Caller, Ref, String} ->
                try
                    {ok, Tokens, _} = erl_scan:string(String),
                    %% many forms can be comma-separated
                    {ok, Forms} = erl_parse:parse_exprs(Tokens),
                    %% eval individually
                    {value, Val, NewBindings} = erl_eval:exprs(Forms, Bindings),
                    Caller ! {ok, Ref, Val},
                    EvalLoop(NewBindings)
                catch
                    T:R ->
                        Caller ! {raise, Ref, T, R, erlang:get_stacktrace},
                        EvalLoop(Bindings)
                end
        end end.

    Send = fun(Pid, String) ->
        Ref = erlang:monitor(process, Pid),
        Pid ! {cmd, self(), Ref, String},
        receive
            {ok, Ref, Value} -> Value;
            {raise, Ref, T, R, S} -> erlang:raise(T,R,S)
        end end.

This should do it.



More information about the erlang-questions mailing list