rework parser DCGs

This commit is contained in:
Simon Forman 2019-08-12 20:12:35 -07:00
parent b25ef2d5f7
commit 120610cd6b
1 changed files with 22 additions and 26 deletions

View File

@ -33,6 +33,28 @@ joy_term(C) --> symbol(C).
symbol(C) --> chars(Chars), !, {Chars \= "==", atom_codes(C, Chars)}.
% TODO: negative numbers, floats, scientific notation.
num(N) --> digits(Codes), !, { number_codes(N, Codes) }.
% Groups of characters.
chars([Ch|Rest]) --> char(Ch), chars(Rest).
chars([Ch]) --> char(Ch).
blanks --> blank, !, blanks.
blanks --> [].
digits([Ch|Rest]) --> digit(Ch), digits(Rest).
digits([Ch]) --> digit(Ch).
% Character types.
char(Ch) --> [Ch], { nonvar(Ch), Ch \== 0'[, Ch \== 0'], between(33, 126, Ch) }.
blank --> [Ch], { nonvar(Ch), Ch =:= 32 ; between(9, 13, Ch) }.
digit(Ch) --> [Ch], { nonvar(Ch), between(48, 57, Ch) }.
% Line is the next new-line delimited line from standard input stream as
% a list of character codes.
@ -43,32 +65,6 @@ 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'], between(33, 126, Ch) }.
blanks --> blank, !, blanks.
blanks --> [].
blank --> [Ch], { Ch =:= 32 ; between(9, 13, Ch) }.
% 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), between(48, 57, C) }.
/*
Print state.