minor cleanup
This commit is contained in:
parent
6e646c012f
commit
558f45bf47
|
|
@ -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 <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) :-
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
:- multifile(func/3).
|
||||
|
||||
func(+, [A, B|C], [D|C]) :-
|
||||
E =.. [+, B, A],
|
||||
catch(D is E, _, D = E).
|
||||
|
|
|
|||
|
|
@ -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)),
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue