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.
 
 
 
 
 
 

112 lines
3.2 KiB

  1. (define (type-wrap obj type)
  2. (set-type obj type)
  3. obj)
  4. (define-syntax defclass (name members :rest body)
  5. "Macro for creatating classes."
  6. (define (underscore sym)
  7. (string->symbol (concat-strings "_" (symbol->string sym))))
  8. (define underscored-members (map underscore members))
  9. ;; the wrapping let environment
  10. (define let-body (list 'let (zip members underscored-members)))
  11. ;; the body
  12. (map (lambda (fun) (append let-body fun)) body)
  13. ;; the dispatch function
  14. (append let-body '(special-lambda
  15. (message :rest args)
  16. "This is the docs for the handle"
  17. (eval (extend (list message) args))))
  18. ;; stuff it all in the constructor function
  19. (list 'define
  20. (pair (string->symbol (concat-strings "make-" (symbol->string name))) underscored-members)
  21. "This is the handle to an object of the class "
  22. let-body))
  23. (define (make-vector3 _x _y _z)
  24. "This is the handle to an object of the class "
  25. (let ((x _x)
  26. (y _y)
  27. (z _z))
  28. (define (get-x) x)
  29. (define (get-y) y)
  30. (define (get-z) z)
  31. (define (set-x new-x) (mutate x new-x))
  32. (define (set-y new-y) (mutate y new-y))
  33. (define (set-z new-z) (mutate z new-z))
  34. (define (length) (** (+ (* x x) (* y y) (* z z)) 0.500000))
  35. (define (scale fac) (mutate x (* fac x)) (mutate y (* fac y)) (mutate z (* fac z)) fac)
  36. (define (add other) (make-vector3 (+ x (other get-x)) (+ y (other get-y)) (+ z (other get-z))))
  37. (define (subtract other) (make-vector3 (- x (other get-x)) (- y (other get-y)) (- z (other get-z))))
  38. (define (scalar-product other) (+ (* x (other get-x)) (* y (other get-y)) (* z (other get-z))))
  39. (define (cross-product other) (make-vector3 (- (* y (other get-z)) (* z (other get-y))) (- (* z (other get-x)) (* x (other get-z))) (- (* x (other get-y)) (* y (other get-x)))))
  40. (define (printout) (printf "[vector3] (" x y z ")"))
  41. (special-lambda
  42. (message :rest args)
  43. "This is the docs for the handle"
  44. (eval (extend (list message) args)))))
  45. ;; (v1 print)
  46. ;; (v1 length)
  47. ;; (v1 get-x)
  48. ;; (v1 set-x 10)
  49. (defclass vector3 (x y z)
  50. (define (get-x) x)
  51. (define (get-y) y)
  52. (define (get-z) z)
  53. (define (set-x new-x) (mutate x new-x))
  54. (define (set-y new-y) (mutate y new-y))
  55. (define (set-z new-z) (mutate z new-z))
  56. (define (length)
  57. (** (+ (* x x) (* y y) (* z z)) 0.5))
  58. (define (scale fac)
  59. (mutate x (* fac x))
  60. (mutate y (* fac y))
  61. (mutate z (* fac z))
  62. fac)
  63. (define (add other)
  64. (make-vector3
  65. (+ x (other get-x))
  66. (+ y (other get-y))
  67. (+ z (other get-z))))
  68. (define (subtract other)
  69. (make-vector3
  70. (- x (other get-x))
  71. (- y (other get-y))
  72. (- z (other get-z))))
  73. (define (scalar-product other)
  74. (+ (* x (other get-x))
  75. (* y (other get-y))
  76. (* z (other get-z))))
  77. (define (cross-product other)
  78. (make-vector3
  79. (- (* y (other get-z)) (* z (other get-y)))
  80. (- (* z (other get-x)) (* x (other get-z)))
  81. (- (* x (other get-y)) (* y (other get-x)))))
  82. (define (printout)
  83. (printf "[vector3] (" x y z ")"))
  84. )
  85. (define v1 (make-vector3 1 2 3))
  86. (define v2 (make-vector3 3 2 1))
  87. (assert (= (v1 scalar-product v2) 10))