@@ -28,7 +28,7 @@ use std::sync::atomic::Ordering;
|
|||||||
extern fn handle_sigint(signal: libc::c_int) {
|
extern fn handle_sigint(signal: libc::c_int) {
|
||||||
let signal = signal::Signal::from_c_int(signal).unwrap();
|
let signal = signal::Signal::from_c_int(signal).unwrap();
|
||||||
if signal == signal::Signal::SIGINT {
|
if signal == signal::Signal::SIGINT {
|
||||||
INTERRUPT.store(true, Ordering::Relaxed);
|
INTERRUPT.store(true, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,11 +36,6 @@ fn main() {
|
|||||||
let handler = signal::SigHandler::Handler(handle_sigint);
|
let handler = signal::SigHandler::Handler(handle_sigint);
|
||||||
unsafe { signal::signal(signal::Signal::SIGINT, handler) }.unwrap();
|
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());
|
let mut wam = Machine::new(readline::input_stream(), Stream::stdout());
|
||||||
wam.run_top_level();
|
wam.run_top_level();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -264,6 +264,7 @@ pub enum SystemClauseType {
|
|||||||
WAMInstructions,
|
WAMInstructions,
|
||||||
WriteTerm,
|
WriteTerm,
|
||||||
WriteTermToChars,
|
WriteTermToChars,
|
||||||
|
ScryerPrologVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SystemClauseType {
|
impl SystemClauseType {
|
||||||
@@ -418,6 +419,7 @@ impl SystemClauseType {
|
|||||||
&SystemClauseType::WAMInstructions => clause_name!("$wam_instructions"),
|
&SystemClauseType::WAMInstructions => clause_name!("$wam_instructions"),
|
||||||
&SystemClauseType::WriteTerm => clause_name!("$write_term"),
|
&SystemClauseType::WriteTerm => clause_name!("$write_term"),
|
||||||
&SystemClauseType::WriteTermToChars => clause_name!("$write_term_to_chars"),
|
&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),
|
("$wam_instructions", 3) => Some(SystemClauseType::WAMInstructions),
|
||||||
("$write_term", 6) => Some(SystemClauseType::WriteTerm),
|
("$write_term", 6) => Some(SystemClauseType::WriteTerm),
|
||||||
("$write_term_to_chars", 7) => Some(SystemClauseType::WriteTermToChars),
|
("$write_term_to_chars", 7) => Some(SystemClauseType::WriteTermToChars),
|
||||||
|
("$scryer_prolog_version", 1) => Some(SystemClauseType::ScryerPrologVersion),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3543,6 +3543,15 @@ impl MachineState {
|
|||||||
unreachable!()
|
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)
|
return_from_clause!(self.last_call, self)
|
||||||
|
|||||||
@@ -1,18 +1,103 @@
|
|||||||
|
|
||||||
:- 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(charsio)).
|
||||||
:- use_module(library(lists)).
|
:- use_module(library(lists)).
|
||||||
:- use_module(library(si)).
|
:- use_module(library(si)).
|
||||||
|
|
||||||
'$repl'([_|Args]) :-
|
:- dynamic(argv/1).
|
||||||
maplist(use_list_of_modules, Args),
|
|
||||||
false.
|
|
||||||
'$repl'(_) :- repl.
|
|
||||||
|
|
||||||
use_list_of_modules(Mod0) :-
|
'$repl'([_|Args0]) :-
|
||||||
atom_chars(Mod, Mod0),
|
\+ argv(_),
|
||||||
catch(use_module(Mod), E, print_exception(E)).
|
( append(Args1, ["--"|Args2], Args0) ->
|
||||||
|
asserta(argv(Args2)),
|
||||||
|
Args = Args1
|
||||||
|
; asserta(argv([])),
|
||||||
|
Args = Args0
|
||||||
|
),
|
||||||
|
delegate_task(Args, []),
|
||||||
|
repl.
|
||||||
|
'$repl'(_) :-
|
||||||
|
( \+ argv(_) -> asserta(argv([]))
|
||||||
|
; true
|
||||||
|
),
|
||||||
|
repl.
|
||||||
|
|
||||||
|
delegate_task([], []).
|
||||||
|
delegate_task([], Goals0) :-
|
||||||
|
reverse(Goals0, Goals),
|
||||||
|
run_goals(Goals),
|
||||||
|
repl.
|
||||||
|
delegate_task([Arg0|Args], Goals0) :-
|
||||||
|
( member(Arg0, ["-h", "--help"]) -> print_help
|
||||||
|
; member(Arg0, ["-v", "--version"]) -> print_version
|
||||||
|
; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0)
|
||||||
|
; atom_chars(Mod, Arg0),
|
||||||
|
catch(use_module(Mod), E, print_exception(E))
|
||||||
|
),
|
||||||
|
delegate_task(Args, Goals0).
|
||||||
|
|
||||||
|
print_help :-
|
||||||
|
write('Usage: scryer-prolog [OPTIONS] [FILES] [-- 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(' '),
|
||||||
|
halt.
|
||||||
|
|
||||||
|
print_version :-
|
||||||
|
'$scryer_prolog_version'(Version),
|
||||||
|
write(Version), nl,
|
||||||
|
halt.
|
||||||
|
|
||||||
|
gather_goal(Type, Args0, Goals) :-
|
||||||
|
length(Args0, N),
|
||||||
|
( N < 1 -> print_help, halt
|
||||||
|
; true
|
||||||
|
),
|
||||||
|
[Gs1|Args] = Args0,
|
||||||
|
Gs =.. [Type, Gs1],
|
||||||
|
delegate_task(Args, [Gs|Goals]).
|
||||||
|
|
||||||
|
arg_type(g).
|
||||||
|
arg_type(t).
|
||||||
|
arg_type(g(_)).
|
||||||
|
arg_type(t(_)).
|
||||||
|
|
||||||
|
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(Gs0)|Goals]) :-
|
||||||
|
( ends_with_dot(Gs0) -> Gs1 = Gs0
|
||||||
|
; append(Gs0, ".", Gs1)
|
||||||
|
),
|
||||||
|
read_term_from_chars(Gs1, Goal),
|
||||||
|
( catch(
|
||||||
|
Goal,
|
||||||
|
Exception,
|
||||||
|
(write(Gs0), write(' causes: '), write(Exception), nl) % halt?
|
||||||
|
)
|
||||||
|
; write('Warning: initialization failed for '),
|
||||||
|
write(Gs0), nl
|
||||||
|
),
|
||||||
|
run_goals(Goals).
|
||||||
|
run_goals([Goal|_]) :-
|
||||||
|
write('caught: '),
|
||||||
|
write(error(domain_error(arg_type, Goal), run_goals/1)), nl,
|
||||||
|
halt.
|
||||||
|
|
||||||
repl :-
|
repl :-
|
||||||
catch(read_and_match, E, print_exception(E)),
|
catch(read_and_match, E, print_exception(E)),
|
||||||
@@ -363,3 +448,4 @@ thread_goals(Goals0, Goals1, Functor) :-
|
|||||||
)
|
)
|
||||||
; Goals1 = Goals0
|
; Goals1 = Goals0
|
||||||
).
|
).
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user