vyre-libs 0.6.2

vyre Category A library ecosystem - pure-IR compositions over vyre-ops hardware primitives
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Nano-subset Rust parser.

use crate::parsing::rust::lex::lexer::core::Token;
use crate::parsing::rust::lex::tokens::*;

/// Expression AST.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
    /// Integer literal with source offset and value.
    LiteralInt(u32, u64),
    /// Boolean literal with source offset and value.
    LiteralBool(u32, bool),
    /// Variable reference by source offset.
    Var(u32),
    /// Binary operation.
    Binary {
        /// Operator token kind.
        op: u16,
        /// Left-hand side.
        lhs: Box<Expr>,
        /// Right-hand side.
        rhs: Box<Expr>,
    },
    /// Borrow expression.
    Borrow {
        /// Whether the borrow is mutable.
        mutable: bool,
        /// Borrowed expression.
        expr: Box<Expr>,
    },
    /// Dereference.
    Deref(Box<Expr>),
    /// Logical negation (`!expr`).
    Not(Box<Expr>),
    /// Arithmetic negation (`-expr`).
    Neg(Box<Expr>),
    /// Function call.
    Call {
        /// Function name source offset.
        name: u32,
        /// Arguments.
        args: Vec<Expr>,
    },
    /// Block expression.
    Block(Vec<Stmt>),
    /// Conditional.
    If {
        /// Condition.
        cond: Box<Expr>,
        /// Then block.
        then_block: Box<Expr>,
        /// Else block (optional).
        else_block: Option<Box<Expr>>,
    },
}

/// Statement AST.
#[derive(Debug, Clone, PartialEq)]
pub enum Stmt {
    /// Let binding.
    Let {
        /// Whether the binding is mutable.
        mutable: bool,
        /// Name source offset.
        name: u32,
        /// Declared type.
        ty: Type,
        /// Initializer expression.
        init: Expr,
    },
    /// Expression statement.
    Expr(Expr),
    /// Assignment to an existing binding (`name = value;`).
    Assign {
        /// Target name source offset.
        name: u32,
        /// Assigned value.
        value: Expr,
    },
    /// Return statement.
    Return(Option<Expr>),
    /// While loop (`while cond { body }`).
    While {
        /// Loop condition.
        cond: Expr,
        /// Loop body.
        body: Vec<Stmt>,
    },
    /// Half-open range loop (`for name in start..end { body }`).
    For {
        /// Loop variable name source offset.
        name: u32,
        /// Inclusive start expression.
        start: Expr,
        /// Exclusive end expression.
        end: Expr,
        /// Loop body.
        body: Vec<Stmt>,
    },
}

/// Types in the nano-subset.
#[derive(Debug, Clone, PartialEq)]
pub enum Type {
    /// 32-bit signed integer.
    I32,
    /// Boolean.
    Bool,
    /// Unit type.
    Unit,
    /// Reference type.
    Ref {
        /// Whether the reference is mutable.
        mutable: bool,
        /// Inner type.
        inner: Box<Type>,
    },
}

/// Function definition.
#[derive(Debug, Clone, PartialEq)]
pub struct Function {
    /// Name source offset.
    pub name: u32,
    /// Parameters: (name offset, type).
    pub params: Vec<(u32, Type)>,
    /// Return type.
    pub ret: Type,
    /// Body statements.
    pub body: Vec<Stmt>,
}

/// A parsed module.
#[derive(Debug, Clone, PartialEq)]
pub struct Module {
    /// Functions in the module.
    pub functions: Vec<Function>,
}

/// Parse error.
#[derive(Debug, Clone, PartialEq)]
pub struct ParseError {
    /// Error message.
    pub message: String,
    /// Token index where the error occurred.
    pub token_index: usize,
}

/// Maximum recursive-descent nesting depth. Hostile input (e.g. thousands of
/// nested parens or `* ! &mut` chains) would otherwise recurse until the native
/// stack overflows — an uncatchable process abort and a clean algorithmic-DoS
/// vector for the frontend. We fail closed with a typed `ParseError` well below
/// any stack limit; real programs never approach this depth.
const MAX_PARSE_DEPTH: usize = 256;

/// Parse a token stream into a `Module`.
pub fn parse(source: &[u8], tokens: &[Token]) -> Result<Module, ParseError> {
    let mut p = Parser {
        source,
        tokens,
        pos: 0,
        depth: 0,
    };
    p.parse_module()
}

struct Parser<'a> {
    source: &'a [u8],
    tokens: &'a [Token],
    pos: usize,
    depth: usize,
}

