Skip to content

Commit fba0be7

Browse files
jjtoltonJ.J.'s Robot
andcommitted
Replace halt flags with -t custom toplevel option
- Remove --halt-on-error and --always-halt flags and environment variables - Add -t FLAG to specify custom toplevel (arity 0 predicate) - Default toplevel is 'repl' if -t is not specified - Using `-t halt` achieves original goal of guaranteed termination - Custom toplevels enable flexible exit strategies (e.g., server mode) - Update help text to document -t flag - Remove halt_on_error and always_halt from MachineArgs struct This is more elegant and flexible than the previous approach, allowing users to define their own toplevel behavior including halt, custom REPLs, or server loops. Examples: scryer-prolog -t halt program.pl # Exits after execution scryer-prolog -t my_repl program.pl # Custom REPL scryer-prolog program.pl # Default REPL Co-Authored-By: J.J.'s Robot <noreply@example.com>
1 parent f050e75 commit fba0be7

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/toplevel.pl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
:- dynamic(disabled_init_file/0).
1818
:- dynamic(started/0).
19+
:- dynamic(custom_toplevel/1).
1920

2021
load_scryerrc :-
2122
( '$home_directory'(HomeDir) ->
@@ -53,7 +54,13 @@
5354
; true
5455
),
5556
(\+ disabled_init_file -> load_scryerrc ; true),
56-
repl.
57+
start_toplevel.
58+
59+
start_toplevel :-
60+
( custom_toplevel(Goal) ->
61+
user:call(Goal)
62+
; repl
63+
).
5764

5865
args_consults_goals([], [], []).
5966
args_consults_goals([Arg|Args], Consults, Goals) :-
@@ -71,12 +78,13 @@
7178
args_consults_goals(Goals1, Consults, Goals),
7279
run_goals(Consults),
7380
run_goals(Goals),
74-
repl.
81+
start_toplevel.
7582

7683
delegate_task([Arg0|Args], Goals0) :-
7784
( ( member(Arg0, ["-h", "--help"]) -> print_help
7885
; member(Arg0, ["-v", "--version"]) -> print_version
7986
; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0)
87+
; member(Arg0, ["-t"]) -> gather_toplevel(Args, Goals0)
8088
; member(Arg0, ["-f"]) -> disable_init_file
8189
; member(Arg0, ["--no-add-history"]) -> ignore_machine_arg
8290
),
@@ -96,6 +104,8 @@
96104
write('Print version information and exit'), nl,
97105
write(' -g, --goal GOAL '),
98106
write('Run the query GOAL'), nl,
107+
write(' -t GOAL '),
108+
write('Use GOAL as custom toplevel (arity 0 predicate)'), nl,
99109
write(' -f '),
100110
write('Fast startup. Do not load initialization file (~/.scryerrc)'), nl,
101111
write(' --no-add-history '),
@@ -117,6 +127,17 @@
117127
Gs =.. [Type, Gs1],
118128
delegate_task(Args, [Gs|Goals]).
119129

130+
gather_toplevel(Args0, Goals0) :-
131+
length(Args0, N),
132+
( N < 1 -> print_help, halt
133+
; true
134+
),
135+
[TopLevel|Args] = Args0,
136+
atom_chars(Goal, TopLevel),
137+
retractall(custom_toplevel(_)),
138+
asserta(custom_toplevel(Goal)),
139+
delegate_task(Args, Goals0).
140+
120141
disable_init_file :-
121142
asserta('disabled_init_file').
122143

@@ -154,7 +175,7 @@
154175
Exception,
155176
( write_term(Goal, [variable_names(VNs),double_quotes(DQ)]),
156177
write(' causes: '),
157-
write_term(Exception, [double_quotes(DQ)]), nl % halt?
178+
write_term(Exception, [double_quotes(DQ)]), nl
158179
)
159180
) -> true
160181
; write('% Warning: initialization failed for: '),

0 commit comments

Comments
 (0)