~subsetpark/ec

ref: big ec/src/operations.janet -rw-r--r-- 860 bytes
3f9b6ebe — Zach Smith Support bigints (note: this does not work on account of floats being coerced to big ints) 8 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(import /src/calc)

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

(defop add 2 +)
(defop sub 2 -)
(defop div 2 /)
(defop mul 2 *)
(defop cmp 2 cmp
  `
  x y -- bool
  Push -1 if x < y;
        0 if x = y;
        1 if x > y.
  `)
(defop pow 2 math/pow)
(defop sqrt 1 math/sqrt)
(defop small 1 |(or (= $ 0) (= $ 1))
  "x -- bool\nPush 1 if x is 0 or 1.")

(defmacro defcmp
  [name arity cmp]
  ~(defop ,name ,arity
     (fn [x y] (if (,cmp x y) 1 0))
     (string/format
       `
      x y -- bool
      Comparison predicate.
      Push 1 if x %s y;
             else 0.
      `
       ,(string cmp))))

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