substrate-lang 0.9.3 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/README.md +1 -1
  2. package/build/compiler/binary.2d43b43d1600@0+0.wasm +0 -0
  3. package/build/compiler/checker.4dd4556dd421@0+0.wasm +0 -0
  4. package/build/compiler/compile.5d8f6407af2a@0+0.wasm +0 -0
  5. package/build/compiler/{core.fc3dab9d1846@0+0.wasm → core.f2defb75e00b@0+0.wasm} +0 -0
  6. package/build/compiler/expander.967ce443d379@0+0.wasm +0 -0
  7. package/build/compiler/fs.01b8aab4a9a8@0+0.wasm +0 -0
  8. package/build/compiler/{leb128.748b1c37277a@0+0.wasm → leb128.15d814b7b4f2@0+0.wasm} +0 -0
  9. package/build/compiler/{link.11b643d35670@0+0.wasm → link.b826ea7c8f17@0+0.wasm} +0 -0
  10. package/build/compiler/{macros.4083601c00db@0+0.wasm → macros.b269e0c42784@0+0.wasm} +0 -0
  11. package/build/compiler/{main.ed62cce43000@0+0.wasm → main.4fc52f204d54@0+0.wasm} +0 -0
  12. package/build/compiler/{ops.b361e858d8e1@0+0.wasm → ops.515b5e05f646@0+0.wasm} +0 -0
  13. package/build/compiler/{path.dcc4712bcc7b@0+0.wasm → path.5a6286472cd4@0+0.wasm} +0 -0
  14. package/build/compiler/program +21 -21
  15. package/build/compiler/prune.406215c3e4f8@0+0.wasm +0 -0
  16. package/build/compiler/reader.694b03770bf0@0+0.wasm +0 -0
  17. package/build/compiler/roots.cefd10e93684@0+0.wasm +0 -0
  18. package/build/compiler/{running.d73f7826cb74@0+0.wasm → running.0d25dbaec018@0+0.wasm} +0 -0
  19. package/build/compiler/serialize.b0d82f3d63d2@0+0.wasm +0 -0
  20. package/build/compiler/{sha256.5fc9d60f8bb2@0+0.wasm → sha256.a6c32b04d799@0+0.wasm} +0 -0
  21. package/build/compiler/substrate.5ee5d0b4d3e7@0+0.wasm +0 -0
  22. package/build/compiler/syntax.32f3e40b61c7@0+0.wasm +0 -0
  23. package/build/compiler/{writer.806bc85aa684@0+0.wasm → writer.00b7802cbe39@0+0.wasm} +0 -0
  24. package/build/sub/{base64.8fba03e1be0a@0+0.wasm → base64.a26bdcf60a2d@0+0.wasm} +0 -0
  25. package/build/sub/build.459c5b9412bd@0+0.wasm +0 -0
  26. package/build/sub/chromium.3d5818f2707d@0+0.wasm +0 -0
  27. package/build/sub/common.1edc613607a0@0+0.wasm +0 -0
  28. package/build/sub/deflate.23f7c4679260@0+0.wasm +0 -0
  29. package/build/sub/doc.0775df5de6fe@0+0.wasm +0 -0
  30. package/build/sub/{doc.566f9861828e@0+0.wasm → doc.ef9965d5d402@0+0.wasm} +0 -0
  31. package/build/sub/env.e5451da6e747@0+0.wasm +0 -0
  32. package/build/sub/events.bf5473ebf220@0+0.wasm +0 -0
  33. package/build/sub/fetch.7790d4943758@0+0.wasm +0 -0
  34. package/build/sub/fetch.96b2d10f025e@0+0.wasm +0 -0
  35. package/build/sub/files.ad0469fdf6fa@0+0.wasm +0 -0
  36. package/build/sub/fs.01b8aab4a9a8@0+0.wasm +0 -0
  37. package/build/sub/git.dc4c0e757ebe@0+0.wasm +0 -0
  38. package/build/sub/grants.c54776c0d7e3@0+0.wasm +0 -0
  39. package/build/sub/gzip.16e372eda1cf@0+0.wasm +0 -0
  40. package/build/sub/hash.660682e4f491@0+0.wasm +0 -0
  41. package/build/sub/{html.20baa1dc7ca6@0+0.wasm → html.a23126eb949a@0+0.wasm} +0 -0
  42. package/build/sub/{html.c1671a102b4d@0+0.wasm → html.e9ed37f34977@0+0.wasm} +0 -0
  43. package/build/sub/{inflate.8899e0933792@0+0.wasm → inflate.59bb61f14001@0+0.wasm} +0 -0
  44. package/build/sub/json.55590d2f518a@0+0.wasm +0 -0
  45. package/build/sub/manifest.ef317480deb0@0+0.wasm +0 -0
  46. package/build/sub/map.f455cca6a42a@0+0.wasm +0 -0
  47. package/build/sub/pack.e02836264f6a@0+0.wasm +0 -0
  48. package/build/sub/{path.dcc4712bcc7b@0+0.wasm → path.5a6286472cd4@0+0.wasm} +0 -0
  49. package/build/sub/path.6891ffea73ca@0+0.wasm +0 -0
  50. package/build/sub/process.7106571fa723@0+0.wasm +0 -0
  51. package/build/sub/program +48 -48
  52. package/build/sub/read.0b93933d62a2@0+0.wasm +0 -0
  53. package/build/sub/{reader.b535e61f99f4@0+0.wasm → reader.694b03770bf0@0+0.wasm} +0 -0
  54. package/build/sub/{root.9b713ec588c1@0+0.wasm → root.f10a4906f460@0+0.wasm} +0 -0
  55. package/build/sub/{roots.6d2942578055@0+0.wasm → roots.cefd10e93684@0+0.wasm} +0 -0
  56. package/build/sub/{sha256.5fc9d60f8bb2@0+0.wasm → sha256.a6c32b04d799@0+0.wasm} +0 -0
  57. package/build/sub/{sha512.b988b9dc6114@0+0.wasm → sha512.c24a792d7891@0+0.wasm} +0 -0
  58. package/build/sub/site.1732b58ec0e8@0+0.wasm +0 -0
  59. package/build/sub/site.ae3913b0b0f4@0+0.wasm +0 -0
  60. package/build/sub/store.a24d7a25daa6@0+0.wasm +0 -0
  61. package/build/sub/stream.126e3bd5d626@0+0.wasm +0 -0
  62. package/build/sub/sub.2e3092140706@0+0.wasm +0 -0
  63. package/build/sub/substrate.5ee5d0b4d3e7@0+0.wasm +0 -0
  64. package/build/sub/{syntax.9857f6784d63@0+0.wasm → syntax.32f3e40b61c7@0+0.wasm} +0 -0
  65. package/build/sub/tar.2a633861e654@0+0.wasm +0 -0
  66. package/build/sub/test.9bd59c73f65e@0+0.wasm +0 -0
  67. package/build/sub/{text.4ab70ac35b5a@0+0.wasm → text.1bc90482bd43@0+0.wasm} +0 -0
  68. package/build/sub/{timer.3809bb17a7a1@0+0.wasm → timer.f35eb62ae446@0+0.wasm} +0 -0
  69. package/build/sub/upgrade.39ee449b00d5@0+0.wasm +0 -0
  70. package/build/sub/{utf8.524c475ecd40@0+0.wasm → utf8.e7216bd054e1@0+0.wasm} +0 -0
  71. package/build/sub/worker.762a9d642af0@0+0.wasm +0 -0
  72. package/build/sub/writer.00b7802cbe39@0+0.wasm +0 -0
  73. package/package.json +1 -1
  74. package/std/archive/tar.sub +44 -44
  75. package/std/browser/chromium.sub +5 -4
  76. package/std/browser/devtools.sub +24 -19
  77. package/std/compress/deflate.sub +10 -12
  78. package/std/compress/gzip.sub +18 -16
  79. package/std/compress/inflate.sub +7 -4
  80. package/std/crypto/password.sub +3 -3
  81. package/std/doc/doc.sub +2 -2
  82. package/std/encoding/json.sub +64 -73
  83. package/std/encoding/wire.sub +15 -15
  84. package/std/http/checks.sub +3 -3
  85. package/std/http/conditional.sub +2 -4
  86. package/std/http/cookie.sub +5 -7
  87. package/std/http/form.sub +9 -12
  88. package/std/http/http.sub +17 -24
  89. package/std/http/multipart.sub +13 -15
  90. package/std/http/route.sub +18 -13
  91. package/std/http/server.sub +21 -23
  92. package/std/http/session.sub +3 -6
  93. package/std/http/sse-client.sub +9 -14
  94. package/std/http/static.sub +5 -5
  95. package/std/http/url.sub +10 -10
  96. package/std/http/websocket.sub +2 -5
  97. package/std/meta/derive.sub +0 -2
  98. package/std/meta/method.sub +41 -0
  99. package/std/meta/type-info.sub +30 -37
  100. package/std/package/files.sub +24 -25
  101. package/std/package/git.sub +26 -33
  102. package/std/package/hash.sub +9 -9
  103. package/std/package/manifest.sub +81 -57
  104. package/std/package/map.sub +5 -9
  105. package/std/package/pack.sub +13 -13
  106. package/std/package/path.sub +1 -4
  107. package/std/package/read.sub +10 -14
  108. package/std/package/site.sub +11 -11
  109. package/std/package/store.sub +16 -12
  110. package/std/package.sub +6 -6
  111. package/std/test/browser.sub +9 -8
  112. package/std/test/build.sub +2 -2
  113. package/std/test/compare.sub +9 -10
  114. package/std/test/page.sub +32 -27
  115. package/std/test/run.sub +9 -8
  116. package/std/test/test.sub +36 -30
  117. package/std/vector/vector.sub +75 -0
  118. package/substrate/VERSION +1 -1
  119. package/substrate/compiler/binary.sub +5 -14
  120. package/substrate/compiler/checker.sub +235 -222
  121. package/substrate/compiler/compile.sub +11 -11
  122. package/substrate/compiler/expander.sub +52 -78
  123. package/substrate/compiler/link.sub +2 -2
  124. package/substrate/compiler/main.sub +9 -9
  125. package/substrate/compiler/ops.sub +1 -1
  126. package/substrate/compiler/prune.sub +12 -17
  127. package/substrate/compiler/reader.sub +4 -4
  128. package/substrate/compiler/roots.sub +11 -10
  129. package/substrate/compiler/running.sub +1 -1
  130. package/substrate/compiler/serialize.sub +57 -50
  131. package/substrate/compiler/substrate.sub +60 -23
  132. package/substrate/compiler/syntax.sub +8 -21
  133. package/substrate/host/errors.mjs +3 -2
  134. package/substrate/host/events.sub +3 -16
  135. package/substrate/host/fs.mjs +5 -4
  136. package/substrate/host/fs.sub +142 -112
  137. package/substrate/host/grants.mjs +1 -1
  138. package/substrate/host/grants.sub +33 -33
  139. package/substrate/host/modules/fake.mjs +1 -1
  140. package/substrate/host/modules/websocket.mjs +4 -4
  141. package/substrate/host/modules/websocket.sub +8 -6
  142. package/substrate/host/node/env.mjs +12 -6
  143. package/substrate/host/node/env.sub +21 -16
  144. package/substrate/host/node/fs.mjs +1 -1
  145. package/substrate/host/node/net.mjs +2 -2
  146. package/substrate/host/node/net.sub +13 -6
  147. package/substrate/host/node/process.mjs +4 -4
  148. package/substrate/host/node/process.sub +28 -28
  149. package/substrate/host/node/signal.mjs +3 -2
  150. package/substrate/host/node/signal.sub +9 -6
  151. package/substrate/host/node/watch.mjs +1 -1
  152. package/substrate/host/node/watch.sub +11 -9
  153. package/substrate/host/stream.sub +2 -0
  154. package/substrate/host/subhost-core.mjs +9 -1
  155. package/substrate/host/web/clipboard.mjs +2 -2
  156. package/substrate/host/web/clipboard.sub +10 -3
  157. package/substrate/host/web/files.mjs +2 -2
  158. package/substrate/host/web/files.sub +6 -2
  159. package/substrate/host/web/fs.mjs +3 -3
  160. package/substrate/host/web/location.mjs +3 -1
  161. package/substrate/host/web/location.sub +8 -4
  162. package/substrate/host/worker-side.mjs +3 -1
  163. package/substrate/host/worker.mjs +6 -5
  164. package/substrate/host/worker.sub +10 -10
  165. package/build/compiler/binary.3823f89ce414@0+0.wasm +0 -0
  166. package/build/compiler/checker.592d37dcbec9@0+0.wasm +0 -0
  167. package/build/compiler/compile.86a2b03b0c9b@0+0.wasm +0 -0
  168. package/build/compiler/expander.689d326e5a1b@0+0.wasm +0 -0
  169. package/build/compiler/fs.563034be70c3@0+0.wasm +0 -0
  170. package/build/compiler/prune.d4a3f9834095@0+0.wasm +0 -0
  171. package/build/compiler/reader.b535e61f99f4@0+0.wasm +0 -0
  172. package/build/compiler/roots.6d2942578055@0+0.wasm +0 -0
  173. package/build/compiler/serialize.b2196de75bc8@0+0.wasm +0 -0
  174. package/build/compiler/substrate.4c780d6dd6d4@0+0.wasm +0 -0
  175. package/build/compiler/syntax.9857f6784d63@0+0.wasm +0 -0
  176. package/build/sub/build.84ac996eda63@0+0.wasm +0 -0
  177. package/build/sub/chromium.e4db7d29897c@0+0.wasm +0 -0
  178. package/build/sub/common.91ddee6b7a67@0+0.wasm +0 -0
  179. package/build/sub/deflate.fc8075e3aed1@0+0.wasm +0 -0
  180. package/build/sub/doc.0b48bbac404b@0+0.wasm +0 -0
  181. package/build/sub/env.f051f8166526@0+0.wasm +0 -0
  182. package/build/sub/events.f952172735e0@0+0.wasm +0 -0
  183. package/build/sub/fetch.097f1cc9502d@0+0.wasm +0 -0
  184. package/build/sub/fetch.5d2f427c995e@0+0.wasm +0 -0
  185. package/build/sub/files.30e55182c5df@0+0.wasm +0 -0
  186. package/build/sub/fs.563034be70c3@0+0.wasm +0 -0
  187. package/build/sub/git.48aefa1644be@0+0.wasm +0 -0
  188. package/build/sub/grants.5bf25700d47c@0+0.wasm +0 -0
  189. package/build/sub/gzip.69588e97c35a@0+0.wasm +0 -0
  190. package/build/sub/hash.3f0d118da63f@0+0.wasm +0 -0
  191. package/build/sub/json.f15a29d99d8d@0+0.wasm +0 -0
  192. package/build/sub/manifest.cf7471880377@0+0.wasm +0 -0
  193. package/build/sub/map.dc516eec27ff@0+0.wasm +0 -0
  194. package/build/sub/pack.55992e8cc09c@0+0.wasm +0 -0
  195. package/build/sub/path.b7a8e1cf3aaa@0+0.wasm +0 -0
  196. package/build/sub/process.aea2c88cb7a0@0+0.wasm +0 -0
  197. package/build/sub/read.cd1e693f4fb4@0+0.wasm +0 -0
  198. package/build/sub/site.7c6d8c7747af@0+0.wasm +0 -0
  199. package/build/sub/site.81548474c323@0+0.wasm +0 -0
  200. package/build/sub/store.d4e845ff24ac@0+0.wasm +0 -0
  201. package/build/sub/stream.4c07247477f7@0+0.wasm +0 -0
  202. package/build/sub/sub.b57e17b6876b@0+0.wasm +0 -0
  203. package/build/sub/substrate.4c780d6dd6d4@0+0.wasm +0 -0
  204. package/build/sub/tar.e482cf873cfc@0+0.wasm +0 -0
  205. package/build/sub/test.0d9c079bb309@0+0.wasm +0 -0
  206. package/build/sub/upgrade.18a5aa0dae89@0+0.wasm +0 -0
  207. package/build/sub/worker.f1190de21b21@0+0.wasm +0 -0
  208. package/build/sub/writer.806bc85aa684@0+0.wasm +0 -0
