~subsetpark/ec

94834b663b7832d197e1f4d314b2e2f5b8e7a716 — Zach Smith 9 months ago 4457018 vectors v0.1.0
Use parens for quotes
5 files changed, 43 insertions(+), 22 deletions(-)

M main.janet
M src/parser.janet
M src/print.janet
M test/parser.janet
M test/regressions.janet
M main.janet => main.janet +22 -1
@@ 53,12 53,33 @@
      @[:special patt] (handle-special s patt)
      _ (calc/push s token))))

(def- max-width 40)
(def delimiters-peg (peg/compile
                     ~{:main (+ :parens :brackets)
                       :parens (* (look -1 ")") (not "...") (to "("))
                       :brackets (* (look -1 "]") (not "...") (to "["))}))

(defn- truncate
  [peek]
  (var buf (buffer peek))
  (var last-length (length buf))
  
  (reverse! buf)
  
  (while (> (length buf) 40)
    (set buf (tracev (peg/replace delimiters-peg "..." buf)))
    (if (= (length buf) last-length) (break))
    (set last-length (length buf)))
  
  (string (reverse! buf)))

(defn- prompt
  [s]
  (->> s
       (calc/peek)
       (print/p)
       (string/format "(%s)> ")))
       (truncate)
       (string/format "<%s> $ ")))

(defn repl
  []

M src/parser.janet => src/parser.janet +10 -10
@@ 38,11 38,11 @@
  [& patt]
  [:parens (string ;patt)])

(defn handle-brackets
(defn handle-quotes
  [& patt]
  (calc/quote-wrap ;patt))

(defn handle-vbrackets
(defn handle-vectors
  [& patt]
  (calc/wrap ;patt))



@@ 65,14 65,14 @@
        (errorf "syntax error: unknown word %s" patt))))

(def- peg (peg/compile
            ~{:main (any (+ :s+ :vbrackets :brackets :token))
              :vbrackets (cmt (* ".[" :main "]") ,handle-vbrackets)
              :brackets (cmt (* "[" :main "]") ,handle-brackets)
              :number-part (some (+ :d "_"))
              :float (cmt (<- (* (? "-") :number-part "." :number-part)) ,handle-float)
              :int (cmt (<- (* (? "-") :number-part)) ,handle-int)
              :word (cmt (<- (some (if-not (+ :s (set "()[]")) 1))) ,handle-word)
              :token (+ :float :int :word)}))
           ~{:main (any (+ :s+ :quotes :vectors :token))
             :quotes (cmt (* "(" :main ")") ,handle-quotes)
             :vectors (cmt (* "[" :main "]") ,handle-vectors)
             :number-part (some (+ :d "_"))
             :float (cmt (<- (* (? "-") :number-part "." :number-part)) ,handle-float)
             :int (cmt (<- (* (? "-") :number-part)) ,handle-int)
             :word (cmt (<- (some (if-not (+ :s (set "()[]")) 1))) ,handle-word)
             :token (+ :float :int :word)}))

(defn parse
  [str]

M src/print.janet => src/print.janet +2 -2
@@ 16,9 16,9 @@

(varfn join [q] nil)

(defmulti p [calc/Quotation] [q] (join q "[" "]"))
(defmulti p [calc/Quotation] [q] (join q "(" ")"))

(defmulti p [calc/Vector] [v] (join v ".[" "]"))
(defmulti p [calc/Vector] [v] (join v "[" "]"))

(varfn join
  [q l r]

M test/parser.janet => test/parser.janet +3 -3
@@ 8,12 8,12 @@
(use /test-support)

(deftest brackets
  (let [[parsed] (parser/parse "[]")]
  (let [[parsed] (parser/parse "()")]
    (is (== nil (parsed :shape)))
    (is (== [] (parsed :data)))))

(deftest vector
  (let [[parsed] (parser/parse ".[]")]
  (let [[parsed] (parser/parse "[]")]
    (is (== [0] (parsed :shape)))
    (is (== [] (parsed :data)))))



@@ 56,7 56,7 @@

(deftest push-adverb
  (let [s (:new calc/Stack)
        parsed (parser/parse "0 [1 2 3] [+] /")]
        parsed (parser/parse "0 [1 2 3] (+) /")]
    (push-all s parsed)

    (is (= 1 (length (s :data))))

M test/regressions.janet => test/regressions.janet +6 -6
@@ 11,7 11,7 @@
(deftest regression1
  # [1] 1 + 1 +
  (def s (:new calc/Stack))
  (let [in (parser/parse ".[1] 1 + 1 +")]
  (let [in (parser/parse "[1] 1 + 1 +")]
    (calc/push s (in 0))
    (vec= [1] (calc/peek s))
    (calc/push s (in 1))


@@ 28,7 28,7 @@
(deftest regression2
  # [4 5] 6 +  8 +
  (def s (:new calc/Stack))
  (let [in (parser/parse ".[4 5] 6 + 8 +")]
  (let [in (parser/parse "[4 5] 6 + 8 +")]
    (push-all s in))

  (def res (calc/pop s))


@@ 37,7 37,7 @@
(deftest regression3
  # [2 3 4] 1 -
  (def s (:new calc/Stack))
  (let [in (parser/parse ".[2 3 4] 1 -")]
  (let [in (parser/parse "[2 3 4] 1 -")]
    (push-all s in))

  (def res (calc/pop s))


@@ 47,7 47,7 @@
  # ([[1] [2] [3]])> !
  #could not find method :- for 0, or :r- for nil
  (def s (:new calc/Stack))
  (let [in (parser/parse ".[.[1].[2]] !")]
  (let [in (parser/parse "[[1][2]] !")]
    (push-all s in))
  (while ((complement empty?) (s :data))
    (def inner-q (calc/pop s))


@@ 56,7 56,7 @@
(deftest regression5
  # .[.[2].[4]] [.[2 1] fill] /
  (def s (:new calc/Stack))
  (let [in (parser/parse ".[.[2].[4]] [.[2 1] fill] /")]
  (let [in (parser/parse "[[2][4]] ([2 1] fill) /")]
    (push-all s in))
  # Distribute `[2 1] fill`:
  # Push [2]


@@ 65,6 65,6 @@
  # Push [4]
  # Push [2 1] fill
  # => [[4 4]]
  (is (= ".[.[2] .[2]] .[.[4] .[4]]" (print/p (s :data)))))
  (is (= "[[2] [2]] [[4] [4]]" (print/p (s :data)))))
  # I think this is right but I don't fully understand it.
(run-tests!)