Skip to main content

ling/parser/
ast.rs

1// src/parser/ast.rs
2
3#[derive(Debug, Clone)]
4pub struct Program {
5    pub items: Vec<Item>,
6}
7
8#[derive(Debug, Clone)]
9pub enum Item {
10    Bind(String, Expr),
11    Fn(FnDef),
12    Mod(String, Vec<Item>),
13    TypeAlias(String, String), // type Name = RawType
14    /// `use "path/to/module"` or `use "path" as ns`
15    Use { path: String, alias: Option<String> },
16}
17
18#[derive(Debug, Clone)]
19pub struct FnDef {
20    pub name: String,
21    pub is_async: bool,
22    pub params: Vec<String>,   // just names; types are parsed but ignored
23    pub body: Vec<Stmt>,
24}
25
26// ─── Expressions ─────────────────────────────────────────────────────────────
27
28#[derive(Debug, Clone)]
29pub enum Expr {
30    Str(String),
31    Number(f64),
32    Bool(bool),
33    Unit,
34    Ident(String),
35    /// `do { stmts }` or anonymous block `{ stmts }`
36    Do(Vec<Stmt>),
37    /// `if cond { then } (else if cond { elif })* (else { else_body })?`
38    If {
39        cond: Box<Expr>,
40        then: Vec<Stmt>,
41        elseifs: Vec<(Expr, Vec<Stmt>)>,
42        else_body: Option<Vec<Stmt>>,
43    },
44    /// `for name in iterable { body }`
45    For {
46        var: String,
47        iter: Box<Expr>,
48        body: Vec<Stmt>,
49    },
50    /// `while cond { body }` / `ขณะที่ cond { body }`
51    While {
52        cond: Box<Expr>,
53        body: Vec<Stmt>,
54    },
55    /// `match expr { arms }`
56    Match(Box<Expr>, Vec<MatchArm>),
57    /// Normal call: `expr(args)`
58    Call(Box<Expr>, Vec<Expr>),
59    /// Method call: `receiver.method(args)`
60    MethodCall {
61        receiver: Box<Expr>,
62        method: String,
63        args: Vec<Expr>,
64    },
65    /// Path like `Mod::fn` or just chained idents; resolved at runtime
66    Path(Vec<String>),
67    /// `lo..hi`
68    Range(Box<Expr>, Box<Expr>),
69    /// `&expr`
70    Ref(Box<Expr>),
71    /// `await expr`
72    Await(Box<Expr>),
73    /// Binary operation
74    BinOp(BinOp, Box<Expr>, Box<Expr>),
75    /// Array/Vec literal `[a, b, c]`
76    Array(Vec<Expr>),
77    /// `expr[idx]`
78    Index(Box<Expr>, Box<Expr>),
79    /// Closure `|| expr` or `|args| expr`
80    Closure(Vec<String>, Box<Expr>),
81}
82
83#[derive(Debug, Clone, PartialEq)]
84pub enum BinOp {
85    Add, Sub, Mul, Div, Rem,
86    Eq, Ne, Lt, Gt, Le, Ge,
87    And, Or,
88}
89
90// ─── Statements ──────────────────────────────────────────────────────────────
91
92#[derive(Debug, Clone)]
93pub enum Stmt {
94    Bind(String, Expr),
95    Expr(Expr),
96    Return(Expr),
97}
98
99// ─── Match arms ──────────────────────────────────────────────────────────────
100
101#[derive(Debug, Clone)]
102pub struct MatchArm {
103    pub pattern: Pattern,
104    pub body: Expr,
105}
106
107#[derive(Debug, Clone)]
108pub enum Pattern {
109    Wildcard,
110    Str(String),
111    Number(f64),
112    Bool(bool),
113    Ident(String),
114    /// Ok(inner), Bad(inner), 好(inner), 坏(inner)
115    Constructor(String, Option<Box<Pattern>>),
116}