]> git.leonardobizzoni.com Git - ObjectOriented-Prolog-Lisp/commitdiff
Migliorato chiamata metodi ma rotta la chiamata al corpo
authorLeonardoBizzoni <leo2002714@gmail.com>
Wed, 3 Jan 2024 15:27:51 +0000 (16:27 +0100)
committerLeonardoBizzoni <leo2002714@gmail.com>
Wed, 3 Jan 2024 15:27:51 +0000 (16:27 +0100)
Lisp/README.org
Lisp/ool.lisp

index f7ced47a4f62f418fc6eea7e7ea8d73b5dd2360c..6149d7295c82497cc0f1a16e570bc5dfc474584a 100644 (file)
 (defun create-methods (classname methods)
   (mapcar
    (lambda (method)
-     (eval (list* 'defun
-                      (first method)
-                      (append '(inst) (second method))
-                      (append '((unless (is-instance inst)
-                                  (error "~A non è un'istanza" inst)))
-                              (cddr method)))))
+     (setf (symbol-function (first method))
+           (lambda (this &rest args)
+               (unless (is-instance this)
+                 (error "~A non è un'istanza" this))
+               (unless (get-method (getf this :methods) (first method))
+                 (error "~A non ha un metodo ~A" (getf this :classname) (first method)))
+               (unless (eq (length args)
+                           (length (second (get-method (getf this :methods) (first method)))))
+                 (error "~A numero di argomenti errato" (first method)))
+                     (cddr (get-method (getf this :methods) (first method))))))
    methods))
+
+(defun get-method (methods name)
+  (cond ((null methods) nil)
+       ((eq (caar methods) name) (car methods))
+        (t (get-method (cdr methods) name))))
 #+end_src
 
 ** Creazione di un'istanza
index 4eba3f86565d586113c2a788129df70ed4d3567a..35d24e5c2f3bf7002b7332afff76623ed4e3c55d 100644 (file)
 (defun create-methods (classname methods)
   (mapcar
    (lambda (method)
-     (eval (list* 'defun
-                      (first method)
-                      (append '(inst) (second method))
-                      (append '((unless (is-instance inst)
-                                  (error "~A non è un'istanza" inst)))
-                              (cddr method)))))
+     (setf (symbol-function (first method))
+           (lambda (this &rest args)
+               (unless (is-instance this)
+                 (error "~A non è un'istanza" this))
+               (unless (get-method (getf this :methods) (first method))
+                 (error "~A non ha un metodo ~A" (getf this :classname) (first method)))
+               (unless (eq (length args)
+                           (length (second (get-method (getf this :methods) (first method)))))
+                 (error "~A numero di argomenti errato" (first method)))
+                     (cddr (get-method (getf this :methods) (first method))))))
    methods))
 
+(defun get-method (methods name)
+  (cond ((null methods) nil)
+       ((eq (caar methods) name) (car methods))
+        (t (get-method (cdr methods) name))))
+
 (defun create-instance (listclass new-fields)
   ;; Se c'è almeno un field da cambiare
   ;; il cui nome non è una proprietà della classe