aver-lang 0.17.0

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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#[allow(unused_imports)]
use crate::aver_generated::domain::ast::*;
#[allow(unused_imports)]
use crate::aver_generated::domain::value::*;
#[allow(unused_imports)]
use crate::*;

#[allow(non_camel_case_types)]
enum __MutualTco1 {
    MatchPatTupleItemsAcc(
        aver_rt::AverList<Pattern>,
        aver_rt::AverList<Val>,
        aver_rt::AverList<(AverStr, Val)>,
    ),
    MatchPatTupleOne(
        Pattern,
        Val,
        aver_rt::AverList<Pattern>,
        aver_rt::AverList<Val>,
        aver_rt::AverList<(AverStr, Val)>,
    ),
}

fn __mutual_tco_trampoline_1(
    mut __state: __MutualTco1,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    loop {
        __state = match __state {
            __MutualTco1::MatchPatTupleItemsAcc(mut pats, mut items, mut acc) => {
                crate::cancel_checkpoint();
                aver_list_match!(pats, [] => { return Ok(acc) }, [pat, restPats] => { aver_list_match!(items, [] => { return Err(AverStr::from("no match")) }, [item, restItems] => __MutualTco1::MatchPatTupleOne(pat, item, restPats, restItems, acc)) })
            }
            __MutualTco1::MatchPatTupleOne(
                mut pat,
                mut item,
                mut restPats,
                mut restItems,
                mut acc,
            ) => {
                crate::cancel_checkpoint();
                let bindings = matchPattern(&pat, &item)?;
                __MutualTco1::MatchPatTupleItemsAcc(
                    restPats,
                    restItems,
                    aver_rt::AverList::concat(&acc, &bindings),
                )
            }
        };
    }
}

/// Accumulate bindings from tuple pattern matching.
pub fn matchPatTupleItemsAcc(
    pats: &aver_rt::AverList<Pattern>,
    items: &aver_rt::AverList<Val>,
    acc: &aver_rt::AverList<(AverStr, Val)>,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    __mutual_tco_trampoline_1(__MutualTco1::MatchPatTupleItemsAcc(
        pats.clone(),
        items.clone(),
        acc.clone(),
    ))
}

/// Match one tuple element and continue with accumulated bindings.
pub fn matchPatTupleOne(
    pat: &Pattern,
    item: &Val,
    restPats: &aver_rt::AverList<Pattern>,
    restItems: &aver_rt::AverList<Val>,
    acc: &aver_rt::AverList<(AverStr, Val)>,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    __mutual_tco_trampoline_1(__MutualTco1::MatchPatTupleOne(
        pat.clone(),
        item.clone(),
        restPats.clone(),
        restItems.clone(),
        acc.clone(),
    ))
}

/// Try to match a value against a pattern. Returns bindings on success.
pub fn matchPattern(pat: &Pattern, v: &Val) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match pat.clone() {
        Pattern::PatWild => Ok(aver_rt::AverList::empty()),
        Pattern::PatInt(n) => matchPatInt(n, v),
        Pattern::PatFloat(f) => matchPatFloat(f, v),
        Pattern::PatBool(b) => matchPatBool(b, v),
        Pattern::PatStr(s) => matchPatStr(s, v),
        Pattern::PatEmpty => matchPatEmpty(v),
        Pattern::PatCons(h, t) => matchPatCons(h, t, v),
        Pattern::PatConstructor(ctorName, bindings) => matchPatConstructor(ctorName, &bindings, v),
        Pattern::PatConstructorId(tag, ctorName, bindings) => {
            matchPatConstructorById(tag, ctorName, &bindings, v)
        }
        Pattern::PatTuple(pats) => matchPatTuple(&pats, v),
        Pattern::PatVar(name) => Ok(aver_rt::AverList::from_vec(vec![(name, v.clone())])),
    }
}

/// Match a PatInt pattern against a value.
pub fn matchPatInt(n: i64, v: &Val) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v.clone() {
        Val::ValInt(m) => {
            if (n == m) {
                Ok(aver_rt::AverList::empty())
            } else {
                Err(AverStr::from("no match"))
            }
        }
        _ => Err(AverStr::from("no match")),
    }
}

/// Match a PatFloat pattern against a value.
pub fn matchPatFloat(f: f64, v: &Val) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v.clone() {
        Val::ValFloat(vf) => {
            if (f == vf) {
                Ok(aver_rt::AverList::empty())
            } else {
                Err(AverStr::from("no match"))
            }
        }
        _ => Err(AverStr::from("no match")),
    }
}

/// Match a PatBool pattern against a value.
pub fn matchPatBool(b: bool, v: &Val) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v.clone() {
        Val::ValBool(vb) => {
            if (b == vb) {
                Ok(aver_rt::AverList::empty())
            } else {
                Err(AverStr::from("no match"))
            }
        }
        _ => Err(AverStr::from("no match")),
    }
}

