ref: d7a4ae651e8334955ad39c4adf02e36b6c99ec2f
parent: 5fe8b9865cfbd7388b8f6355a73b1436d40cfb1e
author: Peter Mikkelsen <[email protected]>
date: Thu Jul 22 14:48:15 EDT 2021
Understand initialization/1 directives
--- a/loader.pl
+++ b/loader.pl
@@ -15,9 +15,26 @@
; atom_concat(File, '.pl', File1),
open(File1, read, Stream)
),
- read_and_handle_terms(Stream, user, _),
- close(Stream).
+ read_and_handle_terms(Stream, user, Module),
+ close(Stream),
+ run_initialization_goals(Module).
+run_initialization_goals(Module) :-
+ ( retract(initialization_goals(Module, Goal)),
+ Module:catch(Goal, E, loader:print_initialization_goal_error(Module, Goal, E)),
+ fail % Backtrack to find more goals
+ ; true
+ ).
+
+print_initialization_goal_error(Module, Goal, Exception) :-
+ write('Initialization goal threw exception:'),
+ nl,
+ write(' Module: '), write(Module), nl,
+ write(' Goal: '), write(Goal), nl,
+ write(' Exception: '), write(Exception), nl,
+ nl.
+
+
read_and_handle_terms(Stream, Module0, Module) :-
( read_one_term(Stream, Term, Singles)
-> handle_term(Term, Singles, Module0, Module1),
@@ -86,6 +103,8 @@
).
handle_directive(op(Priority, Specifier, Operator), Module, Module) :-
Module:op(Priority, Specifier, Operator).
+handle_directive(initialization(T), Module, Module) :-
+ loader:assertz(initialization_goals(Module, T)).
handle_directive(include(F), Module, NewModule) :-
open(F, read, S),
read_and_handle_terms(S, Module, NewModule),