Fold in parser, main loop, and support.

This commit is contained in:
Simon Forman 2019-08-10 19:57:00 -07:00
parent fd5f54992a
commit 1d31ca99c3
2 changed files with 72 additions and 4 deletions

View File

@ -1,4 +1,5 @@
.*\.pyc$ .*\.pyc$
.*\.swp$
.hypothesis .hypothesis
.pytest_cache .pytest_cache
.vscode .vscode

View File

@ -1,6 +1,3 @@
:- dynamic(func/3).
:- discontiguous(func/3).
/* /*
Copyright 2018, 2019 Simon Forman Copyright 2018, 2019 Simon Forman
@ -20,6 +17,12 @@
along with Thun. If not see <http://www.gnu.org/licenses/>. along with Thun. If not see <http://www.gnu.org/licenses/>.
*/ */
:- dynamic(func/3).
:- discontiguous(func/3).
:- initialization(loop).
/* /*
Interpreter Interpreter
thun(Expression, InputStack, OutputStack) thun(Expression, InputStack, OutputStack)
@ -35,7 +38,7 @@ thun([Combo|E], Si, So) :- combo(Combo, Si, S, E, Eo), thun(Eo, S, So).
thun([Unknown|E], Si, So) :- thun([Unknown|E], Si, So) :-
damned_thing(Unknown), damned_thing(Unknown),
write("wtf? "), write(`wtf? `),
write(Unknown), nl, write(Unknown), nl,
So = [[Unknown|E]|Si]. So = [[Unknown|E]|Si].
@ -102,6 +105,70 @@ Definitions
def(x, [dup, i]). def(x, [dup, i]).
/*
Parser
*/
joy_parse([T|S]) --> blanks, joy_term(T), blanks, joy_parse(S).
joy_parse([]) --> [].
joy_term(N) --> num(N), !.
joy_term(S) --> [0'[], !, joy_parse(S), [0']].
joy_term(A) --> chars(Chars), !, {atom_codes(A, Chars)}.
/*
Main Loop
*/
loop :- line(Line), loop(Line, [], _Out).
loop([eof], S, S) :- !.
loop( Line, In, Out) :-
do_line(Line, In, S),
write(S), nl,
line(NextLine), !,
loop(NextLine, S, Out).
do_line(Line, In, Out) :- phrase(joy_parse(E), Line), thun(E, In, Out).
do_line(_Line, S, S) :- write('Err'), nl.
% Line is the next new-line delimited line from standard input stream as
% a list of character codes.
line(Line) :- get_code(X), line(X, Line).
line(10, []) :- !. % break on new-lines.
line(-1, [eof]) :- !. % break on EOF
line(X, [X|Line]) :- get_code(Y), !, line(Y, Line).
chars([Ch|Rest]) --> char(Ch), chars(Rest).
chars([Ch]) --> char(Ch).
char(Ch) --> [Ch], { Ch \== 0'[, Ch \== 0'], Ch >= 33, Ch =< 126 }.
blanks --> blank, !, blanks.
blanks --> [].
blank --> [32].
% TODO: negative numbers, floats, scientific notation.
num(N) --> digits(Codes), !, { num(N, Codes) }.
num(_, []) :- fail, !.
num(N, [C|Codes]) :- number_codes(N, [C|Codes]).
digits([H|T]) --> digit(H), !, digits(T).
digits([]) --> [].
digit(C) --> [C], { nonvar(C), C =< 57, C >= 48 }.