asserta(is_member(field(Name, Value, Type), ClassName)),
add_part(ClassName, OtherParts).
-add_part(ClassName, [method(Name, ArgList, Form) | OtherParts]) :-
+add_part(ClassName, [method(Name, ArgList, Body) | OtherParts]) :-
is_list(ArgList),
is_class(ClassName),
- asserta(is_member(method(Name, ArgList, Form), ClassName)),
+ asserta(is_member(method(Name, ArgList, Body), ClassName)),
+
+ Fn =.. [Name, Instance | ArgList],
+ asserta(Fn :- call_method(Instance, ClassName, ArgList, Body)),
+
add_part(ClassName, OtherParts).
#+end_src
field(InstanceName, Field, Value),
check_fields(InstanceName, Other).
#+end_src
+
+** define_this
+#+begin_src prolog :tangle oop.pl
+define_this(Instance) :-
+ \+ is_list(Instance),
+ !,
+ findall(Key = Value, field(Instance, Key, Value), List),
+ define_this(List).
+
+define_this([]) :- !.
+define_this([=(Key, Value) | Other]) :-
+ asserta(field(this, Key, Value)),
+ define_this(Other).
+#+end_src
+
+** call_method
+#+begin_src prolog :tangle oop.pl
+call_method(Instance, ClassName, ArgList, Body) :-
+ is_instance(Instance, ClassName),
+ define_this(Instance),
+ maplist(call, [Body | ArgList]),
+ !,
+ retractall(field(this, _, _)).
+#+end_src
asserta(is_member(field(Name, Value, Type), ClassName)),
add_part(ClassName, OtherParts).
-add_part(ClassName, [method(Name, ArgList, Form) | OtherParts]) :-
+add_part(ClassName, [method(Name, ArgList, Body) | OtherParts]) :-
is_list(ArgList),
is_class(ClassName),
- asserta(is_member(method(Name, ArgList, Form), ClassName)),
+ asserta(is_member(method(Name, ArgList, Body), ClassName)),
+
+ Fn =.. [Name, Instance | ArgList],
+ asserta(Fn :- call_method(Instance, ClassName, ArgList, Body)),
+
add_part(ClassName, OtherParts).
set_default_fields_for(_InstanceName, _ClassName, []) :- !.
check_fields(InstanceName, [=(Field, Value) | Other]) :-
field(InstanceName, Field, Value),
check_fields(InstanceName, Other).
+
+define_this(Instance) :-
+ \+ is_list(Instance),
+ !,
+ findall(Key = Value, field(Instance, Key, Value), List),
+ define_this(List).
+
+define_this([]) :- !.
+define_this([=(Key, Value) | Other]) :-
+ asserta(field(this, Key, Value)),
+ define_this(Other).
+
+call_method(Instance, ClassName, ArgList, Body) :-
+ is_instance(Instance, ClassName),
+ define_this(Instance),
+ maplist(call, [Body | ArgList]),
+ !,
+ retractall(field(this, _, _)).