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
//! Statement types for Shape AST
use serde::{Deserialize, Serialize};
use super::expressions::Expr;
use super::program::{Assignment, VariableDecl};
use super::span::Span;
use super::types::{ExtendStatement, TypeAnnotation};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Statement {
/// Return statement
Return(Option<Expr>, Span),
/// Break statement
Break(Span),
/// Continue statement
Continue(Span),
/// Variable declaration
VariableDecl(VariableDecl, Span),
/// Assignment
Assignment(Assignment, Span),
/// Expression statement
Expression(Expr, Span),
/// For loop
For(ForLoop, Span),
/// While loop
While(WhileLoop, Span),
/// If statement
If(IfStatement, Span),
/// Comptime-only type extension directive inside comptime handlers/blocks.
Extend(ExtendStatement, Span),
/// Comptime-only directive to remove the current annotation target.
RemoveTarget(Span),
/// Comptime-only directive to set a function parameter type.
SetParamType {
param_name: String,
type_annotation: TypeAnnotation,
span: Span,
},
/// Comptime-only directive to set a function parameter default value.
SetParamValue {
param_name: String,
expression: Expr,
span: Span,
},
/// Comptime-only directive to set a function return type.
SetReturnType {
type_annotation: TypeAnnotation,
span: Span,
},
/// Comptime-only directive to set a function return type from an expression
/// evaluated in comptime context.
SetReturnExpr { expression: Expr, span: Span },
/// Comptime-only directive to replace a function body.
ReplaceBody { body: Vec<Statement>, span: Span },
/// Comptime-only directive to replace a function body from an expression
/// evaluated in comptime context.
ReplaceBodyExpr { expression: Expr, span: Span },
/// Comptime-only directive to replace a module body from an expression
/// evaluated in comptime context.
ReplaceModuleExpr { expression: Expr, span: Span },
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ForLoop {
/// Loop variable or initialization
pub init: ForInit,
/// Loop body
pub body: Vec<Statement>,
/// Whether this is an async for-await: `for await x in stream { ... }`
pub is_async: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ForInit {
/// for x in expr (or destructuring: for {x, y} in expr)
ForIn {
pattern: super::patterns::DestructurePattern,
iter: Expr,
},
/// for (let i = 0; i < 10; i++)
ForC {
init: Box<Statement>,
condition: Expr,
update: Expr,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WhileLoop {
pub condition: Expr,
pub body: Vec<Statement>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IfStatement {
pub condition: Expr,
pub then_body: Vec<Statement>,
pub else_body: Option<Vec<Statement>>,
}
/// Block is a sequence of statements (used in AST extensions)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Block {
pub statements: Vec<Statement>,
}