1use crate::span::Span;
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct Program {
7 pub span: Span,
8 pub module: Option<ModuleDecl>,
9 pub imports: Vec<ImportDecl>,
10 pub prefixes: Vec<PrefixDecl>,
11 pub locales: Vec<LocaleDecl>,
13 pub requires: Vec<CapSpec>,
14 pub items: Vec<Item>,
15}
16
17#[derive(Debug, Clone, PartialEq)]
18pub struct LocaleDecl {
19 pub span: Span,
20 pub code: String,
21}
22
23#[derive(Debug, Clone, PartialEq)]
24pub struct ModuleDecl {
25 pub span: Span,
26 pub name: Name,
27}
28
29#[derive(Debug, Clone, PartialEq)]
30pub struct ImportDecl {
31 pub span: Span,
32 pub path: String,
33 pub alias: Option<String>,
34}
35
36#[derive(Debug, Clone, PartialEq)]
37pub struct PrefixDecl {
38 pub span: Span,
39 pub prefix: String,
40 pub iri: String,
41}
42
43#[derive(Debug, Clone, PartialEq)]
44pub struct CapSpec {
45 pub span: Span,
46 pub id: String,
47 pub args: Vec<NamedArg>,
48}
49
50#[derive(Debug, Clone, PartialEq)]
51pub enum Item {
52 Function(FunctionDecl),
53 Hook(HookDecl),
54 Const(ConstDecl),
55 Enum(EnumDecl),
56 Field(FieldDecl),
57 Material(MaterialDecl),
58 Law(LawDecl),
59 Cell(CellDecl),
60 Present(PresentDecl),
61 Bind(BindDecl),
62 Statement(Stmt),
63}
64
65#[derive(Debug, Clone, PartialEq)]
67pub struct BindDecl {
68 pub span: Span,
69 pub left: Expr,
70 pub right: Expr,
71 pub clamp: Option<(Expr, Expr)>,
72 pub resolve: BindResolve,
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub enum BindResolve {
77 Latest,
78 Left,
79 Right,
80}
81
82#[derive(Debug, Clone, PartialEq)]
84pub struct PresentDecl {
85 pub span: Span,
86 pub name: String,
87 pub properties: Vec<NamedArg>,
88}
89
90#[derive(Debug, Clone, PartialEq)]
92pub struct CellDecl {
93 pub span: Span,
94 pub effect: Option<EffectClass>,
95 pub name: String,
96 pub params: Vec<Param>,
97 pub expr: Expr,
98 pub when: Option<Expr>,
99}
100
101#[derive(Debug, Clone, PartialEq)]
102pub struct FunctionDecl {
103 pub span: Span,
104 pub effect: Option<EffectClass>,
105 pub is_async: bool,
106 pub name: String,
107 pub params: Vec<Param>,
108 pub budget: Vec<NamedArg>,
109 pub ret: Option<TypeExpr>,
110 pub body: Block,
111}
112
113#[derive(Debug, Clone, PartialEq)]
114pub struct HookDecl {
115 pub span: Span,
116 pub path: Vec<String>,
117 pub params: Vec<Param>,
118 pub budget: Vec<NamedArg>,
119 pub ret: Option<TypeExpr>,
120 pub body: Block,
121}
122
123#[derive(Debug, Clone, PartialEq)]
124pub struct ConstDecl {
125 pub span: Span,
126 pub name: String,
127 pub ty: Option<TypeExpr>,
128 pub value: Expr,
129}
130
131#[derive(Debug, Clone, PartialEq)]
142pub struct EnumDecl {
143 pub span: Span,
144 pub name: String,
145 pub variants: Vec<EnumVariant>,
146}
147
148#[derive(Debug, Clone, PartialEq)]
150pub struct EnumVariant {
151 pub span: Span,
152 pub name: String,
153 pub payload: Vec<TypeExpr>,
155}
156
157#[derive(Debug, Clone, PartialEq)]
166pub struct FieldDecl {
167 pub span: Span,
168 pub name: String,
169 pub ty: TypeExpr,
170 pub unit: Option<String>,
173 pub support: FieldSupport,
177 pub representation: FieldRepresentation,
180}
181
182#[derive(Debug, Clone, Copy, PartialEq, Eq)]
184pub enum FieldSupport {
185 Region,
187 Point,
189 Continuant,
191 Stream,
193}
194
195impl Default for FieldSupport {
196 fn default() -> Self {
197 FieldSupport::Region
198 }
199}
200
201#[derive(Debug, Clone, Copy, PartialEq, Eq)]
203pub enum FieldRepresentation {
204 Grid,
206 Mesh,
208 Particles,
210 Analytic,
212 Sampled,
214}
215
216impl Default for FieldRepresentation {
217 fn default() -> Self {
218 FieldRepresentation::Grid
219 }
220}
221
222#[derive(Debug, Clone, PartialEq)]
230pub struct MaterialDecl {
231 pub span: Span,
232 pub name: String,
233 pub properties: Vec<NamedArg>,
237}
238
239#[derive(Debug, Clone, PartialEq)]
247pub struct LawDecl {
248 pub span: Span,
249 pub name: String,
250 pub condition: Expr,
252 pub consequence: Expr,
254}
255
256#[derive(Debug, Clone, Copy, PartialEq, Eq)]
257pub enum EffectClass {
258 Pure,
259 Hot,
260 Cold,
261 Async,
262 External,
263}
264
265#[derive(Debug, Clone, PartialEq)]
266pub struct Param {
267 pub span: Span,
268 pub name: String,
269 pub ty: TypeExpr,
270}
271
272#[derive(Debug, Clone, PartialEq)]
273pub struct NamedArg {
274 pub span: Span,
275 pub name: String,
276 pub value: Expr,
277}
278
279#[derive(Debug, Clone, PartialEq)]
280pub struct Block {
281 pub span: Span,
282 pub stmts: Vec<Stmt>,
283}
284
285#[derive(Debug, Clone, PartialEq)]
286pub enum Stmt {
287 Let {
288 span: Span,
289 mutable: bool,
290 name: String,
291 ty: Option<TypeExpr>,
292 value: Option<Expr>,
293 },
294 LetPat {
295 span: Span,
296 mutable: bool,
297 pattern: Pattern,
298 ty: Option<TypeExpr>,
299 value: Expr,
300 },
301 Assign {
302 span: Span,
303 target: Expr,
304 value: Expr,
305 },
306 If {
307 span: Span,
308 cond: Expr,
309 then_block: Block,
310 else_block: Option<Box<Stmt>>,
311 },
312 For {
313 span: Span,
314 name: String,
315 iter: Expr,
316 body: Block,
317 },
318 While {
319 span: Span,
320 cond: Expr,
321 body: Block,
322 },
323 Match {
324 span: Span,
325 scrutinee: Expr,
326 arms: Vec<MatchArm>,
327 },
328 Return {
329 span: Span,
330 value: Option<Expr>,
331 },
332 Yield {
333 span: Span,
334 value: Option<Expr>,
335 },
336 Transaction {
337 span: Span,
338 args: Vec<NamedArg>,
339 body: Block,
340 },
341 Effect {
342 span: Span,
343 expr: Expr,
344 },
345 Expr {
346 span: Span,
347 expr: Expr,
348 },
349 Block(Block),
350}
351
352#[derive(Debug, Clone, PartialEq)]
353pub struct MatchArm {
354 pub span: Span,
355 pub pattern: Pattern,
356 pub body: ArmBody,
357}
358
359#[derive(Debug, Clone, PartialEq)]
360pub enum ArmBody {
361 Block(Block),
362 Expr(Expr),
363}
364
365#[derive(Debug, Clone, PartialEq)]
366pub enum Pattern {
367 Wildcard,
368 Ident(String),
369 Literal(Literal),
370 Ok(Box<Pattern>),
371 Err(Box<Pattern>),
372 Some(Box<Pattern>),
373 None,
374 Record(Vec<(String, Pattern)>),
375 List(Vec<Pattern>),
376 Constructor {
377 name: String,
378 args: Vec<Pattern>,
379 },
380 Variant {
384 enum_name: String,
385 variant_name: String,
386 args: Vec<Pattern>,
387 },
388}
389
390#[derive(Debug, Clone, PartialEq)]
391pub struct Expr {
392 pub span: Span,
393 pub kind: ExprKind,
394}
395
396#[derive(Debug, Clone, PartialEq)]
397pub enum ExprKind {
398 Literal(Literal),
399 Ident(String),
400 QueryVar(String),
401 Iri(String),
402 Prefixed(String, String),
403 Blank(String),
404 Binary {
405 op: BinOp,
406 left: Box<Expr>,
407 right: Box<Expr>,
408 },
409 Unary {
410 op: UnOp,
411 expr: Box<Expr>,
412 },
413 Await(Box<Expr>),
414 Member {
415 recv: Box<Expr>,
416 name: String,
417 },
418 Call {
419 callee: Box<Expr>,
420 args: Vec<Arg>,
421 },
422 Index {
423 recv: Box<Expr>,
424 index: Box<Expr>,
425 },
426 Try(Box<Expr>),
427 List(Vec<Expr>),
428 Record(Vec<NamedArg>),
429 Triple {
430 subject: Box<Expr>,
431 predicate: Box<Expr>,
432 object: Box<Expr>,
433 },
434 Reified {
435 subject: Box<Expr>,
436 predicate: Box<Expr>,
437 object: Box<Expr>,
438 reifier: Box<Expr>,
439 },
440 Pipe {
441 left: Box<Expr>,
442 right: Box<Expr>,
443 },
444 GraphQuery {
445 is_ask: bool,
446 pattern: String,
447 variables: Vec<String>,
448 },
449 ModalLogic {
450 modality: ModalKind,
451 args: Vec<Expr>,
452 body: Option<Box<Expr>>,
453 },
454 Interpolate(Vec<Expr>),
455 Lambda {
457 params: Vec<String>,
458 body: Box<Expr>,
459 },
460 Tween {
462 from: Box<Expr>,
463 to: Box<Expr>,
464 over: Box<Expr>,
465 ease: Option<String>,
466 spring: Option<Vec<NamedArg>>,
467 },
468 IfElse {
475 cond: Box<Expr>,
476 then_value: Box<Expr>,
477 else_value: Box<Expr>,
478 },
479}
480
481#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
483pub enum ModalKind {
484 DeonticObligate,
485 DeonticPermit,
486 DeonticForbid,
487 EpistemicKnows,
488 EpistemicBelieves,
489 Paraconsistent,
490 LtlGlobally,
491 LtlFinally,
492 LtlUntil,
493 DlSubsumes,
494 N3Defeasible,
495}
496
497#[derive(Debug, Clone, PartialEq)]
498pub enum Arg {
499 Pos(Expr),
500 Named(NamedArg),
501}
502
503#[derive(Debug, Clone, Copy, PartialEq, Eq)]
504pub enum BinOp {
505 Or,
506 And,
507 Eq,
508 Ne,
509 Lt,
510 Le,
511 Gt,
512 Ge,
513 Add,
514 Sub,
515 Mul,
516 Div,
517 Rem,
518}
519
520#[derive(Debug, Clone, Copy, PartialEq, Eq)]
521pub enum UnOp {
522 Not,
523 Neg,
524 Plus,
525}
526
527#[derive(Debug, Clone, PartialEq)]
528pub enum Literal {
529 Null,
530 Bool(bool),
531 Int(i64),
532 UInt(u64),
533 Float(u64), Quantity { value: u64, unit: String }, String(String),
536 Color(ColorLit),
537}
538
539#[derive(Debug, Clone, Copy, PartialEq, Eq)]
541pub struct ColorLit {
542 pub r: u8,
543 pub g: u8,
544 pub b: u8,
545 pub a: u8,
546}
547
548#[derive(Debug, Clone, PartialEq)]
549pub struct TypeExpr {
550 pub span: Span,
551 pub name: String,
552 pub args: Vec<TypeExpr>,
553}
554
555#[derive(Debug, Clone, PartialEq)]
556pub enum Name {
557 Ident(String),
558 Iri(String),
559}
560
561impl Expr {
562 pub fn ident_name(&self) -> Option<&str> {
564 match &self.kind {
565 ExprKind::Ident(s) => Some(s),
566 _ => None,
567 }
568 }
569}
570
571impl Stmt {
572 pub fn span(&self) -> Span {
574 match self {
575 Stmt::Let { span, .. }
576 | Stmt::LetPat { span, .. }
577 | Stmt::Assign { span, .. }
578 | Stmt::If { span, .. }
579 | Stmt::For { span, .. }
580 | Stmt::While { span, .. }
581 | Stmt::Match { span, .. }
582 | Stmt::Return { span, .. }
583 | Stmt::Yield { span, .. }
584 | Stmt::Transaction { span, .. }
585 | Stmt::Effect { span, .. }
586 | Stmt::Expr { span, .. } => *span,
587 Stmt::Block(b) => b.span,
588 }
589 }
590}