~subsetpark/ec

91f88d6a778e1f17c8e3bed2586333e1100b013f — Zach Smith 2 months ago ba83822
Move Symbol to parser
5 files changed, 20 insertions(+), 18 deletions(-)

M src/calc.janet
M src/parser.janet
M src/print.janet
M test/env.janet
M test/parser.janet
M src/calc.janet => src/calc.janet +0 -4
@@ 22,10 22,6 @@
  [n]
  (not (zero? (n :value))))

(defproto Symbol Element
  value {:init? true}
  quoted? {:default false})

(defproto Operation Element
  type {}
  arity {}

M src/parser.janet => src/parser.janet +14 -9
@@ 1,5 1,10 @@
(use fugue)
(import /src/calc)

(defproto Symbol calc/Element
  value {:init? true}
  quoted? {:default false})

(defn handle-parens
  [& patt]
  [:parens (string ;patt)])


@@ 14,23 19,23 @@

(defn handle-float
  [patt]
  (:new calc/Float (scan-number patt)
        :doc patt))
  (calc/new-Float (scan-number patt)
                  :doc patt))

(defn handle-int
  [patt]
  (:new calc/Int (scan-number patt)
        :doc patt))
  (calc/new-Int (scan-number patt)
                :doc patt))

(defn handle-word
  [patt]
  (cond
    (index-of patt ["." "p" "s" "?" "??"]) [:special patt]
    (string/has-prefix? "'" patt) (:new calc/Symbol
                                        (string/slice patt 1)
                                        :quoted? true)
    (:new calc/Symbol patt
          :quoted? false)))
    (string/has-prefix? "'" patt) (new-Symbol
                                    (string/slice patt 1)
                                    :quoted? true)
    (new-Symbol patt
                :quoted? false)))

(def- peg (peg/compile
            ~{:main (any (+ :s+ :quotes :vectors :token))

M src/print.janet => src/print.janet +2 -1
@@ 1,6 1,7 @@
(use fugue)

(import /src/calc)
(import /src/parser)

(defmulti p [calc/Int]
  [n] (string (n :value)))


@@ 14,7 15,7 @@
(defmulti p [calc/Adverb]
  [a] (string (a :type)))

(defmulti p [calc/Symbol]
(defmulti p [parser/Symbol]
  [{:value value}] value)

(varfn join [q] nil)

M test/env.janet => test/env.janet +1 -1
@@ 31,7 31,7 @@
        (is (calc/Int? one))
        (is (= 1 (one :value)))

        (is (calc/Symbol? plus))
        (is (parser/Symbol? plus))
        (is (= "+" (plus :value)))))))

(deftest quotation-application

M test/parser.janet => test/parser.janet +3 -3
@@ 38,14 38,14 @@
(deftest word
  (let [[add i] (parser/parse "+ i")]
    (is (= "+" (add :value)))
    (is (calc/Symbol? add))
    (is (parser/Symbol? add))

    (is (= "i" (i :value)))
    (is (calc/Symbol? i))))
    (is (parser/Symbol? i))))

(deftest symbol
  (let [[parsed] (parser/parse "'foo")]
    (is (calc/Symbol? parsed))
    (is (parser/Symbol? parsed))
    (is (= true (parsed :quoted?)))
    (is (= "foo" (parsed :value)))))