/// Match a string literal pattern.
pub fn matchPatStr(s: AverStr, v: &Val) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v.clone() {
        Val::ValStr(vs) => {
            if (s == vs) {
                Ok(aver_rt::AverList::empty())
            } else {
                Err(AverStr::from("no match"))
            }
        }
        _ => Err(AverStr::from("no match")),
    }
}

/// Match empty list pattern [].
pub fn matchPatEmpty(v: &Val) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v.clone() {
        Val::ValList(items) => {
            let __list_subject = items;
            if __list_subject.is_empty() {
                Ok(aver_rt::AverList::empty())
            } else {
                Err(AverStr::from("no match"))
            }
        }
        _ => Err(AverStr::from("no match")),
    }
}

/// Match cons pattern [h, ..t].
pub fn matchPatCons(
    h: AverStr,
    t: AverStr,
    v: &Val,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v.clone() {
        Val::ValList(items) => {
            aver_list_match!(items, [] => Err(AverStr::from("no match")), [head, tail] => Ok(aver_rt::AverList::from_vec(vec![(h, head), (t, Val::ValList(tail))])))
        }
        _ => Err(AverStr::from("no match")),
    }
}

/// Match constructor pattern. Checks tag name and extracts inner values.
#[inline(always)]
pub fn matchPatConstructor(
    ctorName: AverStr,
    bindings: &aver_rt::AverList<AverStr>,
    v: &Val,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    {
        let __dispatch_subject = ctorName.clone();
        if &*__dispatch_subject == "Result.Ok" {
            matchWrapperPat(v, AverStr::from("Ok"), bindings)
        } else {
            if &*__dispatch_subject == "Result.Err" {
                matchWrapperPat(v, AverStr::from("Err"), bindings)
            } else {
                if &*__dispatch_subject == "Option.Some" {
                    matchWrapperPat(v, AverStr::from("Some"), bindings)
                } else {
                    if &*__dispatch_subject == "Option.None" {
                        matchNonePat(v)
                    } else {
                        matchGenericConstructor(ctorName, bindings, v)
                    }
                }
            }
        }
    }
}

/// Match tuple pattern with nested patterns.
pub fn matchPatTuple(
    pats: &aver_rt::AverList<Pattern>,
    v: &Val,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v.clone() {
        Val::ValTuple(items) => matchPatTupleItems(pats, &items),
        _ => Err(AverStr::from("no match")),
    }
}

/// Match each pattern against corresponding tuple item.
pub fn matchPatTupleItems(
    pats: &aver_rt::AverList<Pattern>,
    items: &aver_rt::AverList<Val>,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    matchPatTupleItemsAcc(pats, items, &aver_rt::AverList::empty())
}

/// Match a generic variant constructor pattern using the full name stored in the value.
pub fn matchGenericConstructor(
    ctorName: AverStr,
    bindings: &aver_rt::AverList<AverStr>,
    v: &Val,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v.clone() {
        Val::ValVariant(_, fullName, fields) => {
            matchGenericCtorCheck(ctorName, fullName, bindings, &fields)
        }
        _ => Err(AverStr::from("no match")),
    }
}

/// Check if variant full name matches constructor name.
#[inline(always)]
pub fn matchGenericCtorCheck(
    ctorName: AverStr,
    fullName: AverStr,
    bindings: &aver_rt::AverList<AverStr>,
    fields: &aver_rt::AverList<Val>,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    if (fullName == ctorName) {
        Ok(zipBindings(bindings, fields))
    } else {
        Err(AverStr::from("no match"))
    }
}

/// Match constructor pattern by integer tag ID. Builtins dispatch directly; user variants confirm both tag and full name.
#[inline(always)]
pub fn matchPatConstructorById(
    tag: i64,
    ctorName: AverStr,
    bindings: &aver_rt::AverList<AverStr>,
    v: &Val,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    {
        let __dispatch_subject = tag;
        if __dispatch_subject == 1i64 {
            matchWrapperPatDirect(v, 1i64, bindings)
        } else {
            if __dispatch_subject == 2i64 {
                matchWrapperPatDirect(v, 2i64, bindings)
            } else {
                if __dispatch_subject == 3i64 {
                    matchWrapperPatDirect(v, 3i64, bindings)
                } else {
                    if __dispatch_subject == 4i64 {
                        matchNonePat(v)
                    } else {
                        matchGenericCtorById(tag, ctorName, bindings, v)
                    }
                }
            }
        }
    }
}

