From ab454375c090048b30e3863dd26f0d50f2e2f4f8 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sun, 11 Aug 2019 17:00:38 -0700 Subject: [PATCH] Clean up REPL formatting. --- thun/gnu-prolog/main.pl | 8 ++++++-- thun/gnu-prolog/parser.pl | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/thun/gnu-prolog/main.pl b/thun/gnu-prolog/main.pl index 421dd2a..595bc57 100644 --- a/thun/gnu-prolog/main.pl +++ b/thun/gnu-prolog/main.pl @@ -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. + diff --git a/thun/gnu-prolog/parser.pl b/thun/gnu-prolog/parser.pl index cd8a35c..4258f89 100644 --- a/thun/gnu-prolog/parser.pl +++ b/thun/gnu-prolog/parser.pl @@ -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]), "]". + +