@@ -8,7 +8,7 @@
8
8
 
9
9
  ;; ---- uses ----
10
10
 
11
- (struct checking [phase i32] [counter i32] [instances (ptr hash-table)] [throwers (ptr hash-table)]
11
+ (struct checking [phase i32] [counter i32] [throwers (ptr hash-table)]
12
12
  [strings (ptr hash-table)] [copies (ptr hash-table)] [layouts (ptr hash-table)] [types (ptr hash-table)]
13
13
  [rethrown datum] [datum datum] [next (ptr checking)])
14
14
 
@@ -23,9 +23,9 @@
23
23
  (define meaning-of (fn (datum) -> datum) 0)
24
24
  (define inline-call-of (fn (datum datum) -> datum) 0)
25
25
  (define inline-copy-of (fn (datum) -> datum) 0)
26
- (define expand-head-of (fn (datum bool) -> datum) 0)
27
- (define scope-shape-of (fn (datum bool) -> datum) 0)
28
- (define apply-form-of (fn (datum datum bool) -> datum) 0)
26
+ (define expand-head-of (fn (datum) -> datum) 0)
27
+ (define scope-shape-of (fn (datum) -> datum) 0)
28
+ (define apply-form-of (fn (datum datum) -> datum) 0)
29
29
  (define prune-local-scopes (fn (datum) -> datum) 0)
30
30
  (define facts-of (fn (datum) -> datum) 0)
31
31
  (define datum-type-of (fn () -> datum) 0)
@@ -33,8 +33,8 @@
33
33
 
34
34
  (define (set-expander! [meaning (fn (datum) -> datum)]
35
35
  [inline-call (fn (datum datum) -> datum)] [inline-copy (fn (datum) -> datum)]
36
- [head (fn (datum bool) -> datum)] [shape (fn (datum bool) -> datum)]
37
- [apply (fn (datum datum bool) -> datum)] [prune (fn (datum) -> datum)] [facts (fn (datum) -> datum)]
36
+ [head (fn (datum) -> datum)] [shape (fn (datum) -> datum)]
37
+ [apply (fn (datum datum) -> datum)] [prune (fn (datum) -> datum)] [facts (fn (datum) -> datum)]
38
38
  [quoted (fn () -> datum)] [syntax-function (fn (datum i32) -> datum)])
39
39
  (set! meaning-of meaning)
40
40
  (set! inline-call-of inline-call)
@@ -48,12 +48,10 @@
48
48
  (set! syntax-function-of syntax-function)
49
49
  (set-type-query! local-type-of))
50
50
 
51
- (define twice i32 0)
52
-
53
- (define (expand-head [s datum]) -> datum (expand-head-of s (!= twice 0)))
51
+ (define (expand-head [s datum]) -> datum (expand-head-of s))
54
52
  (define (expand-in [s datum] [env datum]) -> datum (set! scope-env env) (expand-head s))
55
- (define (scope-shape! [m datum]) -> datum (scope-shape-of m (!= twice 0)))
56
- (define (apply-form! [t datum] [s datum]) -> datum (apply-form-of t s (!= twice 0)))
53
+ (define (scope-shape! [m datum]) -> datum (scope-shape-of m))
54
+ (define (apply-form! [t datum] [s datum]) -> datum (apply-form-of t s))
57
55
 
58
56
  (define (substrate-expansion [kind datum] [env datum] [s datum] [context datum] [states (ptr (ptr void))] [phase i32]) -> datum
59
57
  (let ([outer-here here] [outer-state state] [outer-env scope-env] [out datum 0])
@@ -103,18 +101,15 @@
103
101
 
104
102
  (define (checker-recover!)
105
103
  (set! here 0)
106
- (set! state 0)
107
- (set! twice 0)
108
- (set! type-arguments nil))
104
+ (set! state 0))
109
105
 
110
106
  (define (state-at [states (ptr (ptr void))] [phase i32]) -> (ptr checking)
111
107
  (let ([s (cast (ptr checking) (@ states))])
112
108
  (while (and (!= s 0) (!= (-> s phase) phase)) (set! s (-> s next)))
113
109
  (when (== s 0)
114
- (set! s (cast (ptr checking) (arena-alloc (current-arena) (sizeof checking))))
110
+ (set! s (new (current-arena) checking))
115
111
  (set! (-> s phase) phase)
116
112
  (set! (-> s counter) 0)
117
- (set! (-> s instances) (make-table))
118
113
  (set! (-> s throwers) (make-table))
119
114
  (set! (-> s strings) (make-table))
120
115
  (set! (-> s copies) (make-table))
@@ -138,6 +133,7 @@
138
133
  [('struct) (aggregate-type s 'struct)]
139
134
  [('union) (aggregate-type s 'union)]
140
135
  [('variant) (substrate-variant s)]
136
+ [('enum) (substrate-enum s)]
141
137
  [('substrate-struct-check) (substrate-struct-check s)]
142
138
  [('exception) (substrate-exception s)]
143
139
  [('typedef) (substrate-typedef s)]
@@ -145,7 +141,7 @@
145
141
  [('#%module-begin) (substrate-module-begin s)]
146
142
  [('forms) (forms-form s)]
147
143
  [('#%top-interaction) (syntax-match s [(_ e) (at s (syn `(#%checked interaction ,e)))])]
148
- [('make-cell 'new) (data-form kind s)]
144
+ [('make-cell) (data-form s)]
149
145
  [('test) (substrate-test s)]
150
146
  [('expect) (substrate-expect s)]
151
147
  [('expect-equal) (substrate-expect-equal s)]
@@ -169,7 +165,7 @@
169
165
  (struct code-record [self datum] [code datum])
170
166
 
171
167
  (define (code-box [c datum]) -> datum
172
- (let ([b (cast (ptr code-record) (arena-alloc (current-arena) (sizeof code-record)))])
168
+ (let ([b (new (current-arena) code-record)])
173
169
  (set! (-> b self) (make-record code-kind b))
174
170
  (set! (-> b code) c)
175
171
  (-> b self)))
@@ -223,6 +219,7 @@
223
219
  (define (closure-type? [t datum]) -> bool (and (pair? t) (== (car t) 'closure)))
224
220
  (define (array-type? [t datum]) -> bool (and (pair? t) (== (car t) 'array)))
225
221
  (define (struct-type? [t datum]) -> bool (and (pair? t) (== (car t) 'struct)))
222
+ (define (enum-type? [t datum]) -> bool (and (pair? t) (== (car t) 'enum)))
226
223
  (define (aggregate? [t datum]) -> bool (or (array-type? t) (struct-type? t)))
227
224
  (define (slice-type? [t datum]) -> bool (and (pair? t) (== (car t) 'slice)))
228
225
  ;; A string or a slice: a pointer and a length in one i64, the pointer below.
@@ -252,10 +249,7 @@
252
249
  `(,(car t) ,(for/list ([u (cadr t)]) (shown-as u full))
253
250
  ,@(if (== (caddr t) 'void) nil `(-> ,(shown-as (caddr t) full))))]
254
251
  [(array-type? t) `(array ,(shown-as (cadr t) full) ,(caddr t))]
255
- [(struct-type? t)
256
- (let ([name (if full (cadr t) (caddr t))])
257
- (if (pair? (cdddr t)) (cons name (for/list ([u (cadddr t)]) (shown-as u full))) name))]
258
- [(and (pair? t) (== (car t) 'tvar)) (cadr t)]
252
+ [(or (struct-type? t) (enum-type? t)) (if full (cadr t) (caddr t))]
259
253
  [(values-type? t) (cons 'values (for/list ([u (cdr t)]) (shown-as u full)))]
260
254
  [(== t 'none) 'void]
261
255
  [else (settled-type t)]))
@@ -264,25 +258,11 @@
264
258
  (let ([a (shown t)] [b (shown u)])
265
259
  (if (equal? a b) `(,(shown-as t #t) ,(shown-as u #t)) `(,a ,b))))
266
260
 
267
- (define type-arguments datum nil)
268
-
269
- (define (type-variables [ps datum]) -> datum (for/list ([p ps]) (cons p `(tvar ,p))))
270
-
271
- (define (parse-types-with [s datum] [ts datum]) -> datum
272
- (let ([saved type-arguments] [r datum 0])
273
- (set! type-arguments s)
274
- (set! r (try (for/list ([t ts]) (parse-type t)) [catch-all (set! type-arguments saved) (rethrow)]))
275
- (set! type-arguments saved)
276
- r))
277
-
278
261
  (define (parse-type [s datum]) -> datum
279
262
  (syntax-match s
280
263
  [('quote t) (syntax->datum t)]
281
264
  [(? identifier? x)
282
- (let ([bound (assq (syntax-e x) type-arguments)])
283
- (cond [bound (cdr bound)]
284
- [(memq (syntax-e x) '(void i8 u8 i16 u16 i32 u32 i64 u64 f32 f64 exnref bool string)) (syntax-e x)]
285
- [else (named-type x)]))]
265
+ (if (memq (syntax-e x) '(void i8 u8 i16 u16 i32 u32 i64 u64 f32 f64 exnref bool string)) (syntax-e x) (named-type x))]
286
266
  [('ptr t) `(ptr ,(parse-type t))]
287
267
  [('slice t)
288
268
  (let ([u (kept! (parse-type t) t)])
@@ -299,21 +279,11 @@
299
279
  [('values t ...)
300
280
  (when (null? t) (substrate-error s "expected a type" nil))
301
281
  `(values ,@(for/list ([u t]) (scalar! (parse-type u) u)))]
302
- [((? identifier? h) a ...) (applied-type s h a)]
303
282
  [_ (substrate-error s "expected a type" nil)]))
304
283
 
305
- (define (applied-type [s datum] [h datum] [a datum]) -> datum
306
- (let ([v (type-binding h)])
307
- (unless (and v (== (car v) 'substrate-struct) (pair? (parameters-of v)))
308
- (substrate-error h "~a is not a type that takes types" `(,(syntax-e h))))
309
- (unless (== (length a) (length (parameters-of v)))
310
- (substrate-error s "~a takes ~a type~a, not ~a"
311
- `(,(syntax-e h) ,(fixnum (length (parameters-of v))) ,(plural (length (parameters-of v))) ,(fixnum (length a)))))
312
- `(struct ,(identifier-name h) ,(cadr v) ,(for/list ([t a]) (kept! (complete! (parse-type t) t) t)))))
313
-
314
- (define (parameters-of [v datum]) -> datum (if (> (length v) 4) (list-ref v 4) nil))
284
+ (define (numbers-of [v datum]) -> datum (if (> (length v) 4) (list-ref v 4) 0))
315
285
 
316
- ;; A type's compile-time value: (substrate-struct name (field ...) kind (parameter ...)) or
286
+ ;; A type's compile-time value: (substrate-struct name (field ...) kind), with an enum's case numbers after the kind, or
317
287
  ;; (substrate-typedef type).
318
288
  (define (type-binding [x datum]) -> datum
319
289
  (if (== (identifier-binding-kind x) 'compile-time-value) (type-value (syntax-local-value x)) 0))
@@ -324,7 +294,9 @@
324
294
  known
325
295
  (let* ([l (syntax->list v)]
326
296
  [t datum (syntax-match (if (!= l 0) l nil)
327
- [('substrate-struct n fs k (p ...)) `(substrate-struct ,(syntax-e n) ,fs ,(syntax-e k) ,(for/list ([q p]) (syntax-e q)))]
297
+ [('substrate-struct n fs k) `(substrate-struct ,(syntax-e n) ,fs ,(syntax-e k))]
298
+ [('substrate-struct n fs k (x ...))
299
+ `(substrate-struct ,(syntax-e n) ,fs ,(syntax-e k) ,(for/list ([y x]) (syntax-e y)))]
328
300
  [('substrate-typedef t) `(substrate-typedef ,(syntax->datum t))]
329
301
  [_ 0])])
330
302
  (when (!= t 0) (hash-set! (-> state types) (cast u64 v) (cast u32 t)))
@@ -334,22 +306,18 @@
334
306
  (let ([v (type-binding x)])
335
307
  (when (== v 0) (substrate-error x "~a is not a type" `(,(syntax-e x))))
336
308
  (cond [(== (car v) 'substrate-typedef) (cadr v)]
337
- [(pair? (parameters-of v)) (substrate-error x "~a takes types: write (~a type ...)" `(,(syntax-e x) ,(syntax-e x)))]
309
+ [(!= (numbers-of v) 0) `(enum ,(identifier-name x) ,(cadr v))]
338
310
  [else `(struct ,(identifier-name x) ,(cadr v))])))
339
311
 
340
312
  (define (type-syntax? [s datum]) -> bool
341
313
  (syntax-match s
342
314
  [(? identifier? x) (or (memq (syntax-e x) '(i8 u8 i16 u16 i32 u32 i64 u64 f32 f64 bool string)) (type-binding x))]
343
315
  [('slice t) (type-syntax? t)]
344
- [((? identifier? h) a ...)
345
- (or (memq (syntax-e h) '(ptr array fn closure))
346
- (let ([v (type-binding h)])
347
- (and v (== (car v) 'substrate-struct) (pair? (parameters-of v))
348
- (not (ormap (lambda ([x datum]) -> bool (keyword? (syntax-e x))) a)))))]
316
+ [((? identifier? h) a ...) (!= (memq (syntax-e h) '(ptr array fn closure)) 0)]
349
317
  [_ #f]))
350
318
 
351
319
  (define (scalar! [t datum] [where datum]) -> datum
352
- (unless (or (arithmetic? t) (address-type? t) (== t 'exnref) (== t 'bool) (view-type? t) (and (pair? t) (== (car t) 'tvar)))
320
+ (unless (or (arithmetic? t) (address-type? t) (== t 'exnref) (== t 'bool) (view-type? t) (enum-type? t))
353
321
  (substrate-error where "expected a number or pointer type, not ~s" `(,(shown t))))
354
322
  t)
355
323
 
@@ -394,15 +362,12 @@
394
362
  (let ([known (datum-get (-> state layouts) (cdr t))])
395
363
  (when (!= known 0) (return known))
396
364
  (let* ([v (type-value (syntax-local-value (name-identifier (cadr t))))] [items (syntax->list (caddr v))]
397
- [saved type-arguments] [l datum 0])
398
- (datum-put! (-> state layouts) (cdr t) 'laying-out)
399
- (set! type-arguments (for/list ([p (parameters-of v)] [a (if (pair? (cdddr t)) (cadddr t) nil)]) (cons p a)))
400
- (set! l (try (if (== (cadddr v) 'variant)
401
- (variant-layout items)
402
- (let ([r (lay-out items (== (cadddr v) 'union))])
403
- `(,(fixnum (round-up (fixnum-value (car r)) (fixnum-value (cadr r)))) ,(cadr r) ,(caddr r))))
404
- [catch-all (set! type-arguments saved) (rethrow)]))
405
- (set! type-arguments saved)
365
+ [l (begin
366
+ (datum-put! (-> state layouts) (cdr t) 'laying-out)
367
+ (if (memq (cadddr v) '(variant enum))
368
+ (variant-layout items)
369
+ (let ([r (lay-out items (== (cadddr v) 'union))])
370
+ `(,(fixnum (round-up (fixnum-value (car r)) (fixnum-value (cadr r)))) ,(cadr r) ,(caddr r)))))])
406
371
  (datum-put! (-> state layouts) (cdr t) l)
407
372
  l)))
408
373
 
@@ -437,8 +402,6 @@
437
402
 
438
403
  (define (substrate-define [s datum]) -> datum
439
404
  (syntax-match s
440
- [(_ #:forall ((? identifier? p) ...) ((? identifier? f) ((? identifier? x) t) ...) rest ...)
441
- (let ([r (result-and-body rest)]) (generic s p f x t (car r) (cdr r)))]
442
405
  [(_ ((? identifier? f) ((? identifier? x) t) ...) (? placement? k) rest ...)
443
406
  (let ([r (result-and-body rest)]) (function s f x t (car r) (cdr r) f k))]
444
407
  [(_ ((? identifier? f) ((? identifier? x) t) ...) rest ...)
@@ -465,11 +428,6 @@
465
428
  ,(at s (syn `(#%checked body ,definer (,@xs) ,(if (!= to 0) to '#f) (quote ,(cons rt ps)) ,@body))))))
466
429
  (#%fact ,f (quote (substrate-function ,ps ,rt)))))))
467
430
 
468
- (define (generic [s datum] [ps datum] [f datum] [xs datum] [ts datum] [r datum] [body datum]) -> datum
469
- (parse-types-with (type-variables (for/list ([p ps]) (syntax-e p))) (cons r ts))
470
- (syn `(define-syntax ,f
471
- (quote-syntax (#%form mark (#%substrate-generic (,f (,@ps) (,@(for/list ([x xs] [t ts]) `(,x ,t))) ,r (,@body) ,s)))))))
472
-
473
431
  (define (global-definition [s datum] [x datum] [t datum] [init datum]) -> datum
474
432
  (let ([ty (kept! (complete! (parse-type t) t) t)])
475
433
  (syn `(begin
@@ -538,12 +496,11 @@
538
496
 
539
497
  (define (aggregate-type [s datum] [kind datum]) -> datum
540
498
  (syntax-match s
541
- [(_ #:forall ((? identifier? p) ...) (? identifier? name) field ...) (aggregate name field kind p)]
542
- [(_ (? identifier? name) field ...) (aggregate name field kind nil)]))
499
+ [(_ (? identifier? name) field ...) (aggregate name field kind)]))
543
500
 
544
- (define (aggregate [name datum] [fields datum] [kind datum] [ps datum]) -> datum
501
+ (define (aggregate [name datum] [fields datum] [kind datum]) -> datum
545
502
  (distinct! (for/list ([f fields]) (syntax-match f [((? identifier? g) _) g] [_ (substrate-error f "expected [field type]" nil)])) '"field")
546
- (syn `(begin (define-syntax ,name (quote-syntax (substrate-struct ,name (,@fields) ,kind (,@ps))))
503
+ (syn `(begin (define-syntax ,name (quote-syntax (substrate-struct ,name (,@fields) ,kind)))
547
504
  ,(second-pass (syn `(substrate-struct-check ,name))))))
548
505
 
549
506
  (define (distinct! [ids datum] [what datum])
@@ -554,10 +511,9 @@
554
511
 
555
512
  (define (substrate-variant [s datum]) -> datum
556
513
  (syntax-match s
557
- [(_ #:forall ((? identifier? p) ...) (? identifier? name) c ...) (variant-form name c p)]
558
- [(_ (? identifier? name) c ...) (variant-form name c nil)]))
514
+ [(_ (? identifier? name) c ...) (variant-form name c)]))
559
515
 
560
- (define (variant-form [name datum] [cs datum] [ps datum]) -> datum
516
+ (define (variant-form [name datum] [cs datum]) -> datum
561
517
  (let ([cases (for/list ([c cs])
562
518
  (syntax-match c
563
519
  [((? identifier? n) f ...)
@@ -567,7 +523,7 @@
567
523
  [(? identifier? n) `(,n)]
568
524
  [_ (substrate-error c "expected [case [field type] ...]" nil)]))])
569
525
  (distinct! (for/list ([c cases]) (car c)) '"case")
570
- (syn `(begin (define-syntax ,name (quote-syntax (substrate-struct ,name (,@(for/list ([c cases]) `(,@c))) variant (,@ps))))
526
+ (syn `(begin (define-syntax ,name (quote-syntax (substrate-struct ,name (,@(for/list ([c cases]) `(,@c))) variant)))
571
527
  ,@(for/list ([c cases] [i (numbers-below (length cases))])
572
528
  `(define-syntax ,(car c) (quote-syntax (#%form mark (#%substrate-construct ,name ,i ,(car c))))))
573
529
  ,(second-pass (syn `(substrate-struct-check ,name)))))))
@@ -575,7 +531,7 @@
575
531
  (define (substrate-struct-check [s datum]) -> datum
576
532
  (syntax-match s
577
533
  [(_ name)
578
- (when (null? (parameters-of (type-binding name))) (layout (named-type name)))
534
+ (layout (named-type name))
579
535
  (syn '(begin))]))
580
536
 
581
537
  (define (substrate-exception [s datum]) -> datum
@@ -768,9 +724,6 @@
768
724
  [('offsetof)
769
725
  (unless (and (== n 3) (identifier? (caddr l))) (substrate-error s "expected (offsetof type field)" nil))
770
726
  (use `(,(piece 'type 0 (cadr l)) ,(piece 'name 0 (caddr l))))]
771
- [('inst)
772
- (unless (and (>= n 2) (identifier? (cadr l))) (substrate-error s "expected (inst generic type ...)" nil))
773
- (use (cons (piece 'name 0 (cadr l)) (for/list ([t (cddr l)]) (piece 'type 0 t))))]
774
727
  [('throw)
775
728
  (unless (and (>= n 2) (identifier? (cadr l))) (substrate-error s "expected (throw exception value ...)" nil))
776
729
  (use (cons (piece 'name 0 (cadr l)) (exprs-in 0 (cddr l))))]
@@ -791,6 +744,12 @@
791
744
  [('lambda) (lambda-shape s)]
792
745
  [('match) (match-shape s)]
793
746
  [('try) (try-shape s)]
747
+ [('new)
748
+ (unless (or (== n 3) (== n 4)) (substrate-error s "expected (new arena type), (new arena type count) or (new arena value)" nil))
749
+ (use `(,(cadr l) ,(if (or (== n 4) (type-syntax? (caddr l))) (piece 'type 0 (caddr l)) (caddr l)) ,@(cdddr l)))]
750
+ [('copy)
751
+ (unless (== n 3) (substrate-error s "expected (copy arena items)" nil))
752
+ (use (exprs-in 0 (cdr l)))]
794
753
  [else 0])))
795
754
 
796
755
  ;; A binding is [x type value], [x value], [(x ...) value] or [(x ...) (type ...) value], or [x type]
@@ -1305,27 +1264,20 @@
1305
1264
  (let* ([t (piece-at m 1)] [ty (parse-type t)])
1306
1265
  (unless (struct-type? ty) (substrate-error t "~s is not a struct" `(,(shown ty))))
1307
1266
  (cons 'lit (caddr (field-of ty (piece-at m 2)))))]
1308
- [('inst) (check-inst s m)]
1309
1267
  [('#%datum) (literal s (syntax-e (piece-at m 1)))]
1310
- [else (check-marked s fenv (mark-arguments m) env hint)]))))
1268
+ [('new) (check-new m env)]
1269
+ [('copy) (check-copy m env)]
1270
+ [else (check-marked s fenv (mark-arguments m) env)]))))
1311
1271
 
1312
1272
  (define (mark-arguments [m datum]) -> datum (if (== (car m) 'group) (pieces-from m 1) nil))
1313
1273
 
1314
- (define (check-marked [s datum] [fenv datum] [args datum] [env datum] [hint datum]) -> datum
1274
+ (define (check-marked [s datum] [fenv datum] [args datum] [env datum]) -> datum
1315
1275
  (syntax-match fenv
1316
- [('#%substrate-generic g)
1317
- (if (pair? (syntax-e s))
1318
- (check-generic-call s g args env hint)
1319
- (syntax-match g [(f . _) (substrate-error s "~a is generic: give its types, (inst ~a type ...)" `(,(syntax-e f) ,(syntax-e f)))]))]
1320
1276
  [('#%substrate-construct v i n)
1321
- (let ([t (construct-type s v i args env hint)]) (cons t (construct s t i n args env (temp t))))]))
1322
-
1323
- (define (check-inst [s datum] [m datum]) -> datum
1324
- (let* ([f (piece-at m 1)] [h (expand-head f)])
1325
- (syntax-match (if (== (car h) 'form) (caddr h) 0)
1326
- [('#%substrate-generic g)
1327
- (let ([i (instance g (for/list ([u (pieces-from m 2)]) (parse-type u)) s)]) (cons (cons 'fn (function-type i)) (function-ref i)))]
1328
- [_ (substrate-error f "~a is not a generic function" `(,(syntax->datum f)))])))
1277
+ (let ([t (named-type v)])
1278
+ (if (enum-type? t)
1279
+ (begin (case-fields s t (syntax-e i) n args) (cons t (enum-constant t (syntax-e i))))
1280
+ (cons t (construct s t i n args env (temp t)))))]))
1329
1281
 
1330
1282
  (define (check-core [name datum] [e datum] [env datum] [hint datum]) -> datum
1331
1283
  (let ([m (if (== name 'let*) 0 (core-shape name e))])
@@ -1335,7 +1287,7 @@
1335
1287
  [('#%app)
1336
1288
  (let* ([f (piece-at m 1)] [fm (if (identifier? f) (meaning-of f) 0)])
1337
1289
  (if (literal-head? f fm)
1338
- (check-literal e f (cddr (syntax->list e)) env hint)
1290
+ (check-literal e f (cddr (syntax->list e)) env)
1339
1291
  (check-call e f (callee f fm env) (pieces-from m 2) env)))]
1340
1292
  [('quote) (cons (datum-type) `(quote ,(syntax->datum (piece-at m 1))))]
1341
1293
  [('quote-syntax) (cons (datum-type) `(quote ,(syntax-local-introduce (prune-local-scopes (piece-at m 1)))))]
@@ -1544,6 +1496,7 @@
1544
1496
  (define (let-value [zero bool] [r datum] [t datum] [x datum] [init datum] [stored bool]) -> datum
1545
1497
  (cond [(not zero) (convert r t init stored)]
1546
1498
  [(view-type? t) (const-code 'i64 (fixnum 0))]
1499
+ [(enum-type? t) (i32-const 0)]
1547
1500
  [else (constant (fixnum 0) t x)]))
1548
1501
 
1549
1502
  (define (let-put [r datum] [dst datum] [t datum] [init datum] [env datum]) -> datum
@@ -1695,89 +1648,10 @@
1695
1648
  (case (car p)
1696
1649
  [('call) (cadr (cadddr p))]
1697
1650
  [('array) (array-of-type (cadr p) (caddr p))]
1698
- [else (if (null? (parameters-of (type-binding (cadr p)))) (named-type (cadr p)) 0)])))
1651
+ [else (let ([t (named-type (cadr p))]) (if (enum-type? t) 0 t))])))
1699
1652
 
1700
1653
  (define (temp [t datum]) -> datum (address (frame-place t (frame-slot! t)) 0))
1701
1654
 
1702
- ;; ---- generics ----
1703
-
1704
- (define (check-generic-call [e datum] [g datum] [args datum] [env datum] [hint datum]) -> datum
1705
- (syntax-match g
1706
- [(f (p ...) ((x a) ...) r _ _)
1707
- (let* ([ps (for/list ([q p]) (syntax-e q))]
1708
- [s (begin (set! twice (+ twice 1)) (infer e (parse-types-with (type-variables ps) a) args env))]
1709
- [i (instance g (for/list ([q ps]) (bound-type e s q (syntax-e f))) e)]
1710
- [r (check-call e i `(,(function-type i) #f direct ,(info-of i 'func "a function")) args env)])
1711
- (set! twice (- twice 1))
1712
- r)]))
1713
-
1714
- (define (infer [e datum] [ts datum] [args datum] [env datum]) -> datum
1715
- (unless (== (length ts) (length args))
1716
- (substrate-error e "expected ~a argument~a, got ~a" `(,(fixnum (length ts)) ,(plural (length ts)) ,(fixnum (length args)))))
1717
- (infer-from ts args env (infer-from ts args env nil #f) #t))
1718
-
1719
- (define (infer-from [ts datum] [args datum] [env datum] [s datum] [constants bool]) -> datum
1720
- (cond [(null? ts) s]
1721
- [(known-type? (car ts) s) (infer-from (cdr ts) (cdr args) env s constants)]
1722
- [else
1723
- (let ([r (check (car args) env 0)])
1724
- (infer-from (cdr ts) (cdr args) env
1725
- (if (and (not constants) (literal-type? (car r)))
1726
- s
1727
- (let ([u (unify (car ts) (settled-type (car r)) s)])
1728
- (if (!= u 0)
1729
- u
1730
- (substrate-error (car args) "expected ~s, got ~s" (shown-apart (applied (car ts) s) (car r))))))
1731
- constants))]))
1732
-
1733
- (define (known-type? [t datum] [s datum]) -> bool
1734
- (cond [(and (pair? t) (== (car t) 'tvar)) (!= (assq (cadr t) s) 0)]
1735
- [(pair? t) (andmap (lambda ([u datum]) -> bool (known-type? u s)) t)]
1736
- [else #t]))
1737
-
1738
- (define (applied [t datum] [s datum]) -> datum
1739
- (cond [(and (pair? t) (== (car t) 'tvar)) (let ([b (assq (cadr t) s)]) (if (!= b 0) (cdr b) t))]
1740
- [(pair? t) (for/list ([u t]) (applied u s))]
1741
- [else t]))
1742
-
1743
- (define (unify [t datum] [u datum] [s datum]) -> datum
1744
- (cond [(and (pair? t) (== (car t) 'tvar))
1745
- (let ([b (assq (cadr t) s)])
1746
- (cond [(== b 0) (cons (cons (cadr t) u) s)]
1747
- [(equal? (cdr b) u) s]
1748
- [else 0]))]
1749
- [(and (pair? t) (pair? u) (== (length t) (length u))) (unify-each t u s)]
1750
- [(equal? t u) s]
1751
- [else 0]))
1752
-
1753
- (define (unify-each [t datum] [u datum] [s datum]) -> datum
1754
- (if (null? t) s (let ([s2 (unify (car t) (car u) s)]) (if (!= s2 0) (unify-each (cdr t) (cdr u) s2) 0))))
1755
-
1756
- (define (bound-type [e datum] [s datum] [p datum] [f datum]) -> datum
1757
- (let ([b (assq p s)])
1758
- (if (!= b 0) (cdr b) (substrate-error e "~a's type ~a is not known from its arguments: give it, (inst ~a type ...)" `(,f ,p ,f)))))
1759
-
1760
- (define (instance [g datum] [ts datum] [where datum]) -> datum
1761
- (syntax-match g
1762
- [(f (p ...) ((x a) ...) r (body ...) s)
1763
- (let* ([key (cons (identifier-name f) ts)] [known (datum-get (-> state instances) key)])
1764
- (if (!= known 0)
1765
- known
1766
- (let ([name (fresh (format "~a<~a>" `(,(syntax-e f) ,(joined (for/list ([t ts]) (format "~s" `(,(shown t))))))))]
1767
- [sub (for/list ([q p] [t ts]) (cons (syntax-e q) t))])
1768
- (datum-put! (-> state instances) key (syntax-local-introduce name))
1769
- (let ([saved (body-state)])
1770
- (syntax-local-lift-definition
1771
- (at s (function s name x (for/list ([y a]) (replace y sub)) (replace r sub) (for/list ([b body]) (replace b sub)) f 0)))
1772
- (restore-body! saved))
1773
- (syntax-local-introduce name))))]))
1774
-
1775
- (define (replace [x datum] [sub datum]) -> datum
1776
- (cond [(identifier? x)
1777
- (let ([b (assq (syntax-e x) sub)]) (if (!= b 0) (datum->syntax x (cons 'quote (cons (cdr b) nil)) x) x))]
1778
- [(!= (syntax->list x) 0) (syntax-rewrap x (for/list ([y (syntax->list x)]) (replace y sub)))]
1779
- [else x]))
1780
-
1781
1655
  ;; ---- closures ----
1782
1656
 
1783
1657
  (define (check-lambda [e datum] [m datum] [env datum]) -> datum
@@ -1881,8 +1755,8 @@
1881
1755
  (define (literal-head? [f datum] [fm datum]) -> bool
1882
1756
  (and (!= fm 0) (== (car fm) 'compile-time-value) (!= (struct-binding f) 0)))
1883
1757
 
1884
- (define (check-literal [e datum] [f datum] [args datum] [env datum] [hint datum]) -> datum
1885
- (let* ([pairs (literal-pairs e f args)] [t (literal-type e f pairs env hint)])
1758
+ (define (check-literal [e datum] [f datum] [args datum] [env datum]) -> datum
1759
+ (let* ([pairs (literal-pairs e f args)] [t (named-type f)])
1886
1760
  (cons t (construct-literal t pairs env (temp t)))))
1887
1761
 
1888
1762
  (define (literal-pairs [e datum] [f datum] [args datum]) -> datum
@@ -1901,21 +1775,6 @@
1901
1775
  (when (pair? missing) (substrate-error e "~a needs ~a" `(,(syntax-e f) ,(joined missing)))))
1902
1776
  (reverse out)))
1903
1777
 
1904
- (define (literal-type [e datum] [f datum] [pairs datum] [env datum] [hint datum]) -> datum
1905
- (let ([v (struct-binding f)])
1906
- (cond [(null? (parameters-of v)) (named-type f)]
1907
- [(and (struct-type? hint) (== (cadr hint) (identifier-name f))) hint]
1908
- [else
1909
- (let* ([ps (parameters-of v)]
1910
- [types (parse-types-with (type-variables ps) (for/list ([p pairs]) (declared-type v (car p))))]
1911
- [s (infer e types (for/list ([p pairs]) (cdr p)) env)])
1912
- `(struct ,(identifier-name f) ,(cadr v) ,(for/list ([p ps]) (bound-type e s p (syntax-e f)))))])))
1913
-
1914
- (define (declared-type [v datum] [name datum]) -> datum
1915
- (for/each ([d (syntax->list (caddr v))])
1916
- (syntax-match d [(g gt) (when (== (syntax-e g) name) (return gt))]))
1917
- 0)
1918
-
1919
1778
  (define (construct-literal [t datum] [pairs datum] [env datum] [dst datum]) -> datum
1920
1779
  (let ([d (fresh-key!)] [fields (caddr (layout t))])
1921
1780
  `(let ((,d i32 ,dst to))
@@ -1928,16 +1787,18 @@
1928
1787
  (if (== (car n) 'group)
1929
1788
  `(,(piece-at n 0) (,(cdr (pieces-of n)) ,body ,(cadr c)))
1930
1789
  `(,(piece-syntax n) (#f ,body ,(cadr c))))))]
1931
- [p (struct-base v env)] [t (car p)] [a (fresh-key!)] [tag (fresh-key!)] [nullable (pointer-base? p)]
1790
+ [p (match-base v env)] [t (car p)] [a (fresh-key!)] [tag (fresh-key!)] [nullable (pointer-base? p)]
1932
1791
  [pairs (for/list ([x cs]) (cons (car x) (cadr x)))]
1933
1792
  [nulls (for/list ([nc pairs] #:when (null-clause? (car nc))) nc)]
1934
1793
  [cases (for/list ([nc pairs] #:when (not (null-clause? (car nc)))) nc)])
1935
- (unless (variant-type? t) (substrate-error v "match takes a variant, or a pointer to one, not ~s" `(,(shown t))))
1794
+ (unless (or (variant-type? t) (enum-type? t)) (substrate-error v "match takes a variant, or a pointer to one, not ~s" `(,(shown t))))
1936
1795
  (when (and (pair? nulls) (not nullable))
1937
1796
  (substrate-error (car (car nulls)) "this match takes a variant, not a pointer to one, which is never 0" nil))
1938
1797
  (when (and (pair? nulls) (pair? (cdr nulls))) (substrate-error (car (cadr nulls)) "0 is matched twice" nil))
1939
- (match-arms e p t a tag nullable nulls (for/list ([c cases]) (car c)) (for/list ([c cases]) (cdr c)) nil
1940
- (numbers-below (length (cases-of t))) env hint)))
1798
+ (if (enum-type? t)
1799
+ (enum-match e p t a tag nulls cases env hint)
1800
+ (match-arms e p t a tag nullable nulls (for/list ([c cases]) (car c)) (for/list ([c cases]) (cdr c)) nil
1801
+ (numbers-below (length (cases-of t))) env hint))))
1941
1802
 
1942
1803
  (define (match-arms [e datum] [p datum] [t datum] [a datum] [tag datum] [nullable bool] [nulls datum] [names datum]
1943
1804
  [clauses datum] [arms datum] [left datum] [env datum] [hint datum]) -> datum
@@ -1994,18 +1855,6 @@
1994
1855
  hint)
1995
1856
  hint c)))))
1996
1857
 
1997
- (define (construct-type [e datum] [v datum] [i datum] [args datum] [env datum] [hint datum]) -> datum
1998
- (let ([b (type-binding v)])
1999
- (cond [(null? (parameters-of b)) (named-type v)]
2000
- [(and (struct-type? hint) (== (cadr hint) (identifier-name v))) hint]
2001
- [else
2002
- (let* ([ps (parameters-of b)]
2003
- [fields (parse-types-with (type-variables ps)
2004
- (for/list ([f (cdr (syntax->list (list-ref (syntax->list (caddr b)) (fixnum-value (syntax-e i)))))])
2005
- (syntax-match f [(_ ft) ft])))]
2006
- [s (infer e fields args env)])
2007
- `(struct ,(identifier-name v) ,(cadr b) ,(for/list ([p ps]) (bound-type e s p (syntax-e v)))))])))
2008
-
2009
1858
  (define (case-index [n datum] [t datum]) -> datum
2010
1859
  (syntax-match (let ([h (expand-head n)]) (if (== (car h) 'form) (caddr h) 0))
2011
1860
  [('#%substrate-construct v i _)
@@ -2334,7 +2183,7 @@
2334
2183
  [(float-type? t) (prim-code (op-name t 'convert_i32_u) `(,c))]
2335
2184
  [else (substrate-error where "expected ~s, got bool" `(,(shown t)))])]
2336
2185
  [(or (struct-type? t) (struct-type? from) (closure-type? t) (closure-type? from) (values-type? t) (values-type? from)
2337
- (view-type? t) (view-type? from))
2186
+ (view-type? t) (view-type? from) (enum-type? t) (enum-type? from))
2338
2187
  (unless (equal? from t) (substrate-error where "expected ~s, got ~s" (shown-apart t from)))
2339
2188
  c]
2340
2189
  [(address-type? t)
@@ -2390,6 +2239,13 @@
2390
2239
  (cons to (cdr r))]
2391
2240
  [else (substrate-error x "cannot cast ~s to ~s" `(,(shown from) ,(shown to)))])]
2392
2241
  [(view-type? from) (substrate-error x "cannot cast ~s to ~s" `(,(shown from) ,(shown to)))]
2242
+ [(enum-type? to)
2243
+ (cond [(equal? from to) (cons to (cdr r))]
2244
+ [(integer-type? (as-number from)) (cons to (convert r 'i32 x #f))]
2245
+ [else (substrate-error x "cannot cast ~s to ~s" `(,(shown from) ,(shown to)))])]
2246
+ [(enum-type? from)
2247
+ (unless (integer-type? to) (substrate-error x "cannot cast ~s to ~s" `(,(shown from) ,(shown to))))
2248
+ (cons (promoted to) (numeric (cdr r) 'i32 to #t))]
2393
2249
  [(address-type? to)
2394
2250
  (cond [(== from 'lit) (cons to (const-code 'i32 (cdr r)))]
2395
2251
  [(or (address-type? from) (memq from '(i32 u32))) (cons to (cdr r))]
@@ -2438,6 +2294,9 @@
2438
2294
  (let ([t (cond [(address-type? ta) ta]
2439
2295
  [(address-type? tb) tb]
2440
2296
  [(and (arithmetic? ta) (arithmetic? tb)) (common ta tb)]
2297
+ [(and (enum-type? ta) (equal? ta tb)) ta]
2298
+ [(or (enum-type? ta) (enum-type? tb))
2299
+ (substrate-error e "~a compares two values of the same enum, not ~s and ~s" `(,o ,(shown ta) ,(shown tb)))]
2441
2300
  [else (substrate-error e "~a compares numbers, pointers or strings, not ~s and ~s" `(,o ,(shown ta) ,(shown tb)))])])
2442
2301
  (cons 'bool (prim-code (instruction o t) `(,(convert a t e #f) ,(convert b t e #f)))))]
2443
2302
  [(and (arithmetic? ta) (arithmetic? tb))
@@ -2709,6 +2568,7 @@
2709
2568
  [(== t 'f32) (call-of 'print-float `(,(prim-code 'f64.promote_f32 `(,(cdr r))) ,(i32-const 1)))]
2710
2569
  [(equal? t (datum-type)) `(call ,(syntax-function-of 'print-datum (-> state phase)) (,(cdr r) ,(i32-const display)))]
2711
2570
  [(address-type? t) (call-of 'print-integer `(,(prim-code 'i64.extend_i32_u `(,(cdr r))) ,(i32-const 0)))]
2571
+ [(enum-type? t) (printed-enum r t)]
2712
2572
  [(and (struct-type? t) (variant-type? t)) (printed-variant r t a display)]
2713
2573
  [(and (struct-type? t) (== (type-kind t) 'struct)) (printed-struct r t a display)]
2714
2574
  [else (seq `(,(as-void r) ,(text-printed (format "#<~s>" `(,(shown t))))))])))
@@ -2779,13 +2639,10 @@
2779
2639
 
2780
2640
  (define (src [d datum]) -> datum (datum->syntax here d 0))
2781
2641
 
2782
- (define (data-form [kind datum] [s datum]) -> datum
2783
- (case kind
2784
- [('make-cell)
2785
- (syntax-match s
2786
- [(_ c) (at s (src `(let ([p (cell-bytes ,(cell-size c))]) (set! (@ p) ,c) p)))]
2787
- [(_ a c) (at s (src `(let ([p (cast datum (arena-alloc ,a ,(cell-size c)))]) (set! (@ p) ,c) p)))])]
2788
- [else (syntax-match s [(_ a t) (at s (src `(cast (ptr ,t) (arena-alloc ,a (sizeof ,t)))))])]))
2642
+ (define (data-form [s datum]) -> datum
2643
+ (syntax-match s
2644
+ [(_ c) (at s (src `(let ([p (cell-bytes ,(cell-size c))]) (set! (@ p) ,c) p)))]
2645
+ [(_ a c) (at s (src `(let ([p (cast datum (arena-alloc ,a ,(cell-size c)))]) (set! (@ p) ,c) p)))]))
2789
2646
 
2790
2647
  ;; A cell is a tag and its case's fields.
2791
2648
  (define (cell-size [c datum]) -> datum
@@ -2793,3 +2650,159 @@
2793
2650
  (fixnum 4)
2794
2651
  (let ([fields (cdr (assq (syntax-e (car (syntax->list c))) (cases-of (cadr (datum-type)))))])
2795
2652
  (fixnum (if (null? fields) 4 (let ([f (last fields)]) (+ (fixnum-value (caddr f)) (size-of (cadr f)))))))))
2653
+
2654
+ (define (check-new [m datum] [env datum]) -> datum
2655
+ (if (== (piece-role (list-ref (pieces-of m) 2)) 'type) (new-zeroed m env) (new-value m env)))
2656
+
2657
+ (define (new-value [m datum] [env datum]) -> datum
2658
+ (let* ([s (piece-at m 1)] [x (piece-at m 2)] [arena (expect s (arena-pointer) env #f)] [h (expand-in x env)]
2659
+ [pr (producer-with h x)] [produced (production-type pr)]
2660
+ [r (if (!= produced 0)
2661
+ 0
2662
+ (let ([c (check-with h x env 0)]) (if (== (car c) 'slit) (cons 'string (string-code (cdr c))) (settle c 0 x))))]
2663
+ [t (kept! (complete! (if (!= produced 0) produced (car r)) x) x)] [a (fresh-key!)] [p (fresh-key!)])
2664
+ (cons `(ptr ,t)
2665
+ `(let ((,a i32 ,arena a))
2666
+ (let ((,p i32 ,(allocation-from s (lref a) (i32-const (size-of t))) p))
2667
+ ,(seq `(,(cond [(!= produced 0) (as-void (constructed (lref p) pr produced x t env))]
2668
+ [(aggregate? t) (as-void (copied-into (lref p) r t x))]
2669
+ [else (store t (lref p) 0 (convert r t x #f))])
2670
+ ,(lref p))))))))
2671
+
2672
+ (define (new-zeroed [m datum] [env datum]) -> datum
2673
+ (let* ([u (piece-at m 2)] [t (kept! (complete! (parse-type u) u) u)] [size (size-of t)] [s (piece-at m 1)]
2674
+ [arena (expect s (arena-pointer) env #f)] [a (fresh-key!)] [p (fresh-key!)])
2675
+ (if (null? (cdddr (pieces-of m)))
2676
+ (cons `(ptr ,t)
2677
+ `(let ((,a i32 ,arena a))
2678
+ (let ((,p i32 ,(allocation-from s (lref a) (i32-const size)) p))
2679
+ ,(seq `(,(zero-bytes (lref p) size) ,(lref p))))))
2680
+ (let ([n (fresh-key!)])
2681
+ (cons `(slice ,t)
2682
+ `(let ((,a i32 ,arena a) (,n i32 ,(expect (piece-at m 3) 'u32 env #f) n))
2683
+ (let ((,p i32 ,(allocation-from s (lref a) (allocation-size (lref n) size)) p))
2684
+ ,(seq `(,(prim-code 'memory.fill `(,(lref p) ,(i32-const 0) ,(items-size (lref n) size)))
2685
+ ,(view-code (lref p) (lref n)))))))))))
2686
+
2687
+ (define (check-copy [m datum] [env datum]) -> datum
2688
+ (let* ([s (piece-at m 1)] [arena (expect s (arena-pointer) env #f)] [x (piece-at m 2)] [c (check x env 0)]
2689
+ [r (if (== (car c) 'slit) (cons 'string (string-code (cdr c))) (settle c 0 x))] [t (car r)])
2690
+ (unless (view-type? t) (substrate-error x "copy takes a string or a slice, not ~s" `(,(shown t))))
2691
+ (let ([size (size-of (complete! (view-items t) x))] [a (fresh-key!)] [v (fresh-key!)] [n (fresh-key!)] [p (fresh-key!)])
2692
+ (cons t `(let ((,a i32 ,arena a) (,v i64 ,(cdr r) v))
2693
+ (let ((,n i32 ,(view-length (lref v)) n))
2694
+ (let ((,p i32 ,(allocation-from s (lref a) (allocation-size (lref n) size)) p))
2695
+ ,(seq `(,(prim-code 'memory.copy `(,(lref p) ,(view-pointer (lref v)) ,(items-size (lref n) size)))
2696
+ ,(view-code (lref p) (lref n)))))))))))
2697
+
2698
+ (define (arena-pointer) -> datum `(ptr ,(named-type (datum->syntax here 'arena 0))))
2699
+
2700
+ (define (allocation-from [s datum] [a datum] [size datum]) -> datum
2701
+ (cond [(zero? (syntax-e s)) (call-of 'malloc `(,size))]
2702
+ [(syntax-match s [('& _) #t] [_ #f]) (call-of 'arena-alloc `(,a ,size))]
2703
+ [else
2704
+ (let ([n (fresh-key!)])
2705
+ `(let ((,n i32 ,size n))
2706
+ (if ,(prim-code 'i32.eqz `(,a)) ,(call-of 'malloc `(,(lref n))) ,(call-of 'arena-alloc `(,a ,(lref n))))))]))
2707
+
2708
+ (define (items-size [n datum] [size i32]) -> datum (if (== size 1) n (prim-code 'i32.mul `(,n ,(i32-const size)))))
2709
+
2710
+ ;; Past the limit, a size that malloc refuses.
2711
+ (define (allocation-size [n datum] [size i32]) -> datum
2712
+ (if (== size 0)
2713
+ (i32-const 0)
2714
+ `(select ,(i32-const 2147483584) ,(items-size n size) ,(prim-code 'i32.gt_u `(,n ,(i32-const (/ 2147483584 size)))))))
2715
+
2716
+ ;; ---- enums ----
2717
+
2718
+ (define (substrate-enum [s datum]) -> datum
2719
+ (syntax-match s
2720
+ [(_ (? identifier? name) c ...) (if (null? c) (substrate-error s "expected (enum name case ...)" nil) (enum-form name c))]
2721
+ [_ (substrate-error s "expected (enum name case ...)" nil)]))
2722
+
2723
+ (define (enum-form [name datum] [cs datum]) -> datum
2724
+ (let* ([names (for/list ([c cs])
2725
+ (syntax-match c
2726
+ [(? identifier? n) n]
2727
+ [((? identifier? n) k) (unless (integer? (syntax-e k)) (substrate-error k "expected a number" nil)) n]
2728
+ [_ (substrate-error c "expected a case or [case number]" nil)]))]
2729
+ [given (for/list ([c cs]) (syntax-match c [(_ k) k] [_ 0]))])
2730
+ (distinct! names '"case")
2731
+ (syn `(begin (define-syntax ,name (quote-syntax (substrate-struct ,name (,@(for/list ([n names]) `(,n))) enum ,(case-numbers cs given))))
2732
+ ,@(for/list ([n names] [i (numbers-below (length names))])
2733
+ `(define-syntax ,n (quote-syntax (#%form mark (#%substrate-construct ,name ,i ,n)))))))))
2734
+
2735
+ (define (case-numbers [cs datum] [given datum]) -> datum
2736
+ (let ([next i64 0] [out nil])
2737
+ (while (pair? cs)
2738
+ (let* ([k (car given)] [n (if (== k 0) next (integer-value (syntax-e k)))] [d (make-integer n)])
2739
+ (when (or (< n -2147483648) (> n 2147483647)) (substrate-error (car cs) "a case's number is a 32-bit integer" nil))
2740
+ (when (member d out) (substrate-error (car cs) "two cases have the number ~a" `(,d)))
2741
+ (set! out (cons d out))
2742
+ (set! next (+ n 1)))
2743
+ (set! cs (cdr cs))
2744
+ (set! given (cdr given)))
2745
+ (reverse out)))
2746
+
2747
+ (define (enum-numbers [t datum]) -> datum (numbers-of (type-value (syntax-local-value (name-identifier (cadr t))))))
2748
+
2749
+ (define (enum-constant [t datum] [i datum]) -> datum (const-code 'i32 (list-ref (enum-numbers t) (fixnum-value i))))
2750
+
2751
+ (define (match-base [s datum] [env datum]) -> datum
2752
+ (let* ([h (expand-head s)] [p (if (place-head? h) (place-with h s env) 0)])
2753
+ (if (and (!= p 0) (or (struct-type? (car p)) (view-type? (car p))))
2754
+ p
2755
+ (let ([r (if (!= p 0) (read-place p s) (check-with h s env 0))])
2756
+ (cond [(enum-type? (car r)) `(,(car r) local ,(cdr r))]
2757
+ [(and (pointer? (car r)) (enum-type? (cadr (car r)))) `(,(cadr (car r)) memory ,(cdr r) 0 pointer)]
2758
+ [else (based r s)])))))
2759
+
2760
+ (define (enum-match [e datum] [p datum] [t datum] [a datum] [k datum] [nulls datum] [clauses datum] [env datum] [hint datum])
2761
+ -> datum
2762
+ (let ([left (numbers-below (length (cases-of t)))] [arms nil] [default datum 0] [numbers (enum-numbers t)]
2763
+ [null-arm (if (pair? nulls) (check-sequence (cadddr (car nulls)) (caddr (car nulls)) env hint) 0)])
2764
+ (for/each ([c clauses])
2765
+ (let* ([n (car c)] [x (cadr c)] [cl (cadddr c)] [i (if (== (syntax-e n) '_) 0 (case-index n t))])
2766
+ (when (null? left) (substrate-error cl "this clause can never match" nil))
2767
+ (when (and (!= i 0) (not (member i left))) (substrate-error n "~a is matched twice" `(,(syntax-e n))))
2768
+ (when (pair? x) (substrate-error cl "~a has ~a field~a, not ~a" `(,(syntax-e n) ,(fixnum 0) ,(plural 0) ,(fixnum (length x)))))
2769
+ (let ([r (check-sequence cl (caddr c) env hint)])
2770
+ (if (== i 0)
2771
+ (begin
2772
+ (set! default (cons r cl))
2773
+ (set! left nil))
2774
+ (begin
2775
+ (set! arms (cons `(,(list-ref numbers (fixnum-value i)) ,r ,cl) arms))
2776
+ (set! left (for/list ([j left] #:when (not (equal? j i))) j)))))))
2777
+ (when (and (pair? left) (== default 0))
2778
+ (substrate-error e "this match does not cover ~a" `(,(joined (for/list ([i left]) (car (list-ref (cases-of t) (fixnum-value i))))))))
2779
+ (let* ([arms (reverse arms)]
2780
+ [j (for/fold ([ty (if (!= default 0) (car (car default)) (if (!= null-arm 0) (car null-arm) 'none))]) ([x arms])
2781
+ (join ty (car (cadr x))))]
2782
+ [j (if (!= null-arm 0) (join j (car null-arm)) j)]
2783
+ [ty (if (and (== j 'void) (!= hint 0) (!= hint 'void)) hint (promoted (settled-for j hint)))]
2784
+ [chain `(let ((,k i32 ,(if (== (cadr p) 'local) (caddr p) (load t (lref a) (fixnum-value (cadddr p)))) tag))
2785
+ ,(enum-chain arms default ty k))])
2786
+ (cons ty (cond [(== (cadr p) 'local) chain]
2787
+ [(!= null-arm 0)
2788
+ `(let ((,a i32 ,(caddr p) a))
2789
+ (if ,(prim-code 'i32.eqz `(,(lref a))) ,(as null-arm ty (cadddr (car nulls))) ,chain))]
2790
+ [else `(let ((,a i32 ,(caddr p) a)) ,chain)])))))
2791
+
2792
+ (define (enum-chain [arms datum] [default datum] [ty datum] [k datum]) -> datum
2793
+ (if (null? arms)
2794
+ (if (!= default 0) (as (car default) ty (cdr default)) '(unreachable))
2795
+ `(if ,(prim-code 'i32.eq `(,(lref k) ,(const-code 'i32 (car (car arms)))))
2796
+ ,(as (cadr (car arms)) ty (caddr (car arms)))
2797
+ ,(enum-chain (cdr arms) default ty k))))
2798
+
2799
+ (define (printed-enum [r datum] [t datum]) -> datum
2800
+ (let ([k (fresh-key!)])
2801
+ `(let ((,k i32 ,(cdr r) k)) ,(enum-printed k (for/list ([c (cases-of t)]) (car c)) (enum-numbers t)))))
2802
+
2803
+ (define (enum-printed [k datum] [names datum] [numbers datum]) -> datum
2804
+ (if (null? names)
2805
+ (call-of 'print-integer `(,(prim-code 'i64.extend_i32_s `(,(lref k))) ,(i32-const 1)))
2806
+ `(if ,(prim-code 'i32.eq `(,(lref k) ,(const-code 'i32 (car numbers))))
2807
+ ,(text-printed (format "~a" `(,(car names))))
2808
+ ,(enum-printed k (cdr names) (cdr numbers)))))