impl<'a> Parser<'a> {
    fn peek(&self) -> &Token {
        &self.tokens[self.pos.min(self.tokens.len() - 1)]
    }

    fn advance(&mut self) -> &Token {
        let tok = &self.tokens[self.pos.min(self.tokens.len() - 1)];
        if self.pos + 1 < self.tokens.len() {
            self.pos += 1;
        }
        tok
    }

    fn expect(&mut self, kind: u16) -> Result<&Token, ParseError> {
        let tok = self.peek();
        if tok.kind == kind {
            Ok(self.advance())
        } else {
            Err(ParseError {
                message: format!("expected token kind {}, got {}", kind, tok.kind),
                token_index: self.pos,
            })
        }
    }

    fn parse_module(&mut self) -> Result<Module, ParseError> {
        let mut functions = Vec::new();
        while self.peek().kind != EOF {
            functions.push(self.parse_function()?);
        }
        Ok(Module { functions })
    }

    fn parse_function(&mut self) -> Result<Function, ParseError> {
        self.expect(KW_FN)?;
        let name = self.expect(IDENT)?.start;
        self.expect(LPAREN)?;
        let params = self.parse_params()?;
        self.expect(RPAREN)?;
        let ret = if self.peek().kind == ARROW {
            self.advance();
            self.parse_type()?
        } else {
            Type::Unit
        };
        let body = self.parse_block()?;
        Ok(Function {
            name,
            params,
            ret,
            body,
        })
    }

    fn parse_params(&mut self) -> Result<Vec<(u32, Type)>, ParseError> {
        let mut params = Vec::new();
        if self.peek().kind == RPAREN {
            return Ok(params);
        }
        loop {
            let name = self.expect(IDENT)?.start;
            self.expect(COLON)?;
            let ty = self.parse_type()?;
            params.push((name, ty));
            if self.peek().kind == COMMA {
                self.advance();
            } else {
                break;
            }
        }
        Ok(params)
    }

    fn parse_type(&mut self) -> Result<Type, ParseError> {
        // `&mut &mut ... T` right-recurses here; guard it on the shared counter.
        self.depth += 1;
        let r = if self.depth > MAX_PARSE_DEPTH {
            Err(ParseError {
                message: "type nesting too deep".into(),
                token_index: self.pos,
            })
        } else {
            self.parse_type_inner()
        };
        self.depth -= 1;
        r
    }

    fn parse_type_inner(&mut self) -> Result<Type, ParseError> {
        match self.peek().kind {
            KW_I32 => {
                self.advance();
                Ok(Type::I32)
            }
            KW_BOOL => {
                self.advance();
                Ok(Type::Bool)
            }
            AMP | AMP_MUT => {
                let mutable = self.peek().kind == AMP_MUT;
                self.advance();
                let inner = self.parse_type()?;
                Ok(Type::Ref {
                    mutable,
                    inner: Box::new(inner),
                })
            }
            _ => Err(ParseError {
                message: "expected type".into(),
                token_index: self.pos,
            }),
        }
    }

    fn parse_block(&mut self) -> Result<Vec<Stmt>, ParseError> {
        // `parse_block` is the single convergence point for ALL block nesting:
        // `while`/`loop` bodies, `if`/`else` arms, the fn body, and bare block
        // expressions. The `while` body in particular is reached by a direct
        // `parse_block` call (the cond's `parse_expr` has already decremented),
        // so without guarding here, `while c { while c { ... } }` recurses
        // unbounded and overflows the native stack. Guard at the block so every
        // nesting construct — present and future — fails closed.
        self.depth += 1;
        let r = if self.depth > MAX_PARSE_DEPTH {
            Err(ParseError {
                message: "block nesting too deep".into(),
                token_index: self.pos,
            })
        } else {
            self.parse_block_inner()
        };
        self.depth -= 1;
        r
    }

    fn parse_block_inner(&mut self) -> Result<Vec<Stmt>, ParseError> {
        self.expect(LBRACE)?;
        let mut stmts = Vec::new();
        while self.peek().kind != RBRACE && self.peek().kind != EOF {
            stmts.push(self.parse_stmt()?);
        }
        self.expect(RBRACE)?;
        Ok(stmts)
    }

