From: Markus Triska Date: Sat, 23 May 2020 17:14:29 +0000 (+0200) Subject: ADDED: halt/1, halting with specified exit code X-Git-Tag: v0.8.127~64^2 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=b0df981b01fad6898a00d6fe72407e0d75997437;p=scryer-prolog.git ADDED: halt/1, halting with specified exit code --- diff --git a/src/prolog/clause_types.rs b/src/prolog/clause_types.rs index e685c65f..14c353b2 100644 --- a/src/prolog/clause_types.rs +++ b/src/prolog/clause_types.rs @@ -580,7 +580,7 @@ impl SystemClauseType { ("$get_lh_from_offset_diff", 3) => Some(SystemClauseType::GetLiftedHeapFromOffsetDiff), ("$get_double_quotes", 1) => Some(SystemClauseType::GetDoubleQuotes), ("$get_scc_cleaner", 1) => Some(SystemClauseType::GetSCCCleaner), - ("$halt", 0) => Some(SystemClauseType::Halt), + ("$halt", 1) => Some(SystemClauseType::Halt), ("$head_is_dynamic", 1) => Some(SystemClauseType::HeadIsDynamic), ("$install_scc_cleaner", 2) => Some(SystemClauseType::InstallSCCCleaner), ("$install_inference_counter", 3) => Some(SystemClauseType::InstallInferenceCounter), diff --git a/src/prolog/lib/builtins.pl b/src/prolog/lib/builtins.pl index a4bd6db8..3e1c8d28 100644 --- a/src/prolog/lib/builtins.pl +++ b/src/prolog/lib/builtins.pl @@ -52,7 +52,7 @@ user:term_expansion((:- op(Pred, Spec, [Op | OtherOps])), OpResults) :- findall/3, findall/4, flush_output/0, flush_output/1, get_byte/1, get_byte/2, get_char/1, get_char/2, get_code/1, get_code/2, - halt/0, max_arity/1, number_chars/2, + halt/0, halt/1, max_arity/1, number_chars/2, number_codes/2, once/1, op/3, open/3, open/4, peek_byte/1, peek_byte/2, peek_char/1, peek_char/2, peek_code/1, peek_code/2, @@ -912,7 +912,14 @@ op(Priority, OpSpec, Op) :- ; throw(error(type_error(list, Op), op/3)) % 8.14.3.3 f) ). -halt :- '$halt'. + +halt :- halt(0). + +halt(N) :- + ( -2^31 =< N, N =< 2^31 - 1 -> + '$halt'(N) + ; throw(error(domain_error(exit_code, N), halt/1)) + ). atom_length(Atom, Length) :- ( var(Atom) -> throw(error(instantiation_error, atom_length/2)) % 8.16.1.3 a) diff --git a/src/prolog/machine/system_calls.rs b/src/prolog/machine/system_calls.rs index 0fb224ad..dcc15bc1 100644 --- a/src/prolog/machine/system_calls.rs +++ b/src/prolog/machine/system_calls.rs @@ -3647,7 +3647,15 @@ impl MachineState { self.fail = true; } &SystemClauseType::Halt => { - std::process::exit(0); + let code = self.store(self.deref(self[temp_v!(1)])); + + let code = match Number::try_from((code, &self.heap)) { + Ok(Number::Fixnum(n)) => n as i32, + Ok(Number::Integer(n)) => n.to_i32().unwrap(), + _ => { unreachable!() } + }; + + std::process::exit(code); } &SystemClauseType::InstallSCCCleaner => { let addr = self[temp_v!(1)];