~subsetpark/fugue

55567281ae3786cb21665af0b586b998374f7cea — Zach Smith 2 years ago 854d1d5 v0.5.1
fix #14 - eval symbol when processing match
2 files changed, 15 insertions(+), 3 deletions(-)

M fugue.janet
M test/namespaces.janet
M fugue.janet => fugue.janet +10 -3
@@ 884,6 884,12 @@
         (errorf "Expected a %s, got: %q" ,(string proto) (,get-type-or-proto ,obj))
         ,(tuple obj field)))))

(defn- maybe-get-proto-name
  [proto-symbol]
  # TODO: Handle the case where it's not defined.
  (let [proto (eval proto-symbol)]
    (proto :_name)))

(defmacro match
  ```
  Prototype-aware version of `match`. Introduces one new case form:


@@ 894,12 900,13 @@
  ```
  [x & cases]
  (defn transform
    "preprocessor for match patterns. Translates fugue patterns into std ones"
    [[pattern exp]]
    [(match pattern
       @[(@ '@) proto-name attrs]
       @[(@ '@) proto-symbol attrs]
       (do
         (validate-proto-match proto-name attrs)
         (struct ;(kvs attrs) :_name (string proto-name)))
         (validate-proto-match proto-symbol attrs)
         (struct ;(kvs attrs) :_name (maybe-get-proto-name proto-symbol)))

       pattern)
     exp])

M test/namespaces.janet => test/namespaces.janet +5 -0
@@ 28,4 28,9 @@
  (is (= :ok (a/h :ok)))
  (is (= "myname" (a/h (b/new-B :name "myname")))))

(deftest match-with-prefix
  (let [an-a (:new a/A)]
    (is (= true (fugue/match an-a
                             (@ a/A {}) true)))))

(run-tests!)