OSDN Git Service

Messing around with GNU Prolog.
authorsforman <sforman@hushmail.com>
Tue, 25 Jul 2023 15:51:27 +0000 (08:51 -0700)
committersforman <sforman@hushmail.com>
Tue, 25 Jul 2023 15:51:27 +0000 (08:51 -0700)
commitf2688e311fd6808aa4f97a7f74d34bdbae807b02
tree6547776a8dfc4bdf7b1935ab83f9fb0b12ab8dd7
parenta34a2b1aab981f02d139cf3a1c0aea3fbeded327
Messing around with GNU Prolog.

I have it broken up into three stages: a parser that reads a string from
stdin and emits (Prolog) AST to stdout; an interpreter of sorts that
reads AST from stdin, evaluates it, and then emits the AST of the stack
on stdout; and a printer that reads AST on stdin and prints Joy-ish code
to stdout.

I say Joy-ish because currently math is not evaluated and results of
math appear as expressions, not values.

This is because GNU Prolog doesn't have unbounded integers (it's numbers
are machine integers) so literals that are larger than the machine word
are converted into atoms!  To keep things simple, I made all ints into
atoms, but then you can't evaluate them: '1'+'2' is not '3' (it might be
'12' though.)

So I print them out at expressions:

    $ echo "1 2 3 4 [+ sub /] i" | ./joy_to_ast | ./thun | ./printer

    (1 div (2-(4+3)))

You could almost feed that to, say, Python to evaluate, eh? Or dc with
proper formatting?  (man dc; "Desk Calculator".)

Anyway, it's a start.  The Prolog interpreter is more for things like
type checking and inference, optimizing, compiling, etc.  Symbolic stuff
that's a PITA to express in other languages.  (The old type inference
code in Python was pages long, in Prolog it's just the thun/3 & thun/4
predicates themselves.  At least so far.  There are things we will want
to do eventually that might be a PITA to express in Prolog, eh?
implementations/GNUProlog/Makefile [new file with mode: 0644]
implementations/GNUProlog/joy_to_ast.prolog
implementations/GNUProlog/printer.prolog [new file with mode: 0644]
implementations/GNUProlog/thun.prolog [new file with mode: 0644]