changeset 9de273f70952 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=9de273f70952 user: Simon Forman <sform****@hushm*****> date: Tue Aug 13 11:24:28 2019 -0700 description: Parse floating point numbers. changeset 4a55a817f3f0 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=4a55a817f3f0 user: Simon Forman <sform****@hushm*****> date: Tue Aug 13 11:29:49 2019 -0700 description: Parse ints; move line/{1,2} to main.pl. changeset 0dd62c4d4895 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=0dd62c4d4895 user: Simon Forman <sform****@hushm*****> date: Tue Aug 13 11:48:08 2019 -0700 description: Move DCG stuff to own file. changeset 535eb7f5821e in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=535eb7f5821e user: Simon Forman <sform****@hushm*****> date: Tue Aug 13 11:51:41 2019 -0700 description: Minor cleanup. diffstat: .hgignore | 2 +- thun/gnu-prolog/DCG_basics.pl | 59 +++++++++++++++++++++++++++++++++++++++++++ thun/gnu-prolog/Makefile | 9 ++++-- thun/gnu-prolog/main.pl | 10 +++++++ thun/gnu-prolog/parser.pl | 36 +------------------------ thun/gnu-prolog/thun.pl | 1 - 6 files changed, 78 insertions(+), 39 deletions(-) diffs (175 lines): diff -r 6f359630c98a -r 535eb7f5821e .hgignore --- a/.hgignore Mon Aug 12 22:29:09 2019 -0700 +++ b/.hgignore Tue Aug 13 11:51:41 2019 -0700 @@ -5,4 +5,4 @@ .vscode docs/.ipynb_checkpoints test/* -thun +gnu-prolog/thun diff -r 6f359630c98a -r 535eb7f5821e thun/gnu-prolog/DCG_basics.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thun/gnu-prolog/DCG_basics.pl Tue Aug 13 11:51:41 2019 -0700 @@ -0,0 +1,59 @@ +/* + Copyright 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/>. + + +DCG basics. For some of this I cribbed the source from SWI library code +and adapted it for GNU Prolog Compiler. + +N.B. is_glyph//1 excludes '[' and ']' characters. D'oh! FIXME + +*/ + + +:- set_prolog_flag(double_quotes, codes). + + +% TODO: scientific notation. + +signed_float_or_integer(Codes) --> signed_digits(J), ".", !, digits(I), + { append(J, [0'.|I], Codes) }. +signed_float_or_integer(Codes) --> signed_digits(Codes). + +signed_digits([0'-|Codes]) --> "-", !, digits(Codes). +signed_digits( Codes ) --> digits(Codes). + +% Groups of characters. + +chars(Chars) --> one_or_more(char, Chars). +blanks --> blank, !, blanks | []. +digits(Digits) --> one_or_more(digit, Digits). + +% Character types. + +char(Ch) --> [Ch], { nonvar(Ch), is_glyph(Ch) }. +blank --> [Ch], { nonvar(Ch), is_space(Ch) }. +digit(Ch) --> [Ch], { nonvar(Ch), between(0'0, 0'9, Ch) }. + +is_glyph(Ch) :- Ch =\= 0'[, Ch =\= 0'], between(0'!, 0'~, Ch). +is_space(Ch) :- Ch =:= 32 ; between(9, 13, Ch). + +one_or_more(E, List) --> one_or_more_(List, E). + +one_or_more_([Ch|Rest], P) --> call(P, Ch), one_or_more_(Rest, P). +one_or_more_([Ch], P) --> call(P, Ch). + diff -r 6f359630c98a -r 535eb7f5821e thun/gnu-prolog/Makefile --- a/thun/gnu-prolog/Makefile Mon Aug 12 22:29:09 2019 -0700 +++ b/thun/gnu-prolog/Makefile Tue Aug 13 11:51:41 2019 -0700 @@ -2,14 +2,17 @@ GPLC_OPTIONS=--no-top-level #GPLC_OPTIONS= -thun: thun.pl parser.pl defs.pl main.pl math.pl Makefile - gplc $(GPLC_OPTIONS) -o thun thun.pl parser.pl defs.pl main.pl math.pl +THUN_DEPS=parser.pl defs.pl main.pl math.pl DCG_basics.pl -defs.pl: meta-defs.pl parser.pl defs.txt thun.pl +thun: thun.pl $(THUN_DEPS) Makefile + gplc $(GPLC_OPTIONS) -o thun thun.pl $(THUN_DEPS) + +defs.pl: meta-defs.pl parser.pl defs.txt thun.pl DCG_basics.pl gprolog \ --consult-file meta-defs.pl \ --consult-file parser.pl \ --consult-file thun.pl \ + --consult-file DCG_basics.pl \ --query-goal do math.pl: meta-math.pl diff -r 6f359630c98a -r 535eb7f5821e thun/gnu-prolog/main.pl --- a/thun/gnu-prolog/main.pl Mon Aug 12 22:29:09 2019 -0700 +++ b/thun/gnu-prolog/main.pl Tue Aug 13 11:51:41 2019 -0700 @@ -41,3 +41,13 @@ prompt :- write(`joy? `). show_stack(S) :- nl, print_stack(S), write(` <-top`), nl, 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). + diff -r 6f359630c98a -r 535eb7f5821e thun/gnu-prolog/parser.pl --- a/thun/gnu-prolog/parser.pl Mon Aug 12 22:29:09 2019 -0700 +++ b/thun/gnu-prolog/parser.pl Tue Aug 13 11:51:41 2019 -0700 @@ -34,45 +34,13 @@ symbol(C) --> chars(Chars), !, { Chars \= "==", atom_codes(C, Chars) }. -num(N) --> signed_digits(Codes), !, end_num, { number_codes(N, Codes) }. -% TODO: floats, scientific notation. +num(N) --> number_digits(Codes), { number_codes(N, Codes) }. -signed_digits([45|Codes]) --> "-", !, digits(Codes). -signed_digits( Codes ) --> digits(Codes). +number_digits(Codes) --> signed_float_or_integer(Codes), !, end_num. end_num, [Ch] --> [Ch], { [Ch] = "[" ; is_space(Ch) }. end_num([], []). -% Groups of characters. - -chars(Chars) --> one_or_more(char, Chars). -blanks --> blank, !, blanks | []. -digits(Digits) --> one_or_more(digit, Digits). - -% Character types. - -char(Ch) --> [Ch], { nonvar(Ch), is_glyph(Ch) }. -blank --> [Ch], { nonvar(Ch), is_space(Ch) }. -digit(Ch) --> [Ch], { nonvar(Ch), between(0'0, 0'9, Ch) }. - -is_glyph(Ch) :- Ch =\= 0'[, Ch =\= 0'], between(0'!, 0'~, Ch). -is_space(Ch) :- Ch =:= 32 ; between(9, 13, Ch). - -one_or_more(E, List) --> one_or_more_(List, E). - -one_or_more_([Ch|Rest], P) --> call(P, Ch), one_or_more_(Rest, P). -one_or_more_([Ch], P) --> call(P, Ch). - - -% 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). - /* diff -r 6f359630c98a -r 535eb7f5821e thun/gnu-prolog/thun.pl --- a/thun/gnu-prolog/thun.pl Mon Aug 12 22:29:09 2019 -0700 +++ b/thun/gnu-prolog/thun.pl Tue Aug 13 11:51:41 2019 -0700 @@ -124,7 +124,6 @@ combo(dipd, [P, X, Y|S], S, Ei, Eo) :- append(P, [Y, X|Ei], Eo). combo(dupdip, [P, X|S], [X|S], Ei, Eo) :- append(P, [X|Ei], Eo). -combo(dupdip, [P, X|S], [X|S], Ei, Eo) :- append(P, [X|Ei], Eo). combo(branch, [T, _, true|S], S, Ei, Eo) :- append(T, Ei, Eo). combo(branch, [_, F, false|S], S, Ei, Eo) :- append(F, Ei, Eo).