~subsetpark/ec

ref: 23027903beabdba42f04dc75e4e6b628b66dbd55 ec/src/env.janet -rw-r--r-- 4.8 KiB
23027903 — Zach Smith Factor out logic into surrounding modules 3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(import /src/calc)
(import /src/operations)
(import /src/adverbs)
(import /src/compose)
(import /src/eval)


(def- define (adverbs/new-Adverb
               :arity 2
               :type :define
               :fun-ref (fn define [stack]
                          (let [sym (calc/pop stack)
                                q (calc/pop stack)]
                            (match sym
                              {:value sym-value :quoted? true} (put-in stack [:env sym-value] q))))))

(def dictionary @{"+" operations/add
                  "-" operations/sub
                  "*" operations/mul
                  "x" operations/mul
                  "%" operations/div
                  "sqrt" operations/sqrt
                  ">" operations/gt
                  "<" operations/lt
                  ">=" operations/gte
                  "<=" operations/lte
                  "=" operations/eq
                  "cmp" operations/cmp
                  "pow" operations/pow
                  "small" operations/small

                  # Combinators
                  "each" adverbs/do-each
                  "dist" adverbs/dist
                  "/" adverbs/dist
                  "apply" adverbs/apply-quotation
                  "i" adverbs/apply-quotation
                  "if" adverbs/if
                  "primrec" adverbs/primrec
                  "linrec" adverbs/linrec
                  # Stack operations
                  "clear" adverbs/clear-stack
                  "c" adverbs/clear-stack
                  "swap" adverbs/swap
                  "dup" adverbs/dup
                  "pop" adverbs/pop
                  "stack" adverbs/push-stack
                  "unstack" adverbs/unstack
                  # Quotation
                  "first" adverbs/quotation-first
                  "quote" adverbs/quote-element
                  "compose" adverbs/compose
                  # Vectors
                  "fill" adverbs/fill
                  "slurp" adverbs/slurp
                  "slurpl" adverbs/slurp-left
                  "slurpr" adverbs/slurp
                  "shape" adverbs/shape
                  "concat" adverbs/concat

                  "def" define

                  "dip" compose/dip
                  "sqr" compose/square
                  "abs" compose/abs
                  "wrap" compose/wrap-1
                  "fac" compose/fac
                  "quote-all" compose/quote-all
                  "wrap-all" compose/wrap-all
                  "swop" compose/swop
                  "length" compose/length
                  "sum" compose/sum
                  "product" compose/product})

(defn new-env
  [&opt proto]
  (default proto dictionary)
  (let [s (:new calc/Stack)]
    (put s :env (table/setproto @{} proto))))

(adverbs/defadv
  map-vector 2
  ```
  v q -- v'
  Insert the contents of quotation `q` in between every element of
  `v`. Wrap the resulting elements in a vector.

  For ordinary operations, this is equivalent to applying them
  directly. For instance, `v (dup 1 +) map` is equivalent to
  `v dup 1 +`.
  ```
  (let [q (calc/pop stack)
        v (calc/pop stack)
        substack (new-env (stack :env))]

    (each elem (calc/data v)
      (calc/push substack elem)
      (eval/apply-quotation substack q))

    (calc/push stack (calc/wrap ;(substack :data)))))

(put dictionary "map" map-vector)

(adverbs/defadv
  scan-vector 3
  ```
  v a q -- w
  Insert `a`, then apply quotation `q` after every element of `v`.

  Collect the top element of the stack after each application into a
  new vector.
  ```
  (let [q (calc/pop stack)
        base (calc/pop stack)
        v (calc/pop stack)
        substack (new-env (stack :env))
        return (new-env (stack :env))]

    (calc/push substack base)
    (each elem (calc/data v)
      (calc/push substack elem)
      (eval/apply-quotation substack q)
      (calc/push return (calc/peek substack)))

    (calc/push stack (calc/wrap ;(return :data)))))

(put dictionary "scan" scan-vector)

(adverbs/defadv
  fork 3
  ```
  S a q p -- S a' a'' 
  Apply q to a, and p to a, and push the results on the stack.
  ```
  (let [p (calc/pop stack)
        q (calc/pop stack)
        a (calc/pop stack)
        p-env (new-env (stack :env))
        q-env (new-env (stack :env))]
    # Thread 1
    (calc/push p-env a)
    (eval/apply-quotation p-env p)
    # Thread 2
    (calc/push q-env a)
    (eval/apply-quotation q-env q)
    # Main Thread
    (each elem (q-env :data)
      (calc/push stack elem))
    (each elem (p-env :data)
      (calc/push stack elem))))

(put dictionary "fork" fork)

(compose/defcompose
  range 1
  ```
  n -- v
  generate a vector with numbers 0 up to `n`.
 
  1 swap wrap fill 0 (+) scan
  ```
  (calc/new-Int 1)
  adverbs/swap
  compose/wrap-1
  adverbs/fill
  (:new calc/Int -1)
  (calc/quote-wrap operations/add)
  scan-vector)

(put dictionary "range" range)