changeset 37191890f191 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=37191890f191 user: Simon Forman <sform****@hushm*****> date: Sun Aug 18 10:53:25 2019 -0700 description: truly fork, sort of changeset e0100f4a11e4 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=e0100f4a11e4 user: Simon Forman <sform****@hushm*****> date: Sun Aug 18 11:09:36 2019 -0700 description: read child output after local thun/3 diffstat: thun/gnu-prolog/Makefile | 3 ++- thun/gnu-prolog/defs.pl | 1 - thun/gnu-prolog/defs.txt | 1 - thun/gnu-prolog/fork.pl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) diffs (91 lines): diff -r 438748ae49e1 -r e0100f4a11e4 thun/gnu-prolog/Makefile --- a/thun/gnu-prolog/Makefile Tue Aug 13 11:58:31 2019 -0700 +++ b/thun/gnu-prolog/Makefile Sun Aug 18 11:09:36 2019 -0700 @@ -2,7 +2,7 @@ GPLC_OPTIONS=--no-top-level #GPLC_OPTIONS= -THUN_DEPS=parser.pl defs.pl main.pl math.pl DCG_basics.pl +THUN_DEPS=parser.pl defs.pl main.pl math.pl DCG_basics.pl fork.pl thun: thun.pl $(THUN_DEPS) Makefile gplc $(GPLC_OPTIONS) -o thun thun.pl $(THUN_DEPS) @@ -11,6 +11,7 @@ gprolog \ --consult-file meta-defs.pl \ --consult-file parser.pl \ + --consult-file fork.pl \ --consult-file thun.pl \ --consult-file DCG_basics.pl \ --query-goal do diff -r 438748ae49e1 -r e0100f4a11e4 thun/gnu-prolog/defs.pl --- a/thun/gnu-prolog/defs.pl Tue Aug 13 11:58:31 2019 -0700 +++ b/thun/gnu-prolog/defs.pl Sun Aug 18 11:09:36 2019 -0700 @@ -20,7 +20,6 @@ def(dupdd,[[dup],dipd]). def(dupdipd,[dup,dipd]). def(enstacken,[stack,[clear],dip]). -def(fork,[[i],app2]). def(fourth,[rest,third]). def(gcd,[true,[tuck,mod,dup,0,>],loop,pop]). def(grabN,[[],swap,[cons],times]). diff -r 438748ae49e1 -r e0100f4a11e4 thun/gnu-prolog/defs.txt --- a/thun/gnu-prolog/defs.txt Tue Aug 13 11:58:31 2019 -0700 +++ b/thun/gnu-prolog/defs.txt Sun Aug 18 11:09:36 2019 -0700 @@ -22,7 +22,6 @@ dupdipd == dup dipd enstacken == stack [clear] dip flatten == [] swap [concat] step -fork == [i] app2 fourth == rest third gcd == true [tuck mod dup 0 >] loop pop grabN == [] swap [cons] times diff -r 438748ae49e1 -r e0100f4a11e4 thun/gnu-prolog/fork.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thun/gnu-prolog/fork.pl Sun Aug 18 11:09:36 2019 -0700 @@ -0,0 +1,45 @@ +:- multifile(func/3). + +func(fork, [F, G|S], [X, Y|S]) :- + fork(F, S, R, ChildPID), + thun(G, S, [Y|_]), + read_pipe(R, X), + wait(ChildPID, Status). % FIXME check status!!! + +fork(Expr, Stack, In, ChildPID) :- + mkpipe(In, Out), + fork_prolog(ChildPID), + bar(ChildPID, In, Out, Expr, Stack). + +bar(0, In, Out, Expr, Stack) :- + close(In), + thun(Expr, Stack, [Result|_]), + w(Out, Result), + close(Out), + halt. + +bar(PID, _, Out, _, _) :- + integer(PID), + PID =\= 0, + close(Out). + +read_pipe(In, Result) :- + select([In], R, [], _, 1500), + (R=[In] -> + read(In, Result) + ; + Result=timeout + ), + close(In). + +mkpipe(In, Out) :- + create_pipe(In, Out), + set_stream_buffering(Out, none), + set_stream_buffering(In, none). + +w(Out, Term) :- % To get a term written out and recognized, + write(Out, Term), % you write it to the stream + put_code(Out, 0'.), % add a period at the end + nl(Out), % and a newline even if you set buffering + flush_output(Out). % to none, then flush for good measure. +