shithub: pprolog

Download patch

ref: 0f347162b74d945f509955b6c57e506ab800db7b
parent: 77c7fea4cee74562ad60c7ce96ca830a7ebda8b7
author: Peter Mikkelsen <peter@pmikkelsen.com>
date: Thu Jul 22 15:35:53 EDT 2021

Implement halt/0, halt/1, and understand the --no-repl flag

--- a/builtins.c
+++ b/builtins.c
@@ -70,6 +70,7 @@
 BuiltinProto(builtinop);
 BuiltinProto(builtincurrentops);
 BuiltinProto(builtinnewemptymodule);
+BuiltinProto(builtinhalt);
 
 int compareterms(Term *, Term *);
 
@@ -199,6 +200,8 @@
 		return builtincurrentops;
 	if(Match(L"$new_empty_module", 1))
 		return builtinnewemptymodule;
+	if(Match(L"$halt", 1))
+		return builtinhalt;
 
 	return nil;
 }
@@ -1647,5 +1650,19 @@
 	USED(module);
 	Rune *name = goal->children->text;
 	addemptymodule(name);
+	return 1;
+}
+
+int
+builtinhalt(Term *goal, Binding **bindings, Module *module)
+{
+	USED(bindings);
+	USED(module);
+	vlong exitcode = goal->children->ival;
+	char *msg = nil;
+	if(exitcode != 0)
+		msg = smprint("pprolog exit code: %lld\n", exitcode);
+
+	exits(msg);
 	return 1;
 }
--- a/loader.pl
+++ b/loader.pl
@@ -2,7 +2,10 @@
 
 start(Args) :-
 	catch((load_module_from_file('/sys/lib/prolog/repl.pl'), ReplLoaded = true), E, (print_exception(E), ReplLoaded = false)),
-	( ReplLoaded = true-> repl:repl(Args) ).
+	!,
+	( ReplLoaded = true
+	-> repl:repl(Args)
+	).
 
 print_exception(E) :-
 	write('Caught exception while loading /sys/lib/prolog/repl.pl: '),
--- a/repl.pl
+++ b/repl.pl
@@ -1,17 +1,15 @@
 :- module(repl, []).
 
 repl([_ProgName|Args]) :-
-	write('Welcome to p-prolog version 1.'),
-	nl,
-	write('Started with args: '),
-	write(Args),
-	nl,
-	flush_output,
 	handle_args(Args),
-	repl_loop.
+	( member('--no-repl', Args)
+	-> halt
+	; repl_loop
+	).
 
 handle_arg('-d') :-
 	set_prolog_flag(debug, on).
+handle_arg('--no-repl').
 handle_arg(Arg) :-
 	loader:load_module_from_file(Arg).
 
--- a/stdlib.pl
+++ b/stdlib.pl
@@ -683,6 +683,16 @@
 	current_ops(Operators),
 	member(op(Priority, Op_specifier, Operator), Operators).
 
+% Halting
+
+halt(X) :-
+	is_nonvar(X),
+	is_integer(X),
+	'$halt'(X).
+
+halt :-
+	halt(0).
+
 % Loading prolog text
 
 consult(File) :-