Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

82 строки
2.1 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 `(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 `(type-wrap
  15. (special-lambda
  16. (message :rest args)
  17. "This is the docs for the handle"
  18. (eval (extend (list message) args))) ,(symbol->keyword name)))
  19. ;; stuff it all in the constructor function
  20. `(define
  21. ,(pair (string->symbol (concat-strings "make-" (symbol->string name))) underscored-members)
  22. ,(concat-strings "This is the handle to an object of the class " (symbol->string name))
  23. ,let-body))
  24. (defclass vector3 (x y z)
  25. (define (get-x) x)
  26. (define (get-y) y)
  27. (define (get-z) z)
  28. (define (set-x new-x) (mutate x new-x))
  29. (define (set-y new-y) (mutate y new-y))
  30. (define (set-z new-z) (mutate z new-z))
  31. (define (length)
  32. (** (+ (* x x) (* y y) (* z z)) 0.5))
  33. (define (scale fac)
  34. (mutate x (* fac x))
  35. (mutate y (* fac y))
  36. (mutate z (* fac z))
  37. fac)
  38. (define (add other)
  39. (make-vector3
  40. (+ x (other get-x))
  41. (+ y (other get-y))
  42. (+ z (other get-z))))
  43. (define (subtract other)
  44. (make-vector3
  45. (- x (other get-x))
  46. (- y (other get-y))
  47. (- z (other get-z))))
  48. (define (scalar-product other)
  49. (+ (* x (other get-x))
  50. (* y (other get-y))
  51. (* z (other get-z))))
  52. (define (cross-product other)
  53. (make-vector3
  54. (- (* y (other get-z)) (* z (other get-y)))
  55. (- (* z (other get-x)) (* x (other get-z)))
  56. (- (* x (other get-y)) (* y (other get-x)))))
  57. (define (printout)
  58. (printf "[vector3] (" x y z ")"))
  59. )
  60. (define v1 (make-vector3 1 2 3))
  61. (define v2 (make-vector3 3 2 1))
  62. (assert (= (v1 scalar-product v2) 10))
  63. (assert (= (type v1) :vector3))