Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %
- % @(#) plcs.pl
- % A Translator from LLP/Prolog to CSharp
- % @language system SICStus Prolog 2.1
- %
- % @author Mutsunori Banbara
- % banbara@pascal.seg.kobe-u.ac.jp
- % Nara National College of Technology
- % @author Naoyuki Tamura
- % tamura@kobe-u.ac.jp
- % Faculty of Engineering, Kobe University
- % Modified into a Translator from LLP/Prolog to C#
- % by Jonathan Cook
- % jjc@dcs.ed.ac.uk
- % Division of Informatics, University of Edinburgh
- :- op(1200, fx, (forall)).
- :- op(1190, xfx, (\)).
- :- op(1170, xfx, (:-)).
- :- op(1170, xfx, (-->)).
- :- op(1170, fx, (:-)).
- :- op(1170, fx, (?-)).
- :- op(1150, fx, (resource)).
- :- op(1060, xfy, (&)).
- :- op( 950, xfy, (-<>)).
- :- op( 950, xfy, (=>)).
- :- op( 900, fy, (!)).
- :- op( 500, yfx, (#)).
- :- dynamic '$internal_clause'/2.
- :- dynamic '$internal_predicates'/2.
- :- dynamic '$dynamic_predicates'/2.
- :- dynamic '$resource_predicates'/2.
- :- dynamic '$fco_predicates'/2.
- :- dynamic '$file_name'/1.
- :- dynamic '$dummy_counter'/1.
- :- dynamic '$res_counter'/1.
- % version information
- '$version'('P# 0.1, on March 18 2002; Prolog Cafe 0.44, on November 12 1999').
- /*===============================================================
- Main
- ===============================================================*/
- plcs(File) :-
- '$list_to_string'([File,'.pl'], In),
- write('{translating '), write(In), write(' into C#...}'), nl,
- statistics(runtime, _),
- '$plcs_init'(File),
- '$read_in_program'(In),
- '$compile_all_predicates',
- statistics(runtime, [_,T]),
- write('{'), write(In), write(' translated, '),
- write(T), write(' msec}'), nl.
- '$plcs_init'(File) :-
- retractall('$file_name'(_)),
- retractall('$dummy_counter'(_)),
- retractall('$internal_clause'(_,_)),
- retractall('$internal_predicates'(_,_)),
- retractall('$dynamic_predicates'(_,_)),
- retractall('$resource_predicates'(_,_)),
- retractall('$fco_predicates'(_,_)),
- retractall('$res_counter'(_)),
- assert('$file_name'(File)),
- assert('$dummy_counter'(0)),
- assert('$res_counter'(0)).
- % Read in Program
- '$read_in_program'(File) :-
- open(File, read, In),
- repeat,
- read(In, X),
- '$assert_clause'(X),
- X == end_of_file,
- !,
- close(In).
- '$assert_clause'(end_of_file) :- !.
- '$assert_clause'((:- resource G)) :- !, %for LLP
- '$assert_resource_predicate'(G).
- '$assert_clause'((:- dynamic G)) :- !,
- '$assert_dynamic'(G).
- '$assert_clause'((:- mode _G)) :- !.
- '$assert_clause'((:- multifile _G)) :- !.
- '$assert_clause'((:- public _G)) :- !.
- '$assert_clause'((:- block _G)) :- !.
- '$assert_clause'((:- meta_predicate _G)) :- !.
- '$assert_clause'((:- G)) :- !, call(G).
- '$assert_clause'(Clause0) :-
- '$preprocess'(Clause0, Clause), % DCG, ;$B$N:o=|$J$I(B
- '$assert_cls'(Clause).
- '$assert_cls'((Head :- Body)) :- !,
- '$assert_predicate'(Head),
- assert('$internal_clause'(Head, Body)).
- '$assert_cls'(Head) :- !,
- '$assert_predicate'(Head),
- assert('$internal_clause'(Head, true)).
- '$assert_predicate'(Head) :-
- functor(Head, Functor, Arity),
- clause('$internal_predicates'(Functor, Arity), _),
- !.
- '$assert_predicate'(Head) :-
- functor(Head, Functor, Arity),
- assert('$internal_predicates'(Functor, Arity)).
- % DCG$B$NJQ49$H(B";"$B$N:o=|(B
- '$preprocess'(Clause0, Clause) :-
- expand_term(Clause0, Clause1),
- '$eliminate_disj'(Clause1, Clause, DummyClauses),
- '$assert_dummy_clauses'(DummyClauses).
- '$assert_dummy_clauses'([]) :- !.
- '$assert_dummy_clauses'([C|Cs]) :-
- '$assert_clause'(C),
- '$assert_dummy_clauses'(Cs).
- % resource declaration$B$N=hM}(B
- '$assert_resource_predicate'((G1,G2)) :- !,
- '$assert_resource_predicate'(G1),
- '$assert_resource_predicate'(G2).
- '$assert_resource_predicate'(G) :- G = F/A, !,
- functor(Head, F, A),
- '$assert_res'(F,A),
- '$assert_predicate'(Head).
- '$assert_resource_predicate'(G) :-
- '$message'([G,is,invalid,resource,declaration]),
- fail.
- '$assert_res'(F,A) :-
- clause('$resource_predicates'(F, A), _), !.
- '$assert_res'(F,A) :-
- assert('$resource_predicates'(F, A)).
- % dynamic declaration$B$N=hM}(B
- '$assert_dynamic'((G1,G2)) :- !,
- '$assert_dynamic'(G1),
- '$assert_dynamic'(G2).
- '$assert_dynamic'(G) :- G = F/A,!,
- functor(Head, F, A),
- '$assert_dynamic_predicate'(F/A),
- '$assert_cls'((Head :- clause(Head, Goal), call(Goal))).
- '$assert_dynamic'(G) :-
- '$message'([G,is,invalid,dynamic,declaration]),
- fail.
- '$assert_dynamic_predicate'(F/A) :-
- clause('$dynamic_predicates'(F, A), _),
- !.
- '$assert_dynamic_predicate'(F/A) :-
- assert('$dynamic_predicates'(F, A)).
- /*===============================================================
- $B%W%m%0%i%`Cf$N(B";"$B$r:o=|$9$k%k!<%A%s(B
- ===============================================================*/
- /*
- a :- b;c $B$O(B
- a :- b.
- a :- c.
- $B$N#2$D$N@a$KJQ49$5$l$k!#$^$?F1;~$K!"(B
- (C1 -> C2) $B$O(B ((C1,!,C2) ; fail) $B$K(B
- ((C1 -> C2) ; C3) $B$O(B ((C1,!,C2) ; C3) $B$K(B
- not(C) $B$O(B ((C,!,fail) ; true) $B$K(B
- \+(C) $B$O(B ((C,!,fail) ; true) $B$K(B
- $B$*$N$*$NJQ49$7$?8e!"(B";"$B$r:o=|$9$k!#(B
- $BCm0U(B
- ====
- $B%@%_!<@a$K(B";"$B$,4^$^$l$F$$$F$b!"(B'$assert_clause'/1$B$G$^$?:o=|$5$l$k(B
- $B$N$GLdBj$J$$(B
- */
- '$eliminate_disj'(Cl, NewCl, DummyCls) :-
- '$extract_disj'(Cl, NewCl, Disjs, []),
- '$treat_disj'(Disjs, DummyCls, []).
- '$extract_disj'(Cl, (H :- NewB)) --> {Cl = (H :- B)}, !,
- '$extract_disj'(B, NewB, Cl).
- '$extract_disj'(Cl, Cl) --> !.
- '$extract_disj'((G1&G2), (NewG1&NewG2), Cl) --> !,
- '$extract_disj'(G1, NewG1, Cl),
- '$extract_disj'(G2, NewG2, Cl).
- '$extract_disj'((G1,G2), (NewG1,NewG2), Cl) --> !,
- '$extract_disj'(G1, NewG1, Cl),
- '$extract_disj'(G2, NewG2, Cl).
- '$extract_disj'((R-<>G), (R-<>NewG), Cl) --> !,
- '$extract_disj'(G, NewG, Cl).
- '$extract_disj'((R =>G), (R =>NewG), Cl) --> !,
- '$extract_disj'(G, NewG, Cl).
- '$extract_disj'( !G, !NewG, Cl) --> !,
- '$extract_disj'(G, NewG, Cl).
- '$extract_disj'(G, NewG, Cl) --> {'$is_disj'(G, DisjG)}, !,
- {'$dummy_counter'(N)},
- [disj(DisjG, N, NewG, Cl)],
- {retract('$dummy_counter'(_))},
- {N1 is N+1},
- {assert('$dummy_counter'(N1))}.
- '$extract_disj'(G, G, _) --> !.
- '$is_disj'((C1->C2), ((C1,!,C2);fail)) :- !.
- '$is_disj'(((C1->C2);C3), ((C1,!,C2);C3)) :- !.
- '$is_disj'((C1;C2), (C1;C2)) :- !.
- '$is_disj'(not(C),((C,!,fail);true)) :- !.
- '$is_disj'(\+(C),((C,!,fail);true)).
- '$treat_disj'([]) --> !.
- '$treat_disj'([disj((A;B),N,X,C)|Disjs]) -->
- {'$variables'((A;B), Vars)},
- {'$variables'(C, CVars)},
- {'$intersect_vars'(Vars, CVars, Args)},
- {'$make_dummy_name'(N,Name)},
- {X =.. [Name|Args]},
- '$make_dummy_clauses'((A;B), X),
- '$treat_disj'(Disjs).
- '$intersect_vars'(V1,V2,Out) :-
- sort(V1,Sorted1),
- sort(V2,Sorted2),
- '$intersect_sorted_vars'(Sorted1,Sorted2,Out).
- '$intersect_sorted_vars'([],_,[]) :- !.
- '$intersect_sorted_vars'(_,[],[]).
- '$intersect_sorted_vars'([X|Xs],[Y|Ys],[X|Rs]) :- X == Y, !,
- '$intersect_sorted_vars'(Xs,Ys,Rs).
- '$intersect_sorted_vars'([X|Xs],[Y|Ys],Rs) :- X @< Y, !,
- '$intersect_sorted_vars'(Xs,[Y|Ys],Rs).
- '$intersect_sorted_vars'([X|Xs],[Y|Ys],Rs) :- X @> Y, !,
- '$intersect_sorted_vars'([X|Xs],Ys,Rs).
- '$make_dummy_name'(N,Name) :- integer(N), '$file_name'(File),!,
- '$list_to_string'(['$dummy_', File, '_', N], Name).
- '$make_dummy_clauses'((A;B), X) --> !,
- '$make_dummy_clauses'(A,X),
- '$make_dummy_clauses'(B,X).
- '$make_dummy_clauses'(A, X) -->
- {copy_term((X :- A), DummyCl)},
- [DummyCl].
- /*===============================================================
- $B%W%m%0%i%`$N%3%s%Q%$%k(B
- ===============================================================*/
- /*
- $BF1$8=R8lL>!"%"%j%F%#$r$b$D(BProlog$B$N@a$r%/%i%9$N=89g$K%H%i%s%9%l!<%H$9$k!#(B
- $B8=:_$N%H%i%s%9%l!<%HJ}<0$O(B
- Prolog(*.pl) --> WAM-like$B$JCf4V8@8l(B(*.asm) --> Java(*.cs)
- $B$G$"$k!#(B
- */
- '$compile_all_predicates' :-
- clause('$internal_predicates'(Functor, Arity), _),
- '$compile_predicate'(Functor, Arity, Instructions, []),
- '$create_class_name'(Functor/Arity, Class),
- /* $BCf4V%3!<%I(B */
- %'$list_to_string'([Class, '.asm'], AsmFile),
- %open(AsmFile, write, AsmOut),
- %'$write_intermediate'(AsmOut, Instructions), nl(AsmOut),
- %close(AsmOut),
- /* Java$B%3!<%I$N=PNO(B */
- '$create_filename'(Functor/Arity, Filename), % JJC
- '$list_to_string'([Filename, '.cs'], CsFile),
- open(CsFile, write, CsOut),
- '$generate_cs'(CsOut, Instructions), nl(CsOut),
- close(CsOut),
- fail.
- '$compile_all_predicates'.
- /*===============================================================
- $B%j%=!<%9$N%3%s%Q%$%k(B
- ===============================================================*/
- '$compile_resource'(H, B, Vs, FA) :-
- '$compile_res_pred'(FA, (H :- B), Vs, Instructions0, []),
- '$flatten_code'(Instructions0, Instructions, []),
- '$create_class_name'(FA, Class),
- /* $BCf4V%3!<%I$N=PNO(B */
- %'$list_to_string'([Class, '.asm'], AsmFile),
- %open(AsmFile, write, AsmOut),
- %'$write_intermediate'(AsmOut, Instructions), nl(AsmOut),
- %close(AsmOut),
- /* Java$B%3!<%I$N=PNO(B */
- '$create_filename'( FA, Filename ), %JJC
- '$list_to_string'([Filename, '.cs'], CsFile), %JJC
- open(CsFile, write, CsOut),
- '$generate_cs'(CsOut, Instructions), nl(CsOut),
- close(CsOut),
- !.
- '$compile_res_pred'(FA, ResCl, Vs) -->
- {FA = '$res'(F/A, _N)},
- {length(Vs, M)},
- {N1 is M + A},
- [comment(ResCl)],
- [FA: []],
- [StaticResPred],
- [StaticCode],
- [constructor(FA)],
- [exec_method(F/A, [AVar,PVar], res(N1), ExecCode)],
- [extra_methods(FA)],
- {'$compile_res_clause'(ResCl, Vs, StaticCode, LocalVars, ExecCode, [])},
- {'$gather_static_res_pred'(ExecCode, StaticResPred)},
- {LocalVars = [AVar, _, PVar]}.
- '$gather_static_res_pred'([], []) :- !.
- '$gather_static_res_pred'([put_closure(U,_,_)|Cs], [put_static_pred(U)|S]) :- !,
- '$gather_static_res_pred'(Cs, S).
- '$gather_static_res_pred'([_|Cs], S) :-
- '$gather_static_res_pred'(Cs, S).
- /*===============================================================
- $B%j%=!<%9@a$N%3%s%Q%$%k(B
- ===============================================================*/
- '$compile_res_clause'((Head0 :- Body0), Vs, Static, Local) -->
- {'$transform'((Head0 :- Body0), (Head :- Body))},
- {'$precompile_r'(Head, Body, Vs, Code)},
- '$compile_chunks'(Code, TermInfo),
- {'$compile_ground'(TermInfo, Local, Static, [])},
- !.
- '$compile_res_clause'(ResCl, _, _, _) -->
- {'$message'([compilation,of,resource, ResCl,failed])},
- {fail}.
- '$precompile_r'(Head, Body, Vs, Instrs) :-
- '$precompile_head_r'(Head, Vs, Instrs0, Bs),
- '$precompile_body'(Body, Bs, []),
- '$flatten_code'(Instrs0, Instrs, []).
- '$precompile_head_r'(Head, []) --> !,
- '$precompile_head'(Head).
- '$precompile_head_r'(Head, Vs) -->
- {Head =.. [_|Args]},
- {'$append'(Args, Vs, As)},
- '$precomp_head'(As, 1).
- /*===============================================================
- $B=R8l$N%3%s%Q%$%k(B
- ===============================================================*/
- '$compile_predicate'(Functor, Arity) -->
- {functor(Head, Functor, Arity)},
- {findall((Head :- Body), clause('$internal_clause'(Head, Body),_), Clauses)},
- %{'$compile_pred'(Clauses, Functor/Arity, Code, [])},
- {'$compile_pred'(Clauses, Functor, Arity, Code, [])},
- '$flatten_code'(Code),
- !.
- '$compile_pred'(Clauses, Functor, Arity) --> %$B%j%=!<%9@k8@$"$j(B
- {'$resource_predicates'(Functor, Arity)},
- !,
- '$compile_pred_with_res'(Clauses, Functor/Arity).
- '$compile_pred'(Clauses, Functor, Arity) --> %$B%j%=!<%9@k8@$J$7(B
- '$compile_pred'(Clauses, Functor/Arity).
- %$B%j%=!<%9@k8@$"$j$N>l9g(B
- '$compile_pred_with_res'([], FA) --> !,
- '$generate_root_with_res'([], FA).
- '$compile_pred_with_res'([Clause], FA) --> !,
- '$generate_root_with_res'([Clause], FA),
- '$compile_one_pred'([Clause], FA).
- '$compile_pred_with_res'(Clauses, FA) -->
- '$generate_root_with_res'(Clauses, FA),
- '$compile_pred0'(Clauses, FA, 1).
- %$B%j%=!<%9@k8@$J$7$N>l9g(B
- '$compile_pred'([], _) --> !.
- %'$compile_pred'([Clause], FA) --> !,
- % '$generate_root'([Clause], FA),
- % '$compile_one_pred'([Clause], FA).
- '$compile_pred'([Clause], FA) --> !,
- {copy_term(Clause, Clause1)},
- [comment(Clause1)],
- [FA: []],
- [StaticResPred],
- [StaticCode],
- [constructor(FA)],
- [exec_method(FA, [AVar,PVar], root_one_nores, ExecCode)],
- [extra_methods(FA)],
- % $B@a$N%3%s%Q%$%k(B
- {'$compile_clause'(Clause, StaticCode, LocalVars, ExecCode, [])},
- {'$gather_static_res_pred'(ExecCode, StaticResPred)},
- {LocalVars = [AVar, _, PVar]}.
- '$compile_pred'(Clauses, FA) -->
- %{write('start '), write(FA),nl},
- '$generate_switch_nores'(Clauses, FA),
- '$compile_pred0'(Clauses, FA, 1).
- %{write('end '), write(FA),nl}.
- %%%%%%%%%%%%%%%%
- %'$generate_switch_nores'([] , _, _, _) --> !.
- %'$generate_switch_nores'([_], _, _, _) --> !.
- '$generate_switch_nores'(Clauses, F/0) --> !,
- [F/0: []],
- [StaticPred],
- [constructor(F/0)],
- {'$select_type'(Clauses, var, 1, All)},
- '$generate_noswitch_nores'(F/0, All, Sub),
- {'$generate_static_pred'(F/0, All, Sub, StaticPred, [])}.
- '$generate_switch_nores'(Clauses, FA) -->
- [FA: []],
- [StaticPred],
- [constructor(FA)],
- {'$select_type'(Clauses, var, 1, All)},
- {LV = FA+var},
- {'$generate_sw'(Clauses, int, FA, All, LI, INT, [])},
- {'$generate_sw'(Clauses, con, FA, All, LC, CON, [])},
- {'$generate_sw'(Clauses, str, FA, All, LS, STR, [])},
- {'$generate_sw'(Clauses, lis, FA, All, LL, LIS, [])},
- '$gen_switch_on_term_nores'(FA, All, SWTPred, LV, LI, LC, LS, LL),
- {'$generate_static_pred'(FA, All, SWTPred, StaticPred, [])},
- [INT],
- [CON],
- [STR],
- [LIS].
- '$gen_switch_on_term_nores'(FA, All, Sub, L, L, L, L, L) --> !,
- '$generate_noswitch_nores'(FA, All, Sub).
- '$gen_switch_on_term_nores'(FA, All, [LV,LI,LC,LS,LL], LV, LI, LC, LS, LL) -->
- %[constructor(FA)],
- [exec_method(FA, [], root_nores, [switch_on_term(LV,LI,LC,LS,LL)])],
- [extra_methods(FA)],
- [LV : []],
- '$generate_tries'(All, LV).
- '$generate_noswitch_nores'(FA, All, [FA+sub/1]) --> !,
- {All = [N|Ns]},
- %[put_static_pred(FA+sub/1)],
- %[constructor(FA)],
- [exec_method(FA, [], root_nores, [try(FA+N, FA+sub/1)])],
- [extra_methods(FA)],
- [FA+sub/1 : []],
- '$generate_tries2'(Ns, FA, sub, 2).
- %%%%%%%%%%%%%%%%
- %'$generate_root'([], _) --> !.
- %'$generate_root'([_], FA) --> !,
- % [FA: []],
- % [put_static_pred(FA+top)],
- % [constructor(FA)],
- % [exec_method(FA, [], root, [jump(FA+top)])],
- % [extra_methods(FA)].
- %'$generate_root'(Clauses, FA) --> !,
- % [FA: []],
- % [put_static_pred(FA+top)],
- % [StaticPred],
- % [constructor(FA)],
- % [exec_method(FA, [], root, [jump(FA+top)])],
- % [extra_methods(FA)],
- % '$generate_switch'(Clauses, FA, All, SWTPred), %for indexing
- % {'$generate_static_pred'(FA, All, SWTPred, StaticPred, [])}.
- '$generate_root_with_res'([], FA) --> !,
- {FA = F/A, I is A+1},
- [FA: []],
- [put_static_con(F, A, functor)],
- [put_static_pred('$fail')],
- [put_static_pred(FA+res/0)],
- [put_static_pred(FA+res/1)],
- [put_static_pred(FA+res/2)],
- [put_static_pred(FA+res/3)],
- [constructor(FA)],
- [exec_method(FA, [], root, [look_up_hash(fail),
- pickup_resource(FA,I,fail),
- has_more_resource(FA+res/1),
- try_resource(FA+res/1, FA+res/0)])],
- [extra_methods(FA)],
- [FA+res/0: []],
- [exec_method(FA, [], try, [restore_resource,
- pickup_resource(FA,I,FA+res/2),
- has_more_resource(FA+res/3),
- retry_resource(FA+res/1, FA+res/0)])],
- [FA+res/1: []],
- [exec_method(FA, [], try, [consume_and_exec_closure(I)])],
- [FA+res/2: []],
- [exec_method(FA, [], try, [trust_resource('$fail')])],
- [FA+res/3: []],
- [exec_method(FA, [], try, [trust_resource(FA+res/1)])].
- '$generate_root_with_res'([_], FA) --> !,
- {FA = F/A, I is A+1},
- [FA: []],
- [put_static_con(F, A, functor)],
- [put_static_pred(FA+top)],
- [put_static_pred(FA+res/0)],
- [put_static_pred(FA+res/1)],
- [put_static_pred(FA+res/2)],
- [constructor(FA)],
- [exec_method(FA, [], root, [look_up_hash(FA+top),
- pickup_resource(FA,I,FA+top),
- try_resource(FA+res/1, FA+res/0)])],
- [extra_methods(FA)],
- [FA+res/0: []],
- [exec_method(FA, [], try, [restore_resource,
- pickup_resource(FA,I,FA+res/2),
- retry_resource(FA+res/1, FA+res/0)])],
- [FA+res/1: []],
- [exec_method(FA, [], try, [consume_and_exec_closure(I)])],
- [FA+res/2: []],
- [exec_method(FA, [], try, [trust_resource(FA+top)])].
- '$generate_root_with_res'(Clauses, FA) --> !,
- {FA = F/A, I is A+1},
- [FA: []],
- [put_static_con(F, A, functor)],
- [put_static_pred(FA+top)],
- [put_static_pred(FA+res/0)],
- [put_static_pred(FA+res/1)],
- [put_static_pred(FA+res/2)],
- [StaticPred],
- [constructor(FA)],
- [exec_method(FA, [], root, [look_up_hash(FA+top),
- pickup_resource(FA,I,FA+top),
- try_resource(FA+res/1, FA+res/0)])],
- [extra_methods(FA)],
- '$generate_switch'(Clauses, FA, All, SWTPred), %for indexing
- {'$generate_static_pred'(FA, All, SWTPred, StaticPred, [])},
- [FA+res/0: []],
- [exec_method(FA, [], try, [restore_resource,
- pickup_resource(FA,I,FA+res/2),
- retry_resource(FA+res/1, FA+res/0)])],
- [FA+res/1: []],
- [exec_method(FA, [], try, [consume_and_exec_closure(I)])],
- [FA+res/2: []],
- [exec_method(FA, [], try, [trust_resource(FA+top)])].
- % $B3F=R8l$N%3%s%Q%$%k(B
- '$compile_one_pred'([Clause], FA) -->
- {copy_term(Clause, Clause1)},
- [comment(Clause1)],
- [FA+top: []],
- [StaticResPred],
- [StaticCode],
- [exec_method(FA, [AVar,PVar], top, ExecCode)],
- % $B@a$N%3%s%Q%$%k(B
- {'$compile_clause'(Clause, StaticCode, LocalVars, ExecCode, [])},
- {'$gather_static_res_pred'(ExecCode, StaticResPred)},
- {LocalVars = [AVar, _, PVar]}.
- '$compile_pred0'([], _, _) --> !.
- '$compile_pred0'([Clause|Clauses], FA, N) -->
- {copy_term(Clause, Clause1)},
- [comment(Clause1)],
- [FA+N: []],
- [StaticResPred],
- [StaticCode],
- [exec_method(FA, [AVar,PVar], normal, ExecCode)],
- % $B@a$N%3%s%Q%$%k(B
- {'$compile_clause'(Clause, StaticCode, LocalVars, ExecCode, [])},
- {'$gather_static_res_pred'(ExecCode, StaticResPred)},
- {LocalVars = [AVar, _, PVar]},
- {M is N + 1},
- '$compile_pred0'(Clauses, FA, M).
- '$generate_static_pred'(FA, All, Ss)-->
- {'$normal_clauses'(FA, All, Ns)},
- {'$append'(Ss, Ns, Ps0)},
- {sort(Ps0, Ps)},
- '$gen_static_pred'(Ps).
- '$gen_static_pred'([]) --> !.
- '$gen_static_pred'([P|Ps]) -->
- [put_static_pred(P)],
- '$gen_static_pred'(Ps).
- '$normal_clauses'(_, [], []) :- !.
- '$normal_clauses'(FA, [N|Ns], [FA+N|Ps]) :-
- '$normal_clauses'(FA, Ns, Ps).
- /*---------------------------------------------------------------
- Indexing$B$N=hM}%k!<%A%s(B
- switch_on_term, try, retry, trust$BL?Na$r@8@.$9$k!#(B
- ---------------------------------------------------------------*/
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- '$generate_switch'([] , _, _, _) --> !.
- '$generate_switch'([_], _, _, _) --> !.
- '$generate_switch'(Clauses, F/0, All, []) --> !,
- [F/0+top: []],
- {'$select_type'(Clauses, var, 1, All)},
- '$generate_noswitch'(F/0, All).
- '$generate_switch'(Clauses, FA, All, SWTPred) -->
- [FA+top: []],
- {'$select_type'(Clauses, var, 1, All)},
- [SWT],
- {LV = FA+var},
- '$generate_sw'(Clauses, int, FA, All, LI),
- '$generate_sw'(Clauses, con, FA, All, LC),
- '$generate_sw'(Clauses, str, FA, All, LS),
- '$generate_sw'(Clauses, lis, FA, All, LL),
- {'$gen_switch_on_term'(FA, All, SWTPred, LV, LI, LC, LS, LL, SWT, [])}.
- '$gen_switch_on_term'(FA, All, [], L, L, L, L, L) --> !,
- '$generate_noswitch'(FA, All).
- '$gen_switch_on_term'(FA, All, [LV,LI,LC,LS,LL], LV, LI, LC, LS, LL) -->
- [exec_method(FA, [], top, [switch_on_term(LV,LI,LC,LS,LL)])],
- [LV : []],
- '$generate_tries'(All, LV).
- '$generate_noswitch'(FA, All) --> !,
- {All = [N|Ns]},
- [put_static_pred(FA+sub/1)],
- [exec_method(FA, [], top, [try(FA+N, FA+sub/1)])],
- [FA+sub/1 : []],
- '$generate_tries2'(Ns, FA, sub, 2).
- '$generate_sw'(Clauses, Type, FA, All, L) -->
- {'$select_type'(Clauses, Type, 1, Ns)},
- {Ns = All},
- !,
- {L = FA+var}.
- '$generate_sw'(Clauses, Type, FA, _, L) -->
- {'$select_type'(Clauses, Type, 1, Ns)},
- '$generate_sw1'(Ns, Type, FA, L).
- '$generate_sw1'([], _, _, '$fail') --> !.
- '$generate_sw1'([N], _, FA, FA+N) --> !.
- '$generate_sw1'(Ns, Type, FA, L) -->
- {L = FA+Type},
- [L: []],
- '$generate_tries'(Ns, L).
- '$generate_tries'(Ns, FA+Type) -->
- '$generate_tries'(Ns, FA, Type, 1).
- '$generate_tries'([N|Ns], FA, T, I) -->
- [put_static_pred(FA+T/I)],
- [exec_method(FA, [], try, [try(FA+N, FA+T/I)])],
- [FA+T/I : []],
- {J is I + 1},
- '$generate_tries2'(Ns, FA, T, J).
- '$generate_tries2'([N], FA, _, _) --> !,
- [exec_method(FA, [], trust, [trust(FA+N)])].
- '$generate_tries2'([N|Ns], FA, T, I) --> !,
- [put_static_pred(FA+T/I)],
- [exec_method(FA, [], retry, [retry(FA+N, FA+T/I)])],
- [FA+T/I : []],
- {J is I + 1},
- '$generate_tries2'(Ns, FA, T, J).
- '$select_type'([], _, _, []) :- !.
- '$select_type'([Clause|Clauses], Type, N, [N|Ns]) :-
- '$match_type'(Clause, Type),
- !,
- N1 is N + 1,
- '$select_type'(Clauses, Type, N1, Ns).
- '$select_type'([_|Clauses], Type, N, Ns) :-
- N1 is N + 1,
- '$select_type'(Clauses, Type, N1, Ns).
- '$match_type'(_, var) :- !.
- '$match_type'((Head :- _), Type) :-
- arg(1, Head, A1),
- '$match_type1'(A1, Type).
- '$match_type1'(A1, _) :- var(A1), !.
- '$match_type1'(A1, Type) :- integer(A1), !, Type=int.
- '$match_type1'(A1, Type) :- atom(A1), !, Type=con.
- '$match_type1'([_|_], Type) :- !, Type=lis.
- '$match_type1'(A1, Type) :- functor(A1, _, _), !, Type=str.
- /*===============================================================
- $B@a$N%3%s%Q%$%k(B
- ===============================================================*/
- '$compile_clause'((_Head :- Body), [], []) -->
- {nonvar(Body)}, {Body = '$CS'(CsCode)}, !,
- [cs_code(CsCode)].
- '$compile_clause'((Head0 :- Body0), StaticCode, VarInfo) -->
- {'$transform'((Head0 :- Body0), (Head :- Body))}, % Binary$B@a$KJQ49(B
- {'$precompile'(Head, Body, Code)}, %$BAH9~$_=R8l$J$I$N%W%l%3%s%Q%$%k(B
- '$compile_chunks'(Code, TermInfo), %put,get$B$N%3%s%Q%$%k(B
- {'$compile_ground'(TermInfo, VarInfo, StaticCode, [])},
- !.
- '$compile_clause'(Clause, _, _) -->
- {'$message'([compilation,of,Clause,failed])},
- {fail}.
- /*-------------------------------------------------------------------
- Prolog$B@a$+$i(BBinary$B@a$X$NJQ49%k!<%A%s(B
- For example, the well-known append/3 program:
- append([], Y, Y).
- append([X|Xs], Y, [X|Z]) :- append(Xs, Y, Z).
- is transformed to
- append([], Y, Y, Cont) :- true(Cont).
- append([X|Xs], Y, [X|Z], Cont) :- append(Xs, Y, Z, Cont).
- Binarization technique was developed by P.Tarau and M.Boyer,
- please see:
- * "Elementary Logic Programs"
- P.Tarau and M.Boyer
- Programming Language Implementation and Logic Programming,
- p.159--173, LNCS 456, Springer Verlag, 1990
- * "BinProlog 5.40 User Guide"
- P.Tarau
- Available from http://clement.info.umoncton.ca/BinProlog/
- -------------------------------------------------------------------*/
- '$transform'(Cl, BinCl) :- !, '$transform'(Cl, cont, BinCl).
- '$transform'((H :- B0), Cont, (H :- B)) :- !,
- '$transform_body'(B0, Cont, B).
- '$transform'(H, Cont, (H :- true(Cont))).
- '$transform_body'(B0, Cont, B) :-
- '$trans_body'(B0, Cut, B1, []),
- '$transform_body1'(Cut, B1, B2),
- '$binarize_body'(B2, Cont, B).
- '$transform_body1'(Cut, B, B) :- var(Cut), !.
- '$transform_body1'('$cut'(Level), ['$cut'(Level)|Bs], ['$neck_cut'|Bs]) :- !.
- '$transform_body1'('$cut'(Level), Bs, ['$get_level'(Level)|Bs]).
- '$trans_body'(G, Cut) --> {var(G)}, !,
- '$trans_body'(call(G), Cut).
- '$trans_body'(!, Cut) --> !,
- {Cut = '$cut'(Level)},
- ['$cut'(Level)].
- '$trans_body'(otherwise, _) --> !.
- '$trans_body'(true, _) --> !.
- '$trans_body'(fail, _) --> !, ['$fail'].
- '$trans_body'(false, _) --> !, ['$fail'].
- '$trans_body'(halt, _) --> !, ['$abort'].
- '$trans_body'(abort, _) --> !, ['$abort'].
- '$trans_body'(top, _) --> !, ['$top'].
- '$trans_body'(erase, _) --> !, ['$top'].
- '$trans_body'((G1,G2), Cut) --> !,
- '$trans_body'(G1, Cut),
- '$trans_body'(G2, Cut).
- % for LLP
- '$trans_body'((G1&G2), Cut) --> !,
- ['$begin_with'],
- '$trans_body'(G1, Cut),
- ['$mid_with'],
- '$trans_body'(G2, Cut),
- ['$end_with'].
- '$trans_body'((!R-<>G), Cut) --> !,
- '$trans_body'((R=>G), Cut).
- '$trans_body'((R-<>G), Cut) --> !,
- ['$begin_imp'(L1)],
- '$precompile_resource'(G, R, G1),
- ['$mid_imp'(L2)],
- '$trans_body'(G1, Cut),
- ['$end_imp'(L1, L2)].
- '$trans_body'((R=>G), Cut) --> !,
- ['$begin_imp'(L1)],
- '$precompile_exp_resource'(G, R, G1),
- ['$mid_exp_imp'(L2)],
- '$trans_body'(G1, Cut),
- ['$end_exp_imp'(L1, L2)].
- '$trans_body'(!G, Cut) --> !,
- ['$begin_bang'],
- '$trans_body'(G, Cut),
- ['$end_bang'].
- '$trans_body'(G, _) -->
- '$precompile_builtin'(G), !.
- '$trans_body'(G, _) -->
- [G].
- '$binarize_body'([], Cont, true(Cont)) :- !.
- '$binarize_body'([G], Cont, Body) :- !,
- '$add_cont'(G, Cont, Body).
- '$binarize_body'([G|Gs], Cont, Body) :-
- '$binarize_body'(Gs, Cont, Body0),
- '$add_cont'(G, Body0, Body).
- '$add_cont'(G, Cont, Binbody) :-
- G =.. [F|A0],
- '$append'(A0, [Cont], A),
- Binbody =.. [F|A].
- /*---------------------------------------------------------------
- $B%j%=!<%9$N%W%l%3%s%Q%$%k(B
- ---------------------------------------------------------------*/
- /* Linear$B%j%=!<%9(B */
- '$precompile_resource'(G, R, G) --> {var(G)}, !,
- '$precomp_resource'(R).
- '$precompile_resource'((R1-<>G1), R0, G) --> !,
- '$precompile_resource'(G1, (R0,R1), G).
- '$precompile_resource'(G, R, G) -->
- '$precomp_resource'(R).
- '$precomp_resource'(R) -->
- {'$decomp_resource'(R, R1)},
- '$precomp_res'(R1).
- '$precomp_res'((R1,R2)) --> !,
- '$precomp_res'(R1),
- ['$more_imp'],
- '$precomp_res'(R2).
- '$precomp_res'((R1&R2)) --> !,
- '$precomp_res'(R1),
- '$precomp_res'(R2).
- '$precomp_res'(resource(H, B, Vs)) -->
- {'$inc_res_counter'(N)},
- {functor(H,F,A), I = '$res'(F/A,N)},
- {copy_term([H,B,Vs,I], [H1,B1,Vs1,I1])},
- {'$compile_resource'(H1,B1, Vs1, I1)},
- ['$add_res'(H, '$closure'(I, Vs))].
- /* Exponential$B%j%=!<%9(B */
- '$precompile_exp_resource'(G, R, G) --> {var(G)}, !,
- '$precomp_exp_resource'(R).
- '$precompile_exp_resource'((R1=>G1), R0, G) --> !,
- '$precompile_exp_resource'(G1, (R0,R1), G).
- '$precompile_exp_resource'((!R1-<>G1), R0, G) --> !,
- '$precompile_exp_resource'(G1, (R0,R1), G).
- '$precompile_exp_resource'(G, R, G) -->
- '$precomp_exp_resource'(R).
- '$precomp_exp_resource'(R) -->
- {'$decomp_resource'(R, R1)},
- '$precomp_exp_res'(R1).
- '$precomp_exp_res'((R1,R2)) --> !,
- '$precomp_exp_res'(R1),
- '$precomp_exp_res'(R2).
- '$precomp_exp_res'((R1&R2)) --> !,
- '$precomp_exp_res'(R1),
- '$precomp_exp_res'(R2).
- '$precomp_exp_res'(resource(H, B, Vs)) -->
- {'$inc_res_counter'(N)},
- {functor(H,F,A), I = '$res'(F/A,N)},
- {copy_term([H,B,Vs,I], [H1,B1,Vs1,I1])},
- {'$compile_resource'(H1,B1, Vs1, I1)},
- ['$add_exp_res'(H, '$closure'(I, Vs))].
- '$inc_res_counter'(N) :-
- retract('$res_counter'(N)),
- N1 is N+1,
- assert('$res_counter'(N1)).
- /* $B%j%=!<%9$r%W%j%_%F%#%V%j%=!<%9$KJ,2r(B */
- '$decomp_resource'(R, R1) :- '$decomp_R'(R, R1).
- '$decomp_R'(R, _) :- var(R), !,
- '$message'([resource,R,should,not,be,a,variable]),
- fail.
- '$decomp_R'((R1,R2), (RR1,RR2)) :- !,
- '$decomp_R'(R1, RR1),
- '$decomp_R'(R2, RR2).
- '$decomp_R'(R, RR) :-
- '$decomp_S'(R, RR).
- '$decomp_S'(R, _) :- var(R), !,
- '$message'([resource,R,should,not,be,a,variable]),
- fail.
- '$decomp_S'((R1&R2), (RR1&RR2)) :- !,
- '$decomp_S'(R1, RR1),
- '$decomp_S'(R2, RR2).
- '$decomp_S'((forall Xs\R1&R2), RR) :- !,
- '$decomp_S'(((forall Xs\R1)&(forall Xs\R2)), RR).
- '$decomp_S'((B -<> (forall Xs\R)), RR) :- !,
- '$decomp_S'((forall Xs\ B-<>R), RR).
- '$decomp_S'((B => (forall Xs\R)), RR) :- !,
- '$decomp_S'((forall Xs\ !B-<>R), RR).
- '$decomp_S'((B1 -<> B2 -<> H), RR) :- !,
- '$decomp_S'(((B1,B2) -<> H), RR).
- '$decomp_S'((B1 => B2 -<> H), RR) :- !,
- '$decomp_S'(((!B1,B2) -<> H), RR).
- '$decomp_S'((B1 -<> B2 => H), RR) :- !,
- '$decomp_S'(((B1,!B2) -<> H), RR).
- '$decomp_S'((B1 => B2 => H), RR) :- !,
- '$decomp_S'(((!B1,!B2) -<> H), RR).
- '$decomp_S'((B -<> (H1&H2)), RR) :- !,
- '$decomp_S'(((B -<> H1)&(B -<> H2)), RR).
- '$decomp_S'((B => (H1&H2)), RR) :- !,
- '$decomp_S'(((!B -<> H1)&(!B -<> H2)), RR).
- '$decomp_S'((forall Xs\B -<> H), RR) :- '$resource_head'(H), !,
- '$free_variables'([B,H], Vs),
- '$forall_vars'(Xs, FVs, []),
- '$delete_forall_vars'(Vs, FVs, Vs1),
- RR = resource(H, B, Vs1).
- '$decomp_S'((B -<> H), RR) :- '$resource_head'(H), !,
- '$free_variables'([B,H], Vs),
- RR = resource(H, B, Vs).
- '$decomp_S'((forall Xs\B => H), RR) :- '$resource_head'(H), !,
- '$free_variables'([B,H], Vs),
- '$forall_vars'(Xs, FVs, []),
- '$delete_forall_vars'(Vs, FVs, Vs1),
- RR = resource(H, !B, Vs1).
- '$decomp_S'((B => H), RR) :- '$resource_head'(H), !,
- '$free_variables'([B,H], Vs),
- RR = resource(H, !B, Vs).
- '$decomp_S'((forall Xs\H), RR) :- '$resource_head'(H), !,
- '$free_variables'(H, Vs),
- '$forall_vars'(Xs, FVs, []),
- '$delete_forall_vars'(Vs, FVs, Vs1),
- RR = resource(H, true, Vs1).
- '$decomp_S'(H, RR) :- '$resource_head'(H), !,
- '$free_variables'(H, Vs),
- RR = resource(H, true, Vs).
- '$decomp_S'(R, _) :-
- '$message'([resource,R,is,invalid]),
- fail.
- '$resource_head'(H) :-
- \+ (H = !(_)),
- \+ (H = (_ , _)),
- \+ (H = (_ & _)),
- \+ (H = (_ -<> _)),
- \+ (H = (forall _)).
- '$free_variables'(X, Vs) :-
- '$variables'(X, Vs).
- '$forall_vars'(X) --> {nonvar(X), X = (X1,X2)},!,
- {var(X1)}, [X1], '$forall_vars'(X2).
- '$forall_vars'(X) --> {var(X)}, [X], !.
- '$forall_vars'(X) --> !,
- {'$message'([free,variable,X,is,invalid]), fail}.
- '$delete_forall_vars'([X|Xs], FV, Ys) :- '$memq'(X, FV), !,
- '$delete_forall_vars'(Xs, FV, Ys).
- '$delete_forall_vars'([X|Xs], FV, [X|Ys]) :- !,
- '$delete_forall_vars'(Xs, FV, Ys).
- '$delete_forall_vars'([], _, []).
- %'$precomp_put'(X, X) --> {var(X)}, !.
- %'$precomp_put'(X, U) -->
- % [put(X, U)].
- /*---------------------------------------------------------------
- Head$B$H(BBody$B$N%W%l%3%s%Q%$%k(B
- ---------------------------------------------------------------*/
- '$precompile'(Head, Body0, Instrs) :-
- '$precompile_head'(Head, Instrs0, Bs),
- '$first_call_opt'(Head, Body0, Body),
- '$precompile_body'(Body, Bs, []),
- '$flatten_code'(Instrs0, Instrs, []).
- '$precompile_head'(Head) -->
- {Head =.. [_|Args]},
- '$precomp_head'(Args, 1).
- '$precomp_head'([], _) --> !.
- '$precomp_head'([A|As], I) -->
- [get(A, a(I))],
- {I1 is I + 1},
- '$precomp_head'(As, I1).
- '$first_call_opt'(true, Body, Body) :- !.
- '$first_call_opt'(Head, Body, NewBody) :-
- Head =..[F1|As1],
- Body =..[F2|As2],
- length(As1, L1),
- length(As2, L2),
- F1 == F2, L1 =:= L2-1,
- !,
- assert('$fco_predicates'(F1, L1)),
- NewBody =.. ['$FCO'|As2].
- '$first_call_opt'(_, Body, Body).
- '$precompile_body'(true(cont)) -->!,
- [execute(cont)].
- '$precompile_body'(G) -->
- {G =.. [F|Args]},
- '$precomp_call'(F, Args, Vs),
- {P =.. [F|Vs]},
- [execute(P)].
- '$precomp_call'(_, [G], [V]) --> !,
- '$precomp_cont'(G, V).
- '$precomp_call'(F, [A|As], [V|Vs]) -->
- '$precomp_put'(F, A, V),
- %[put(A, V)],
- '$precomp_call'(F, As, Vs).
- '$precomp_cont'(cont, V) --> !, {cont = V}.
- '$precomp_cont'(G, V) -->
- {G =.. [F|Args]},
- '$precomp_call'(F, Args, Vs),
- {P =.. [F|Vs]},
- [put_cont(P, V)].
- '$precomp_put'(F, X, V) -->
- {(F == '$add_res'; F == '$add_exp_res')},
- {nonvar(X)},
- {X = '$closure'(Res, Vs)},
- !,
- [put_free_vars(Vs, W)],
- [put_closure(Res, W, V)].
- '$precomp_put'(_, X, V) --> [put(X, V)].
- %'$precomp_cont'(cont, V) --> !, {cont = V}.
- %'$precomp_cont'(G, V) -->
- % {G =.. [F|Args]},
- % '$precomp_call'(F, Args, Vs),
- % {P =.. [F|Vs]},
- % [put_cont(P, V)].
- /*---------------------------------------------------------------
- $BAH$_9~$_=R8l$N%W%l%3%s%Q%$%k(B
- ---------------------------------------------------------------*/
- '$precompile_builtin'(X == Y) --> !, ['$equality_of_term'(X, Y)].
- '$precompile_builtin'(X \== Y) --> !, ['$inequality_of_term'(X, Y)].
- '$precompile_builtin'(X = Y) --> !, ['$unify'(X, Y)].
- '$precompile_builtin'(?=(X, Y)) --> !, ['$identical_or_cannot_unify'(X, Y)].
- '$precompile_builtin'(X @< Y) --> !, ['$before'(X, Y)].
- '$precompile_builtin'(X @> Y) --> !, ['$after'(X, Y)].
- '$precompile_builtin'(X @=< Y) --> !, ['$not_after'(X, Y)].
- '$precompile_builtin'(X @>= Y) --> !, ['$not_before'(X, Y)].
- '$precompile_builtin'(compare(X,Y,Z)) --> !, ['$compare'(X,Y,Z)].
- '$precompile_builtin'(functor(X,Y,Z)) --> !, ['$functor'(X,Y,Z)].
- '$precompile_builtin'(arg(X,Y,Z)) --> !, ['$arg'(X,Y,Z)].
- '$precompile_builtin'(var(X)) --> !, ['$var'(X)].
- '$precompile_builtin'(nonvar(X)) --> !, ['$nonvar'(X)].
- '$precompile_builtin'(atom(X)) --> !, ['$atom'(X)].
- '$precompile_builtin'(integer(X)) --> !, ['$integer'(X)].
- '$precompile_builtin'(atomic(X)) --> !, ['$atomic'(X)].
- '$precompile_builtin'(copy_term(X,Y)) --> !, ['$copy_term'(X,Y)].
- % JJC changed java_ to cs_ and added load_assembly_1
- '$precompile_builtin'(load_assembly(X)) --> !, ['$load_assembly'(X)].
- '$precompile_builtin'(cs_object(X)) --> !, ['$cs_object'(X)].
- '$precompile_builtin'(cs_constructor(X,Y)) --> !, ['$cs_constructor'(X,Y)].
- '$precompile_builtin'(cs_method(X,Y,Z)) --> !, ['$cs_method'(X,Y,Z)].
- '$precompile_builtin'(cs_get_field(X,Y,Z)) --> !, ['$cs_get_field'(X,Y,Z)].
- '$precompile_builtin'(cs_set_field(X,Y,Z)) --> !, ['$cs_set_field'(X,Y,Z)].
- '$precompile_builtin'(cs_term(X,Y)) --> !, ['$cs_term'(X,Y)].
- '$precompile_builtin'(url_source(X,Y)) --> !, ['$url_source'(X,Y)].
- %$B$3$l$I$&$9$k!)(B
- '$precompile_builtin'(call(X)) --> !, ['$call'(X)].
- '$precompile_builtin'(open_table(X)) --> !, ['$open_table'(X)].
- '$precompile_builtin'(clear_table(X)) --> !, ['$clear_table'(X)].
- '$precompile_builtin'(close_table(X)) --> !, ['$close_table'(X)].
- '$precompile_builtin'(set_table(X)) --> !, ['$set_table'(X)].
- '$precompile_builtin'(current_table(X)) --> !, ['$current_table'(X)].
- '$precompile_builtin'(current_input(X)) --> !, ['$current_input'(X)].
- '$precompile_builtin'(current_output(X)) --> !, ['$current_output'(X)].
- '$precompile_builtin'(set_input(X)) --> !, ['$set_input'(X)].
- '$precompile_builtin'(set_output(X)) --> !, ['$set_output'(X)].
- '$precompile_builtin'(close(X)) --> !, ['$close'(X)].
- '$precompile_builtin'(flush_output(X)) --> !, ['$flush_output'(X)].
- '$precompile_builtin'(flush_output) --> !, ['$flush_output'].
- '$precompile_builtin'(get0(X)) --> !, ['$get0'(X)].
- '$precompile_builtin'(get0(S,X)) --> !, ['$get0'(S,X)].
- '$precompile_builtin'(get(X)) --> !, ['$get'(X)].
- '$precompile_builtin'(get(S,X)) --> !, ['$get'(S,X)].
- '$precompile_builtin'(put(X)) --> !, ['$put'(X)].
- '$precompile_builtin'(put(S,X)) --> !, ['$put'(S,X)].
- '$precompile_builtin'(tab(X)) --> !, ['$tab'(X)].
- '$precompile_builtin'(tab(S,X)) --> !, ['$tab'(S,X)].
- '$precompile_builtin'(nl) --> !, ['$nl'].
- '$precompile_builtin'(nl(S)) --> !, ['$nl'(S)].
- %'$precompile_builtin'(X =.. Y) --> !, ['$univ'(X, Y)].
- '$precompile_builtin'(X =:= Y) --> !,
- '$precompile_is'(U, X),
- '$precompile_is'(V, Y),
- ['$arith_equal'(U, V)].
- '$precompile_builtin'(X =\= Y) --> !,
- '$precompile_is'(U, X),
- '$precompile_is'(V, Y),
- ['$arith_not_equal'(U, V)].
- '$precompile_builtin'(X > Y) --> !,
- '$precompile_is'(U, X),
- '$precompile_is'(V, Y),
- ['$greater_than'(U, V)].
- '$precompile_builtin'(X >= Y) --> !,
- '$precompile_is'(U, X),
- '$precompile_is'(V, Y),
- ['$greater_or_equal'(U, V)].
- '$precompile_builtin'(X < Y) --> !,
- '$precompile_is'(U, X),
- '$precompile_is'(V, Y),
- ['$less_than'(U, V)].
- '$precompile_builtin'(X =< Y) --> !,
- '$precompile_is'(U, X),
- '$precompile_is'(V, Y),
- ['$less_or_equal'(U, V)].
- '$precompile_builtin'(X is Y) --> !,
- '$generate_is'(Y, X).
- /* $B;;=Q1i;;$N%W%l%3%s%Q%$%k(B */
- '$precompile_is'(X, X) --> {var(X)}, !.
- '$precompile_is'(X, Y) --> '$generate_is'(Y, X).
- '$generate_is'(X, A) --> {var(X)}, !, ['$is'(A, X)].
- '$generate_is'(X, _) --> {float(X)}, !,
- {'$message'([floating,point,numbers,are,not,supported])},
- {fail}.
- '$generate_is'(X, A) --> {integer(X)}, !, {X = A}.
- '$generate_is'([X], A) --> !, '$gen_is'(X, A).
- '$generate_is'(+(X), A) --> !, '$gen_is'(X, A).
- '$generate_is'(-(X), A) --> !, '$generate_is'(-1*X, A).
- '$generate_is'(\(X), A) --> !,
- '$gen_is'(X, U),
- ['$bitwise_neg'(U, A)].
- '$generate_is'(X+Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$plus'(U, V, A)].
- '$generate_is'(X-Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$minus'(U, V, A)].
- '$generate_is'(X*Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$multi'(U, V, A)].
- '$generate_is'(X/Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$float_quotient'(U, V, A)].
- '$generate_is'(X//Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$int_quotient'(U, V, A)].
- '$generate_is'(X mod Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$remainder'(U, V, A)].
- '$generate_is'(X<<Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$leftshift'(U, V, A)].
- '$generate_is'(X>>Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$rightshift'(U, V, A)].
- '$generate_is'(X/\Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$bitwise_conj'(U, V, A)].
- '$generate_is'(X\/Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$bitwise_disj'(U, V, A)].
- '$generate_is'(X#Y, A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$bitwise_exclusive_or'(U, V, A)].
- '$generate_is'(integer(X), A) --> !,
- '$gen_is'(X, U),
- ['$to_integer'(U, A)].
- %'$generate_is'(float(X), A) --> !, '$gen_is'(X, U),
- % ['$to_float'(U, A)].
- '$generate_is'(abs(X), A) --> !,
- '$gen_is'(X, U),
- ['$abs'(U, A)].
- '$generate_is'(max(X,Y), A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$max'(U, V, A)].
- '$generate_is'(min(X,Y), A) --> !,
- '$gen_is'(X, U),
- '$gen_is'(Y, V),
- ['$min'(U, V, A)].
- '$generate_is'(X, _) -->
- {'$message'([unknown,arithemetic,expression,X])},
- {fail}.
- '$gen_is'(X, A) --> {var(X)}, {var(A)}, !, {X = A}.
- '$gen_is'(X, A) --> '$generate_is'(X, A).
- /*---------------------------------------------------------------
- $B%3%s%Q%$%k(B
- ---------------------------------------------------------------*/
- '$compile_chunks'(Chunk, TermInfo) -->
- {'$alloc_voids'(Chunk, [], Alloc)}, /* void$BJQ?t$N%A%'%C%/(B */
- '$compile_chunk'(Chunk, Alloc, TermInfo).
- '$compile_chunk'([], _, []) --> !.
- '$compile_chunk'(Chunk, Alloc, TermInfo) -->
- {'$free_x_reg'(Chunk, 1, XN), SN = 1, PN = 1},
- {TermInfo0 = [XN, SN, PN, Alloc]},
- '$comp_chunk'(Chunk, TermInfo0, TermInfo).
- '$comp_chunk'([], TI, TI) --> !.
- '$comp_chunk'([(L:[])|Cs], TI0, TI) --> !,
- [L:[]],
- '$comp_chunk'(Cs, TI0, TI).
- '$comp_chunk'([(L:C)|Cs], TI0, TI) --> !,
- [L:[]],
- '$comp_chunk'([C|Cs], TI0, TI).
- '$comp_chunk'([C|Cs], TI0, TI) --> !,
- '$comp_instr'(C, TI0, TI1), /* get, put$BL?Na$N@8@.(B */
- '$comp_chunk'(Cs, TI1, TI).
- /* $B;HMQ2DG=$J(Ba_register$B$N@hF,HV9f$rC5$9(B */
- '$free_x_reg'([], XN, XN).
- '$free_x_reg'([get(_,V)|Cs], XN0, XN) :- nonvar(V), V = a(N), !,
- '$mymax'(N+1, XN0, XN1),
- '$free_x_reg'(Cs, XN1, XN).
- '$free_x_reg'([put(_,V)|Cs], XN0, XN) :- nonvar(V), V = a(N), !,
- '$mymax'(N+1, XN0, XN1),
- '$free_x_reg'(Cs, XN1, XN).
- '$free_x_reg'([_|Cs], XN0, XN) :-
- '$free_x_reg'(Cs, XN0, XN).
- /* void$BJQ?t$N%A%'%C%/(B */
- '$alloc_voids'(Chunks, Alloc0, Alloc) :-
- '$variables'(Chunks, Vars),
- '$alloc_voids1'(Vars, Chunks, Alloc0, Alloc).
- '$alloc_voids1'([], _, Alloc, Alloc).
- '$alloc_voids1'([V|Vars], Chunks, Alloc0, Alloc) :-
- '$count_variable'(V, Chunks, 1),
- !,
- Alloc1 = [[V,void,_Seen]|Alloc0],
- '$alloc_voids1'(Vars, Chunks, Alloc1, Alloc).
- '$alloc_voids1'([_|Vars], Chunks, Alloc0, Alloc) :-
- '$alloc_voids1'(Vars, Chunks, Alloc0, Alloc).
- %alloc_var([], _, Alloc, Alloc).
- '$alloc_voids1'([V|Vars], Chunks, Alloc0, Alloc) :-
- '$count_variable'(V, Chunks, 1),
- !,
- Alloc1 = [[V,void,_Seen]|Alloc0],
- '$alloc_voids1'(Vars, Chunks, Alloc1, Alloc).
- '$alloc_voids1'([_|Vars], Chunks, Alloc0, Alloc) :-
- '$alloc_voids1'(Vars, Chunks, Alloc0, Alloc).
- /*---------------------------------------------------------------
- get, put$BEy$N%3%s%Q%$%k(B
- ---------------------------------------------------------------*/
- /*
- comp_instr(+Instr, +TermInfo0, -TermInfo)
- Instr : Intermediate instruction
- TermInfo : [X, Y, Z, Alloc]
- X : a(X),a(X+1),... are available (for term)
- Y : s(Y),s(Y+1),... are available (for ground term)
- Z : p(Z),p(Z+1),... are available (for cont. predicate)
- Alloc : [[Term,Type,Seen],...]
- */
- '$comp_instr'(get(X, A), TermInfo0, TermInfo) --> !,
- '$gen_get'(X, A, TermInfo0, TermInfo).
- '$comp_instr'(put(X, V), TermInfo0, TermInfo) --> !,
- '$gen_put'(X, V, TermInfo0, TermInfo).
- '$comp_instr'(put_cont(X, V), TermInfo0, TermInfo) --> !,
- '$gen_put_cont'(X, V, TermInfo0, TermInfo).
- '$comp_instr'(put_free_vars(X, V), TermInfo0, TermInfo) --> !,
- '$gen_put_free_vars'(X, V, TermInfo0, TermInfo).
- '$comp_instr'(put_closure(X,Y,V), TermInfo0, TermInfo) --> !,
- '$gen_put_closure'(X, Y, V, TermInfo0, TermInfo).
- '$comp_instr'(Instr, TermInfo, TermInfo) --> [Instr].
- /*---------------------------------------------------------------
- get, unify$BL?Na$N@8@.(B
- ---------------------------------------------------------------*/
- '$gen_get'(X, A, TI0, TI) -->
- '$gen_get'([A=X], TI0, TI).
- '$gen_get'([], TI, TI) --> !.
- '$gen_get'([A=X|_], TI, TI) --> {var(A)}, !,
- {'$message'([get(X,A),error]), fail}.
- '$gen_get'([A=X|Instrs], TI0, TI) --> {var(X)},
- {'$assign_reg'(X, R, Seen, TI0, TI1)},
- {nonvar(Seen)}, !,
- '$gen_get_var'(R, Seen, A),
- '$gen_get'(Instrs, TI1, TI).
- /* $B$3$l$G$$$$$G$N$+$J!)(B */
- '$gen_get'([A=X|Instrs], TI0, TI) --> {var(X)}, !,
- {TI0 = [XN,SN,PN,Alloc0]},
- {TI1 = [XN,SN,PN,[[X,A,Seen]|Alloc0]]},
- {Seen = yes},
- '$gen_get'(Instrs, TI1, TI).
- '$gen_get'([_=X|_], _, _) --> {float(X)}, !,
- {'$message'([floating,point,numbers,are,not,supported])}, {fail}.
- '$gen_get'([A=X|Instrs], TI0, TI) --> {integer(X)}, !,
- {'$assign_reg'(X, R, Seen, TI0, TI1)},
- {Seen = yes},
- [get_int(R, A)],
- '$gen_get'(Instrs, TI1, TI).
- '$gen_get'([A=X|Instrs], TI0, TI) --> {atom(X)}, !,
- {'$assign_reg'(X, R, Seen, TI0, TI1)},
- {Seen = yes},
- [get_con(R, A)],
- '$gen_get'(Instrs, TI1, TI).
- /* ground$B9`$O(Bstatic term$B$H$7$F8e$G=hM}(B */
- '$gen_get'([A=X|Instrs], TI0, TI) --> {ground(X), X = [X1|X2]}, !,
- '$gen_put_args'([X1,X2], _, TI0, TI1),
- {'$assign_reg'(X, R, Seen, TI1, TI2)},
- {Seen = yes},
- [get_ground(R, A)],
- '$gen_get'(Instrs, TI2, TI).
- /* ground$B9`$O(Bstatic term$B$H$7$F8e$G=hM}(B */
- '$gen_get'([A=X|Instrs], TI0, TI) -->
- {ground(X), X =..[F|Args], functor(X,_,Arity)},
- !,
- {'$assign_functor'('$f_n'(F,Arity), _, TI0, TI00)},
- '$gen_put_args'(Args, _, TI00, TI1),
- {'$assign_reg'(X, R, Seen, TI1, TI2)},
- {Seen = yes},
- [get_ground(R, A)],
- '$gen_get'(Instrs, TI2, TI).
- '$gen_get'([A=X|Instrs], TI0, TI) --> {X = [X1|X2]}, !,
- [get_list(A, UnifyCode)],
- {'$gen_unify'([X1,X2], Instrs1, TI0, TI1, UnifyCode, [])},
- '$gen_get'(Instrs1, TI1, TI2),
- '$gen_get'(Instrs, TI2, TI).
- '$gen_get'([A=X|Instrs], TI0, TI) -->
- {X =.. [F|Args], functor(X,_,Arity)},
- {'$assign_functor'('$f_n'(F,Arity), R, TI0, TI1)},
- [get_str(R, A, UnifyCode)],
- {'$gen_unify'(Args, Instrs1, TI1, TI2, UnifyCode, [])},
- '$gen_get'(Instrs1, TI2, TI3),
- '$gen_get'(Instrs, TI3, TI).
- '$gen_get_var'(void, _, _) --> !. /* void$BJQ?t$N>l9g$OL5;k(B */
- %'$gen_get_var'(R, Seen, A) --> {var(Seen)}, !,
- % {Seen = yes},
- % [get_var(R, A)].
- '$gen_get_var'(R, _, A) --> [get_val(R, A)].
- '$gen_unify'([], [], TI, TI) --> !.
- '$gen_unify'([X|Xs], Instrs, TI0, TI) --> {var(X)}, !,
- {'$assign_reg'(X, R, Seen, TI0, TI1)},
- '$gen_unify_var'(R, Seen),
- '$gen_unify'(Xs, Instrs, TI1, TI).
- '$gen_unify'([X|_], _, _, _) --> {float(X)}, !,
- {'$message'([floating,point,numbers,are,not,supported])},{fail}.
- '$gen_unify'([X|Xs], Instrs, TI0, TI) --> {integer(X)}, !,
- {'$assign_reg'(X, R, Seen, TI0, TI1)},
- {Seen = yes},
- [unify_int(R)],
- '$gen_unify'(Xs, Instrs, TI1, TI).
- '$gen_unify'([X|Xs], Instrs, TI0, TI) --> {atom(X)}, !,
- {'$assign_reg'(X, R, Seen, TI0, TI1)},
- {Seen = yes},
- [unify_con(R)],
- '$gen_unify'(Xs, Instrs, TI1, TI).
- '$gen_unify'([X|Xs], Instrs, TI0, TI) --> {ground(X), X = [X1|X2]}, !,
- '$gen_put_args'([X1,X2], _, TI0, TI1),
- {'$assign_reg'(X, R, Seen, TI1, TI2)},
- {Seen = yes},
- [unify_ground(R)],
- '$gen_unify'(Xs, Instrs, TI2, TI).
- '$gen_unify'([X|Xs], Instrs, TI0, TI) -->
- {ground(X), X =..[F|Args], functor(X,_,Arity)},
- !,
- {'$assign_functor'('$f_n'(F,Arity), _, TI0, TI00)},
- '$gen_put_args'(Args, _, TI00, TI1),
- {'$assign_reg'(X, R, Seen, TI1, TI2)},
- {Seen = yes},
- [unify_ground(R)],
- '$gen_unify'(Xs, Instrs, TI2, TI).
- '$gen_unify'([X|Xs], [R=X|Instrs], TI0, TI) -->
- {'$assign_reg'(X, R, Seen, TI0, TI1)},
- '$gen_unify_var'(R, Seen),
- '$gen_unify'(Xs, Instrs, TI1, TI).
- '$gen_unify_var'(void, _) --> !, [unify_void(1)].
- '$gen_unify_var'(R, Seen) --> {var(Seen)}, !,
- {Seen = yes}, [unify_var(R)].
- '$gen_unify_var'(R, _) --> [unify_val(R)].
- /*---------------------------------------------------------------
- put$BL?Na$N@8@.(B
- ---------------------------------------------------------------*/
- '$gen_put'(_, A, _, _) --> {nonvar(A)}, !,
- {'$message'([A,should,be,an,unbound,variable])},
- {fail}.
- '$gen_put'(X, A, TI0, TI) --> {var(X)}, !,
- {'$assign_reg'(X, R, Seen, TI0, TI)},
- '$gen_put_var'(R, Seen, A).
- '$gen_put'(X, _, _, _) --> {float(X)}, !,
- {'$message'([floating,point,numbers,are,not,supported])},{fail}.
- '$gen_put'(X, A, TI0, TI) --> {atomic(X)}, !,
- {'$assign_reg'(X, R, Seen, TI0, TI)},
- '$gen_put_ground'(R, Seen, A).
- '$gen_put'(X, A, TI0, TI) --> {ground(X), X = [X1|X2]}, !,
- '$gen_put_args'([X1,X2], _, TI0, TI1),
- {'$assign_reg'(X, R, Seen, TI1, TI)},
- '$gen_put_ground'(R, Seen, A).
- '$gen_put'(X, A, TI0, TI) -->
- {ground(X), X =..[F|Args], functor(X,_,Arity)},
- !,
- {'$assign_functor'('$f_n'(F,Arity), _, TI0, TI00)},
- '$gen_put_args'(Args, _, TI00, TI1),
- {'$assign_reg'(X, R, Seen, TI1, TI)},
- '$gen_put_ground'(R, Seen, A).
- '$gen_put'(X, A, TI0, TI) --> {X = [X1|X2]}, !,
- {'$assign_reg'(_, R, Seen, TI0, TI1)},
- {Seen = yes, A = R},
- '$gen_put_args'([X1,X2], [R1,R2], TI1, TI),
- [put_list(R1, R2, R)].
- '$gen_put'(X, A, TI0, TI) -->
- {'$assign_reg'(_, R0, Seen, TI0, TI1)},
- {Seen = yes, A = R0},
- {X =.. [F|Args], functor(X,_,Arity)},
- {'$assign_functor'('$f_n'(F,Arity), R, TI1, TI11)},
- '$gen_put_args'(Args, Rs, TI11, TI2),
- {TI2 = [XN,SN|Ls]},
- {SN1 is SN+1, TI = [XN,SN1|Ls]},
- [put_str_args(Rs, h(SN))],
- [put_str(R, h(SN), R0)].
- %%%%%%%%%%%%%%%%
- '$assign_functor'(X, R, TI0, TI) :-
- X = '$f_n'(_, _),
- '$assign_reg'(X, R, Seen, TI0, TI),
- nonvar(Seen),
- R = f(_),
- !.
- '$assign_functor'(X, R, TI0, TI) :-
- X = '$f_n'(_, _),
- TI0 = [AN,SN,PN,Alloc],
- R = f(SN),
- SN1 is SN+1,
- Alloc1 = [[X,R,yes]|Alloc],
- TI = [AN,SN1,PN,Alloc1].
- %%%%%%%%%%%%%%%%
- '$gen_put_args'([], [], TI, TI) --> !.
- '$gen_put_args'([X|Xs], [R|Rs], TI0, TI) -->
- '$gen_put'(X, R, TI0, TI1),
- '$gen_put_args'(Xs, Rs, TI1, TI).
- '$gen_put_var'(void, _, A) --> !, {A = void}.
- '$gen_put_var'(R, Seen, A) --> {var(Seen)}, !,
- {Seen = yes, R = A},
- [put_var(R)].
- '$gen_put_var'(R, _, A) --> {R = A}.
- '$gen_put_ground'(R, Seen, A) --> {var(Seen)}, !,
- {Seen = yes, R = A}.
- '$gen_put_ground'(R, _, A) --> {R = A}.
- /* closure$B4XO"(B */
- '$gen_put_free_vars'(X, A, TI0, TI) --> !,
- '$gen_put_args'(X, Rs, TI0, TI1),
- {TI1 = [XN,SN|Ls]},
- {SN1 is SN+1, TI = [XN,SN1|Ls]},
- {A = h(SN)},
- [put_free_vars(Rs, A)].
- '$gen_put_closure'(X, Y, V, TI0, TI) --> !,
- {'$assign_reg'(_, R, Seen, TI0, TI)},
- {Seen = yes, V = R},
- [put_closure(X, Y, R)].
- /* continuation$B%4!<%k(B */
- '$gen_put_cont'(X, V, [AN,SN,PN,Alloc], [AN,SN,PN1,Alloc]) -->
- {V = p(PN), PN1 is PN + 1},
- [put_cont(X, V)].
- /*---------------------------------------------------------------
- ground$B9`$N%3%s%Q%$%k(B
- ground$B9`$O(BJava$B$G$O(Bstatic$B$J9`$K%H%i%s%9%l!<%H$5$l$k!#(B
- ---------------------------------------------------------------*/
- '$compile_ground'([], []) --> !.
- '$compile_ground'(TermInfo0, [XN,SN,PN]) -->
- {TermInfo0 = [_,_,_,Alloc]},
- {'$pickup_ground'(Alloc, Terms, [])},
- '$gen_static_terms'(Terms, TermInfo0, TermInfo),
- {TermInfo = [XN,SN,PN,_]}.
- '$pickup_ground'([]) --> !.
- '$pickup_ground'([[X,V,_]|TI]) --> {V = s(_)}, !,
- '$pickup_ground'(TI),
- [X=V].
- '$pickup_ground'([[X,V,_]|TI]) --> {V = f(_)}, !,
- '$pickup_ground'(TI),
- [X=V].
- '$pickup_ground'([_|TI]) --> '$pickup_ground'(TI).
- '$gen_static_terms'([], TI, TI) --> !.
- '$gen_static_terms'([P|Ps0], TI0, TI) -->
- '$gen_static'(P, Ps0, Ps, TI0, TI1),
- '$gen_static_terms'(Ps, TI1, TI).
- '$gen_static'(T=_, _, _, _, _) --> {var(T)}, !,
- {'$message'([T,should,not,be,an,unbound,variable])}, {fail}.
- '$gen_static'(T=_, _, _, _, _) --> {float(T)}, !,
- {'$message'([floating,point,numbers,are,not,supported])}, {fail}.
- '$gen_static'(T=R, Ps, Ps, TI, TI) --> {integer(T)}, !,
- [put_static_int(T, R)].
- '$gen_static'(T=R, Ps, Ps, TI, TI) --> {atom(T)}, !,
- [put_static_con(T, R)].
- '$gen_static'(T=R, Ps, Ps, TI, TI) --> {ground(T), T = '$f_n'(F, A)}, !,
- [put_static_con(F, A, R)].
- '$gen_static'(T=R, Ps0, Ps, TI0, TI) --> {T = [X1|X2]}, !,
- '$gen_static_args'([X1,X2], [R1,R2], Ps0, Ps, TI0, TI),
- [put_static_list(R1, R2, R)].
- '$gen_static'(T=R0, Ps0, Ps, TI0, TI) -->
- {T =.. [F|Args], functor(T,_,Arity)},
- '$gen_static_args'(['$f_n'(F, Arity)|Args], [R|Rs], Ps0, Ps, TI0, TI1),
- {TI1 = [A,S|Ls]},
- {S1 is S+1, TI = [A,S1|Ls]},
- [put_static_str_args(Rs, h(S))],
- [put_static_str(R, h(S), R0)].
- '$gen_static_args'([], [], Ps, Ps, TI, TI) --> !.
- '$gen_static_args'([X|Xs], [R|Rs], Ps0, Ps, TI0, TI) -->
- {'$assign_functor'(X, R, TI0, TI1)},
- !,
- {'$delete'(Ps0, X=R, Ps1)},
- '$gen_static_args'(Xs, Rs, Ps1, Ps, TI1, TI).
- '$gen_static_args'([X|Xs], [R|Rs], Ps0, Ps, TI0, TI) -->
- {'$assign_reg'(X, R, yes, TI0, TI1)},
- {'$delete'(Ps0, X=R, Ps1)},
- '$gen_static_args'(Xs, Rs, Ps1, Ps, TI1, TI).
- /*---------------------------------------------------------------
- $B%f!<%F%#%j%F%#(B(Prolog$B$+$iCf4V%3!<%I$^$G(B)
- ---------------------------------------------------------------*/
- '$list_to_string'([], S) :- !, S = ''.
- '$list_to_string'([X|Xs], S) :-
- name(X, L1),
- '$list_to_string'(Xs, S1),
- name(S1, L2),
- '$append'(L1, L2, L3),
- name(S, L3).
- '$variables'(X, Vs) :- '$variables'(X, [], Vs).
- '$variables'(X, Vs, Vs) :- var(X), '$memq'(X, Vs), !.
- '$variables'(X, Vs, [X|Vs]) :- var(X), !.
- '$variables'(X, Vs0, Vs0) :- atomic(X), !.
- '$variables'([X|Xs], Vs0, Vs) :- !,
- '$variables'(X, Vs0, Vs1), '$variables'(Xs, Vs1, Vs).
- '$variables'(X, Vs0, Vs) :- X =.. Xs, '$variables'(Xs, Vs0, Vs).
- '$memq'(X, [Y|_]) :- X==Y, !.
- '$memq'(X, [_|Ys]) :- '$memq'(X, Ys).
- '$flatten_code'([]) --> !.
- '$flatten_code'([(L: C)|Code]) --> !,
- [L: []],
- '$flatten_code'([C|Code]).
- '$flatten_code'([Code1|Code2]) --> !,
- '$flatten_code'(Code1),
- '$flatten_code'(Code2).
- '$flatten_code'(Code) --> [Code].
- '$append'([], Zs, Zs).
- '$append'([X|Xs], Ys, [X|Zs]) :- '$append'(Xs, Ys, Zs).
- '$message'([]) :- !, nl.
- '$message'([M|Ms]) :- write(M), write(' '), '$message'(Ms).
- '$mymax'(X, Y, Z) :- X1 is X, Y1 is Y, '$max1'(X1, Y1, Z).
- '$max1'(X, Y, X) :- X >= Y, !.
- '$max1'(_, Y, Y).
- '$count_variable'(V, X, 1) :- V == X, !.
- '$count_variable'(_, X, 0) :- var(X), !.
- '$count_variable'(_, X, 0) :- atomic(X), !.
- '$count_variable'(V, [X|Y], N) :- !,
- '$count_variable'(V, X, N1),
- '$count_variable'(V, Y, N2),
- N is N1 + N2.
- '$count_variable'(V, X, N) :-
- X =.. Xs,
- '$count_variable'(V, Xs, N).
- /* $B4{$K(Ballocate$B$5$l$F$$$k>l9g(B */
- '$assign_reg'(X, Type, Seen, [AN,SN,PN,Alloc], [AN,SN,PN,Alloc]) :-
- '$allocated'(Alloc, X, [Type,Seen]),
- !.
- /* X$B$,(Bground$B9`$N>l9g(B($B8e$G(Bstatic term$B$H$7$F=hM}$5$l$k(B) */
- '$assign_reg'(X, Type, Seen, [AN,SN,PN,Alloc], [AN,SN1,PN,Alloc1]) :-
- ground(X),
- !,
- Type = s(SN),
- SN1 is SN + 1,
- Alloc1 = [[X,Type,Seen]|Alloc].
- '$assign_reg'(X, Type, Seen, [AN,SN,PN,Alloc], [AN1,SN,PN,Alloc1]) :-
- Type = a(AN),
- AN1 is AN + 1,
- Alloc1 = [[X,Type,Seen]|Alloc].
- '$allocated'([[V|X]|_], V0, X) :- V == V0, !.
- '$allocated'([_|Alloc], V0, X) :- '$allocated'(Alloc, V0, X).
- '$delete'([X|Xs], X, Ys) :- '$delete'(Xs, X, Ys).
- '$delete'([X|Xs], Z, [X|Ys]) :- X \== Z, '$delete'(Xs, Z, Ys).
- '$delete'([], _, []).
- /*===============================================================
- $BCf4V%3!<%I$NL?Na0lMw(B
- ===============================================================*/
- /*
- Get Instructions
- ================
- get_var(X, A)
- get_val(X, A)
- get_int(S, A)
- get_con(S, A)
- get_list(A, ["list of unify instructions"])
- get_str(S, ["list of unify instructions"])
- get_ground(S, A)
- Unify Instructions
- ==================
- unify_void(N)
- unify_var(A)
- unify_val(A)
- unify_int(S)
- unify_con(S)
- unify_ground(S)
- Put Instructions
- ================
- put_var(A)
- put_list(X1, X2, A)
- put_str_args([A1...AN], H)
- put_str(S, H, A)
- put_cont(Pred, R)
- put_static_int(INT, S)
- put_static_con(ATOM, S)
- put_static_con(ATOM, ARITY, S)
- put_static_list(S1, S2, S)
- put_static_str_args([S1...SN], H)
- put_static_str(S1, H, S)
- put_static_pred(F/A+Op)
- Control Instructions
- ====================
- execute(X)
- Choice Instructions
- ===================
- switch_on_term(LV,LI,LC,LS,LL)
- try(FA+N, FA+T/I)
- retry(FA+N, FA+T/I)
- trust(FA+N)
- Special Instructions
- ===================
- jump(L)
- Resource Instructions
- =====================
- put_closure(X, Y, R)
- put_free_vars(Rs, A)
- pickup_resource(FA,I,L)
- restore_resource
- try_resource(FA+M, FA+N)
- retry_resource(FA+M, FA+N)
- trust_resource(FA+N)
- %consume(I,J)
- %execute_closure(J)
- consume_and_exec_closure(I)
- has_more_resource(FA+N)
- Others
- ======
- cont, void --> special argument
- Label:[A]
- constructor(FA)
- exec_method(FA, VarInfo, Type, Code)
- extra_methods(FA)
- */
- /*===============================================================
- $BCf4V%3!<%I$N=PNO(B
- ===============================================================*/
- '$write_intermediate'(_, []) :- !.
- '$write_intermediate'(Out, [Instruction|Instructions]) :- !,
- '$write_intermediate'(Out, Instruction),
- '$write_intermediate'(Out, Instructions).
- '$write_intermediate'(Out, comment(Comment)) :- !,
- write(Out, '%%% '),
- writeq(Out, Comment),
- nl(Out).
- '$write_intermediate'(Out, debug(Comment)) :- !,
- tab(Out, 8),
- write(Out, '%%% '),
- writeq(Out, Comment), nl(Out).
- '$write_intermediate'(Out, (Label: Instruction)) :- !,
- writeq(Out, Label), write(Out, ' :'),
- nl(Out),
- '$write_intermediate'(Out, Instruction).
- '$write_intermediate'(Out, exec_method(X, Y, Z, W)) :- !,
- tab(Out, 8),
- write(Out, 'exec_method('),
- writeq(Out, X), write(Out, ','),
- writeq(Out, Y), write(Out, ','),
- writeq(Out, Z), write(Out, ',['),
- nl(Out),
- write_imd_exec(Out, W),
- tab(Out, 8),
- write(Out, '].'), nl(Out).
- '$write_intermediate'(Out, Instruction) :-
- tab(Out, 8),
- writeq(Out, Instruction), write(Out, '.'),
- nl(Out).
- write_imd_exec(_, []) :- !.
- write_imd_exec(Out, [P]) :- !,
- tab(Out, 12),
- writeq(Out, P), nl(Out).
- write_imd_exec(Out, [P|Ps]) :-
- tab(Out, 12),
- writeq(Out, P), write(Out, ','),
- nl(Out),
- write_imd_exec(Out, Ps).
- /*===============================================================
- Java$B%3!<%I$N=PNO(B
- ===============================================================*/
- '$generate_cs'(Out, Code) :-
- %'$write_cs'(Out, Code),
- '$write_cs0'(Out, Code),
- write(Out, '}'),
- nl(Out),
- write(Out, '}'), % added by JJC to close the namespace (hope this is right).
- nl(Out).
- '$write_cs0'(Out, Instructions) :-
- '$member'(Instr, Instructions),
- '$write_cs'(Out, Instr),
- fail.
- '$write_cs0'(_, _) :- !.
- '$print_release_message'(Out, FA) :-
- '$version'(Version),
- '$file_name'(File),
- '$create_class_name'(FA, Class),
- '$writeln_stream'(Out, 0, ['/*']),
- '$writeln_stream'(Out, 0, [' * *** Please do not edit ! ***']),
- '$writeln_stream'(Out, 0, [' * @(#) ', Class, '.cs']),
- '$writeln_stream'(Out, 0, [' * @procedure ', FA, ' in ', File, '.pl']),
- '$writeln_stream'(Out, 0, [' */']),
- nl(Out),
- '$writeln_stream'(Out, 0, ['/*']),
- '$writeln_stream'(Out, 0, [' * @version ', Version]),
- '$writeln_stream'(Out, 0, [' * @author Mutsunori Banbara (banbara@pascal.seg.kobe-u.ac.jp)']),
- '$writeln_stream'(Out, 0, [' * @author Naoyuki Tamura (tamura@kobe-u.ac.jp)']),
- '$writeln_stream'(Out, 0, [' * Modified by Jonathan Cook (jjc@dcs.ed.ac.uk)']),
- '$writeln_stream'(Out, 0, [' */']).
- '$write_cs'(_, []) :- !.
- %'$write_cs'(Out, [Instruction|Instructions]) :- !,
- % '$write_cs'(Out, Instruction),
- % '$write_cs'(Out, Instructions).
- /*---------------------------------------------------------------
- $B%3%a%s%H(B
- ---------------------------------------------------------------*/
- '$write_cs'(_, comment(_)) :- !.
- '$write_cs'(_, debug(_)) :- !.
- %'$write_cs'(Out, comment(Comment)) :- !,
- % write(Out, '// '),
- % writeq(Out, Comment), nl(Out).
- %
- %'$write_cs'(Out, debug(Comment)) :- !,
- % tab(Out, 8),
- % write(Out, '// '),
- % writeq(Out, Comment), nl(Out).
- /*---------------------------------------------------------------
- $B%/%i%9@k8@(B
- ---------------------------------------------------------------*/
- /* JJC
- * extends -> :
- * final -> sealed or readonly
- * putting in overrides/virutals
- * toString -> ToString
- * namespace declarations, not yet *closed*
- */
- '$write_cs'(Out, (Label: Instruction)) :- !,
- '$write_cs_class'(Out, Label),
- '$write_cs'(Out, Instruction).
- '$write_cs_class'(Out, Label) :- Label = '$res'(_,_), !,
- '$print_release_message'(Out, Label),
- '$create_class_name'(Label, N),
- '$writeln_stream'(Out, 0, ['namespace JJC.Psharp.Resources {'] ), %JJC
- '$writeln_stream'(Out, 0, [] ), %JJC
- '$writeln_stream'(Out, 0, ['using JJC.Psharp.Lang;'] ),
- '$writeln_stream'(Out, 0, ['using JJC.Psharp.Lang.Resource;'] ), %JJC
- '$writeln_stream'(Out, 0, ['using Predicates = JJC.Psharp.Predicates;'] ), %JJC
- '$writeln_stream'(Out, 0, ['using Resources = JJC.Psharp.Resources;'] ), %JJC
- '$writeln_stream'(Out, 0, [] ), %JJC
- '$writeln_stream'(Out, 0, ['public sealed class ', N, ' : Predicate {']).
- '$write_cs_class'(Out, Label) :- Label = F/A, !,
- '$print_release_message'(Out, Label),
- '$create_class_name'(F/A, N),
- '$writeln_stream'(Out, 0, ['namespace JJC.Psharp.Predicates {'] ), %JJC
- '$writeln_stream'(Out, 0, [] ), %JJC
- '$writeln_stream'(Out, 0, ['using JJC.Psharp.Lang;'] ),
- '$writeln_stream'(Out, 0, ['using JJC.Psharp.Lang.Resource;'] ), %JJC
- '$writeln_stream'(Out, 0, ['using Predicates = JJC.Psharp.Predicates;'] ), %JJC
- '$writeln_stream'(Out, 0, ['using Resources = JJC.Psharp.Resources;'] ), %JJC
- '$writeln_stream'(Out, 0, [] ), % this and following four lines: JJC
- ( clause('$fco_predicates'(F,A),_) ->
- '$writeln_stream'(Out, 0, ['public class ', N, ' : Predicate, FcoPredicate {'])
- ; '$writeln_stream'(Out, 0, ['public class ', N, ' : Predicate {'])
- ).
- % '$writeln_stream'(Out, 0, ['public class ', N, ' : Predicate {']). % , GOAL REMOVED JJC
- % ( clause('$fco_predicates'(F,A),_) ->
- % '$writeln_stream'(Out, 4, ['static internal ', N, ' entry_code;'])
- % ; true
- % ).
- '$write_cs_class'(Out, Label) :- Label = FA+Type,
- write(Out, '}'), nl(Out), nl(Out),
- '$create_class_name'(FA+Type, N1),
- '$create_class_name'(FA, N2),
- '$writeln_stream'(Out, 0, ['sealed class ', N1, ' : ', N2, ' {']).
- /*---------------------------------------------------------------
- static$BJQ?t$N@k8@(B
- ---------------------------------------------------------------*/
- '$write_cs'(Out, put_static_pred(FA)) :- !,
- '$create_pred_name'(FA, Pred),
- '$create_qualified_name'(FA, Class),
- '$writeln_stream'(Out,
- 4,
- ['static internal readonly Predicate ',
- Pred, ' = new ', Class, '();'
- ]).
- '$write_cs'(Out, put_static_con(Atom, S)) :- atom(Atom), !,
- '$create_reg_name'(S, SN),
- '$atom_to_string'(Atom, String),
- '$writeln_stream'(Out,
- 4,
- ['static internal readonly SymbolTerm ',
- SN, ' = SymbolTerm.makeSymbol("', String, '");'
- ]).
- '$write_cs'(Out, put_static_con(Atom, Arity, S)) :- atom(Atom), integer(Arity),!,
- '$create_reg_name'(S, SN),
- '$atom_to_string'(Atom, String),
- '$writeln_stream'(Out,
- 4,
- ['static internal readonly SymbolTerm ',
- SN, ' = SymbolTerm.makeSymbol("', String, '"', ', ', Arity, ');'
- ]).
- '$write_cs'(Out, put_static_int(Int, S)) :- integer(Int), !,
- '$create_reg_name'(S, SN),
- '$writeln_stream'(Out,
- 4,
- ['static internal readonly IntegerTerm ',
- SN, ' = new IntegerTerm(', Int, ');'
- ]).
- '$write_cs'(Out, put_static_list(S1, S2, S)) :- !,
- '$create_reg_name'(S, SN),
- '$create_reg_name'(S1, SN1),
- '$create_reg_name'(S2, SN2),
- '$writeln_stream'(Out,
- 4,
- ['static internal readonly ListTerm ',
- SN, ' = new ListTerm(', SN1, ', ', SN2, ');'
- ]).
- '$write_cs'(Out, put_static_str(S1, H, S)) :- !,
- '$create_reg_name'(S, SN),
- '$create_reg_name'(H, HN),
- '$create_reg_name'(S1, SN1),
- '$writeln_stream'(Out,
- 4,
- ['static internal readonly StructureTerm ',
- SN, ' = new StructureTerm(', SN1, ', ', HN, ');'
- ]).
- '$write_cs'(Out, put_static_str_args(Ss, H)) :- !,
- '$create_reg_name'(H, HN),
- '$create_str_args'(Ss, SNs),
- '$writeln_stream'(Out, 4, ['static internal readonly Term[] ', HN, ' = '|SNs]).
- /*---------------------------------------------------------------
- $B%3%s%9%H%i%/%?(B
- ---------------------------------------------------------------*/
- '$write_cs'(Out, constructor(FA)) :- FA = '$res'(_,_), !,
- '$create_class_name'(FA, Name),
- '$writeln_stream'(Out, 4, ['public ', Name, '(){}']).
- '$write_cs'(Out, constructor(F/A)) :- !,
- '$wj_public_arg_decl'(Out, A), /* local$B$J0z?t$h$&$NJQ?t$N@k8@(B */
- nl(Out),
- '$create_class_name'(F/A, Name),
- tab(Out, 4),
- '$writel_stream'(Out, ['public ', Name, '(']), /* $BIaDL$N%3%s%9%H%i%/%?(B */
- '$wj_assign_number'(Out, A, 'Term a', 1, ', '),
- write(Out, 'Predicate cont) {'),
- nl(Out),
- '$wj_constructor_args'(Out, 1, A),
- '$writeln_stream'(Out, 4, ['}']),
- nl(Out),
- /* call/1$BMQ$N0z?t$J$7$N%3%s%9%H%i%/%?(B */
- '$writeln_stream'(Out, 4, ['public ', Name, '(){}']),
- '$writeln_stream'(Out, 4, ['public override void setArgument(Term[] args, Predicate cont) {']),
- '$wj_constructor_args_for_call'(Out, 0, A),
- '$writeln_stream'(Out, 4, ['}']).
- '$wj_public_arg_decl'(_, Arity) :- Arity =< 0, !.
- '$wj_public_arg_decl'(Out, Arity) :- Arity > 0,
- nl(Out),
- tab(Out, 4),
- write(Out, 'public Term '),
- '$wj_assign_number'(Out, Arity, 'arg', 1, ';'),
- nl(Out).
- '$wj_constructor_args'(Out, N, Arity) :- N > Arity, !,
- '$writeln_stream'(Out, 8, ['this.cont = cont;']).
- '$wj_constructor_args'(Out, N, Arity) :-
- '$writeln_stream'(Out, 8, ['arg', N, ' = ', 'a', N, '; ']),
- M is N + 1,
- '$wj_constructor_args'(Out, M, Arity).
- '$wj_constructor_args_for_call'(Out, N, A) :- N > A-1, !,
- '$writeln_stream'(Out, 8, ['this.cont = cont;']).
- '$wj_constructor_args_for_call'(Out, N, A) :-
- M is N + 1,
- '$writeln_stream'(Out, 8, ['arg', M, ' = ', 'args[', N, ']; ']),
- '$wj_constructor_args_for_call'(Out, M, A).
- /*---------------------------------------------------------------
- exec$B%a%=%C%I(B
- exec_method(Functor/Arity, VarInfo, Type, Code))
- * VarInfo = [AVars, Pvars] or []
- * Type = root, top, normal, try, retry, trust
- [exec_method(FA, [AVar,PVar], root_nores, ExecCode)],
- ---------------------------------------------------------------*/
- '$write_cs'(Out, exec_method(_, _, T, [execute(cont)])) :- !,
- nl(Out),
- '$writeln_stream'(Out, 4, ['public override Predicate exec( Prolog engine ) {']), % argument added JJC
- ( T == root_one_nores ->
- '$writeln_stream'(Out, 8, ['return cont;'])
- ;
- '$writeln_stream'(Out, 8, ['return engine.cont;'])
- ),
- '$writeln_stream'(Out, 4, ['}']).
- '$write_cs'(Out, exec_method(Functor/Arity, VarInfo, Type, Code)) :- !,
- nl(Out),
- '$write_cs_exec'(Out, Functor, Arity, VarInfo, Type),
- %'$write_cs'(Out, Code),
- '$write_cs0'(Out, Code),
- '$writeln_stream'(Out, 4, ['}']).
- '$write_cs_exec'(Out, _, _, VarInfo, res(N)) :- !,
- '$wj_exec_normal'(Out, N, VarInfo).
- '$write_cs_exec'(Out, Functor, Arity, _, root) :- !,
- '$wj_exec_root'(Out, Functor, Arity).
- '$write_cs_exec'(Out, Functor, Arity, _, root_nores) :- !,
- '$wj_exec_root'(Out, Functor, Arity).
- '$write_cs_exec'(Out, Functor, Arity, VarInfo, root_one_nores) :- !,
- '$wj_exec_root_one_nores'(Out, Functor, Arity, VarInfo).
- '$write_cs_exec'(Out, _, Arity, VarInfo, normal) :- !,
- '$wj_exec_normal'(Out, Arity, VarInfo).
- '$write_cs_exec'(Out, _, _, [], top) :- !,
- '$writeln_stream'(Out, 4, ['public override Predicate exec( Prolog engine ) {']). % argument added JJC
- %'$writeln_stream'(Out, 8, ['engine.setB0();']).
- '$write_cs_exec'(Out, _, Arity, VarInfo, top) :- !,
- '$wj_exec_normal'(Out, Arity, VarInfo).
- %'$writeln_stream'(Out, 8, ['engine.setB0();']).
- '$write_cs_exec'(Out, _, _, _, _) :-
- '$writeln_stream'(Out, 4, ['public override Predicate exec( Prolog engine ) {']). % argument added JJC
- /* root */
- '$wj_exec_root'(Out, Functor, Arity) :-
- '$writeln_stream'(Out, 4, ['public override Predicate exec( Prolog engine ) {']), % argument added JJC
- ( clause('$fco_predicates'(Functor,Arity),_) ->
- '$writeln_stream'(Out, 8, ['engine.SetEntryCode( this );']) % JJC: engine. added
- ; true
- ),
- '$wj_root_args'(Out, 1, Arity),
- '$writeln_stream'(Out, 4, ['}']), nl(Out),
- '$writeln_stream'(Out, 4, ['public virtual Predicate call( Prolog engine ) {']), % argument added JJC
- '$writeln_stream'(Out, 8, ['engine.setB0();']).
- '$wj_root_args'(Out, N, Arity) :- N > Arity, !,
- '$writeln_stream'(Out, 8, ['engine.cont = cont;']),
- '$writeln_stream'(Out, 8, ['return call( engine );']). % added engine
- '$wj_root_args'(Out, N, Arity) :-
- '$writeln_stream'(Out, 8, ['engine.aregs[', N, '] = ', 'arg', N, ';']),
- M is N + 1,
- '$wj_root_args'(Out, M, Arity).
- /* root_one_nores */
- '$wj_exec_root_one_nores'(Out, Functor, Arity, [AN, PN]) :- !,
- '$writeln_stream'(Out, 4, ['public override Predicate exec( Prolog engine ) {']), % argument added JJC
- '$writeln_stream'(Out, 8, ['engine.setB0();']),
- %( clause('$fco_predicates'(Functor,Arity),_) ->
- % '$writeln_stream'(Out, 8, ['entry_code = engine.GetEntryCode( this );'])
- %; true
- %),
- ( clause('$fco_predicates'(Functor,Arity),_) ->
- '$message'([procedure,Functor/Arity,contains,an,infinite,loop]), abort % spelling mistake corrected JJC
- ; true
- ),
- '$wj_a_register'(Out, AN),
- '$wj_p_register'(Out, PN),
- '$wj_deref_one'(Out, 1, Arity),
- nl(Out).
- '$wj_deref_one'(_, N, Arity) :- N > Arity, !.
- '$wj_deref_one'(Out, N, Arity) :-
- '$writeln_stream'(Out, 8, ['a',N,' = ','arg', N, '.dereference();']),
- M is N + 1,
- '$wj_deref_one'(Out, M, Arity).
- /* normal */
- '$wj_exec_normal'(_, _, []) :- !.
- '$wj_exec_normal'(Out, Arity, [AN, PN]) :-
- '$writeln_stream'(Out, 4, ['public override Predicate exec( Prolog engine ) {']), % argument added JJC
- '$wj_a_register'(Out, AN),
- '$wj_p_register'(Out, PN),
- '$wj_deref'(Out, 1, Arity),
- nl(Out).
- '$wj_deref'(Out, N, Arity) :- N > Arity, !,
- '$writeln_stream'(Out, 8, ['Predicate cont = engine.cont;']). %%%% THIS IS THE ONE YOU WANT TO CHANGE
- '$wj_deref'(Out, N, Arity) :-
- '$writeln_stream'(Out, 8, ['a',N,' = ','engine.aregs[', N, '].dereference();']),
- M is N + 1,
- '$wj_deref'(Out, M, Arity).
- '$wj_a_register'(_, N) :- N =< 1, !.
- '$wj_a_register'(Out, N) :- M is N-1,
- tab(Out, 8),
- write(Out, 'Term '),
- '$wj_assign_number'(Out, M, a, 1, ;),
- nl(Out).
- '$wj_p_register'(_, N) :- N =< 1, !.
- '$wj_p_register'(Out, N) :- M is N-1,
- tab(Out, 8),
- write(Out, 'Predicate '),
- '$wj_assign_number'(Out, M, p, 1, ;),
- nl(Out).
- /*---------------------------------------------------------------
- Indexing$BL?Na(B(switch_on_term, try, retry, truct)
- ---------------------------------------------------------------*/
- '$write_cs'(Out, switch_on_term(LV, LI, LC, LS, LL)) :- !,
- '$create_pred_name'(LV, N1),
- '$create_pred_name'(LI, N2),
- '$create_pred_name'(LC, N3),
- '$create_pred_name'(LS, N4),
- '$create_pred_name'(LL, N5),
- '$writeln_stream'(Out, 8, ['return engine.switch_on_term(']),
- '$writeln_stream'(Out, 35, [N1, ',']),
- '$writeln_stream'(Out, 35, [N2, ',']),
- '$writeln_stream'(Out, 35, [N3, ',']),
- '$writeln_stream'(Out, 35, [N4, ',']),
- '$writeln_stream'(Out, 35, [N5]),
- '$writeln_stream'(Out, 35, [');']).
- '$write_cs'(Out, try(L1, L2)) :- !,
- '$create_pred_name'(L1, N1),
- '$create_pred_name'(L2, N2),
- '$writeln_stream'(Out, 8, ['return engine.jtry(', N1, ', ', N2, ');']).
- '$write_cs'(Out, retry(L1, L2)) :- !,
- '$create_pred_name'(L1, N1),
- '$create_pred_name'(L2, N2),
- '$writeln_stream'(Out, 8, ['return engine.retry(', N1, ', ', N2, ');']).
- '$write_cs'(Out, trust(L)) :- !,
- '$create_pred_name'(L, N),
- '$writeln_stream'(Out, 8, ['return engine.trust(', N, ');']).
- /*---------------------------------------------------------------
- $B$H$j$"$($:(B
- ---------------------------------------------------------------*/
- '$write_cs'(Out, jump(L)) :- !,
- '$create_pred_name'(L, N),
- '$writeln_stream'(Out, 8, ['return ', N, ';']).
- /*---------------------------------------------------------------
- $B$=$NB>$N%a%=%C%I=PNO(B
- * arity()
- * toString()
- ---------------------------------------------------------------*/
- '$write_cs'(Out, extra_methods(FA)) :- FA = '$res'(_F/A, _), !,
- '$write_cs_arity'(Out, A),
- '$create_pred_name'(FA, Name),
- '$write_cs_toString'(Out, Name, 0).
- '$write_cs'(Out, extra_methods(FA)) :- FA = F/A, !,
- '$write_cs_arity'(Out, A),
- '$write_cs_toString'(Out, F, A).
- '$write_cs_arity'(Out, A) :-
- nl(Out),
- '$writeln_stream'(Out,4,['public override int arity() { return ', A, ';', ' }']).
- '$write_cs_toString'(Out, F, A) :-
- nl(Out),
- '$atom_to_string'(F, F1),
- '$wj_toString_args'(F1, A, Args),
- '$writeln_stream'(Out, 4, ['public override string ToString() {']),
- '$writeln_stream'(Out, 8, ['return '|Args]),
- '$writeln_stream'(Out, 4, ['}']).
- '$wj_toString_args'(F, A, As) :- A =:= 0, !,
- As = ['"', F, '";'].
- '$wj_toString_args'(F, A, As) :- A > 0,
- '$wj_toString_args0'(A, [], As0),
- '$append'(['"', F, '(" + '|As0], [' + ")";'], As).
- '$wj_toString_args0'(A, X, Y) :- A =:= 1, !,
- Y = ['arg', A|X].
- '$wj_toString_args0'(A, X, Y) :- A > 1,
- A1 is A-1,
- '$wj_toString_args0'(A1, [' + ', '", "', ' + ', 'arg', A|X], Y).
- /*---------------------------------------------------------------
- get$BL?Na$N=PNO(B
- ---------------------------------------------------------------*/
- '$write_cs'(Out, get_var(X, A)) :- !,
- '$create_reg_name'(X, X1),
- '$create_reg_name'(A, A1),
- '$writeln_stream'(Out, 8, [X1, ' = ', A1, ';']).
- '$write_cs'(Out, get_val(X, A)) :- !, '$write_cs_get4'(Out, X, A).
- '$write_cs'(Out, get_con(S, A)) :- !, '$write_cs_get4'(Out, S, A).
- '$write_cs'(Out, get_int(S, A)) :- !, '$write_cs_get4'(Out, S, A).
- '$write_cs'(Out, get_ground(S, A)) :- !, '$write_cs_get4'(Out, S, A).
- '$write_cs_get4'(Out, X, A) :-
- '$create_reg_name'(X, X1),
- '$create_reg_name'(A, A1),
- '$writeln_stream'(Out,
- 8,
- ['if ( !', X1, '.unify(', A1, ', ',
- 'engine.trail) ) return engine.fail();'
- ]).
- /*---------------------------------------------------------------
- get_list$BL?Na(B
- ---------------------------------------------------------------*/
- '$write_cs'(Out, get_list(X, UnifyInstr)) :- !,
- '$create_reg_name'(X, A),
- '$writeln_stream'(Out, 8, ['if ( ', A, '.isList() ){']),
- '$wj_get_list_read'(Out, A, UnifyInstr),
- '$writeln_stream'(Out, 8, ['} else if ( ', A, '.isVariable() ){']),
- '$wj_get_list_write'(Out, A, UnifyInstr),
- '$writeln_stream'(Out, 8, ['} else {']),
- '$writeln_stream'(Out, 12, ['return engine.fail();']),
- '$writeln_stream'(Out, 8, ['}']).
- /* read$B%b!<%I(B */
- '$wj_get_list_read'(Out, A, [U1, U2]) :-
- '$unify_list_r'(Out, car, A, U1),
- '$unify_list_r'(Out, cdr, A, U2).
- '$unify_list_r'(_, _, _, unify_void(_)) :- !.
- '$unify_list_r'(Out, C, A, unify_var(X)) :- !,
- '$create_reg_name'(X, Y),
- '$writeln_stream'(Out,
- 12,
- [Y, ' = ', '((ListTerm)', A, ').', C, ';'
- % removed a () before the ;
- ]).
- '$unify_list_r'(Out, C, A, unify_val(X)) :- !, '$unify_list_r4'(Out, C, A, X).
- '$unify_list_r'(Out, C, A, unify_int(X)) :- !, '$unify_list_r4'(Out, C, A, X).
- '$unify_list_r'(Out, C, A, unify_con(X)) :- !, '$unify_list_r4'(Out, C, A, X).
- '$unify_list_r'(Out, C, A, unify_ground(X)) :- !, '$unify_list_r4'(Out, C, A, X).
- '$unify_list_r4'(Out, C, A, X) :-
- '$create_reg_name'(X, Y),
- '$writeln_stream'(Out,
- 12,
- % removed a () in what is now ''
- ['if ( !', Y, '.unify(((ListTerm)', A, ').', C, '',
- ', ',
- 'engine.trail) )'
- ]),
- '$writeln_stream'(Out, 16, ['return engine.fail();']).
- /* write$B%b!<%I(B */
- '$wj_get_list_write'(Out, A, [U1, U2]) :-
- '$unify_list_w'(Out, U1, Car),
- '$unify_list_w'(Out, U2, Cdr),
- '$writeln_stream'(Out,
- 12,
- ['if ( !', A, '.unify(new ListTerm(',
- Car, ', ', Cdr, '), ', 'engine.trail) )']),
- '$writeln_stream'(Out, 16, ['return engine.fail();']).
- %'$unify_list_w'(_, unify_void(_), C) :- !, C = 'new VariableTerm()'.
- '$unify_list_w'(_, unify_void(_), C) :- !, C = 'engine.makeVariable()'.
- '$unify_list_w'(Out, unify_var(X), Y) :- !,
- '$create_reg_name'(X, Y),
- '$writeln_stream'(Out, 12, [Y, ' = engine.makeVariable();']).
- %'$writeln_stream'(Out, 12, [Y, ' = new VariableTerm();']).
- '$unify_list_w'(_, unify_val(X), Y) :- !, '$create_reg_name'(X, Y).
- '$unify_list_w'(_, unify_int(X), Y) :- !, '$create_reg_name'(X, Y).
- '$unify_list_w'(_, unify_con(X), Y) :- !, '$create_reg_name'(X, Y).
- '$unify_list_w'(_, unify_ground(X), Y) :- !, '$create_reg_name'(X, Y).
- /*---------------------------------------------------------------
- get_str$BL?Na(B
- ---------------------------------------------------------------*/
- '$write_cs'(Out, get_str(R, A, UnifyInstr)) :- !,
- '$create_reg_name'(A, N1),
- '$create_reg_name'(R, N2),
- /* read$B%b!<%I(B */
- '$writeln_stream'(Out, 8, ['if ( ', N1, '.isStructure() ){']),
- %'$writeln_stream'(Out,
- % 12,
- % ['StructureTerm str = (StructureTerm)', N1, ';']),
- '$writeln_stream'(Out,
- 12,
- ['if (', N2, ' != ((StructureTerm)', N1, ').functor )']),
- '$writeln_stream'(Out, 16, ['return engine.fail();']),
- '$writeln_stream'(Out,
- 12,
- ['Term[] args = ((StructureTerm)',
- N1, ').args;']),
- '$wj_get_str_read'(Out, UnifyInstr, 0),
- /* write$B%b!<%I(B */
- '$writeln_stream'(Out, 8, ['} else if (', N1, '.isVariable() ){']),
- '$wj_get_str_write'(Out, N1, N2, UnifyInstr),
- '$writeln_stream'(Out, 8, ['} else {']),
- '$writeln_stream'(Out, 12, ['return engine.fail();']),
- '$writeln_stream'(Out, 8, ['}']).
- /* read$B%b!<%I(B */
- '$wj_get_str_read'(_, [], _) :- !.
- '$wj_get_str_read'(Out, [U|Us], I) :-
- '$unify_str_r'(Out, U, I),
- I1 is I+1,
- '$wj_get_str_read'(Out, Us, I1).
- '$unify_str_r'(_, unify_void(_), _) :- !.
- '$unify_str_r'(Out, unify_var(X), I) :- !,
- '$create_reg_name'(X, A),
- '$writeln_stream'(Out, 12, [A, ' = args[', I, '];']).
- '$unify_str_r'(Out, unify_val(X), I) :- !, '$unify_str_r4'(Out, X, I).
- '$unify_str_r'(Out, unify_int(X), I) :- !, '$unify_str_r4'(Out, X, I).
- '$unify_str_r'(Out, unify_con(X), I) :- !, '$unify_str_r4'(Out, X, I).
- '$unify_str_r'(Out, unify_ground(X), I) :- !, '$unify_str_r4'(Out, X, I).
- '$unify_str_r4'(Out, X, I) :-
- '$create_reg_name'(X, A),
- '$writeln_stream'(Out,
- 12,
- ['if ( !', A,
- '.unify(args[', I, '], engine.trail) )']),
- '$writeln_stream'(Out, 16, ['return engine.fail();']).
- /* write$B%b!<%I(B */
- '$wj_get_str_write'(Out, A, Func, UnifyInstr) :- !,
- '$unify_str_write'(Out, UnifyInstr, Args),
- '$writeln_stream'(Out, 12, ['Term[] args = '|Args]),
- '$writeln_stream'(Out,
- 12,
- ['if ( !', A,
- '.unify(new StructureTerm(',
- Func, ', args), engine.trail) )']),
- '$writeln_stream'(Out, 16, ['return engine.fail();']).
- '$unify_str_write'(Out, UnifyInstr, Args) :-
- '$unify_str_w'(Out, UnifyInstr, Y),
- '$append'(['{'|Y], ['};'], Args).
- '$unify_str_w'(Out, [U], [A]) :- !,
- '$unify_str_w0'(Out, U, A).
- '$unify_str_w'(Out, [U|Us], [A, ', '|As]) :-
- '$unify_str_w0'(Out, U, A),
- '$unify_str_w'(Out, Us, As).
- %'$unify_str_w0'(_, unify_void(_), X) :- !, X = 'new VariableTerm()'.
- '$unify_str_w0'(_, unify_void(_), X) :- !, X = 'engine.makeVariable()'.
- '$unify_str_w0'(Out, unify_var(X), A) :- !,
- '$create_reg_name'(X, A),
- '$writeln_stream'(Out, 12, [A, ' = engine.makeVariable();']).
- %'$writeln_stream'(Out, 12, [A, ' = new VariableTerm();']).
- '$unify_str_w0'(_, unify_val(X), Y) :- !, '$create_reg_name'(X, Y).
- '$unify_str_w0'(_, unify_int(X), Y) :- !, '$create_reg_name'(X, Y).
- '$unify_str_w0'(_, unify_con(X), Y) :- !, '$create_reg_name'(X, Y).
- '$unify_str_w0'(_, unify_ground(X), Y) :- !, '$create_reg_name'(X, Y).
- /*---------------------------------------------------------------
- put$BL?Na$N=PNO(B
- ---------------------------------------------------------------*/
- '$write_cs'(Out, put_var(A)) :- !,
- '$create_reg_name'(A, A1),
- '$writeln_stream'(Out, 8, [A1, ' = engine.makeVariable();']).
- %'$writeln_stream'(Out, 8, [A1, ' = new VariableTerm();']).
- '$write_cs'(Out, put_list(Car, Cdr, A)) :- !,
- '$create_reg_name'(A, A1),
- '$create_reg_name'(Car, Car1),
- '$create_reg_name'(Cdr, Cdr1),
- '$writeln_stream'(Out,
- 8,
- [A1, ' = new ListTerm(', Car1, ', ', Cdr1, ');'
- ]).
- '$write_cs'(Out, put_str(Func, Args, A)) :- !,
- '$create_reg_name'(A, A1),
- '$create_reg_name'(Args, Args1),
- '$create_reg_name'(Func, Func1),
- '$writeln_stream'(Out,
- 8,
- [A1, ' = new StructureTerm(', Func1, ', ', Args1, ');'
- ]).
- '$write_cs'(Out, put_str_args(As, H)) :- !,
- '$create_reg_name'(H, H1),
- '$create_str_args'(As, As1),
- '$writeln_stream'(Out, 8, ['Term[] ', H1, ' = '|As1]).
- '$write_cs'(Out, put_cont(Pred, P)) :- !,
- '$create_reg_name'(P, P1),
- Pred =.. [F|Args0],
- length(Args0, Arity0),
- Arity is Arity0 - 1,
- '$create_qualified_name'(F/Arity, ClassName),
- '$create_cont_args'(Args0, Args),
- '$writeln_stream'(Out, 8, [P1, ' = new ', ClassName|Args]).
- '$create_cont_args'(X, Z) :-
- '$create_reg_args'(X, Y),
- '$append'(['('|Y], [');'], Z).
- /*---------------------------------------------------------------
- execute(X)
- ---------------------------------------------------------------*/
- '$write_cs'(Out, execute(P)) :- P == cont, !,
- '$writeln_stream'(Out, 8, ['return cont;']).
- '$write_cs'(Out, execute(P)) :- !,
- P =.. [F|Args],
- '$write_cs_cont'(Out, F, Args).
- '$write_cs_cont'(Out, F, Args) :- F == '$FCO', !,
- '$wj_fco_cont'(Out, Args, 1),
- '$writeln_stream'(Out, 8, ['return engine.GetEntryCode( this ).call( engine );']). % modified JJC
- '$write_cs_cont'(Out, F, Args0) :-
- length(Args0, Arity0),
- Arity is Arity0 - 1,
- '$create_qualified_name'(F/Arity, ClassName),
- '$create_cont_args'(Args0, Args),
- '$writeln_stream'(Out, 8, ['return new ', ClassName|Args]).
- '$wj_fco_cont'(Out, [A], _) :- !,
- '$create_reg_name'(A, A1),
- '$writeln_stream'(Out, 8, ['engine.cont = ', A1, ';']).
- '$wj_fco_cont'(Out, [A|As], I0) :-
- '$create_reg_name'(A, A1),
- '$writeln_stream'(Out, 8, ['engine.aregs[', I0, '] = ', A1, ';']),
- I is I0+1,
- '$wj_fco_cont'(Out, As, I).
- /*---------------------------------
- $B%j%=!<%9(B
- ---------------------------------*/
- '$write_cs'(Out, put_free_vars([], H)) :- !,
- '$create_reg_name'(H, H1),
- '$writeln_stream'(Out, 8, ['Term[] ', H1, ' = {};']).
- '$write_cs'(Out, put_free_vars(As, H)) :- !,
- '$create_reg_name'(H, H1),
- '$create_str_args'(As, As1),
- '$writeln_stream'(Out, 8, ['Term[] ', H1, ' = '|As1]).
- '$write_cs'(Out, put_closure(X, Y, R)) :- !,
- '$create_pred_name'(X, XX),
- '$create_reg_name'(Y, YY),
- '$create_reg_name'(R, RR),
- '$writeln_stream'(Out, 8, [RR, ' = new ClosureTerm( ', XX, ', ', YY, ');']).
- %'$write_cs'(Out, put_closure(X, Y, R)) :- !,
- % '$create_class_name'(X, XX),
- % '$create_reg_name'(Y, YY),
- % '$create_reg_name'(R, RR),
- % '$writeln_stream'(Out, 8, [RR, ' = new ClosureTerm(new ', XX, '()', ', ', YY, ');']).
- '$write_cs'(Out, look_up_hash(L)) :- !,
- '$writeln_stream'(Out, 8, ['if ( !functor.res.isList() )']),
- ( L == fail -> '$writeln_stream'(Out, 12, ['return engine.fail();'])
- ; '$create_pred_name'(L, LL),
- '$writeln_stream'(Out, 12, ['return ', LL, ';'])
- ),
- '$writeln_stream'(Out, 8, ['engine.lookUpHash(functor);']).
- '$write_cs'(Out, pickup_resource(_, I, L)) :- !,
- '$writeln_stream'(Out, 8, ['if ( !engine.pickupResource(functor, ', I, ') )']),
- ( L == fail -> '$writeln_stream'(Out, 12, ['return engine.fail();'])
- ; '$create_pred_name'(L, LL),
- '$writeln_stream'(Out, 12, ['return ', LL, ';'])
- ).
- '$write_cs'(Out, has_more_resource(L)) :- !,
- '$create_pred_name'(L, P),
- '$writeln_stream'(Out, 8, ['if ( !engine.hasMoreResource() )']),
- '$writeln_stream'(Out, 12, ['return ', P, ';']).
- '$write_cs'(Out, restore_resource) :- !,
- '$writeln_stream'(Out, 8, ['engine.restoreResource();']).
- '$write_cs'(Out, try_resource(L1, L0)) :- !,
- '$create_pred_name'(L1, P1),
- '$create_pred_name'(L0, P0),
- '$writeln_stream'(Out, 8, ['return engine.tryResource(', P1, ', ', P0, ');']).
- '$write_cs'(Out, retry_resource(L1, L0)) :- !,
- '$create_pred_name'(L1, P1),
- '$create_pred_name'(L0, P0),
- '$writeln_stream'(Out, 8, ['return engine.retryResource(', P1, ', ', P0, ');']).
- '$write_cs'(Out, trust_resource(L)) :- !,
- '$create_pred_name'(L, P),
- '$writeln_stream'(Out, 8, ['return engine.trustResource(', P, ');']).
- %'$write_cs'(Out, consume(I,J)) :- !,
- % '$writeln_stream'(Out, 8, ['engine.consume(', I, ', ', J, ');']).
- %'$write_cs'(Out, execute_closure(J)) :- !,
- % '$writeln_stream'(Out, 8, ['return engine.executeClosure(', J, ');']).
- '$write_cs'(Out, consume_and_exec_closure(I)) :- !,
- '$writeln_stream'(Out, 8, ['return engine.executeClosure(engine.consume(', I, '));']).
- /*---------------------------------
- $B$=$NB>(B
- ---------------------------------*/
- '$write_cs'(_, Instruction) :-
- '$message'([Instruction,is,an,invalid,instruction]),
- fail.
- /*---------------------------------------------------------------
- $B%f!<%F%#%j%F%#(B
- ---------------------------------------------------------------*/
- '$member'(X, [X|_]).
- '$member'(X, [_|Ys]) :- '$member'(X, Ys).
- '$writel_stream'(_, []) :- !.
- '$writel_stream'(Out, [X|Xs]) :-
- write(Out, X),
- '$writel_stream'(Out, Xs).
- '$writeln_stream'(Out, Tab, Xs) :-
- tab(Out, Tab),
- '$writel_stream'(Out, Xs),
- nl(Out).
- '$change_name'(X, Y) :- X =.. [F0|A], atom(F0), !,
- name(F0, L0),
- '$change_name0'(L0, L),
- name(F, L),
- Y =.. [F|A].
- '$change_name'(_, _) :-
- write('Invalid Input!, Can not rename'), nl.
- /* START JJC: idiomatic Prolog names --(one to one)--> idiomatic C# names
- */
- '$change_name0'( X, Z ) :-
- X = [Xh|Xt],
- '$change_start_of_name'( Xh, XhC ),
- '$change_rest_of_name'( Xt, XtC ),
- '$append'( XhC, XtC, Z ).
- '$change_start_of_name'( X, [Z] ) :- '$islower'( X ), !, '$toupper'( X, Z ).
- '$change_start_of_name'( X, Z ) :- X = 95, !, Z = "__".
- '$change_start_of_name'( X, Z ) :- '$isdigit'( X ), !, Z = [95, X].
- '$change_start_of_name'( X, Z ) :- '$isupper'( X ), !, Z = [95, X].
- '$change_start_of_name'( X, Z ) :- name( N, [X] ), '$replacement'( N, Z ).
- '$change_rest_of_name'( [], [] ) :- !.
- '$change_rest_of_name'( "_", "_underscore" ) :- !.
- '$change_rest_of_name'( [X|Xs], [X|Ys] ) :- '$islower'( X ), !,
- '$change_rest_of_name'( Xs, Ys ).
- '$change_rest_of_name'( [X|Xs], [X|Ys] ) :- '$isdigit'( X ), !,
- '$change_rest_of_name'( Xs, Ys ).
- '$change_rest_of_name'( [X|Xs], [95,X|Ys] ) :- '$isupper'( X ), !,
- '$change_rest_of_name'( Xs, Ys ).
- '$change_rest_of_name'( [95,X|Xs], [Y|Ys] ) :- '$islower'( X ), !,
- '$toupper'( X, Y ), '$change_rest_of_name'( Xs, Ys ).
- '$change_rest_of_name'( [95,X|Xs], [95,95,X|Z] ) :- '$isupper'( X ), !,
- '$change_rest_of_name'( Xs, Z ).
- '$change_rest_of_name'( [95,X|Xs], [95,95,X|Z] ) :- '$isdigit'( X ), !,
- '$change_rest_of_name'( Xs, Z ).
- '$change_rest_of_name'( [95,X|Xs], [95,95|Z] ) :-
- name( N, [X] ), '$replacement'( N, Xr ), !, '$append'( Xr, Y, Z ),
- '$change_rest_of_name'( Xs, Y ).
- '$change_rest_of_name'( [X|Xs], [95|Z] ) :- name( N, [X] ), '$replacement'( N, Y ), !,
- '$append'( Y, W, Z ),
- '$change_rest_of_name'( Xs, W ).
- '$change_rest_of_name'( [95,95|Xs], [95,95|Z] ) :-
- '$append'( "underscore", W, Z ),
- '$change_rest_of_name'( Xs, W ).
- '$replacement'( '+', "plus" ).
- '$replacement'( '-', "dash" ).
- '$replacement'( '*', "star" ).
- '$replacement'( '/', "slash" ).
- '$replacement'( '\', "bslash" ).
- '$replacement'( '^', "caret" ).
- '$replacement'( '<', "less" ).
- '$replacement'( '>', "gtr" ).
- '$replacement'( '=', "eq" ).
- '$replacement'( '`', "bquote" ).
- '$replacement'( '~', "tilde" ).
- '$replacement'( ':', "colon" ).
- '$replacement'( '.', "stop" ).
- '$replacement'( '?', "qn" ).
- '$replacement'( '@', "at" ).
- '$replacement'( '#', "hash" ).
- '$replacement'( '&', "amp" ).
- '$replacement'( ';', "scolon" ).
- '$replacement'( '!', "bang" ).
- '$replacement'( ',', "comma" ).
- '$replacement'( '$', "dollar_" ).
- '$islower'( X ) :- "a" =< X, X =< "z".
- '$isupper'( X ) :- "A" =< X, X =< "Z".
- '$isdigit'( X ) :- "0" =< X, X =< "9".
- '$toupper'( X, Z ) :- Z is X - "a" + "A".
- '$constants'(['+','-','*','/','\','^','<','>','=','`',
- '~',':','.','?','@','#','&',';','!',',']).
- '$create_qualified_name'(FA, S) :- FA = '$res'(F/A, N), !,
- '$file_name'(File),
- '$change_name'(F, F1),
- '$change_name'(File, File1),
- % put in resource just to get it working - structure this better later.
- '$list_to_string'([ 'Resources.resource_', N, '_', File1, '_', F1, '_', A], S). % removed RES_
- '$create_qualified_name'(FA, S) :-
- '$create_pred_name'(FA, Pred),
- '$list_to_string'([ 'Predicates.', Pred], S).
- /* END JJC */
- /* $$B$OH4$$$F$"$k(B */
- /* $B=R8lL>$N:n@.(B $BNc(B) foo/2 --> foo_2 */
- '$create_pred_name'(FA, X) :- atom(FA), !,
- '$change_name'(FA, FA1),
- '$list_to_string'([FA1, '_', 0], X).
- /* $B%j%=!<%9$NL>A0(B */
- '$create_pred_name'(FA, X) :- FA = '$res'(F/A, N), !,
- '$change_name'(F, F1),
- '$list_to_string'([F1, '_', A, '_res', N], X).
- %'$create_pred_name'(FA, X) :- FA = '$res'(F/A, N), !,
- % '$file_name'(File),
- % '$change_name'(F, F1),
- % '$list_to_string'(['$res', N, '_', File, '_', F1, '_', A], X).
- '$create_pred_name'(FA, X) :- FA = F/A, !,
- '$change_name'(F, F1),
- '$list_to_string'([F1, '_', A], X).
- '$create_pred_name'(FA, X) :- FA = F/A+G/B, !,
- '$change_name'(F, F1),
- '$list_to_string'([F1, '_', A, '_', G, '_', B], X).
- '$create_pred_name'(FA, X) :- FA = F/A+N,
- '$change_name'(F, F1),
- '$list_to_string'([F1, '_', A, '_', N], X).
- /* START JJC
- * Most irritatingly Windows' filesystems are not case sensitive,
- * so we will put a ' before the initial letter if it is lower case,
- * and a space before a subsequent letter if it is upper case
- */
- '$create_filename'( X, Z ) :-
- '$create_class_name'( X, Y ),
- name( Y, N ),
- N = [Yh|Yt],
- '$change_start_of_filename'( Yh, YhC ),
- '$change_rest_of_filename'( Yt, YtC ),
- '$append'( YhC, YtC, Zn ),
- name( Z, Zn ).
- '$change_start_of_filename'( X, Z ) :- '$islower'( X ), !, Z = [39,X].
- '$change_start_of_filename'( X, [X] ).
- '$change_rest_of_filename'( [], [] ) :- !.
- '$change_rest_of_filename'( [X|Xs], [32,X|Ys ] ) :- '$isupper'( X ), !,
- '$change_rest_of_filename'( Xs, Ys ).
- '$change_rest_of_filename'( [X|Xs], [X|Ys] ) :-
- '$change_rest_of_filename'( Xs, Ys ).
- /* END JJC */
- /* $B%/%i%9L>$N:n@.(B */
- '$create_class_name'(FA, S) :- FA = '$res'(F/A, N), !,
- '$file_name'(File),
- '$change_name'(F, F1),
- '$change_name'(File, File1),
- % put in resource just to get it working - structure this better later.
- '$list_to_string'([ 'resource_', N, '_', File1, '_', F1, '_', A], S). % removed RES_
- '$create_class_name'(FA, S) :-
- '$create_pred_name'(FA, Pred),
- '$list_to_string'([Pred], S). % removed PRED_
- %'$create_res_class_name'(FA+N, S) :-
- % '$create_pred_name'(FA, Pred),
- % '$list_to_string'(['RES_', N, '_', Pred], S).
- /* Register$BL>$N:n@.(B $BNc(B) a(3) --> a3 */
- '$create_reg_name'(X, _) :- var(X),!,
- '$message'([invalid,argument,in,create_reg_name/2]), fail.
- '$create_reg_name'(a(N), X) :- !, '$list_to_string'(['a', N], X).
- '$create_reg_name'(s(N), X) :- !, '$list_to_string'(['s', N], X).
- '$create_reg_name'(p(N), X) :- !, '$list_to_string'(['p', N], X).
- '$create_reg_name'(h(N), X) :- !, '$list_to_string'(['h', N], X).
- '$create_reg_name'(f(N), X) :- !, '$list_to_string'(['f', N], X).
- %'$create_reg_name'(void, X) :- !, X = 'new VariableTerm()'.
- '$create_reg_name'(void, X) :- !, X = 'engine.makeVariable()'.
- '$create_reg_name'(cont, X) :- !, X = 'cont'.
- '$create_reg_name'(functor, X) :- !, X = 'functor'.
- '$create_reg_name'(P, P) :-
- '$message'([invalid,argument,in,create_reg_name/2]), fail.
- /* Prolog$B$N(Batom$B$r(BJava$B$N(BString$B$X(B */
- '$atom_to_string'(PrologAtom, CsAtom) :-
- name(PrologAtom, Ps),
- '$add_backslash'(Ps, Js),
- name(CsAtom, Js).
- '$add_backslash'([], []) :- !.
- '$add_backslash'([P|Ps], [P,P|Js]) :- P =:= "\", !,
- '$add_backslash'(Ps, Js).
- '$add_backslash'([P|Ps], [Q,P|Js]) :- P =:= """", !,
- Q is "\",
- '$add_backslash'(Ps, Js).
- '$add_backslash'([P|Ps], [P|Js]) :-
- '$add_backslash'(Ps, Js).
- /* $BG[Ns@k8@$N1&B&$N:n@.(B [a(1),a(2)] --> ['{'a1,', ', a2, '};'] */
- '$create_str_args'(X, Z) :-
- '$create_reg_args'(X, Y),
- '$append'(['{'|Y], ['};'], Z).
- /* $B%+%s%^$G6h@Z$i$l$?(BRegister$BL>$NNs(B [a(1),a(2)] --> [a1,',',a2] */
- '$create_reg_args'([X], [Y]) :- !,
- '$create_reg_name'(X, Y).
- '$create_reg_args'([X|Xs], [Y, ', '|Ys]) :-
- '$create_reg_name'(X, Y),
- '$create_reg_args'(Xs, Ys).
- /* $BJXMx(B */
- '$wj_assign_number'(_, N, _, _, _) :- N =< 0, !.
- '$wj_assign_number'(Out, N, Sym, I, End) :- I =:= N, !,
- '$writel_stream'(Out, [Sym, I, End]).
- '$wj_assign_number'(Out, N, Sym, I, End) :-
- I < N,
- J is I+1,
- '$writel_stream'(Out, [Sym, I, ', ']),
- '$wj_assign_number'(Out, N, Sym, J, End).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement