...then the branch combinator works as intended. (Although the constraint-based stuff was also cool, it would have captured information from the comparison.)
?- joy(`[32 >] [++] [--] ifte`, Si, So).
Si = [_6598|_6600],
So = [_6598+1|_6600] ;
Si = [_6598|_6600],
So = [_6598-1|_6600] ;
false.
?- sjc(hmm, `[32 >] [++] [--] ifte`).
func(hmm, [A|B], [A+1|B]).
true ;
func(hmm, [A|B], [A-1|B]).
true ;
false.
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...
I like them but then you are constrained (pun intended) to only using integers. I'll probably bring them back at some point, either as an alternate implementation or their own commands.
That eliminates all the recursive calls to thun/3 (outside of thun itself, which is tail recursive.) That means that this Joy interpreter is now fully CPS.
All state is contained in the stack and expression, nothing is hidden in the Prolog "call stack".
python -m joy.vui
With PyGame installed that starts the "VUI" on my system. Neat.
The initial joy home (~/.joypy) is not quite compatible with the one set up by the GUI code. One simple way to fix that would be to e,g, make this use ~/.thun instead.
It turns out that the binary_number relation is used in such a way that it needs to be able to backtrack to preprend leading zeros to the list of bits it constructs to automatically build bitfields of a given width (with the collect//2 DCG.)