-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.rs
More file actions
422 lines (377 loc) · 13.4 KB
/
Copy pathast.rs
File metadata and controls
422 lines (377 loc) · 13.4 KB
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
use crate::span::Span;
use serde::{Deserialize, Serialize};
// ─── Types ────────────────────────────────────────────────────────────────────
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TypeExpr {
Named(String),
Generic(String, Vec<TypeExpr>),
Array(Box<TypeExpr>),
Slice(Box<TypeExpr>, Option<usize>),
Tuple(Vec<TypeExpr>),
Fn(Vec<TypeExpr>, Box<TypeExpr>),
Optional(Box<TypeExpr>),
Ref(Box<TypeExpr>),
RefMut(Box<TypeExpr>),
Void,
I8, I16, I32, I64, I128,
U8, U16, U32, U64, U128,
F32, F64,
Bool, String, Bytes,
}
// ─── Import paths ─────────────────────────────────────────────────────────────
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ImportKind {
/// use "std -> module -> sub" from "alias"
Std { path: Vec<String>, alias: Option<String> },
/// use "core -> runtime" from "alias"
Core { path: Vec<String>, alias: Option<String> },
/// use "vira -> pkgname" or "vira -> pkgname/1.2" from "alias"
Vira { name: String, version: Option<String>, alias: Option<String> },
/// use "github -> libname" from "alias"
Github { name: String, alias: Option<String> },
/// use "python -> numpy" from "np"
Python { name: String, version: Option<String>, alias: Option<String> },
/// use "bytes -> pkgname" from "alias"
BytesRepo { name: String, version: Option<String>, alias: Option<String> },
/// use "mod -> name" — deprecated, use `mod name` syntax
#[allow(deprecated)]
ModFile { path: String, alias: Option<String> },
}
// ─── Literals ─────────────────────────────────────────────────────────────────
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum InterpPart {
Text(String),
Expr(Box<Expr>),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
Int(i64),
Float(f64),
Bool(bool),
String(String),
Interpolated(Vec<InterpPart>),
Nil,
Bytes(Vec<u8>),
}
// ─── Patterns ─────────────────────────────────────────────────────────────────
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Pattern {
Wildcard(Span),
Ident(String, Span),
Literal(Literal, Span),
Tuple(Vec<Pattern>, Span),
Struct { name: String, fields: Vec<(String, Pattern)>, span: Span },
Enum { qualified_type: Option<String>, variant: String, inner: Vec<Pattern>, span: Span },
Or(Vec<Pattern>, Span),
Range(Box<Pattern>, Box<Pattern>, bool, Span),
}
// ─── Expressions ──────────────────────────────────────────────────────────────
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Expr {
Literal(Literal, Span),
Ident(String, Span),
BinOp(Box<Expr>, BinOp, Box<Expr>, Span),
UnOp(UnOp, Box<Expr>, Span),
Assign(Box<Expr>, Box<Expr>, Span),
CompoundAssign(Box<Expr>, BinOp, Box<Expr>, Span),
FieldAccess(Box<Expr>, String, Span),
IndexAccess(Box<Expr>, Box<Expr>, Span),
MethodCall(Box<Expr>, String, Vec<Expr>, Span),
Call(Box<Expr>, Vec<Expr>, Span),
If {
condition: Box<Expr>,
then_body: Vec<Stmt>,
elsif_branches: Vec<(Expr, Vec<Stmt>)>,
else_body: Option<Vec<Stmt>>,
span: Span,
},
Match {
subject: Box<Expr>,
arms: Vec<MatchArm>,
span: Span,
},
While {
condition: Box<Expr>,
body: Vec<Stmt>,
span: Span,
},
For {
pattern: Pattern,
iterable: Box<Expr>,
body: Vec<Stmt>,
span: Span,
},
Do {
body: Vec<Stmt>,
span: Span,
},
StructLit(String, Vec<(String, Expr)>, Span),
ArrayLit(Vec<Expr>, Span),
TupleLit(Vec<Expr>, Span),
Closure {
params: Vec<Param>,
return_type: Option<TypeExpr>,
body: Vec<Stmt>,
span: Span,
},
Cast(Box<Expr>, TypeExpr, Span),
Range(Box<Expr>, Box<Expr>, bool, Span),
Unsafe(Vec<Stmt>, Option<ArenaConfig>, Span),
Return(Option<Box<Expr>>, Span),
SelfExpr(Span),
Try(Box<Expr>, Span),
Await(Box<Expr>, Span),
/// Module path access: `module::function` or `module::CONST`.
/// Segments are the dotted/coloned path components (e.g. ["json", "parse"]).
Path(Vec<String>, Span),
}
impl Expr {
pub fn span(&self) -> &Span {
match self {
Expr::Literal(_, s) => s,
Expr::Ident(_, s) => s,
Expr::BinOp(_, _, _, s) => s,
Expr::UnOp(_, _, s) => s,
Expr::Assign(_, _, s) => s,
Expr::CompoundAssign(_, _, _, s) => s,
Expr::FieldAccess(_, _, s) => s,
Expr::IndexAccess(_, _, s) => s,
Expr::MethodCall(_, _, _, s) => s,
Expr::Call(_, _, s) => s,
Expr::If { span, .. } => span,
Expr::Match { span, .. } => span,
Expr::While { span, .. } => span,
Expr::For { span, .. } => span,
Expr::Do { span, .. } => span,
Expr::StructLit(_, _, s) => s,
Expr::ArrayLit(_, s) => s,
Expr::TupleLit(_, s) => s,
Expr::Closure { span, .. } => span,
Expr::Cast(_, _, s) => s,
Expr::Range(_, _, _, s) => s,
Expr::Unsafe(_, _, s) => s,
Expr::Return(_, s) => s,
Expr::SelfExpr(s) => s,
Expr::Try(_, s) => s,
Expr::Await(_, s) => s,
Expr::Path(_, s) => s,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BinOp {
Add, Sub, Mul, Div, Mod,
Eq, NotEq, Lt, Gt, LtEq, GtEq,
And, Or,
BitAnd, BitOr, BitXor, Shl, Shr,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum UnOp {
Neg, Not, BitNot, Ref, RefMut, Deref,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MatchArm {
pub pattern: Pattern,
pub guard: Option<Expr>,
pub body: Vec<Stmt>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ArenaKind { General, Fixed, Pool, Page, Ring }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ManualKind { Modern, Classic }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum UnsafeMode {
Arena { kind: ArenaKind, size: Option<usize> },
Manual(ManualKind),
Raw,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ArenaConfig {
pub mode: UnsafeMode,
}
// ─── Statements ───────────────────────────────────────────────────────────────
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Stmt {
Let {
name: String,
ty: Option<TypeExpr>,
mutable: bool,
value: Option<Expr>,
span: Span,
},
Expr(Expr, Span),
Return(Option<Expr>, Span),
Import(ImportKind, Option<String>, Span),
Break(Option<Expr>, Span),
Continue(Span),
Item(Item),
}
// ─── Items ────────────────────────────────────────────────────────────────────
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Item {
FnDef(FnDef),
StructDef(StructDef),
EnumDef(EnumDef),
TraitDef(TraitDef),
ImplBlock(ImplBlock),
TypeAlias { name: String, ty: TypeExpr, pub_: bool, span: Span },
Extern(ExternBlock),
ModDecl {
name: String,
pub_: bool,
inline: Option<Vec<Item>>,
span: Span,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TypeParam {
pub name: String,
pub bounds: Vec<String>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Attribute {
pub name: String,
pub args: Vec<AttrArg>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AttrArg {
Ident(String),
KeyValue(String, String),
Lit(String),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FnDef {
pub attrs: Vec<Attribute>,
pub type_params: Vec<TypeParam>,
pub name: String,
pub params: Vec<Param>,
pub return_type: Option<TypeExpr>,
pub body: Vec<Stmt>,
pub pub_: bool,
pub is_unsafe: bool,
pub is_async: bool,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Param {
pub name: String,
pub ty: TypeExpr,
pub mutable: bool,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StructDef {
pub attrs: Vec<Attribute>,
pub type_params: Vec<TypeParam>,
pub name: String,
pub fields: Vec<StructField>,
pub pub_: bool,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StructField {
pub name: String,
pub ty: TypeExpr,
pub pub_: bool,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EnumDef {
pub attrs: Vec<Attribute>,
pub type_params: Vec<TypeParam>,
pub name: String,
pub variants: Vec<EnumVariant>,
pub pub_: bool,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EnumVariant {
pub name: String,
pub fields: EnumVariantFields,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum EnumVariantFields {
Unit,
Tuple(Vec<TypeExpr>),
Struct(Vec<StructField>),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TraitDef {
pub attrs: Vec<Attribute>,
pub type_params: Vec<TypeParam>,
pub name: String,
pub methods: Vec<TraitMethod>,
pub pub_: bool,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TraitMethod {
pub name: String,
pub params: Vec<Param>,
pub return_type: Option<TypeExpr>,
pub default_body: Option<Vec<Stmt>>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImplBlock {
pub type_name: String,
pub trait_name: Option<String>,
pub methods: Vec<FnDef>,
pub span: Span,
}
// ─── Module ───────────────────────────────────────────────────────────────────
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Directive {
pub kind: DirectiveKind,
pub value: String,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DirectiveKind {
Dynamic,
Fast,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Module {
pub file: String,
pub edition: Option<String>,
pub directives: Vec<Directive>,
pub items: Vec<Item>,
pub imports: Vec<(ImportKind, Option<String>, Span)>,
}
// ─── Extern ───────────────────────────────────────────────────────────────────
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExternBlock {
pub lang: ExternLang,
pub link_kind: ExternLinkKind,
pub library: Option<String>,
pub functions: Vec<ExternFnDecl>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ExternLang {
C,
Rust,
Cpp,
/// extern [python, "numpy"] is ... end
/// Functions declared here are called via an embedded/subprocess
/// CPython bridge (see hsh_py_eval / hsh_py_call in the runtime).
/// Replaces the older `use "python -> numpy" from "np"` form, which
/// only installed the pip package but had no defined call ABI.
Python,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ExternLinkKind { Static, Dynamic }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExternFnDecl {
pub name: String,
pub params: Vec<Param>,
pub return_type: Option<TypeExpr>,
pub variadic: bool,
pub span: Span,
}