From b911d2fda4d92b81280079585b9b7b790f192fe0 Mon Sep 17 00:00:00 2001 From: notoria Date: Fri, 1 May 2020 15:57:50 +0200 Subject: [PATCH 1/3] Enhanced command line --- src/prolog/toplevel.pl | 124 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 116 insertions(+), 8 deletions(-) diff --git a/src/prolog/toplevel.pl b/src/prolog/toplevel.pl index 7072017d..05cb6b6e 100644 --- a/src/prolog/toplevel.pl +++ b/src/prolog/toplevel.pl @@ -1,18 +1,126 @@ -:- module('$toplevel', ['$repl'/1, consult/1, use_module/1, use_module/2]). +:- module('$toplevel', ['$repl'/1, consult/1, use_module/1, use_module/2, + argv/1]). :- use_module(library(charsio)). :- use_module(library(lists)). :- use_module(library(si)). -'$repl'([_|Args]) :- - maplist(use_list_of_modules, Args), - false. -'$repl'(_) :- repl. +:- dynamic(argv/1). -use_list_of_modules(Mod0) :- - atom_chars(Mod, Mod0), - catch(use_module(Mod), E, print_exception(E)). +'$repl'([_|Args]) :- + \+ argv(_), + delegate_task(Args, []), + repl. +'$repl'(_) :- + ( \+ argv(_) -> asserta(argv([])) + ; true + ), + repl. + +delegate_task([], []). +delegate_task([], Goals0) :- + asserta(argv([])), + reverse(Goals0, Goals), + run_goals(Goals), + repl. +delegate_task([Arg0|Args], Goals0) :- + ( member(Arg0, ["-h", "--help"]) -> print_help(Args) + ; member(Arg0, ["-v", "--version"]) -> print_version(Args) + ; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0) + ; member(Arg0, ["-t", "--toplevel"]) -> gather_goal(t, Args, Goals0) + ; atom_chars(Mod, Arg0), + asserta(argv(Args)), + catch(use_module(Mod), E, print_exception(E)), + reverse(Goals0, Goals), + run_goals(Goals), + repl + ), + delegate_task(Args, Goals0). + +print_help(Args) :- + write('Usage: scryer-prolog [OPTIONS] FILE [ARGUMENTS]'), + nl, nl, + write('Options:'), nl, + write(' -h, --help '), + write('Display this message'), nl, + write(' -v, --version '), + write('Print version information and exit'), nl, + write(' -g, --goal GOAL '), + write('Run the query GOAL'), nl, + write(' -t, --toplevel GOAL '), + % write(' '), + write('Run the query GOAL and halt'), nl, + halt. + +print_version(Args) :- + write('v0.8.120'), nl, % TODO: Something better is required here. + halt. + +gather_goal(Type, Args0, Goals) :- + length(Args0, N), + ( N < 1 -> + % throw(error(resource_error(not_enough_argument), run_goal/1)) + write('caught: '), + write(error(resource_error(not_enough_argument), gather_goal/3)), + nl, + halt + ; true + ), + [Goal1|Args] = Args0, + ( Type = g -> Goal = g(Goal1) + ; Type = t -> Goal = t(Goal1) + ; write('caught: '), + write(error(domain_error(not_arg_type, Type), gather_goal/3)), nl, + halt + ), + delegate_task(Args, [Goal|Goals]). + +ends_with_dot(Ls0) :- + reverse(Ls0, Ls), + layout_and_dot(Ls). + +layout_and_dot(['.'|_]). +layout_and_dot([C|Cs]) :- + char_type(C, layout), + layout_and_dot(Cs). + +run_goals([]). +run_goals([g(Goal0)|Goals]) :- + ( ends_with_dot(Goal0) -> Goal1 = Goal0 + ; append(Goal0, ".", Goal1) + ), + read_term_from_chars(Goal1, Goal), + ( catch(Goal, E, print_exception_warning(E)) + ; write('Warning: initialization failed for '), + write(Goal0), nl + ), + run_goals(Goals). +run_goals([t(Goal0)|_]) :- + ( ends_with_dot(Goal0) -> Goal1 = Goal0 + ; append(Goal0, ".", Goal1) + ), + read_term_from_chars(Goal1, Goal), + ( catch(Goal, E, print_exception_warning(E)) + ; write('Warning: initialization failed for '), + write(Goal0), nl + ), + halt. +run_goals([Goal|_]) :- + write('caught: '), + write(error(domain_error(not_arg_type, Goal), run_goals/1)), nl, + halt. + +print_exception_warning(E) :- + ( E == error('$interrupt_thrown', repl) -> nl % print the + % exception on a + % newline to evade + % "^C". + ; true + ), + write_term('Warning: ', [quoted(false), max_depth(20)]), + writeq(E), + nl. repl :- catch(read_and_match, E, print_exception(E)), From 6ae7ae0210f24d55fd49636298b7b7d1ec6ec75e Mon Sep 17 00:00:00 2001 From: notoria Date: Fri, 1 May 2020 16:46:31 +0200 Subject: [PATCH 2/3] Enhanced version --- src/main.rs | 5 ----- src/prolog/clause_types.rs | 3 +++ src/prolog/machine/system_calls.rs | 9 +++++++++ src/prolog/toplevel.pl | 3 ++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index c8866b38..fc85a959 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,11 +36,6 @@ fn main() { let handler = signal::SigHandler::Handler(handle_sigint); unsafe { signal::signal(signal::Signal::SIGINT, handler) }.unwrap(); - if env::args().skip(1).any(|a| a == "-v" || a == "--version") { - println!("{:}", git_version!(cargo_prefix = "cargo:", fallback = "unknown")); - return; - } - let mut wam = Machine::new(readline::input_stream(), Stream::stdout()); wam.run_top_level(); } diff --git a/src/prolog/clause_types.rs b/src/prolog/clause_types.rs index 9b550c35..83e144eb 100644 --- a/src/prolog/clause_types.rs +++ b/src/prolog/clause_types.rs @@ -264,6 +264,7 @@ pub enum SystemClauseType { WAMInstructions, WriteTerm, WriteTermToChars, + ScryerPrologVersion, } impl SystemClauseType { @@ -418,6 +419,7 @@ impl SystemClauseType { &SystemClauseType::WAMInstructions => clause_name!("$wam_instructions"), &SystemClauseType::WriteTerm => clause_name!("$write_term"), &SystemClauseType::WriteTermToChars => clause_name!("$write_term_to_chars"), + &SystemClauseType::ScryerPrologVersion => clause_name!("$scryer_prolog_version"), } } @@ -552,6 +554,7 @@ impl SystemClauseType { ("$wam_instructions", 3) => Some(SystemClauseType::WAMInstructions), ("$write_term", 6) => Some(SystemClauseType::WriteTerm), ("$write_term_to_chars", 7) => Some(SystemClauseType::WriteTermToChars), + ("$scryer_prolog_version", 1) => Some(SystemClauseType::ScryerPrologVersion), _ => None, } } diff --git a/src/prolog/machine/system_calls.rs b/src/prolog/machine/system_calls.rs index a0e5762a..144bcd8b 100644 --- a/src/prolog/machine/system_calls.rs +++ b/src/prolog/machine/system_calls.rs @@ -3515,6 +3515,15 @@ impl MachineState { unreachable!() } } + &SystemClauseType::ScryerPrologVersion => { + use crate::git_version::git_version; + let version = self[temp_v!(1)]; + let buffer = + git_version!(cargo_prefix = "cargo:", fallback = "unknown"); + let chars = buffer.chars().map(|c| Addr::Char(c)); + let result = Addr::HeapCell(self.heap.to_list(chars)); + self.unify(version, result); + } }; return_from_clause!(self.last_call, self) diff --git a/src/prolog/toplevel.pl b/src/prolog/toplevel.pl index 05cb6b6e..d691c66e 100644 --- a/src/prolog/toplevel.pl +++ b/src/prolog/toplevel.pl @@ -54,7 +54,8 @@ print_help(Args) :- halt. print_version(Args) :- - write('v0.8.120'), nl, % TODO: Something better is required here. + '$scryer_prolog_version'(Version), + write(Version), nl, halt. gather_goal(Type, Args0, Goals) :- From de8a017e80547b193e3c55b13a65b44b16d0e630 Mon Sep 17 00:00:00 2001 From: notoria Date: Fri, 1 May 2020 18:52:41 +0200 Subject: [PATCH 3/3] Enhanced command line #2 --- src/prolog/toplevel.pl | 76 +++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/src/prolog/toplevel.pl b/src/prolog/toplevel.pl index d691c66e..d3202fb2 100644 --- a/src/prolog/toplevel.pl +++ b/src/prolog/toplevel.pl @@ -8,8 +8,14 @@ :- dynamic(argv/1). -'$repl'([_|Args]) :- +'$repl'([_|Args0]) :- \+ argv(_), + ( append(Args1, ["--"|Args2], Args0) -> + asserta(argv(Args2)), + Args = Args1 + ; asserta(argv([])), + Args = Args0 + ), delegate_task(Args, []), repl. '$repl'(_) :- @@ -20,7 +26,6 @@ delegate_task([], []). delegate_task([], Goals0) :- - asserta(argv([])), reverse(Goals0, Goals), run_goals(Goals), repl. @@ -30,16 +35,12 @@ delegate_task([Arg0|Args], Goals0) :- ; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0) ; member(Arg0, ["-t", "--toplevel"]) -> gather_goal(t, Args, Goals0) ; atom_chars(Mod, Arg0), - asserta(argv(Args)), - catch(use_module(Mod), E, print_exception(E)), - reverse(Goals0, Goals), - run_goals(Goals), - repl + catch(use_module(Mod), E, print_exception(E)) ), delegate_task(Args, Goals0). -print_help(Args) :- - write('Usage: scryer-prolog [OPTIONS] FILE [ARGUMENTS]'), +print_help(_) :- + write('Usage: scryer-prolog [OPTIONS] [FILES] [-- ARGUMENTS]'), nl, nl, write('Options:'), nl, write(' -h, --help '), @@ -53,29 +54,28 @@ print_help(Args) :- write('Run the query GOAL and halt'), nl, halt. -print_version(Args) :- +print_version(_) :- '$scryer_prolog_version'(Version), write(Version), nl, halt. gather_goal(Type, Args0, Goals) :- length(Args0, N), - ( N < 1 -> - % throw(error(resource_error(not_enough_argument), run_goal/1)) - write('caught: '), - write(error(resource_error(not_enough_argument), gather_goal/3)), - nl, - halt + ( N < 1 -> print_help(_), halt ; true ), - [Goal1|Args] = Args0, - ( Type = g -> Goal = g(Goal1) - ; Type = t -> Goal = t(Goal1) + [Gs1|Args] = Args0, + ( member(Type, [g, t]) -> Gs =.. [Type, Gs1] ; write('caught: '), - write(error(domain_error(not_arg_type, Type), gather_goal/3)), nl, + write(error(domain_error(arg_type, Type), gather_goal/3)), nl, halt ), - delegate_task(Args, [Goal|Goals]). + delegate_task(Args, [Gs|Goals]). + +arg_type(g). +arg_type(t). +arg_type(g(_)). +arg_type(t(_)). ends_with_dot(Ls0) :- reverse(Ls0, Ls), @@ -87,29 +87,37 @@ layout_and_dot([C|Cs]) :- layout_and_dot(Cs). run_goals([]). -run_goals([g(Goal0)|Goals]) :- - ( ends_with_dot(Goal0) -> Goal1 = Goal0 - ; append(Goal0, ".", Goal1) +run_goals([g(Gs0)|Goals]) :- + ( ends_with_dot(Gs0) -> Gs1 = Gs0 + ; append(Gs0, ".", Gs1) ), - read_term_from_chars(Goal1, Goal), - ( catch(Goal, E, print_exception_warning(E)) + read_term_from_chars(Gs1, Goal), + ( catch( + Goal, + Exception, + (write(Gs0), write(' causes: '), write(Exception), nl) % halt? + ) ; write('Warning: initialization failed for '), - write(Goal0), nl + write(Gs0), nl ), run_goals(Goals). -run_goals([t(Goal0)|_]) :- - ( ends_with_dot(Goal0) -> Goal1 = Goal0 - ; append(Goal0, ".", Goal1) +run_goals([t(Gs0)|_]) :- + ( ends_with_dot(Gs0) -> Gs1 = Gs0 + ; append(Gs0, ".", Gs1) ), - read_term_from_chars(Goal1, Goal), - ( catch(Goal, E, print_exception_warning(E)) + read_term_from_chars(Gs1, Goal), + ( catch( + Goal, + Exception, + (write(Gs0), write(' causes: '), write(Exception), nl, halt) + ) ; write('Warning: initialization failed for '), - write(Goal0), nl + write(Gs0), nl ), halt. run_goals([Goal|_]) :- write('caught: '), - write(error(domain_error(not_arg_type, Goal), run_goals/1)), nl, + write(error(domain_error(arg_type, Goal), run_goals/1)), nl, halt. print_exception_warning(E) :-