Try to handle expressions in branch combinator.

If the expression isn't 'true' or 'false' atoms then we assume it's a comparison expression and try to check its truth value.

If this fails then it will try both branches, to allow for e.g. compilation.  THis almost works, but there's a choice point or something that gets hit before it tries the false path,

?- joy(` [32 >] [++] [--] ifte`, Si, So).
Si = [_2076|_2078],
So = [_2076+1|_2078] ;
wtf? +
Si = [_2076|_2078],
So = [[+], 1, _2076|_2078] ;
Si = [_2076|_2078],
So = [_2076-1|_2078] ;
wtf? -
Si = [_2076|_2078],
So = [[-], 1, _2076|_2078] ;
wtf? branch
Si = [_2076|_2078],
So = [[branch], [++], [--], _2076>32, _2076|_2078] ;
wtf? swap
Si = [_2076|_2078],
So = [[swap, branch], [--], [++], _2076>32, _2076|_2078] ;
wtf? first
Si = [_2076|_2078],
So = [[first, [++], [--], swap, branch], [_2076>32|_2078], _2076|_2078]


etc...
This commit is contained in:
Simon Forman 2019-07-20 16:45:28 -07:00
parent c676dce2f9
commit 714bf2cab6
1 changed files with 13 additions and 2 deletions

View File

@ -192,8 +192,15 @@ combo(dipd, [P, X, Y|S], S, Ei, Eo) :- append(P, [Y, X|Ei], Eo).
combo(dupdip, [P, X|S], [X|S], Ei, Eo) :- append(P, [X|Ei], Eo).
combo(branch, [T, _, true|S], S, Ei, Eo) :- append(T, Ei, Eo).
combo(branch, [_, F, false|S], S, Ei, Eo) :- append(F, Ei, Eo).
combo(branch, [T, _, true|S], S, Ei, Eo) :- !, append(T, Ei, Eo).
combo(branch, [_, F, false|S], S, Ei, Eo) :- !, append(F, Ei, Eo).
combo(branch, [T, F, Expr|S], S, Ei, Eo) :-
catch(
(Expr -> append(T, Ei, Eo) ; append(F, Ei, Eo)),
_,
try_both_branches(T, F, Ei, Eo) % in case of error.
).
combo(loop, [_, false|S], S, E, E ).
combo(loop, [B, true|S], S, Ei, Eo) :- append(B, [B, loop|Ei], Eo).
@ -213,6 +220,10 @@ combo(genrec, [R1, R0, Then, If|S],
append(R0, [Quoted|R1], Else).
try_both_branches(T, _, Ei, Eo) :- append(T, Ei, Eo).
try_both_branches(_, F, Ei, Eo) :- append(F, Ei, Eo).
/*
Compiler
*/