Skip to main content

shape_ast/ast/
statements.rs

1//! Statement types for Shape AST
2
3use serde::{Deserialize, Serialize};
4
5use super::expressions::Expr;
6use super::program::{Assignment, VariableDecl};
7use super::span::Span;
8use super::types::{ExtendStatement, TypeAnnotation};
9
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11pub enum Statement {
12    /// Return statement
13    Return(Option<Expr>, Span),
14    /// Break statement
15    Break(Span),
16    /// Continue statement
17    Continue(Span),
18    /// Variable declaration
19    VariableDecl(VariableDecl, Span),
20    /// Assignment
21    Assignment(Assignment, Span),
22    /// Expression statement
23    Expression(Expr, Span),
24    /// For loop
25    For(ForLoop, Span),
26    /// While loop
27    While(WhileLoop, Span),
28    /// If statement
29    If(IfStatement, Span),
30    /// Comptime-only type extension directive inside comptime handlers/blocks.
31    Extend(ExtendStatement, Span),
32    /// Comptime-only directive to remove the current annotation target.
33    RemoveTarget(Span),
34    /// Comptime-only directive to set a function parameter type.
35    SetParamType {
36        param_name: String,
37        type_annotation: TypeAnnotation,
38        span: Span,
39    },
40    /// Comptime-only directive to set a function parameter default value.
41    SetParamValue {
42        param_name: String,
43        expression: Expr,
44        span: Span,
45    },
46    /// Comptime-only directive to set a function return type.
47    SetReturnType {
48        type_annotation: TypeAnnotation,
49        span: Span,
50    },
51    /// Comptime-only directive to set a function return type from an expression
52    /// evaluated in comptime context.
53    SetReturnExpr { expression: Expr, span: Span },
54    /// Comptime-only directive to replace a function body.
55    ReplaceBody { body: Vec<Statement>, span: Span },
56    /// Comptime-only directive to replace a function body from an expression
57    /// evaluated in comptime context.
58    ReplaceBodyExpr { expression: Expr, span: Span },
59    /// Comptime-only directive to replace a module body from an expression
60    /// evaluated in comptime context.
61    ReplaceModuleExpr { expression: Expr, span: Span },
62}
63
64#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
65pub struct ForLoop {
66    /// Loop variable or initialization
67    pub init: ForInit,
68    /// Loop body
69    pub body: Vec<Statement>,
70    /// Whether this is an async for-await: `for await x in stream { ... }`
71    pub is_async: bool,
72}
73
74#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
75pub enum ForInit {
76    /// for x in expr (or destructuring: for {x, y} in expr)
77    ForIn {
78        pattern: super::patterns::DestructurePattern,
79        iter: Expr,
80    },
81    /// for (let i = 0; i < 10; i++)
82    ForC {
83        init: Box<Statement>,
84        condition: Expr,
85        update: Expr,
86    },
87}
88
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub struct WhileLoop {
91    pub condition: Expr,
92    pub body: Vec<Statement>,
93}
94
95#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
96pub struct IfStatement {
97    pub condition: Expr,
98    pub then_body: Vec<Statement>,
99    pub else_body: Option<Vec<Statement>>,
100}
101
102/// Block is a sequence of statements (used in AST extensions)
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104pub struct Block {
105    pub statements: Vec<Statement>,
106}