~subsetpark/ec

0f5a4a72ac5c34a153a2534fbf32ae55c27109a1 — Zach Smith 7 months ago 65da5cf
Regularlize get-shape
4 files changed, 23 insertions(+), 15 deletions(-)

M src/calc.janet
M src/operations.janet
M src/repl.janet
M test/calc.janet
M src/calc.janet => src/calc.janet +15 -7
@@ 3,7 3,10 @@
(defproto Stack ()
  env {}
  data {})
(defmethod _init Stack [self] (put self :data @[]))

(defmethod _init Stack
  [self]
  (put self :data @[]))

(defproto Element ()
  doc {:default ""})


@@ 27,7 30,7 @@
  type {:allocate-value :quotation})

(defproto Vector Quotation
  shape {:default [] :init? true}
  shape {:default [] :init? true :getter false}
  data {:init? true}
  type {:allocate-value :vector})



@@ 54,7 57,7 @@
          ((table/getproto elem) :_name)))

(defmethod get-shape Number [self] [])
(defmethod get-shape Vector [self] (shape self))
(defmethod get-shape Vector [self] (self :shape))

(defmethod fill Number
  [element shape-to-fill]


@@ 67,7 70,7 @@

(defmethod fill Vector
  [self shape-to-fill]
  (let [x (array ;(shape self))
  (let [x (array ;(get-shape self))
        y (array ;shape-to-fill)]

    (while (not (empty? x))


@@ 75,10 78,10 @@
            yi (array/pop y)]
        (unless (= xi yi)
          (errorf "Shape error: can't fill vector with shape %q to %q"
                  (shape self) shape-to-fill))))
                  (get-shape self) shape-to-fill))))

    (reduce (fn [acc length]
              (let [new-shape (tuple length ;(shape acc))
              (let [new-shape (tuple length ;(get-shape acc))
                    new-data (array/new-filled length acc)]
                (new-Vector new-shape new-data)))
            self


@@ 86,7 89,11 @@

# TODO: Do we want to do something more sophisticated here?
(declare-open-multi make-element)
(extend-multi make-element [:number] [n] (new-Float n :doc (string n)))

(extend-multi make-element [:number]
              [n]
              (new-Float n :doc (string n)))

(extend-multi make-element [:boolean]
              [b]
              (if b


@@ 106,6 113,7 @@
        (errorf "Vector error: can't wrap heterogeneous data. Expected shape %j, got %j."
                inner-shape
                (get-shape datum))))

    (new-Vector (tuple (length data) ;inner-shape) data)))

(defmethod pop Stack

M src/operations.janet => src/operations.janet +1 -1
@@ 11,7 11,7 @@
  (let [x (first args)]
    (if (calc/Vector? x)
      (calc/new-Vector
        (x :shape)
        (calc/get-shape x)
        (map (partial inner-apply op) ;(map |(@ calc/Vector $ :data) args)))
      (let [f (fun-ref op)
            unwrapped (map |(calc/value $) args)]

M src/repl.janet => src/repl.janet +5 -5
@@ 120,11 120,11 @@
                (parser/parse)
                (handle-commands (@)))

           ([err fib]
            (eprint err)
            (if (os/getenv "EC_TRACEBACK")
              (propagate err fib)
              (set (@ data) bak)))))))
        ([err fib]
          (eprint err)
          (if (os/getenv "EC_TRACEBACK")
            (propagate err fib)
            (set (@ data) bak)))))))

(defn handle-lines
  ```

M test/calc.janet => test/calc.janet +2 -2
@@ 105,7 105,7 @@
  (calc/push s c/wrap-all)

  (def vec (calc/pop s))
  (is (= [2] (calc/shape vec)))
  (is (= [2] (calc/get-shape vec)))
  (is (empty? (s :data))))

(deftest wrap-quotations


@@ 115,7 115,7 @@
  (calc/push s c/wrap-all)

  (def vec (calc/pop s))
  (is (= [2 2] (calc/shape vec)))
  (is (= [2 2] (calc/get-shape vec)))
  (is (empty? (s :data))))

(deftest wrap-failure