+/*
+ 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/>.
+
+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) :-
:- 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
:- multifile(func/3).
+
func(+, [A, B|C], [D|C]) :-
E =.. [+, B, A],
catch(D is E, _, D = E).
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)),
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([], []).
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.
phrase(format_joy(RStack), Codes),
maplist(put_code, Codes).
-
-
% Print Joy expressions as text.
format_joy(Tail) --> {var(Tail)}, !, [46, 46, 46].
format_term([A|As]) --> "[", format_joy([A|As]), "]".
format_term(F) --> { write_to_codes(Codes, F)}, Codes.
-