shithub: pprolog

Download patch

ref: daadb2b174846cda95e51e0c4c94bcb748da4a69
parent: 18ae80eac7b678d71b13710b978a03863c7e3fd2
author: Peter Mikkelsen <peter@pmikkelsen.com>
date: Sun Jul 18 14:22:05 EDT 2021

Load extra arguments as module files

--- a/builtins.c
+++ b/builtins.c
@@ -62,6 +62,7 @@
 BuiltinProto(builtincharcode);
 BuiltinProto(builtinchoicestacksize);
 BuiltinProto(builtincollectgarbage);
+BuiltinProto(builtinloadmodulefromfile);
 
 int compareterms(Term *, Term *);
 
@@ -175,6 +176,8 @@
 		return builtinchoicestacksize;
 	if(Match(L"$collect_garbage", 0))
 		return builtincollectgarbage;
+	if(Match(L"$load_module_from_file", 1))
+		return builtinloadmodulefromfile;
 
 	return nil;
 }
@@ -1421,4 +1424,25 @@
 	if(amount != 0 & flagdebug)
 		print("Collected %lld bytes of garbage\n", amount);
 	return 1;
+}
+
+int
+builtinloadmodulefromfile(Term *goal, Binding **bindings, Module *module)
+{
+	USED(bindings);
+	USED(module);
+	Term *file = goal->children;
+
+	if(file->tag == VariableTerm)
+		Throw(instantiationerror());
+	if(file->tag != AtomTerm)
+		Throw(typeerror(L"atom", file));
+
+	char *filestr = smprint("%S", file->text);
+	Module *m = parsemodule(filestr);
+	free(filestr);
+	if(m)
+		return 1;
+	else
+		return 0;
 }
\ No newline at end of file
--- a/repl.pl
+++ b/repl.pl
@@ -12,9 +12,11 @@
 handle_arg('-d') :-
 	set_prolog_flag(debug, on).
 handle_arg(Arg) :-
-	write('Unhandled command line argument: '),
-	writeq(Arg),
-	nl.
+	( '$load_module_from_file'(Arg)
+	-> write('Loaded module from file: ')
+	; write('Failed to load module from file: ')
+	),
+	write(Arg), nl.
 
 handle_args([Arg|Rest]) :- handle_arg(Arg), !, handle_args(Rest).
 handle_args([]).