Merge branch 'master' into library-use-case

# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	src/bin/scryer-prolog.rs
#	src/loader.pl
#	src/machine/mock_wam.rs
#	src/machine/mod.rs
#	src/machine/system_calls.rs
This commit is contained in:
Nicolas Luck
2023-08-03 20:16:32 +02:00
117 changed files with 17744 additions and 10848 deletions

View File

@@ -1,6 +1,7 @@
:- module('$toplevel', [argv/1,
copy_term/3]).
:- use_module(library(atts), [call_residue_vars/2]).
:- use_module(library(charsio)).
:- use_module(library(error)).
:- use_module(library(files)).
@@ -82,7 +83,7 @@ print_help :-
print_version :-
'$scryer_prolog_version'(Version),
write(Version), nl,
maplist(put_char, Version), nl,
halt.
gather_goal(Type, Args0, Goals) :-
@@ -114,18 +115,27 @@ layout_and_dot([C|Cs]) :-
layout_and_dot(Cs).
run_goals([]).
run_goals([g(Gs0)|Goals]) :-
run_goals([g(Gs0)|Goals]) :- !,
( ends_with_dot(Gs0) -> Gs1 = Gs0
; append(Gs0, ".", Gs1)
),
read_from_chars(Gs1, Goal),
( catch(
user:Goal,
Exception,
(write(Goal), write(' causes: '), write(Exception), nl) % halt?
)
; write('Warning: initialization failed for '),
write(Gs0), nl
double_quotes_option(DQ),
catch(read_term_from_chars(Gs1, Goal, [variable_names(VNs)]),
E,
( write_term(Gs0, [double_quotes(DQ)]),
write(' cannot be read: '), write(E), nl,
halt
)
),
( catch(user:Goal,
Exception,
( write_term(Goal, [variable_names(VNs),double_quotes(DQ)]),
write(' causes: '),
write_term(Exception, [double_quotes(DQ)]), nl % halt?
)
) -> true
; write('Warning: initialization failed for: '),
write_term(Goal, [variable_names(VNs),double_quotes(DQ)]), nl
),
run_goals(Goals).
run_goals([Goal|_]) :-
@@ -180,8 +190,9 @@ submit_query_and_print_results_(Term, VarList) :-
'$get_b_value'(B),
bb_put('$report_all', false),
bb_put('$report_n_more', 0),
call(user:Term),
write_eqs_and_read_input(B, VarList),
expand_goal(Term, user, Term0),
atts:call_residue_vars(user:Term0, AttrVars),
write_eqs_and_read_input(B, VarList, AttrVars),
!.
submit_query_and_print_results_(_, _) :-
( bb_get('$answer_count', 0) ->
@@ -203,22 +214,29 @@ submit_query_and_print_results(Term, VarList) :-
needs_bracketing(Value, Op) :-
catch((functor(Value, F, _),
current_op(EqPrec, EqSpec, Op),
current_op(FPrec, _, F)),
_,
false),
( EqPrec < FPrec ->
true
; FPrec > 0, F == Value, graphic_token_char(F) ->
true
; F \== '.', '$quoted_token'(F) ->
true
; EqPrec == FPrec,
memberchk(EqSpec, [fx,xfx,yfx])
nonvar(Value),
functor(Value, F, Arity),
atom(F),
current_op(FPrec, FSpec, F),
current_op(EqPrec, EqSpec, Op),
arity_specifier(Arity, FSpec),
( Arity =:= 0
; EqPrec < FPrec
; EqPrec =:= FPrec,
member(EqSpec, [fx,xfx,yfx])
).
arity_specifier(0, _).
arity_specifier(1, S) :- atom_chars(S, [_,_]).
arity_specifier(2, S) :- atom_chars(S, [_,_,_]).
double_quotes_option(DQ) :-
( current_prolog_flag(double_quotes, chars) -> DQ = true
; DQ = false
).
write_goal(G, VarList, MaxDepth) :-
double_quotes_option(DQ),
( G = (Var = Value) ->
( var(Value) ->
select((Var = _), VarList, NewVarList)
@@ -226,18 +244,19 @@ write_goal(G, VarList, MaxDepth) :-
),
write(Var),
write(' = '),
( needs_bracketing(Value, (=)) ->
( needs_bracketing(Value, =) ->
write('('),
write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth)]),
write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth), double_quotes(DQ)]),
write(')')
; write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth)])
; write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth), double_quotes(DQ)])
)
; G == [] ->
write('true')
; write_term(G, [quoted(true), variable_names(VarList), max_depth(MaxDepth)])
; write_term(G, [quoted(true), variable_names(VarList), max_depth(MaxDepth), double_quotes(DQ)])
).
write_last_goal(G, VarList, MaxDepth) :-
double_quotes_option(DQ),
( G = (Var = Value) ->
( var(Value) ->
select((Var = _), VarList, NewVarList)
@@ -245,11 +264,11 @@ write_last_goal(G, VarList, MaxDepth) :-
),
write(Var),
write(' = '),
( needs_bracketing(Value, (=)) ->
( needs_bracketing(Value, =) ->
write('('),
write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth)]),
write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth), double_quotes(DQ)]),
write(')')
; write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth)]),
; write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth), double_quotes(DQ)]),
( trailing_period_is_ambiguous(Value) ->
write(' ')
; true
@@ -257,7 +276,7 @@ write_last_goal(G, VarList, MaxDepth) :-
)
; G == [] ->
write('true')
; write_term(G, [quoted(true), variable_names(VarList), max_depth(MaxDepth)])
; write_term(G, [quoted(true), variable_names(VarList), max_depth(MaxDepth), double_quotes(DQ)])
).
write_eq((G1, G2), VarList, MaxDepth) :-
@@ -269,8 +288,7 @@ write_eq(G, VarList, MaxDepth) :-
write_last_goal(G, VarList, MaxDepth).
graphic_token_char(C) :-
memberchk(C, ['#', '$', '&', '*', '+', '-', '.', ('/'), ':',
'<', '=', '>', '?', '@', '^', '~', ('\\')]).
memberchk(C, [#, $, &, *, +, -, ., /, :, <, =, >, ?, @, ^, ~, \]).
list_last_item([C], C) :- !.
list_last_item([_|Cs], D) :-
@@ -286,11 +304,10 @@ trailing_period_is_ambiguous(Value) :-
term_variables_under_max_depth(Term, MaxDepth, Vars) :-
'$term_variables_under_max_depth'(Term, MaxDepth, Vars).
write_eqs_and_read_input(B, VarList) :-
write_eqs_and_read_input(B, VarList, AttrVars) :-
gather_query_vars(VarList, OrigVars),
% one layer of depth added for (=/2) functor
'$term_variables_under_max_depth'(OrigVars, 22, Vars0),
'$term_attributed_variables'(VarList, AttrVars),
'$project_atts':project_attributes(Vars0, AttrVars),
copy_term(AttrVars, AttrVars, AttrGoals),
term_variables(AttrGoals, AttrGoalVars),