Clean up REPL formatting.

This commit is contained in:
Simon Forman 2019-08-11 17:00:38 -07:00
parent 4d33f32674
commit ab454375c0
2 changed files with 43 additions and 2 deletions

View File

@ -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.

View File

@ -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]), "]".