~subsetpark/ec

ec/src/calc.janet -rw-r--r-- 3.4 KiB
7304ccde — Zach Smith make handle-token a generic 3 days 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(use fugue)

(defproto Stack ()
  env {}
  data {})

(defmethod _init Stack
  [self]
  (put self :data @[]))

(defproto Element ()
  doc {:default ""})

(defproto Number Element)

(defproto Int Number
  type {:allocate-value :integer}
  value {:init? true})

(defproto Float Number
  type {:allocate-value :float}
  value {:init? true})

(defmethod is-true? Number
  [n]
  (not (zero? (n :value))))

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

(defproto Vector Quotation
  shape {:default [] :init? true :getter false}
  data {:init? true}
  type {:allocate-value :vector})

(defproto Thunk Quotation
  data {:init? true}
  type {:allocate-value :thunk})

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

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

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

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

(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 fill Number
  [element shape-to-fill]
  (if (empty? shape-to-fill)
    element
    (let [y (array ;shape-to-fill)
          yi (last y)
          v (new-Vector [yi] (array/new-filled yi element))]
      (fill v y))))

(defmethod fill Vector
  [self shape-to-fill]
  (let [x (array ;(get-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 vector with shape %q to %q"
                  (get-shape self) shape-to-fill))))

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

# TODO: Do we want to do something more sophisticated here?
(declare-open-multi make-element)

(extend-multi make-element [:number]
              [n]
              (new-Float n :doc (string n)))

(extend-multi make-element [:boolean]
              [b]
              (if b
                (make-element 1)
                (make-element 0)))

(defn quote-wrap
  [& data]
  (new-Quotation data))

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

    (new-Vector (tuple (length data) ;inner-shape) data)))

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

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

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

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

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

(declare-open-multi push)

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

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