PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/internal/src/mtscheme/core.clj

http://github.com/martintrojer/scheme-clojure
Clojure | 76 lines | 28 code | 17 blank | 31 comment | 5 complexity | 119c99b41e3c924b4c3b88d3ebab7aea MD5 | raw file
Possible License(s): GPL-3.0
  1. (ns mtscheme.core
  2. (:gen-class)
  3. (:refer-clojure :exclude [cond cons let]))
  4. ;; cond
  5. (defmacro cond [& args]
  6. (when args
  7. (clojure.core/let [fst# (ffirst args)]
  8. (list `if (if (= fst# (symbol "else")) :else fst#)
  9. (second (first args))
  10. (clojure.core/cons 'cond (next args))))))
  11. ;;(clojure.walk/macroexpand-all
  12. ;;(macroexpand-1
  13. ;; '(cond ((< x 0) (- 0 x)) ((= x 0) 100) (else x)))
  14. ;;(macroexpand-1
  15. ;;(clojure.walk/macroexpand-all
  16. ;; '(cond ((> 3 2) 'greater)
  17. ;; ((< 3 2) 'less)))
  18. (defn cons [fst snd]
  19. (clojure.core/cons fst (if (coll? snd) snd [snd])))
  20. ;; append
  21. (def append concat)
  22. ;; car / cdr
  23. (def car first)
  24. (def cdr rest)
  25. ;; null?
  26. (def null? empty?)
  27. ;;(coll? 1)
  28. ;; define
  29. (defmacro define [name-and-params body]
  30. (if (coll? name-and-params) ; function or var definition?
  31. `(defn ~(first name-and-params) [~@(rest name-and-params)] ~body)
  32. `(def ~name-and-params ~body)))
  33. ;;(defmacro define [& args]
  34. ;; `(def ~@args))
  35. ;; (macroexpand '(define lisa 1))
  36. ;; (macroexpand '(define nisse (+ 1 1 1)))
  37. ; let
  38. (defmacro let [& args]
  39. (clojure.core/let [body# (first (rest args))
  40. vars# (reduce concat [] (first args))]
  41. `(clojure.core/let [~@vars#] ~body#)))
  42. ;; (macroexpand '(let lisa 1)) ; serror
  43. ;; (macroexpand '(let lisa 1 (+ lisa 1))) ; serror
  44. ;; (macroexpand '(let lisa (+ 1 1))) ; serror
  45. ;; (macroexpand '(let (a 1))) ; serror
  46. ;; (macroexpand '(let ((a 1))))
  47. ;; (macroexpand '(let ((a 1)) a))
  48. ;; (macroexpand '(let ((a 1)(b 2))(+ a b)))
  49. ;; (macroexpand '(let (a 1 b 2)))
  50. ;; (macroexpand (let ((a (+ 1 1))(b 3)) (+ a b)))
  51. ;; begin
  52. (defmacro begin [& args]
  53. `(do ~@args))
  54. ;; lambda
  55. (defmacro lambda [fst snd & _]
  56. `(fn [~@fst] ~snd))
  57. ;; (macroexpand-1 '(lambda (x) (+ x v)))
  58. ;; display
  59. (def display println)