diff --git a/thun/gnu-prolog/Makefile b/thun/gnu-prolog/Makefile index e80ca25..f8c1c6e 100644 --- a/thun/gnu-prolog/Makefile +++ b/thun/gnu-prolog/Makefile @@ -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 @@ 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 fork.pl \ --consult-file thun.pl \ --consult-file DCG_basics.pl \ --query-goal do diff --git a/thun/gnu-prolog/defs.pl b/thun/gnu-prolog/defs.pl index 5575c41..af6466d 100644 --- a/thun/gnu-prolog/defs.pl +++ b/thun/gnu-prolog/defs.pl @@ -20,7 +20,6 @@ def(drop,[[rest],times]). 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 --git a/thun/gnu-prolog/defs.txt b/thun/gnu-prolog/defs.txt index 039ecd2..ffc698f 100644 --- a/thun/gnu-prolog/defs.txt +++ b/thun/gnu-prolog/defs.txt @@ -22,7 +22,6 @@ dupdd == [dup] dipd 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 --git a/thun/gnu-prolog/fork.pl b/thun/gnu-prolog/fork.pl new file mode 100644 index 0000000..a25767b --- /dev/null +++ b/thun/gnu-prolog/fork.pl @@ -0,0 +1,41 @@ +:- multifile(func/3). + +func(fork, [F, G|S], [X, Y|S]) :- + fork(F, S, X, ChildPID), + thun(G, S, [Y|_]), + wait(ChildPID, Status). % FIXME check status!!! + +fork(Expr, Stack, Result, ChildPID) :- + mkpipe(In, Out), + fork_prolog(ChildPID), + bar(ChildPID, In, Out, Expr, Stack, Result). + +bar(0, In, Out, Expr, Stack, Result) :- + close(In), + thun(Expr, Stack, [Result|_]), + w(Out, Result), + close(Out), + halt. + +bar(P, In, Out, Expr, Stack, Result) :- + integer(P), P =\= 0, + close(Out), + 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. +