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 return type.
41    SetReturnType {
42        type_annotation: TypeAnnotation,
43        span: Span,
44    },
45    /// Comptime-only directive to set a function return type from an expression
46    /// evaluated in comptime context.
47    SetReturnExpr { expression: Expr, span: Span },
48    /// Comptime-only directive to replace a function body.
49    ReplaceBody { body: Vec<Statement>, span: Span },
50    /// Comptime-only directive to replace a function body from an expression
51    /// evaluated in comptime context.
52    ReplaceBodyExpr { expression: Expr, span: Span },
53    /// Comptime-only directive to replace a module body from an expression
54    /// evaluated in comptime context.
55    ReplaceModuleExpr { expression: Expr, span: Span },
56}
57
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
59pub struct ForLoop {
60    /// Loop variable or initialization
61    pub init: ForInit,
62    /// Loop body
63    pub body: Vec<Statement>,
64    /// Whether this is an async for-await: `for await x in stream { ... }`
65    pub is_async: bool,
66}
67
68#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
69pub enum ForInit {
70    /// for x in expr (or destructuring: for {x, y} in expr)
71    ForIn {
72        pattern: super::patterns::DestructurePattern,
73        iter: Expr,
74    },
75    /// for (let i = 0; i < 10; i++)
76    ForC {
77        init: Box<Statement>,
78        condition: Expr,
79        update: Expr,
80    },
81}
82
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
84pub struct WhileLoop {
85    pub condition: Expr,
86    pub body: Vec<Statement>,
87}
88
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub struct IfStatement {
91    pub condition: Expr,
92    pub then_body: Vec<Statement>,
93    pub else_body: Option<Vec<Statement>>,
94}
95
96/// Block is a sequence of statements (used in AST extensions)
97#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
98pub struct Block {
99    pub statements: Vec<Statement>,
100}