From 17b18b2cc496ce4e47988feac9d021ea21816502 Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Wed, 20 Dec 2023 12:33:30 +0100 Subject: [PATCH] Metodi ez --- Prolog/README.org | 32 ++++++++++++++++++++++++++++++-- Prolog/oop.pl | 26 ++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Prolog/README.org b/Prolog/README.org index ebe70c8..9b22483 100644 --- a/Prolog/README.org +++ b/Prolog/README.org @@ -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 diff --git a/Prolog/oop.pl b/Prolog/oop.pl index b6f6d4f..06bcfda 100644 --- a/Prolog/oop.pl +++ b/Prolog/oop.pl @@ -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, _, _)). -- 2.52.0