Advertisement
Guest User

Untitled

a guest
Aug 4th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.17 KB | None | 0 0
  1.  
  2. (deftag compile [body] (quasiquote (eval-when-compile ~body)))
  3.  
  4. #compile
  5. (import [hy.models [HyList HyDict HySymbol HyKeyword]])
  6.  
  7. #compile
  8. (defn annotation-symbol? [form]
  9.   (and (instance? HyKeyword form) (= (name form) ":")))
  10.  
  11. #compile
  12. (defn ldrop [count coll] (list (drop count coll)))
  13.  
  14. #compile
  15. (defn get-annotations [args annotations valid-args]
  16.   (if
  17.     (empty? args) None
  18.  
  19.     (= (len args) 1) (.append valid-args (get args 0))
  20.  
  21.     (annotation-symbol? (get args 1))
  22.       (do
  23.         (.append annotations (mangle (get args 0)))
  24.         (.append annotations (get args 2))
  25.         (.append valid-args (get args 0))
  26.         (get-annotations (ldrop 3 args) annotations valid-args))
  27.  
  28.     (do
  29.       (.append valid-args (get args 0))
  30.       (get-annotations (ldrop 1 args) annotations valid-args))))
  31.  
  32. (defmacro defna [name args expression]
  33.   (do
  34.      (setv annotations [])
  35.      (setv valid-args  [])
  36.      (get-annotations args annotations valid-args)
  37.      (quasiquote
  38.       (do
  39.         (defn ~name ~(HyList valid-args) ~expression)
  40.         (assoc (. ~name __annotations__) ~@annotations)))))
  41.  
  42. ;; Example
  43.  
  44. (defna add+ [a :: int b :: int c] (+ a c))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement