provide better variable names in write_term_to_chars/3 (#340)
This commit is contained in:
@@ -3,6 +3,51 @@
|
|||||||
|
|
||||||
:- use_module(library(iso_ext)).
|
:- use_module(library(iso_ext)).
|
||||||
|
|
||||||
|
|
||||||
|
fabricate_var_name(VarName, N) :-
|
||||||
|
char_code('A', AC),
|
||||||
|
LN is N mod 26 + AC,
|
||||||
|
char_code(LC, LN),
|
||||||
|
NN is N // 26,
|
||||||
|
( NN =:= 0 ->
|
||||||
|
atom_chars(VarName, ['_', LC])
|
||||||
|
; number_chars(NN, NNChars),
|
||||||
|
atom_chars(VarName, ['_', LC | NNChars])
|
||||||
|
).
|
||||||
|
|
||||||
|
var_list_contains_name([VarName = _ | VarList], VarName0) :-
|
||||||
|
( VarName == VarName0 -> true
|
||||||
|
; var_list_contains_name(VarList, VarName0)
|
||||||
|
).
|
||||||
|
|
||||||
|
var_list_contains_variable([_ = Var | VarList], Var0) :-
|
||||||
|
( Var == Var0 -> true
|
||||||
|
; var_list_contains_variable(VarList, Var0)
|
||||||
|
).
|
||||||
|
|
||||||
|
make_new_var_name(V, VarName, N, N1, VarList) :-
|
||||||
|
fabricate_var_name(VarName0, N),
|
||||||
|
( var_list_contains_name(VarList, VarName0) ->
|
||||||
|
N0 is N + 1,
|
||||||
|
make_new_var_name(V, VarName, N0, N1, VarList)
|
||||||
|
; VarName = VarName0,
|
||||||
|
N1 is N + 1
|
||||||
|
).
|
||||||
|
|
||||||
|
extend_var_list(Value, VarList, NewVarList) :-
|
||||||
|
term_variables(Value, Vars),
|
||||||
|
extend_var_list_(Vars, 0, VarList, NewVarList).
|
||||||
|
|
||||||
|
extend_var_list_([], N, VarList, VarList).
|
||||||
|
extend_var_list_([V|Vs], N, VarList, NewVarList) :-
|
||||||
|
( var_list_contains_variable(VarList, V) ->
|
||||||
|
extend_var_list_(Vs, N, VarList, NewVarList)
|
||||||
|
; make_new_var_name(V, VarName, N, N1, VarList),
|
||||||
|
NewVarList = [VarName = V | NewVarList0],
|
||||||
|
extend_var_list_(Vs, N1, VarList, NewVarList0)
|
||||||
|
).
|
||||||
|
|
||||||
|
|
||||||
read_term_from_chars(Chars, Term) :-
|
read_term_from_chars(Chars, Term) :-
|
||||||
( var(Chars) ->
|
( var(Chars) ->
|
||||||
throw(error(instantiation_error, read_term_from_chars/2))
|
throw(error(instantiation_error, read_term_from_chars/2))
|
||||||
@@ -36,4 +81,5 @@ write_term_to_chars(Term, Options, Chars) :-
|
|||||||
builtins:inst_member_or(Options, quoted(Quoted), quoted(false)),
|
builtins:inst_member_or(Options, quoted(Quoted), quoted(false)),
|
||||||
builtins:inst_member_or(Options, variable_names(VarNames), variable_names([])),
|
builtins:inst_member_or(Options, variable_names(VarNames), variable_names([])),
|
||||||
builtins:inst_member_or(Options, max_depth(MaxDepth), max_depth(0)),
|
builtins:inst_member_or(Options, max_depth(MaxDepth), max_depth(0)),
|
||||||
'$write_term_to_chars'(Term, IgnoreOps, NumberVars, Quoted, VarNames, MaxDepth, Chars).
|
extend_var_list(Term, VarNames, NewVarNames),
|
||||||
|
'$write_term_to_chars'(Term, IgnoreOps, NumberVars, Quoted, NewVarNames, MaxDepth, Chars).
|
||||||
|
|||||||
@@ -423,6 +423,16 @@ impl Machine {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
compile_user_module(&mut wam,
|
||||||
|
Stream::from(CHARSIO),
|
||||||
|
true,
|
||||||
|
ListingSource::from_file_and_path(
|
||||||
|
clause_name!("si"),
|
||||||
|
lib_path.clone(),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
if wam.compile_top_level().is_err() {
|
if wam.compile_top_level().is_err() {
|
||||||
panic!("Loading '$toplevel' module failed");
|
panic!("Loading '$toplevel' module failed");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
:- use_module(library(lists)).
|
|
||||||
:- use_module(library(si)).
|
|
||||||
|
|
||||||
:- module('$toplevel', ['$repl'/1, consult/1, use_module/1, use_module/2]).
|
:- module('$toplevel', ['$repl'/1, consult/1, use_module/1, use_module/2]).
|
||||||
|
|
||||||
|
:- use_module(library(charsio)).
|
||||||
|
:- use_module(library(lists)).
|
||||||
|
:- use_module(library(si)).
|
||||||
|
|
||||||
'$repl'([_|Args]) :-
|
'$repl'([_|Args]) :-
|
||||||
maplist('$use_list_of_modules', Args),
|
maplist('$use_list_of_modules', Args),
|
||||||
false.
|
false.
|
||||||
@@ -67,57 +69,14 @@
|
|||||||
memberchk(EqSpec, [fx,xfx,yfx])
|
memberchk(EqSpec, [fx,xfx,yfx])
|
||||||
).
|
).
|
||||||
|
|
||||||
'$fabricate_var_name'(VarName, N) :-
|
|
||||||
char_code('A', AC),
|
|
||||||
LN is N mod 26 + AC,
|
|
||||||
char_code(LC, LN),
|
|
||||||
NN is N // 26,
|
|
||||||
( NN =:= 0 ->
|
|
||||||
atom_chars(VarName, ['_', LC])
|
|
||||||
; number_chars(NN, NNChars),
|
|
||||||
atom_chars(VarName, ['_', LC | NNChars])
|
|
||||||
).
|
|
||||||
|
|
||||||
'$var_list_contains_name'([VarName = _ | VarList], VarName0) :-
|
|
||||||
( VarName == VarName0 -> true
|
|
||||||
; '$var_list_contains_name'(VarList, VarName0)
|
|
||||||
).
|
|
||||||
|
|
||||||
'$var_list_contains_variable'([_ = Var | VarList], Var0) :-
|
|
||||||
( Var == Var0 -> true
|
|
||||||
; '$var_list_contains_variable'(VarList, Var0)
|
|
||||||
).
|
|
||||||
|
|
||||||
'$make_new_var_name'(V, VarName, N, N1, VarList) :-
|
|
||||||
'$fabricate_var_name'(VarName0, N),
|
|
||||||
( '$var_list_contains_name'(VarList, VarName0) ->
|
|
||||||
N0 is N + 1,
|
|
||||||
'$make_new_var_name'(V, VarName, N0, N1, VarList)
|
|
||||||
; VarName = VarName0,
|
|
||||||
N1 is N + 1
|
|
||||||
).
|
|
||||||
|
|
||||||
'$extend_var_list'(Value, VarList, NewVarList) :-
|
|
||||||
term_variables(Value, Vars),
|
|
||||||
'$extend_var_list_'(Vars, 0, VarList, NewVarList).
|
|
||||||
|
|
||||||
'$extend_var_list_'([], N, VarList, VarList).
|
|
||||||
'$extend_var_list_'([V|Vs], N, VarList, NewVarList) :-
|
|
||||||
( '$var_list_contains_variable'(VarList, V) ->
|
|
||||||
'$extend_var_list_'(Vs, N, VarList, NewVarList)
|
|
||||||
; '$make_new_var_name'(V, VarName, N, N1, VarList),
|
|
||||||
NewVarList = [VarName = V | NewVarList0],
|
|
||||||
'$extend_var_list_'(Vs, N1, VarList, NewVarList0)
|
|
||||||
).
|
|
||||||
|
|
||||||
'$write_goal'(G, VarList, MaxDepth) :-
|
'$write_goal'(G, VarList, MaxDepth) :-
|
||||||
( G = (Var = Value) ->
|
( G = (Var = Value) ->
|
||||||
write(Var),
|
write(Var),
|
||||||
write(' = '),
|
write(' = '),
|
||||||
( '$needs_bracketing'(Value, (=)) ->
|
( '$needs_bracketing'(Value, (=)) ->
|
||||||
write('('),
|
write('('),
|
||||||
write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
||||||
write(')')
|
write(')')
|
||||||
; write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)])
|
; write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)])
|
||||||
)
|
)
|
||||||
; G == [] ->
|
; G == [] ->
|
||||||
@@ -130,14 +89,14 @@
|
|||||||
write(Var),
|
write(Var),
|
||||||
write(' = '),
|
write(' = '),
|
||||||
( '$needs_bracketing'(Value, (=)) ->
|
( '$needs_bracketing'(Value, (=)) ->
|
||||||
write('('),
|
write('('),
|
||||||
write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
||||||
write(')')
|
write(')')
|
||||||
; write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
; write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
||||||
( '$trailing_period_is_ambiguous'(Value) ->
|
( '$trailing_period_is_ambiguous'(Value) ->
|
||||||
write(' ')
|
write(' ')
|
||||||
; true
|
; true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
; G == [] ->
|
; G == [] ->
|
||||||
write('true')
|
write('true')
|
||||||
@@ -167,7 +126,7 @@
|
|||||||
'$graphic_token_char'(Char).
|
'$graphic_token_char'(Char).
|
||||||
|
|
||||||
'$write_eqs_and_read_input'(B, VarList) :-
|
'$write_eqs_and_read_input'(B, VarList) :-
|
||||||
'$extend_var_list'(VarList, VarList, NewVarList),
|
charsio:extend_var_list(VarList, VarList, NewVarList),
|
||||||
sort(NewVarList, SortedVarList),
|
sort(NewVarList, SortedVarList),
|
||||||
'$get_b_value'(B0),
|
'$get_b_value'(B0),
|
||||||
'$gather_goals'(SortedVarList, SortedVarList, Goals),
|
'$gather_goals'(SortedVarList, SortedVarList, Goals),
|
||||||
|
|||||||
Reference in New Issue
Block a user