35
36:- module(machine,
37 [ gc_heap/0,
38 trimcore/0,
39
40 abolish_table_info/0,
41 close_open_tables/1, 42
43 str_cat/3,
44
45 parsort/4, 46
47 term_type/2,
48
49 xsb_expand_file_name/2, 50 expand_filename_no_prepend/2, 51 parse_filename/4, 52
53 conset/2, 54 conget/2, 55
56 slash/1, 57
58 xsb_backtrace/1, 59 xwam_state/2 60 ]). 61:- use_module(library(debug)). 62:- use_module(library(error)). 63:- use_module(library(prolog_stack)).
69gc_heap :-
70 garbage_collect.
76trimcore :-
77 trim_stacks.
83abolish_table_info.
90close_open_tables(_).
94str_cat(A, B, AB) :-
95 must_be(atom, A),
96 must_be(atom, B),
97 atom_concat(A, B, AB).
103parsort(_List, Spec, _Dupl, _Sorted) :-
104 var(Spec),
105 !,
106 uninstantiation_error(Spec).
107parsort(_List, _Spec, Dupl, _Sorted) :-
108 var(Dupl),
109 !,
110 uninstantiation_error(Dupl).
111parsort(List, asc, 0, Sorted) :- !, sort(0, @=<, List, Sorted).
112parsort(List, asc, _, Sorted) :- !, sort(0, @<, List, Sorted).
113parsort(List, [], 0, Sorted) :- !, sort(0, @=<, List, Sorted).
114parsort(List, [], _, Sorted) :- !, sort(0, @<, List, Sorted).
115parsort(List, desc, 0, Sorted) :- !, sort(0, @>=, List, Sorted).
116parsort(List, desc, _, Sorted) :- !, sort(0, @>, List, Sorted).
117parsort(List, SortSpec, Dupl, Sorted) :-
118 must_be(list, SortSpec),
119 reverse(SortSpec, Rev),
120 parsort_(Rev, Dupl, List, Sorted).
121
122parsort_([], _, List, List).
123parsort_([H|T], Dupl, List0, List) :-
124 parsort_1(H, Dupl, List0, List1),
125 parsort_(T, Dupl, List1, List).
126
127parsort_1(asc(I), 0, List, Sorted) :- !, sort(I, @=<, List, Sorted).
128parsort_1(asc(I), _, List, Sorted) :- !, sort(I, @<, List, Sorted).
129parsort_1(desc(I), 0, List, Sorted) :- !, sort(I, @>=, List, Sorted).
130parsort_1(desc(I), _, List, Sorted) :- !, sort(I, @>, List, Sorted).
131parsort_1(Spec, _, _, _) :-
132 domain_error(parsort_spec, Spec).
138term_type(Term, Type) :-
139 ( atom(Term)
140 -> Type = 5
141 ; compound(Term)
142 -> ( Term = [_|_]
143 -> Type = 3
144 ; Type = 1
145 )
146 ; integer(Term)
147 -> Type = 2
148 ; float(Term)
149 -> Type = 6
150 ; var(Term)
151 -> Type = 0
152 ; assertion(fail)
153 ).
154
155
163xsb_expand_file_name(File, Expanded) :-
164 absolute_file_name(File, Expanded, [expand(true)]).
170expand_filename_no_prepend(File, Expanded) :-
171 expand_file_name(File, Absolute),
172 working_directory(Dir0, Dir0),
173 ensure_slash(Dir0, Dir),
174 ( atom_concat(Dir, Ex0, Absolute)
175 -> Expanded = Ex0
176 ; Expanded = Absolute
177 ).
183parse_filename(FileName, Dir, Base, Extension) :-
184 sub_atom(FileName, 0, _, _, '~'),
185 !,
186 expand_file_name(FileName, Absolute),
187 parse_filename_2(Absolute, Dir, Base, Extension).
188parse_filename(FileName, Dir, Base, Extension) :-
189 parse_filename_2(FileName, Dir, Base, Extension).
190
191parse_filename_2(FileName, Dir, Base, Extension) :-
192 file_directory_name(FileName, Dir0),
193 ( Dir0 == '.'
194 -> Dir = ''
195 ; ensure_slash(Dir0, Dir)
196 ),
197 file_base_name(FileName, File),
198 file_name_extension(Base, Extension, File).
199
200ensure_slash(Dir, DirS) :-
201 sub_atom(Dir, _, _, 0, '/'),
202 !,
203 DirS = Dir.
204ensure_slash(Dir, DirS) :-
205 atom_concat(Dir, '/', DirS).
214conset(Name, Value) :-
215 set_flag(Name, Value).
216
217conget(Name, Value) :-
218 get_flag(Name, Value).
224slash(Slash) :-
225 current_prolog_flag(dir_sep, Slash).
233xsb_backtrace(Backtrace) :-
234 get_prolog_backtrace(25, Backtrace).
240xwam_state(2, DelayReg) :-
241 !,
242 ( '$tbl_delay_list'([_|_])
243 -> DelayReg = 1
244 ; DelayReg = 0
245 ).
246xwam_state(Id, _Value) :-
247 domain_error(xwam_state, Id)