Move parser to own file.
This commit is contained in:
parent
3af9e7e174
commit
32e77f6d73
|
|
@ -1,7 +1,7 @@
|
||||||
GPLC_OPTIONS="--min-size"
|
GPLC_OPTIONS="--min-size"
|
||||||
|
|
||||||
thun: thun.pl
|
thun: thun.pl parser.pl
|
||||||
gplc $(GPLC_OPTIONS) -o thun thun.pl
|
gplc $(GPLC_OPTIONS) -o thun thun.pl parser.pl
|
||||||
|
|
||||||
defs.pl: meta-defs.pl defs.txt
|
defs.pl: meta-defs.pl defs.txt
|
||||||
gprolog --consult-file meta-defs.pl defs.txt
|
gprolog --consult-file meta-defs.pl defs.txt
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,72 @@
|
||||||
|
/*
|
||||||
|
Copyright 2018, 2019 Simon Forman
|
||||||
|
|
||||||
|
This file is part of Thun
|
||||||
|
|
||||||
|
Thun is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Thun is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with Thun. If not see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
Parser
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
:- set_prolog_flag(double_quotes, codes).
|
||||||
|
|
||||||
|
joy_parse([T|J]) --> blanks, joy_term(T), blanks, joy_parse(J).
|
||||||
|
joy_parse([]) --> [].
|
||||||
|
|
||||||
|
joy_term(N) --> num(N), !.
|
||||||
|
joy_term(J) --> "[", !, joy_parse(J), "]".
|
||||||
|
joy_term(C) --> symbol(C).
|
||||||
|
|
||||||
|
symbol(C) --> chars(Chars), !, {Chars \= [61, 61], atom_codes(C, Chars)}.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
% 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 }.
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -205,17 +205,6 @@ 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
|
Main Loop
|
||||||
|
|
@ -236,39 +225,3 @@ 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 }.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue