941ada042c5cc27c8abb19ab4ca5b6a66c8c99df — Zach Smith 1 year, 6 months ago 1f2b4c7
Use more getters
3 files changed, 27 insertions(+), 30 deletions(-)

M src/adverbs.janet
M src/calc.janet
M src/eval.janet
M src/adverbs.janet => src/adverbs.janet +9 -9
@@ 74,7 74,7 @@
  (let [q (calc/pop stack)
        r (calc/pop stack)
        s (calc/quote-wrap ;(r :data) ;(q :data))]
        s (calc/quote-wrap ;(calc/Quotation/data r) ;(calc/Quotation/data q))]
    (calc/push stack s)))

(defadv concat 2

@@ 84,7 84,7 @@
  (let [q (calc/pop stack)
        r (calc/pop stack)
        s (calc/wrap ;(r :data) ;(q :data))]
        s (calc/wrap ;(calc/Vector/data r) ;(calc/Vector/data q))]
    (calc/push stack s)))

(defadv clear-stack 0

@@ 92,7 92,7 @@
  S -- <>
  Clear the entire stack.
  (array/clear (stack :data)))
  (array/clear (calc/Stack/data stack)))

(defadv dup 1

@@ 123,7 123,7 @@
  (let [elem (calc/pop stack)
        vector (calc/pop stack)
        new-vector (if (calc/Vector? vector)
                     (calc/wrap ;(vector :data) elem)
                     (calc/wrap ;(calc/data vector) elem)
                     (calc/wrap vector elem))]
    (calc/push stack new-vector)))

@@ 138,7 138,7 @@
  (let [vector (calc/pop stack)
        elem (calc/pop stack)
        new-vector (if (calc/Vector? vector)
                     (calc/wrap elem ;(vector :data))
                     (calc/wrap elem ;(calc/data vector))
                     (calc/wrap elem vector))]
    (calc/push stack new-vector)))

@@ 154,7 154,7 @@
    (unless (calc/shape? shape)
      (error "Fill error: top argument must be a shape"))

    (let [unwrapped-shape (map |($ :value) (shape :data))
    (let [unwrapped-shape (map |(calc/value $) (calc/data shape))
          new-quotation (calc/fill elem unwrapped-shape)]
      (calc/push stack new-quotation))))

@@ 216,14 216,14 @@
  Push a quotation containing all the elements of the stack.
  (let [s (calc/wrap ;(stack :data))]
  (let [s (calc/wrap ;(calc/Stack/data stack))]
    (calc/push stack s)))

(defadv push-stack 0
  Push a quotation containing all the elements of the stack.
  (let [s (calc/quote-wrap ;(stack :data))]
  (let [s (calc/quote-wrap ;(calc/Stack/data stack))]
    (calc/push stack s)))

(defadv unstack 1

@@ 231,7 231,7 @@
  Pop a quotation or vector from the stack and make that the new stack.
  (let [q (calc/pop stack)]
    (put stack :data (array ;(q :data)))))
    (put stack :data (array ;(calc/data q)))))

(defadv primrec 3

M src/calc.janet => src/calc.janet +16 -19
@@ 47,17 47,17 @@
  type {:allocate-value :thunk})

(defmethod null? Number
  [n] (zero? (n :value)))
  [n] (zero? (value n)))

(defmethod null? Quotation
  [q] (empty? (q :data)))
  [q] (empty? (data q)))

(defmethod pred Number
  [n] (:new (table/getproto n) (- (n :value) 1)))
  [n] (:new (table/getproto n) (- (value n) 1)))

(defmethod pred Quotation
  (let [rest (array/slice (q :data) 1)]
  (let [rest (array/slice (data q) 1)]
    (:new (table/getproto q) ;rest)))

(defgeneric get-shape [elem]

@@ 65,10 65,7 @@
          ((table/getproto elem) :_name)))

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

(defmethod get-arity Adverb [a] (a :arity))
(defmethod get-arity Operation [a] (a :arity))
(defmethod get-shape Vector [self] (shape self))

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

@@ 112,9 109,9 @@
    (if (Vector? x)
      (:new Vector
            (x :shape)
            (map (partial inner-apply op) ;(map |($ :data) args)))
      (let [f (op :fun-ref)
            unwrapped (map |($ :value) args)]
            (map (partial inner-apply op) ;(map |(data $) args)))
      (let [f (fun-ref op)
            unwrapped (map |(value $) args)]
        (->> (f ;unwrapped)

@@ 142,22 139,22 @@

(defmethod pop Stack
  (-> (self :data) (array/pop)))
  (-> (data self) (array/pop)))

(defmethod peek Stack
  (-> (self :data) (last)))
  (-> (data self) (last)))

(defmulti size [Stack]
  (-> (self :data) (length)))
  (-> (data self) (length)))

(defmulti size [:tuple] [t] (length t))
(defmulti size [_] [_] 0)

(defn check-arity
  [stack elem]
  (let [arity (get-arity elem)]
  (let [arity (arity elem)]
    (when (> arity (size stack))
      (errorf "Not enough stack: %s has arity %i; Stack has size %i"
              (string (elem :type))

@@ 171,7 168,7 @@
              (check-arity self op)

              (def buf @[])
              (loop [_ :range [0 (get-arity op)]]
              (loop [_ :range [0 (arity op)]]
                (array/insert buf 0 (pop self)))

              (->> (apply-operation op ;buf)

@@ 181,12 178,12 @@
(extend-multi push [Stack Adverb]
              [self adv]
              (check-arity self adv)
              ((adv :fun-ref) self))
              ((fun-ref adv) self))

(extend-multi push [Stack _]
              [self item]
              (array/push (self :data) item))
              (array/push (data self) item))

(defn shape?
  (and (Vector? obj) (all Number*? (obj :data))))
  (and (Vector? obj) (all Number*? (data obj))))

M src/eval.janet => src/eval.janet +2 -2
@@ 21,7 21,7 @@
    # Mark the stored quotation for evaluation by creating a Thunk out
    # of it, which is a Quotation that evaluates its contents when
    # pushed to the stack.
    (q (calc/Quotation? q)) (:new calc/Thunk (q :data))
    (q (calc/Quotation? q)) (:new calc/Thunk (calc/data q))
    nil (errorf "Eval error: unknown word %s" sym-value)
    elem elem))

@@ 51,7 51,7 @@
(extend-multi calc/push
              [calc/Stack calc/Thunk]
              [stack thunk]
              (let [evaled (map |(eval stack $) (thunk :data))
              (let [evaled (map |(eval stack $) (calc/data thunk))
                    q (:new calc/Quotation evaled)]
                (apply-quotation stack q)))