You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 regels
1.9 KiB

  1. (define-syntax defclass (name members :rest body)
  2. "Macro for creatating classes."
  3. (defun underscore (sym)
  4. (string->symbol (concat-strings "_" (symbol->string sym))))
  5. (define underscored-members (map underscore members))
  6. ;; the wrapping let environment
  7. (define let-body (list 'let (zip members underscored-members)))
  8. ;; the body
  9. (map (lambda (fun) (append let-body fun)) body)
  10. ;; the dispatch function
  11. (append let-body (list 'special-lambda '(message :rest args)
  12. "This is the docs for the handle"
  13. '(eval (extend (list message) args))))
  14. ;; stuff it all in the constructor function
  15. (eval (list 'defun (string->symbol (concat-strings "make-" (symbol->string name))) underscored-members
  16. "This is the handle to an object of the class "
  17. let-body)))
  18. ;; (v1 print)
  19. ;; (v1 length)
  20. ;; (v1 get-x)
  21. ;; (v1 set-x 10)
  22. (defclass vector3 (x y z)
  23. (defun get-x () x)
  24. (defun get-y () y)
  25. (defun get-z () z)
  26. (defun set-x (new-x) (mutate x new-x))
  27. (defun set-y (new-y) (mutate y new-y))
  28. (defun set-z (new-z) (mutate z new-z))
  29. (defun length ()
  30. (** (+ (* x x) (* y y) (* z z)) 0.5))
  31. (defun scale (fac)
  32. (mutate x (* fac x))
  33. (mutate y (* fac y))
  34. (mutate z (* fac z))
  35. fac)
  36. (defun add (other)
  37. (make-vector3
  38. (+ x (other get-x))
  39. (+ y (other get-y))
  40. (+ z (other get-z))))
  41. (defun subtract (other)
  42. (make-vector3
  43. (- x (other get-x))
  44. (- y (other get-y))
  45. (- z (other get-z))))
  46. (defun scalar-product (other)
  47. (+ (* x (other get-x))
  48. (* y (other get-y))
  49. (* z (other get-z))))
  50. (defun cross-product (other)
  51. (make-vector3
  52. (- (* y (other get-z)) (* z (other get-y)))
  53. (- (* z (other get-x)) (* x (other get-z)))
  54. (- (* x (other get-y)) (* y (other get-x)))))
  55. (defun printout ()
  56. (printf "[vector3] (" x y z ")"))
  57. )
  58. (define v1 (make-vector3 1 2 3))
  59. (define v2 (make-vector3 3 2 1))