~subsetpark/ec

ref: cb9047649f01b15e78a1cd66b6dc710c38792038 ec/src/calc.janet -rw-r--r-- 3.4 KiB
cb904764 — Zach Smith Add some more useful adverbs 11 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
(use fugue)

(defproto Stack () data {:default @[]})

(defproto Element ())

(defproto Int Element value {:init? true})
(defproto Float Element value {:init? true})

(defproto Operation Element type {} arity {:default 0} fun-ref {})
(defproto Adverb Element type {} arity {:default 0})

(defproto Quotation Element
  shape {:default [] :init? true}
  data {:init? true}
  type {:allocate-value :quotation})

(defgeneric get-shape [_] [])
(defmethod get-shape Quotation [self] (self :shape))

(defgeneric get-arity [_] -1)
(defmethod get-arity Adverb [a] (a :arity))
(defmethod get-arity Operation [a] (a :arity))
(defmethod get-arity Quotation [q]
  (reduce |(+ $0 (get-arity $1)) 0 (q :data)))

(defgeneric fill
  [element shape-to-fill]
  (if (empty? shape-to-fill)
    element
    (let [y (array ;shape-to-fill)
          yi (last y)
          v (:new Quotation [yi] (array/new-filled yi element))]
      (fill v y))))

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

    (while (not (empty? x))
      (let [xi (array/pop x)
            yi (array/pop y)]
        (unless (= xi yi)
          (errorf "Shape error: can't fill quotation with shape %q to %q"
                  (shape self) shape-to-fill))))

    (reduce (fn [acc length]
              (let [new-shape (tuple length ;(shape acc))
                    new-data (array/new-filled length acc)]
                (:new Quotation new-shape new-data)))
            self
            (reverse y))))

# TODO: Do we want to do something more sophisticated here?
(defmulti make-element [:number] [n] (:new Float n))
(defmulti make-element [_] [v] v)

(defn- inner-apply
  [op & args]
  (let [x (first args)]
    (if (Quotation? x)
      (:new Quotation
            (x :shape)
            (map (partial inner-apply op) ;(map |($ :data) args)))
      (let [f (op :fun-ref)
            unwrapped (map |($ :value) args)]
        (->> (f ;unwrapped)
             (make-element))))))

(defmethod apply-operation Operation
  [op & args]
  (let [cmpr (comp length get-shape)
        largest (get-shape (extreme |(> (cmpr $0) (cmpr $1)) args))
        filled (map |(fill $ largest) args)]
    (inner-apply op ;filled)))

(defn wrap
  [& data]
  (let [hd (first data)
        inner-shape (get-shape hd)]
    (each datum data
      (unless (deep= (get-shape datum) inner-shape)
        (errorf "Quotation error: can't wrap heterogeneous data. Expected %j, got %j."
                inner-shape
                (get-shape datum))))
    (:new Quotation (tuple (length data) ;inner-shape) data)))

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

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

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

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

(defmulti push [Stack Operation]
  [self op]
  (check-arity self op)

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

  (->> (apply-operation op ;buf)
       (make-element)
       (push self)))

(defmulti push [Stack Adverb]
  [self adv]
  (check-arity self adv)
  ((adv :fun-ref) self))

(defmulti push [Stack _]
  [self item]
  (array/push (self :data) item))

(defn push-all
  [stack args]
  (each arg args
    (push stack arg)))