Clean up REPL formatting.
This commit is contained in:
parent
4d33f32674
commit
ab454375c0
|
|
@ -22,12 +22,13 @@ Main Loop
|
|||
|
||||
:- initialization(loop).
|
||||
|
||||
loop :- line(Line), loop(Line, [], _Out).
|
||||
loop :- prompt, line(Line), loop(Line, [], _Out).
|
||||
|
||||
loop([eof], S, S) :- !.
|
||||
loop( Line, In, Out) :-
|
||||
do_line(Line, In, S),
|
||||
write(S), nl,
|
||||
show_stack(S),
|
||||
prompt,
|
||||
line(NextLine), !,
|
||||
loop(NextLine, S, Out).
|
||||
|
||||
|
|
@ -35,3 +36,6 @@ loop( Line, In, Out) :-
|
|||
do_line(Line, In, Out) :- phrase(joy_parse(E), Line), thun(E, In, Out).
|
||||
do_line(_Line, S, S) :- write('Err'), nl.
|
||||
|
||||
prompt :- write(`joy? `).
|
||||
show_stack(S) :- nl, print_stack(S), write(` <-top`), nl, nl.
|
||||
|
||||
|
|
|
|||
|
|
@ -68,3 +68,40 @@ digits([]) --> [].
|
|||
|
||||
digit(C) --> [C], { nonvar(C), C =< 57, C >= 48 }.
|
||||
|
||||
|
||||
/*
|
||||
|
||||
Print state.
|
||||
|
||||
*/
|
||||
|
||||
format_state(Stack, Expression, Codes) :-
|
||||
reverse(Stack, RStack),
|
||||
phrase(format_joy(RStack), RStackCodes),
|
||||
phrase(format_joy(Expression), ExpressionCodes),
|
||||
append(RStackCodes, [32, 46, 32|ExpressionCodes], Codes).
|
||||
|
||||
|
||||
frump(Stack, Expression) :-
|
||||
format_state(Stack, Expression, Codes),
|
||||
maplist(put_code, Codes), nl.
|
||||
|
||||
print_stack(Stack) :-
|
||||
reverse(Stack, RStack),
|
||||
phrase(format_joy(RStack), Codes),
|
||||
maplist(put_code, Codes).
|
||||
|
||||
|
||||
|
||||
% Print Joy expressions as text.
|
||||
|
||||
format_joy(Tail) --> {var(Tail)}, !, [46, 46, 46].
|
||||
format_joy([T]) --> format_term(T), !.
|
||||
format_joy([T|S]) --> format_term(T), " ", format_joy(S).
|
||||
format_joy([]) --> [].
|
||||
|
||||
format_term(N) --> {number(N), number_codes(N, Codes)}, Codes.
|
||||
format_term(A) --> { atom(A), atom_codes(A, Codes)}, Codes.
|
||||
format_term([A|As]) --> "[", format_joy([A|As]), "]".
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue