aver-lang 0.15.2

VM and transpiler for Aver, a statically-typed language designed for AI-assisted development
Documentation
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
;; Numeric parsing — both return Result<numeric, String>:
;;   rt_int_from_str   — Result<Int, String>; never errors today
;;                        (out-of-spec input silently parses as 0).
;;   rt_float_from_str — Result<Float, String>; rejects non-digits
;;                        (other than `-`, `.`, `e`/`E`, `+`/`-`),
;;                        decimal-point twice, dangling exponent
;;                        sign, empty/lone-`-` input. Failure case
;;                        wraps the original input str as the error.

(func $rt_int_from_str (param $str i32) (result i32)
  (local $len i32)
  (local $value i64)
  (local $idx i32)
  (local $negative i32)

  ;; len = header.byte_len
  local.get $str
  i64.load
  i64.const 0xFFFFFFFF
  i64.and
  i32.wrap_i64
  local.set $len

  ;; Optional leading '-'
  local.get $len
  i32.const 0
  i32.gt_s
  if
    local.get $str
    i32.load8_u offset=8
    i32.const 0x2D   ;; '-'
    i32.eq
    if
      i32.const 1
      local.set $negative
      i32.const 1
      local.set $idx
    end
  end

  block
    loop
      local.get $idx
      local.get $len
      i32.ge_u
      br_if 1

      local.get $value
      i64.const 10
      i64.mul
      local.get $str
      local.get $idx
      i32.add
      i32.load8_u offset=8
      i32.const 0x30   ;; '0'
      i32.sub
      i64.extend_i32_u
      i64.add
      local.set $value

      local.get $idx
      i32.const 1
      i32.add
      local.set $idx
      br 0
    end
  end

  local.get $negative
  if
    i64.const 0
    local.get $value
    i64.sub
    local.set $value
  end

  i32.const 0   ;; WRAP_OK
  local.get $value
  i32.const 0
  call $rt_wrap
)
(export "rt_int_from_str" (func $rt_int_from_str))

(func $rt_float_from_str (param $str i32) (result i32)
  (local $len i32)
  (local $idx i32)
  (local $negative i32)
  (local $seen_dot i32)
  (local $saw_digit i32)
  (local $ch i32)
  (local $digit i32)
  (local $exp_state i32)        ;; 0=none, 1=just-saw-e, 2=after-sign-or-digit
  (local $exp_negative i32)
  (local $saw_exp_digit i32)
  (local $exp_value i32)
  (local $value f64)
  (local $frac_div f64)

  local.get $str
  i64.load
  i64.const 0xFFFFFFFF
  i64.and
  i32.wrap_i64
  local.set $len

  f64.const 0
  local.set $value
  f64.const 1
  local.set $frac_div

  ;; Leading '-'
  local.get $len
  i32.const 0
  i32.gt_s
  if
    local.get $str
    i32.load8_u offset=8
    i32.const 0x2D   ;; '-'
    i32.eq
    if
      i32.const 1
      local.set $negative
      i32.const 1
      local.set $idx
    end
  end

  block
    loop
      local.get $idx
      local.get $len
      i32.ge_u
      br_if 1

      local.get $str
      local.get $idx
      i32.add
      i32.load8_u offset=8
      local.set $ch

      ;; Exponent sign right after e/E.
      local.get $exp_state
      i32.const 1
      i32.eq
      local.get $ch
      i32.const 0x2B   ;; '+'
      i32.eq
      i32.and
      if
        i32.const 2
        local.set $exp_state
        local.get $idx
        i32.const 1
        i32.add
        local.set $idx
        br 1
      end

      local.get $exp_state
      i32.const 1
      i32.eq
      local.get $ch
      i32.const 0x2D   ;; '-'
      i32.eq
      i32.and
      if
        i32.const 1
        local.set $exp_negative
        i32.const 2
        local.set $exp_state
        local.get $idx
        i32.const 1
        i32.add
        local.set $idx
        br 1
      end

      ;; Inside exponent digits.
      local.get $exp_state
      if
        local.get $ch
        i32.const 0x30   ;; '0'
        i32.lt_u
        if
          i32.const 1   ;; WRAP_ERR
          local.get $str
          i32.const 1
          call $rt_wrap_i32
          return
        end
        local.get $ch
        i32.const 0x39   ;; '9'
        i32.gt_u
        if
          i32.const 1
          local.get $str
          i32.const 1
          call $rt_wrap_i32
          return
        end

        i32.const 1
        local.set $saw_exp_digit
        i32.const 2
        local.set $exp_state

        local.get $ch
        i32.const 0x30
        i32.sub
        local.set $digit

        local.get $exp_value
        i32.const 10
        i32.mul
        local.get $digit
        i32.add
        local.set $exp_value

        local.get $idx
        i32.const 1
        i32.add
        local.set $idx
        br 1
      end

      ;; Decimal point.
      local.get $ch
      i32.const 0x2E   ;; '.'
      i32.eq
      if
        local.get $seen_dot
        if
          i32.const 1
          local.get $str
          i32.const 1
          call $rt_wrap_i32
          return
        end
        i32.const 1
        local.set $seen_dot
        local.get $idx
        i32.const 1
        i32.add
        local.set $idx
        br 1
      end

      ;; Exponent marker.
      local.get $ch
      i32.const 0x65   ;; 'e'
      i32.eq
      local.get $ch
      i32.const 0x45   ;; 'E'
      i32.eq
      i32.or
      if
        local.get $saw_digit
        i32.eqz
        if
          i32.const 1
          local.get $str
          i32.const 1
          call $rt_wrap_i32
          return
        end
        i32.const 1
        local.set $exp_state
        local.get $idx
        i32.const 1
        i32.add
        local.set $idx
        br 1
      end

      ;; Mantissa digit.
      local.get $ch
      i32.const 0x30
      i32.lt_u
      if
        i32.const 1
        local.get $str
        i32.const 1
        call $rt_wrap_i32
        return
      end
      local.get $ch
      i32.const 0x39
      i32.gt_u
      if
        i32.const 1
        local.get $str
        i32.const 1
        call $rt_wrap_i32
        return
      end

      i32.const 1
      local.set $saw_digit

      local.get $ch
      i32.const 0x30
      i32.sub
      local.set $digit

      local.get $seen_dot
      if
        local.get $frac_div
        f64.const 10
        f64.mul
        local.set $frac_div
        local.get $value
        local.get $digit
        f64.convert_i32_u
        local.get $frac_div
        f64.div
        f64.add
        local.set $value
      else
        local.get $value
        f64.const 10
        f64.mul
        local.get $digit
        f64.convert_i32_u
        f64.add
        local.set $value
      end

      local.get $idx
      i32.const 1
      i32.add
      local.set $idx
      br 0
    end
  end

  ;; Reject empty / lone '-'.
  local.get $saw_digit
  i32.eqz
  if
    i32.const 1
    local.get $str
    i32.const 1
    call $rt_wrap_i32
    return
  end

  ;; Reject dangling exponent marker (state == 1).
  local.get $exp_state
  i32.const 1
  i32.eq
  if
    i32.const 1
    local.get $str
    i32.const 1
    call $rt_wrap_i32
    return
  end

  ;; Reject exponent sign with no digits (state == 2 && !saw_exp_digit).
  local.get $exp_state
  i32.const 2
  i32.eq
  local.get $saw_exp_digit
  i32.eqz
  i32.and
  if
    i32.const 1
    local.get $str
    i32.const 1
    call $rt_wrap_i32
    return
  end

  ;; Apply exponent.
  block
    loop
      local.get $exp_value
      i32.eqz
      br_if 1

      local.get $exp_negative
      if
        local.get $value
        f64.const 10
        f64.div
        local.set $value
      else
        local.get $value
        f64.const 10
        f64.mul
        local.set $value
      end

      local.get $exp_value
      i32.const 1
      i32.sub
      local.set $exp_value
      br 0
    end
  end

  ;; Apply sign.
  local.get $negative
  if
    f64.const 0
    local.get $value
    f64.sub
    local.set $value
  end

  ;; Result.Ok(value)
  i32.const 0   ;; WRAP_OK
  local.get $value
  call $rt_wrap_f64
)
(export "rt_float_from_str" (func $rt_float_from_str))