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
// Minimal AST to satisfy eval.rs (and leave room for a parser later)
use serde::{Deserialize, Serialize};
/// Visibility modifier for declarations
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Default)]
pub enum Visibility {
/// Public - can be imported by other modules
Pub,
/// Private (default) - only accessible within the module
#[default]
Private,
}
/// Condition for conditional compilation
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CfgCondition {
/// Platform check: cfg(windows), cfg(linux), cfg(macos)
Platform(String),
/// Feature check: cfg(feature = "name")
Feature(String),
/// Negation: cfg(not(condition))
Not(Box<CfgCondition>),
/// All conditions: cfg(all(a, b))
All(Vec<CfgCondition>),
/// Any condition: cfg(any(a, b))
Any(Vec<CfgCondition>),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Stmt {
Let {
name: String,
value: Expr,
/// `let mut` support
is_mut: bool,
/// Visibility: `pub let x = ...` makes x importable
#[serde(default)]
visibility: Visibility,
/// The annotation in `let x: Int = ...`, checked when the value is bound.
/// `None` when the binding has no annotation.
#[serde(default)]
ty: Option<String>,
/// `name = value` written without `let`. A refusal to rebind a name then
/// reads as an assignment, not a `let`.
#[serde(default)]
bare: bool,
},
Expr(Expr),
/// Field assignment: `r.a.b = value`. `r` is rebound to a copy of its
/// Record with the field set (created when missing). Records are values, so
/// other bindings of the old Record do not see the change.
SetField {
name: String,
path: Vec<String>,
value: Expr,
},
/// Import statement: `import "path"` or `import { a, b } from "path"`
Import {
/// Items to import (empty means import all as module)
items: Vec<ImportItem>,
/// Module path or package name
source: String,
/// Optional alias for the module: `import "path" as name`
alias: Option<String>,
},
/// Export statement: `export { a, b }` or `export { a as x }`
/// Re-exports: `export { a, b } from "path"`
Export {
/// Items to export
items: Vec<ExportItem>,
/// Optional source for re-exports
from_source: Option<String>,
},
/// Conditional compilation: #[cfg(condition)] statement
Cfg {
/// The condition to check
condition: CfgCondition,
/// The statement to conditionally include
body: Box<Stmt>,
},
}
/// An item in an import statement
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ImportItem {
/// Original name in the module
pub name: String,
/// Optional alias: `import { foo as bar }`
pub alias: Option<String>,
}
/// An item in an export statement
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ExportItem {
/// Name to export (local name or re-exported name)
pub name: String,
/// Optional alias: `export { foo as bar }`
pub alias: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Expr {
// literals
LitInt(i64),
LitFloat(f64),
LitStr(String),
LitBool(bool),
Null,
// variables
Ident(String),
// collections
Array(Vec<Expr>),
Record(Vec<(String, Expr)>),
// lambda
Lambda {
params: Vec<String>,
body: Box<Expr>,
},
/// Async lambda: async fn(x) => expr
AsyncLambda {
params: Vec<String>,
body: Box<Expr>,
},
/// Await expression: await expr
Await(Box<Expr>),
/// Try expression: try { expr } catch { handler }
TryCatch {
try_expr: Box<Expr>,
catch_var: Option<String>, // Optional error binding: catch e { ... }
catch_expr: Box<Expr>,
},
/// Throw expression: throw "error message" or throw expr
Throw(Box<Expr>),
// call
Call {
callee: Box<Expr>,
args: Vec<Expr>, // positional
named: Vec<(String, Expr)>, // named: key = expr
},
// pipeline: left | right (where right is typically a Call)
Pipe {
left: Box<Expr>,
right: Box<Expr>,
},
// operators
Binary {
left: Box<Expr>,
op: BinOp,
right: Box<Expr>,
},
Unary {
op: UnOp,
expr: Box<Expr>,
},
// member access: record.field
MemberAccess {
object: Box<Expr>,
field: String,
},
/// Postfix indexing: `expr[index]`. An Array takes an Int (negative counts
/// from the end), a String takes an Int and yields one character, and a
/// Record takes a String key. Written with no space before the `[`, so
/// `a[0]` indexes while `f [1, 2]` stays a word call with an array argument.
Index {
object: Box<Expr>,
index: Box<Expr>,
},
// pattern matching
Match {
scrutinee: Box<Expr>,
arms: Vec<MatchArm>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct MatchArm {
pub pattern: Pattern,
pub guard: Option<Box<Expr>>, // optional `if condition`
pub body: Box<Expr>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Pattern {
// Wildcard: _
Wildcard,
// Variable binding: x
Ident(String),
// Literal patterns (a numeric literal matches by the same equality as `==`)
LitInt(i64),
LitFloat(f64),
LitStr(String),
LitBool(bool),
Null,
// Constructor pattern: Some(x), None
Constructor { name: String, args: Vec<Pattern> },
// Array pattern: [a, b, c]
Array(Vec<Pattern>),
// Record pattern: {x, y} or {x: px, y: py}
Record(Vec<(String, Pattern)>),
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
pub enum UnOp {
Neg,
Not,
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Rem,
Pow,
Eq,
Ne,
Lt,
Lte,
Gt,
Gte,
And,
Or,
}