    fn parse_stmt(&mut self) -> Result<Stmt, ParseError> {
        match self.peek().kind {
            KW_LET => self.parse_let(),
            KW_RETURN => self.parse_return(),
            KW_WHILE => {
                self.advance();
                let cond = self.parse_expr()?;
                let body = self.parse_block()?;
                Ok(Stmt::While { cond, body })
            }
            KW_FOR => self.parse_for(),
            _ => {
                let expr = self.parse_expr()?;
                // `name = value;` is an assignment to an existing binding.
                if let Expr::Var(name) = expr {
                    if self.peek().kind == ASSIGN {
                        self.advance();
                        let value = self.parse_expr()?;
                        self.expect(SEMI)?;
                        return Ok(Stmt::Assign { name, value });
                    }
                    // Compound assignment `name += e` / `name -= e` desugars to
                    // `name = name <op> e`, mirroring rustc's i32 semantics with
                    // no new AST/IR surface. The synthetic `Var(name)` read
                    // reuses the target offset; this is sound for the i32-only
                    // subset because `+=`/`-=` never operate on references, so
                    // the read can never register a borrow loan.
                    if matches!(self.peek().kind, PLUS_EQ | MINUS_EQ) {
                        let op = if self.advance().kind == PLUS_EQ {
                            PLUS
                        } else {
                            MINUS
                        };
                        let rhs = self.parse_expr()?;
                        self.expect(SEMI)?;
                        let value = Expr::Binary {
                            op,
                            lhs: Box::new(Expr::Var(name)),
                            rhs: Box::new(rhs),
                        };
                        return Ok(Stmt::Assign { name, value });
                    }
                }
                // Block-like expression statements (`if`/`else`, `{ ... }`) are
                // valid without a trailing semicolon, matching Rust; any other
                // expression statement still requires one.
                if matches!(expr, Expr::If { .. } | Expr::Block(_)) {
                    if self.peek().kind == SEMI {
                        self.advance();
                    }
                } else {
                    self.expect(SEMI)?;
                }
                Ok(Stmt::Expr(expr))
            }
        }
    }

    fn parse_let(&mut self) -> Result<Stmt, ParseError> {
        self.expect(KW_LET)?;
        let mutable = if self.peek().kind == KW_MUT {
            self.advance();
            true
        } else {
            false
        };
        let name = self.expect(IDENT)?.start;
        self.expect(COLON)?;
        let ty = self.parse_type()?;
        self.expect(ASSIGN)?;
        let init = self.parse_expr()?;
        self.expect(SEMI)?;
        Ok(Stmt::Let {
            mutable,
            name,
            ty,
            init,
        })
    }

    fn parse_for(&mut self) -> Result<Stmt, ParseError> {
        self.expect(KW_FOR)?;
        let name = self.expect(IDENT)?.start;
        self.expect(KW_IN)?;
        let start = self.parse_expr()?;
        self.expect(DOTDOT)?;
        let end = self.parse_expr()?;
        let body = self.parse_block()?;
        Ok(Stmt::For {
            name,
            start,
            end,
            body,
        })
    }

    fn parse_return(&mut self) -> Result<Stmt, ParseError> {
        self.expect(KW_RETURN)?;
        let expr = if self.peek().kind != SEMI {
            Some(self.parse_expr()?)
        } else {
            None
        };
        self.expect(SEMI)?;
        Ok(Stmt::Return(expr))
    }

    fn parse_expr(&mut self) -> Result<Expr, ParseError> {
        // Depth-guard the single transitive recursion point for all
        // paren/call/if/block/while nesting; fail closed before the native
        // stack overflows on hostile input.
        self.depth += 1;
        let r = if self.depth > MAX_PARSE_DEPTH {
            Err(ParseError {
                message: "expression nesting too deep".into(),
                token_index: self.pos,
            })
        } else {
            self.parse_or()
        };
        self.depth -= 1;
        r
    }

    fn parse_or(&mut self) -> Result<Expr, ParseError> {
        let mut lhs = self.parse_and()?;
        while self.peek().kind == OROR {
            let op = self.advance().kind;
            lhs = Expr::Binary {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(self.parse_and()?),
            };
        }
        Ok(lhs)
    }

    fn parse_and(&mut self) -> Result<Expr, ParseError> {
        let mut lhs = self.parse_cmp()?;
        while self.peek().kind == ANDAND {
            let op = self.advance().kind;
            lhs = Expr::Binary {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(self.parse_cmp()?),
            };
        }
        Ok(lhs)
    }

    fn parse_cmp(&mut self) -> Result<Expr, ParseError> {
        let mut lhs = self.parse_term()?;
        while matches!(self.peek().kind, EQ | LT | NE | GT | LE | GE) {
            let op = self.advance().kind;
            lhs = Expr::Binary {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(self.parse_term()?),
            };
        }
        Ok(lhs)
    }

