34
35:- module(pce_goal_expansion, []). 36:- use_module(pce_realise). 37:- use_module(pce_boot(pce_expansion), [pce_compiling/1]). 38:- use_module(pce_boot(pce_principal)). 39:- require([ pce_error/1
40 , append/3
41 ]). 42
43expandable(send, A) :- A >= 2.
44expandable(get, A) :- A >= 3.
45expandable(send_super, A) :- A >= 2.
46expandable(get_super, A) :- A >= 3.
47expandable(pce_catch_error, 2).
48
50
51expand(OldSend, Send) :-
52 compound(OldSend),
53 OldSend =.. [ send, R, Sel | Args ],
54 atom(Sel),
55 Args \== [],
56 !,
57 Msg =.. [Sel|Args],
58 Send = send(R, Msg).
59expand(OldGet, Get) :-
60 compound(OldGet),
61 OldGet =.. [ get, R, Sel | AllArgs ],
62 atom(Sel),
63 append(Args, [Result], AllArgs),
64 Args \== [],
65 !,
66 Msg =.. [Sel|Args],
67 Get = get(R, Msg, Result).
68
70
71expand(send(R, Msg), send_class(R, Super, SuperMsg)) :-
72 compound(Msg),
73 Msg =.. [send_super, Selector | Args],
74 !,
75 selector(Selector),
76 current_super_class(send_super, Super),
77 SuperMsg =.. [Selector|Args].
78expand(get(R, Msg, Answer), get_class(R, Super, SuperMsg, Answer)) :-
79 compound(Msg),
80 Msg =.. [get_super, Selector | Args],
81 !,
82 selector(Selector),
83 current_super_class(get_super, Super),
84 SuperMsg =.. [Selector|Args].
85expand(send_super(R, Msg), send_class(R, Super, Msg)) :-
86 !,
87 current_super_class(send_super, Super).
88expand(get_super(R, Msg, V), get_class(R, Super, Msg, V)) :-
89 !,
90 current_super_class(get_super, Super).
91expand(SendSuperN, send_class(R, Super, Msg)) :-
92 compound(SendSuperN),
93 SendSuperN =.. [send_super, R, Sel | Args],
94 selector(Sel),
95 Msg =.. [Sel|Args],
96 current_super_class(send_super, Super).
97expand(GetSuperN, get_class(R, Super, Msg, Answer)) :-
98 compound(GetSuperN),
99 GetSuperN =.. [get_super, R, Sel | AllArgs],
100 selector(Sel),
101 append(Args, [Answer], AllArgs),
102 Msg =.. [Sel|Args],
103 current_super_class(get_super, Super).
104expand(pce_catch_error(E,G), pce_catch_error(E, EG)) :-
105 expand_goal(G, EG).
106
107selector(Sel) :-
108 atom(Sel),
109 !.
110selector(Sel) :-
111 pce_error(error(type_error(selector, Sel), _)),
112 fail.
119current_super_class(_, Super) :-
120 pce_compiling(Class),
121 super_class(Class, Super),
122 !.
123current_super_class(Op, _) :-
124 pce_error(context_error(Op, nosuper, goal)),
125 fail.
134super_class(Class, Super) :-
135 pce_expansion:attribute(Class, super, Super),
136 !.
137super_class(Class, Super) :-
138 pce_prolog_class(Class, Super),
139 !.
140super_class(Class, Super) :-
141 get(@classes, member, Class, ClassObject),
142 get(ClassObject, super_class_name, Super).
143
144
145 148
149pce_ifhostproperty(prolog(sicstus),
150( user:goal_expansion(Goal, _Context, ExpandedGoal) :-
151 compound(Goal),
152 functor(Goal, Name, Arity),
153 expandable(Name, Arity),
154 expand(Goal, ExpandedGoal)
155),
156( system:goal_expansion(Goal, ExpandedGoal) :-
157 compound(Goal),
158 compound_name_arity(Goal, Name, Arity),
159 expandable(Name, Arity),
160 expand(Goal, ExpandedGoal)
161))