]> git.leonardobizzoni.com Git - ObjectOriented-Prolog-Lisp/commitdiff
Metodi ez
authorLeonardoBizzoni <leo2002714@gmail.com>
Wed, 20 Dec 2023 11:33:30 +0000 (12:33 +0100)
committerLeonardoBizzoni <leo2002714@gmail.com>
Wed, 20 Dec 2023 11:33:30 +0000 (12:33 +0100)
Prolog/README.org
Prolog/oop.pl

index ebe70c899d075ab46c9d92e755646d6253c93786..9b22483b4036e5fed07d4841522bc4f92c083864 100644 (file)
@@ -204,11 +204,15 @@ add_part(ClassName, [field(Name, Value, Type) | OtherParts]) :-
     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
 
@@ -252,3 +256,27 @@ check_fields(InstanceName, [=(Field, Value) | Other]) :-
     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
index b6f6d4f7dd32781a4226a4576e3d9baaa395064a..06bcfda5d09b5b27504232d84ca4c3f7d73215eb 100644 (file)
@@ -147,11 +147,15 @@ add_part(ClassName, [field(Name, Value, Type) | OtherParts]) :-
     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, []) :- !.
@@ -185,3 +189,21 @@ check_fields(_InstanceName, []).
 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, _, _)).