    fn parse_term(&mut self) -> Result<Expr, ParseError> {
        let mut lhs = self.parse_factor()?;
        while matches!(self.peek().kind, PLUS | MINUS) {
            let op = self.advance().kind;
            lhs = Expr::Binary {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(self.parse_factor()?),
            };
        }
        Ok(lhs)
    }

    fn parse_factor(&mut self) -> Result<Expr, ParseError> {
        let mut lhs = self.parse_unary()?;
        while matches!(self.peek().kind, STAR | SLASH | PERCENT) {
            let op = self.advance().kind;
            lhs = Expr::Binary {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(self.parse_unary()?),
            };
        }
        Ok(lhs)
    }

    fn parse_unary(&mut self) -> Result<Expr, ParseError> {
        // `* ! &` chains right-recurse here without going through parse_expr,
        // so this self-recursion needs its own depth guard (shared counter).
        self.depth += 1;
        let r = if self.depth > MAX_PARSE_DEPTH {
            Err(ParseError {
                message: "expression nesting too deep".into(),
                token_index: self.pos,
            })
        } else {
            self.parse_unary_inner()
        };
        self.depth -= 1;
        r
    }

    fn parse_unary_inner(&mut self) -> Result<Expr, ParseError> {
        match self.peek().kind {
            AMP | AMP_MUT => {
                let mutable = self.peek().kind == AMP_MUT;
                self.advance();
                Ok(Expr::Borrow {
                    mutable,
                    expr: Box::new(self.parse_unary()?),
                })
            }
            STAR => {
                self.advance();
                Ok(Expr::Deref(Box::new(self.parse_unary()?)))
            }
            BANG => {
                self.advance();
                Ok(Expr::Not(Box::new(self.parse_unary()?)))
            }
            MINUS => {
                self.advance();
                Ok(Expr::Neg(Box::new(self.parse_unary()?)))
            }
            _ => self.parse_primary(),
        }
    }

    fn parse_primary(&mut self) -> Result<Expr, ParseError> {
        match self.peek().kind {
            LPAREN => {
                self.advance();
                let inner = self.parse_expr()?;
                self.expect(RPAREN)?;
                Ok(inner)
            }
            LITERAL_INT => {
                let tok = *self.advance();
                // rustc treats a literal exceeding u128 as an unconditional hard
                // error ("integer literal is too large"), which `--cap-lints
                // allow` cannot suppress; literals within u128 are merely the
                // capped `overflowing_literals` lint (accepted, then wrapped to
                // the target type). Match that boundary exactly: parse as u128,
                // reject on overflow. Storing the low 64 bits is value-faithful
                // for the i32-only subset because `v as u64 as i32 == v as i32`.
                match tok.text(self.source).parse::<u128>() {
                    Ok(v) => Ok(Expr::LiteralInt(tok.start, v as u64)),
                    Err(_) => Err(ParseError {
                        message: "integer literal is too large".into(),
                        token_index: self.pos,
                    }),
                }
            }
            LITERAL_BOOL => {
                let tok = *self.advance();
                let b = tok.text(self.source) == "true";
                Ok(Expr::LiteralBool(tok.start, b))
            }
            IDENT => {
                let name = self.advance().start;
                if self.peek().kind == LPAREN {
                    self.advance();
                    let mut args = Vec::new();
                    if self.peek().kind != RPAREN {
                        loop {
                            args.push(self.parse_expr()?);
                            if self.peek().kind == COMMA {
                                self.advance();
                            } else {
                                break;
                            }
                        }
                    }
                    self.expect(RPAREN)?;
                    Ok(Expr::Call { name, args })
                } else {
                    Ok(Expr::Var(name))
                }
            }
            LBRACE => Ok(Expr::Block(self.parse_block()?)),
            KW_IF => {
                self.advance();
                let cond = Box::new(self.parse_expr()?);
                let then_block = Box::new(Expr::Block(self.parse_block()?));
                let else_block = if self.peek().kind == KW_ELSE {
                    self.advance();
                    if self.peek().kind == KW_IF {
                        Some(Box::new(self.parse_expr()?))
                    } else {
                        Some(Box::new(Expr::Block(self.parse_block()?)))
                    }
                } else {
                    None
                };
                Ok(Expr::If {
                    cond,
                    then_block,
                    else_block,
                })
            }
            _ => Err(ParseError {
                message: "unexpected token in expression".into(),
                token_index: self.pos,
            }),
        }
    }
}