From 9e180e81732abaf6c2726d3ccd446f59a610a87d Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sun, 11 Aug 2019 14:51:13 -0700 Subject: [PATCH] Extract mainloop to own file. --- thun/gnu-prolog/Makefile | 4 ++-- thun/gnu-prolog/main.pl | 37 +++++++++++++++++++++++++++++++++++++ thun/gnu-prolog/thun.pl | 25 ------------------------- 3 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 thun/gnu-prolog/main.pl diff --git a/thun/gnu-prolog/Makefile b/thun/gnu-prolog/Makefile index 527d872..f2d6ba1 100644 --- a/thun/gnu-prolog/Makefile +++ b/thun/gnu-prolog/Makefile @@ -1,7 +1,7 @@ GPLC_OPTIONS="--min-size" -thun: thun.pl parser.pl defs.pl - gplc $(GPLC_OPTIONS) -o thun thun.pl parser.pl defs.pl +thun: thun.pl parser.pl defs.pl main.pl + gplc $(GPLC_OPTIONS) -o thun thun.pl parser.pl defs.pl main.pl defs.pl: meta-defs.pl parser.pl defs.txt gprolog --consult-file meta-defs.pl --consult-file parser.pl --query-goal do diff --git a/thun/gnu-prolog/main.pl b/thun/gnu-prolog/main.pl new file mode 100644 index 0000000..421dd2a --- /dev/null +++ b/thun/gnu-prolog/main.pl @@ -0,0 +1,37 @@ +/* + Copyright 2019 Simon Forman + + This file is part of Thun + + Thun is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Thun is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Thun. If not see . + +Main Loop + +*/ + +:- initialization(loop). + +loop :- line(Line), loop(Line, [], _Out). + +loop([eof], S, S) :- !. +loop( Line, In, Out) :- + do_line(Line, In, S), + write(S), nl, + line(NextLine), !, + loop(NextLine, S, Out). + + +do_line(Line, In, Out) :- phrase(joy_parse(E), Line), thun(E, In, Out). +do_line(_Line, S, S) :- write('Err'), nl. + diff --git a/thun/gnu-prolog/thun.pl b/thun/gnu-prolog/thun.pl index 1b3fa79..d4ab2b6 100644 --- a/thun/gnu-prolog/thun.pl +++ b/thun/gnu-prolog/thun.pl @@ -20,10 +20,6 @@ % :- dynamic(func/3). % :- discontiguous(func/3). - -:- initialization(loop). - - /* Interpreter thun(Expression, InputStack, OutputStack) @@ -194,24 +190,3 @@ prepare_mapping( _, _, [], Out, Out) :- !. prepare_mapping( P, S, [T|In], Acc, Out) :- prepare_mapping(P, S, In, [[T|S], P, infrst|Acc], Out). - - -/* -Main Loop -*/ - -loop :- line(Line), loop(Line, [], _Out). - -loop([eof], S, S) :- !. -loop( Line, In, Out) :- - do_line(Line, In, S), - write(S), nl, - line(NextLine), !, - loop(NextLine, S, Out). - - -do_line(Line, In, Out) :- phrase(joy_parse(E), Line), thun(E, In, Out). -do_line(_Line, S, S) :- write('Err'), nl. - - -