/// Match Ok/Err/Some wrapper by integer tag. 1=Ok, 2=Err, 3=Some.
#[inline(always)]
pub fn matchWrapperPatDirect(
    v: &Val,
    tag: i64,
    bindings: &aver_rt::AverList<AverStr>,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    {
        let __dispatch_subject = tag;
        if __dispatch_subject == 1i64 {
            match v.clone() {
                Val::ValOk(inner) => {
                    let inner = (*inner).clone();
                    Ok(zipBindings(
                        bindings,
                        &aver_rt::AverList::from_vec(vec![inner]),
                    ))
                }
                _ => Err(AverStr::from("no match")),
            }
        } else {
            if __dispatch_subject == 2i64 {
                match v.clone() {
                    Val::ValErr(inner) => {
                        let inner = (*inner).clone();
                        Ok(zipBindings(
                            bindings,
                            &aver_rt::AverList::from_vec(vec![inner]),
                        ))
                    }
                    _ => Err(AverStr::from("no match")),
                }
            } else {
                if __dispatch_subject == 3i64 {
                    match v.clone() {
                        Val::ValSome(inner) => {
                            let inner = (*inner).clone();
                            Ok(zipBindings(
                                bindings,
                                &aver_rt::AverList::from_vec(vec![inner]),
                            ))
                        }
                        _ => Err(AverStr::from("no match")),
                    }
                } else {
                    Err(AverStr::from("no match"))
                }
            }
        }
    }
}

/// Match a user-defined variant by integer tag first, then full constructor name for collision safety.
pub fn matchGenericCtorById(
    tag: i64,
    ctorName: AverStr,
    bindings: &aver_rt::AverList<AverStr>,
    v: &Val,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v.clone() {
        Val::ValVariant(vTag, fullName, fields) => {
            if (vTag == tag) {
                if (fullName == ctorName) {
                    Ok(zipBindings(bindings, &fields))
                } else {
                    Err(AverStr::from("no match"))
                }
            } else {
                Err(AverStr::from("no match"))
            }
        }
        _ => Err(AverStr::from("no match")),
    }
}

/// Match Option.None pattern.
pub fn matchNonePat(v: &Val) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    match v {
        Val::ValNone => Ok(aver_rt::AverList::empty()),
        _ => Err(AverStr::from("no match")),
    }
}

/// Match Ok/Err/Some wrapper and bind inner value.
#[inline(always)]
pub fn matchWrapperPat(
    v: &Val,
    tag: AverStr,
    bindings: &aver_rt::AverList<AverStr>,
) -> Result<aver_rt::AverList<(AverStr, Val)>, AverStr> {
    crate::cancel_checkpoint();
    {
        let __dispatch_subject = tag;
        if &*__dispatch_subject == "Ok" {
            match v.clone() {
                Val::ValOk(inner) => {
                    let inner = (*inner).clone();
                    Ok(zipBindings(
                        bindings,
                        &aver_rt::AverList::from_vec(vec![inner]),
                    ))
                }
                _ => Err(AverStr::from("no match")),
            }
        } else {
            if &*__dispatch_subject == "Err" {
                match v.clone() {
                    Val::ValErr(inner) => {
                        let inner = (*inner).clone();
                        Ok(zipBindings(
                            bindings,
                            &aver_rt::AverList::from_vec(vec![inner]),
                        ))
                    }
                    _ => Err(AverStr::from("no match")),
                }
            } else {
                if &*__dispatch_subject == "Some" {
                    match v.clone() {
                        Val::ValSome(inner) => {
                            let inner = (*inner).clone();
                            Ok(zipBindings(
                                bindings,
                                &aver_rt::AverList::from_vec(vec![inner]),
                            ))
                        }
                        _ => Err(AverStr::from("no match")),
                    }
                } else {
                    Err(AverStr::from("no match"))
                }
            }
        }
    }
}

/// Pair binding names with values.
#[inline(always)]
pub fn zipBindings(
    names: &aver_rt::AverList<AverStr>,
    vals: &aver_rt::AverList<Val>,
) -> aver_rt::AverList<(AverStr, Val)> {
    crate::cancel_checkpoint();
    zipBindingsAcc(names.clone(), vals.clone(), aver_rt::AverList::empty())
}

/// Accumulate binding pairs in reverse, then reverse at end.
#[inline(always)]
pub fn zipBindingsAcc(
    mut names: aver_rt::AverList<AverStr>,
    mut vals: aver_rt::AverList<Val>,
    mut acc: aver_rt::AverList<(AverStr, Val)>,
) -> aver_rt::AverList<(AverStr, Val)> {
    loop {
        crate::cancel_checkpoint();
        let reversed = acc.reverse();
        return aver_list_match!(names, [] => reversed, [n, ns] => { aver_list_match!(vals, [] => reversed, [v, vs] => { {
            let __tmp2 = aver_rt::AverList::prepend((n, v), &acc);
            names = ns;
            vals = vs;
            acc = __tmp2;
            continue;
        } }) });
    }
}