~subsetpark/ec

ref: bd73813f66ec440d293ebe5a8f1e3237343c5ab0 ec/src/operations.janet -rw-r--r-- 610 bytes
bd73813f — Zach Smith Introduce vectors and .[] syntax 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(import /src/calc)

(defmacro defop
  [name arity out-arity f]
  ~(def ,name (:new calc/Operation
                    :arity ,arity
                    :out-arity ,out-arity
                    :type ,(keyword name)
                    :fun-ref ,f)))

(defmacro defcmp
  [name arity out-arity cmp]
  ~(defop ,name ,arity ,out-arity
     (fn [x y] (if (,cmp x y) 1 0))))

(defop add 2 1 +)
(defop sub 2 1 -)
(defop div 2 1 /)
(defop mul 2 1 *)
(defop cmp 2 1 cmp)
(defop pow 2 1 math/pow)
(defop sqrt 1 1 math/sqrt)

(defcmp lt 2 1 <)
(defcmp gt 2 1 >)
(defcmp lte 2 1 <=)
(defcmp gte 2 1 >=)
(defcmp eq 2 1 =)