we have macros. OOOOOOFFF
This commit is contained in:
+72
-21
@@ -1,29 +1,80 @@
|
||||
;; (define-syntax defclass (name members :rest functions)
|
||||
;; (list 'defun (string->symbol (concat-strings "make-" (symbol->string name)))
|
||||
|
||||
(defun make-vector (x y z)
|
||||
(let ((local-x x)
|
||||
(local-y y)
|
||||
(local-z z)
|
||||
(defun (macro (@name @params :rest @body)
|
||||
(eval (pair 'define-upwards (pair @name (pair (pair 'lambda (pair @params @body)) nil)))))))
|
||||
|
||||
(defun set-x (new-x) (mutate local-x new-x))
|
||||
(defun set-y (new-y) (mutate local-y new-y))
|
||||
(defun set-z (new-z) (mutate local-z new-z))
|
||||
;; ))
|
||||
|
||||
(defun dispatch (message)
|
||||
(if (= message ::get-x)
|
||||
local-x
|
||||
(if (= message ::set-x)
|
||||
set-x
|
||||
nil)))
|
||||
;; (defclass vector3 (x y z)
|
||||
;; ;; getters and setters will be auto generated
|
||||
|
||||
(break)
|
||||
dispatch))
|
||||
;; (defun ->length ()
|
||||
;; (** (+ (* x x) (* y y) (* z z)) 0.5))
|
||||
|
||||
(define v (make-vector 1 2 3))
|
||||
;; (defun ->scale (fac)
|
||||
;; (mutate x (* fac x))
|
||||
;; (mutate y (* fac y))
|
||||
;; (mutate z (* fac z)))
|
||||
|
||||
(print (v ::get-x))
|
||||
((v ::set-x) 19)
|
||||
(print (v ::get-x))
|
||||
;; )
|
||||
|
||||
(defun make-vector3 (x-coord y-coord z-coord)
|
||||
(let ((x x-coord)
|
||||
(y y-coord)
|
||||
(z z-coord))
|
||||
|
||||
(defun ->get-x () x)
|
||||
(defun ->get-y () y)
|
||||
(defun ->get-z () z)
|
||||
(defun ->set-x (new-x) (mutate x new-x))
|
||||
(defun ->set-y (new-y) (mutate y new-y))
|
||||
(defun ->set-z (new-z) (mutate z new-z))
|
||||
|
||||
(defun ->length ()
|
||||
(** (+ (* x x) (* y y) (* z z)) 0.5))
|
||||
|
||||
(defun ->scale (fac)
|
||||
(mutate x (* fac x))
|
||||
(mutate y (* fac y))
|
||||
(mutate z (* fac z)))
|
||||
|
||||
(defun ->print ()
|
||||
(print "[vector3] (")
|
||||
(print x)
|
||||
(print " ")
|
||||
(print y)
|
||||
(print " ")
|
||||
(print z)
|
||||
(print ")\n"))
|
||||
|
||||
(defun ->+ (other)
|
||||
(make-vector
|
||||
(+ x ((other ->get-x)))
|
||||
(+ y ((other ->get-y)))
|
||||
(+ z ((other ->get-z)))))
|
||||
|
||||
(defun ->- (other)
|
||||
(make-vector
|
||||
(- x ((other ->get-x)))
|
||||
(- y ((other ->get-y)))
|
||||
(- z ((other ->get-z)))))
|
||||
|
||||
(defun ->scalar-product (other)
|
||||
(+ (* x ((other ->get-x)))
|
||||
(* y ((other ->get-y)))
|
||||
(* z ((other ->get-z)))))
|
||||
|
||||
(defun ->cross-product (other)
|
||||
(make-vector
|
||||
(- (* y ((other ->get-z))) (* z ((other ->get-y))))
|
||||
(- (* z ((other ->get-x))) (* x ((other ->get-z))))
|
||||
(- (* x ((other ->get-y))) (* y ((other ->get-x))))))
|
||||
|
||||
(special-lambda (message) (eval message))))
|
||||
|
||||
(define v1 (make-vector3 1 2 3))
|
||||
(define v2 (make-vector3 3 2 1))
|
||||
|
||||
(print ((v1 ->length)))
|
||||
(print ((v2 ->length)))
|
||||
|
||||
(read " ")
|
||||
|
||||
Reference in New Issue
Block a user