~subsetpark/ec

151ca3ef17cc7cd0a6715a4e88d57984831d6b6a — Zach Smith 8 months ago 0c23e09
Cover get-shape for all protos
2 files changed, 12 insertions(+), 8 deletions(-)

M src/calc.janet
M src/operations.janet
M src/calc.janet => src/calc.janet +4 -2
@@ 37,10 37,12 @@
  data {:init? true}
  type {:allocate-value :vector})

(defgeneric get-shape [elem]
  (errorf "Shape error: attempted vector operation on a %s"
          ((table/getproto elem) :_name)))

(defmethod get-shape Number [self] [])
(defmethod get-shape Vector [self] (self :shape))
(defmethod get-shape Quotation [_]
  (error "Shape error: attempted vector operation on a quotation"))

(defgeneric get-arity [_] 0)
(defmethod get-arity Adverb [a] (a :arity))

M src/operations.janet => src/operations.janet +8 -6
@@ 14,18 14,20 @@
  ~(defop ,name ,arity ,out-arity
     (fn [x y] (if (,cmp x y) 1 0))
     (string/format
       `x y -- p
       Comparison predicate.
       Push 1 if x %s y;
              else 0.
       ` ,(string cmp))))
      `
      x y -- bool
      Comparison predicate.
      Push 1 if x %s y;
             else 0.
      ` ,(string cmp))))

(defop add 2 1 +)
(defop sub 2 1 -)
(defop div 2 1 /)
(defop mul 2 1 *)
(defop cmp 2 1 cmp
  `x y -- p
  `
  x y -- bool
  Push -1 if x < y;
        0 if x = y;
        1 if x > y.