From 558f45bf47b0a7dc18bae28e93852ccdd046b554 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Tue, 20 Aug 2019 22:00:27 -0700 Subject: [PATCH] minor cleanup --- thun/gnu-prolog/fork.pl | 22 ++++++++++++++++++++++ thun/gnu-prolog/main.pl | 11 +++++------ thun/gnu-prolog/math.pl | 1 + thun/gnu-prolog/meta-math.pl | 3 ++- thun/gnu-prolog/parser.pl | 6 ++---- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/thun/gnu-prolog/fork.pl b/thun/gnu-prolog/fork.pl index 39beaaf..e2aa8be 100644 --- a/thun/gnu-prolog/fork.pl +++ b/thun/gnu-prolog/fork.pl @@ -1,9 +1,31 @@ +/* + 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 . + +A fork function that actually forks. Experimental. + +*/ :- multifile(func/3). func(fork, [F, G|S], [X, Y|S]) :- fork(F, S, R, ChildPID), % Send F off to the child, thun(G, S, [Y|_]), % Run G locally, read_pipe(R, X), % Collect the result from F, + % FIXME deal with X=timeout... wait(ChildPID, Status). % FIXME check status!!! fork(Expr, Stack, In, ChildPID) :- diff --git a/thun/gnu-prolog/main.pl b/thun/gnu-prolog/main.pl index 306ed76..ee7fc89 100644 --- a/thun/gnu-prolog/main.pl +++ b/thun/gnu-prolog/main.pl @@ -25,27 +25,26 @@ Main Loop :- initialization(loop). -loop :- prompt, line(Line), loop(Line, [], _Out). +loop :- prompt(Line), loop(Line, [], _Out). loop([eof], S, S) :- !. loop( Line, In, Out) :- do_line(Line, In, S), show_stack(S), - prompt, - line(NextLine), !, + prompt(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. -prompt :- write(`joy? `). +prompt(Line) :- write(`joy? `), get_line(Line). show_stack(S) :- nl, print_stack(S), write(` <-top`), nl, nl. -% Line is the next new-line delimited line from standard input stream as +% Line is the next newget_line-delimited line from standard input stream as % a list of character codes. -line(Line) :- get_code(X), line(X, Line). +get_line(Line) :- get_code(X), line(X, Line). line(10, []) :- !. % break on new-lines. line(-1, [eof]) :- !. % break on EOF diff --git a/thun/gnu-prolog/math.pl b/thun/gnu-prolog/math.pl index 6a41cd2..8e4727f 100644 --- a/thun/gnu-prolog/math.pl +++ b/thun/gnu-prolog/math.pl @@ -1,4 +1,5 @@ :- multifile(func/3). + func(+, [A, B|C], [D|C]) :- E =.. [+, B, A], catch(D is E, _, D = E). diff --git a/thun/gnu-prolog/meta-math.pl b/thun/gnu-prolog/meta-math.pl index c706806..f48b7c4 100644 --- a/thun/gnu-prolog/meta-math.pl +++ b/thun/gnu-prolog/meta-math.pl @@ -78,7 +78,8 @@ writeln(Stream, Thing) :- do :- open(`math.pl`, write, Stream), - write(Stream, `:- multifile(func/3).`), nl(Stream), + write(Stream, `:- multifile(func/3).`), + nl(Stream), nl(Stream), print_o(Stream, math_operator(Op)), print_o(Stream, comparison_operator(Op)), print_o(Stream, comparison_operator(Op, Po)), diff --git a/thun/gnu-prolog/parser.pl b/thun/gnu-prolog/parser.pl index c60d328..f24577b 100644 --- a/thun/gnu-prolog/parser.pl +++ b/thun/gnu-prolog/parser.pl @@ -38,6 +38,8 @@ num(N) --> number_digits(Codes), { number_codes(N, Codes) }. number_digits(Codes) --> signed_float_or_integer(Codes), !, end_num. +% At the end of a number look ahead one character for a space +% or '[' character, or the end of the code list. end_num, [Ch] --> [Ch], { [Ch] = "[" ; is_space(Ch) }. end_num([], []). @@ -54,7 +56,6 @@ format_state(Stack, Expression, Codes) :- 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. @@ -64,8 +65,6 @@ print_stack(Stack) :- phrase(format_joy(RStack), Codes), maplist(put_code, Codes). - - % Print Joy expressions as text. format_joy(Tail) --> {var(Tail)}, !, [46, 46, 46]. @@ -79,4 +78,3 @@ format_term(V) --> { var(V), write_to_codes(Codes, V)}, Codes. format_term([A|As]) --> "[", format_joy([A|As]), "]". format_term(F) --> { write_to_codes(Codes, F)}, Codes. - -- 2.11.0