shithub: pprolog

Download patch

ref: 8a1e32e6a8c441f8358bd580c655d5ff48716fa0
parent: c8867502df27f516b0d46b1a254f0da572bdadb6
author: Peter Mikkelsen <peter@pmikkelsen.com>
date: Fri Jul 16 16:30:26 EDT 2021

Handle -d option in prolog

--- a/builtins.c
+++ b/builtins.c
@@ -1418,7 +1418,7 @@
 	USED(bindings);
 	USED(module);
 	vlong amount = collectgarbage();
-	if(amount != 0 & debug)
+	if(amount != 0 & flagdebug)
 		print("Collected %lld bytes of garbage\n", amount);
 	return 1;
 }
\ No newline at end of file
--- a/dat.h
+++ b/dat.h
@@ -105,8 +105,6 @@
 	CompoundTerm,
 };
 
-int debug;
-
 /* Flags */
 enum {
 	BoundedTrue,
@@ -124,8 +122,8 @@
 };
 
 enum {
-	DebugOn,
 	DebugOff,
+	DebugOn,
 };
 
 enum {
--- a/eval.c
+++ b/eval.c
@@ -25,7 +25,7 @@
 		if(catcher)
 			continue;
 
-		if(debug)
+		if(flagdebug)
 			print("Working goal: %S:%S\n", module->name, prettyprint(goal, 0, 0, 0, nil));
 
 		Binding *bindings = nil;
@@ -76,7 +76,7 @@
 Backtrack:
 				if(choicestack == nil)
 					return 0;
-				if(debug)
+				if(flagdebug)
 					print("Backtracking..\n");
 				Choicepoint *cp = choicestack;
 				choicestack = cp->next;
--- a/flags.c
+++ b/flags.c
@@ -26,8 +26,8 @@
 };
 
 static Rune *debugvals[] = {
-	[DebugOn] = L"on",
-	[DebugOff] = L"off"
+	[DebugOff] = L"off",
+	[DebugOn] = L"on"
 };
 
 static Rune *unknownvals[] = {
--- a/main.c
+++ b/main.c
@@ -11,14 +11,6 @@
 void
 main(int argc, char *argv[])
 {
-	ARGBEGIN{
-	case 'd':
-		debug = 1;
-		break;
-	default:
-		usage();
-	}ARGEND
-
 	clausenr = 2; /* Start at two since 0 is for the facts in the database, and 1 is for queries */
 	initflags();
 	initstreams();
--- a/module.c
+++ b/module.c
@@ -51,7 +51,7 @@
 				print("Module name should be an atom in: %S\n", prettyprint(directive, 0, 0, 0, nil));
 				return nil;
 			}
-			if(debug)
+			if(flagdebug)
 				print("Public list for module '%S': %S\n", modulename->text, prettyprint(publiclist, 0, 0, 0, nil));
 			m = getmodule(modulename->text);
 		}
--- a/repl.pl
+++ b/repl.pl
@@ -1,12 +1,23 @@
 :- module(repl, []).
 
-repl(Args) :-
+repl([ProgName|Args]) :-
 	write('Welcome to p-prolog version 1'),
 	nl,
 	write('Started with args: '),
 	write(Args),
 	nl,
+	handle_args(Args),
 	repl_loop.
+
+handle_arg('-d') :-
+	set_prolog_flag(debug, on).
+handle_arg(Arg) :-
+	write('Unhandled command line argument: '),
+	writeq(Arg),
+	nl.
+
+handle_args([Arg|Rest]) :- handle_arg(Arg), !, handle_args(Rest).
+handle_args([]).
 
 repl_loop :-
 	catch(read_eval_print, E, print_exception(E)),