Skip to main content

shape_ast/ast/
program.rs

1//! Program structure types for Shape AST
2
3use serde::{Deserialize, Serialize};
4
5use super::data_sources::{DataSourceDecl, QueryDecl};
6use super::docs::DocComment;
7use super::docs::ProgramDocs;
8use super::expressions::Expr;
9use super::functions::{AnnotationDef, ForeignFunctionDef, FunctionDef, FunctionParameter};
10use super::modules::{ExportStmt, ImportStmt, ModuleDecl};
11use super::patterns::DestructurePattern;
12use super::queries::Query;
13use super::span::Span;
14use super::statements::Statement;
15use super::streams::StreamDef;
16use super::tests::TestDef;
17use super::types::{
18    EnumDef, ExtendStatement, ImplBlock, InterfaceDef, StructTypeDef, TraitDef, TypeAliasDef,
19    TypeAnnotation, TypeParam,
20};
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct Program {
24    pub items: Vec<Item>,
25    #[serde(default)]
26    pub docs: ProgramDocs,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub enum Item {
31    /// Import statement
32    Import(ImportStmt, Span),
33    /// Export statement
34    Export(ExportStmt, Span),
35    /// Module definition
36    Module(ModuleDecl, Span),
37    /// Type alias definition
38    TypeAlias(TypeAliasDef, Span),
39    /// Interface definition
40    Interface(InterfaceDef, Span),
41    /// Trait definition (like interface but with `trait` keyword)
42    Trait(TraitDef, Span),
43    /// Enum definition
44    Enum(EnumDef, Span),
45    /// Type extension
46    Extend(ExtendStatement, Span),
47    /// Impl block (impl Trait for Type { ... })
48    Impl(ImplBlock, Span),
49    /// Function definition
50    Function(FunctionDef, Span),
51    /// Query
52    Query(Query, Span),
53    /// Variable declaration (let, var, const)
54    VariableDecl(VariableDecl, Span),
55    /// Variable assignment
56    Assignment(Assignment, Span),
57    /// Expression evaluation
58    Expression(Expr, Span),
59    /// Stream definition
60    Stream(StreamDef, Span),
61    /// Test definition
62    Test(TestDef, Span),
63    /// Optimize statement (Phase 3)
64    Optimize(OptimizeStatement, Span),
65    /// Annotation definition (annotation warmup(...) { ... })
66    AnnotationDef(AnnotationDef, Span),
67    /// Struct type definition (type Point { x: number, y: number })
68    StructType(StructTypeDef, Span),
69    /// Data source declaration (datasource Name: DataSource<T> = provider(...))
70    DataSource(DataSourceDecl, Span),
71    /// Query declaration (query Name: Query<T, Params> = sql(source, "..."))
72    QueryDecl(QueryDecl, Span),
73    /// Statement (treated as top-level code)
74    Statement(Statement, Span),
75    /// Compile-time block at top level: `comptime { stmts }`
76    /// Executed during compilation; side effects only (result discarded).
77    Comptime(Vec<Statement>, Span),
78    /// Builtin type declaration (declaration-only intrinsic)
79    BuiltinTypeDecl(BuiltinTypeDecl, Span),
80    /// Builtin function declaration (declaration-only intrinsic)
81    BuiltinFunctionDecl(BuiltinFunctionDecl, Span),
82    /// Foreign function definition: `fn python analyze(data: DataTable) -> number { ... }`
83    ForeignFunction(ForeignFunctionDef, Span),
84}
85
86#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
87pub struct VariableDecl {
88    pub kind: VarKind,
89    /// Explicit mutability: `let mut x = ...`
90    /// When false with VarKind::Let, the binding is immutable (OwnedImmutable).
91    /// When true with VarKind::Let, the binding is mutable (OwnedMutable).
92    /// VarKind::Var always has flexible ownership: always mutable,
93    /// function-scoped, with smart clone/move inference on initialization.
94    #[serde(default)]
95    pub is_mut: bool,
96    pub pattern: DestructurePattern,
97    pub type_annotation: Option<TypeAnnotation>,
98    pub value: Option<Expr>,
99    /// Explicit ownership modifier on the initializer: `let x = move y` or `let x = clone y`
100    #[serde(default)]
101    pub ownership: OwnershipModifier,
102}
103
104/// Explicit ownership transfer modifier on variable initialization.
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
106pub enum OwnershipModifier {
107    /// No explicit modifier — inferred from context.
108    /// For `var`: smart inference (move if dead, clone if live).
109    /// For `let`: always move.
110    #[default]
111    Inferred,
112    /// `move` — explicitly force a move, invalidating the source.
113    Move,
114    /// `clone` — explicitly clone the source value.
115    Clone,
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
119pub enum VarKind {
120    Let,
121    Var,
122    Const,
123}
124
125#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
126pub struct Assignment {
127    pub pattern: DestructurePattern,
128    pub value: Expr,
129}
130
131/// Declaration-only intrinsic type in std/core metadata.
132#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
133pub struct BuiltinTypeDecl {
134    pub name: String,
135    pub name_span: Span,
136    #[serde(default)]
137    pub doc_comment: Option<DocComment>,
138    pub type_params: Option<Vec<TypeParam>>,
139}
140
141/// Declaration-only intrinsic function in std/core metadata.
142#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
143pub struct BuiltinFunctionDecl {
144    pub name: String,
145    pub name_span: Span,
146    #[serde(default)]
147    pub doc_comment: Option<DocComment>,
148    pub type_params: Option<Vec<TypeParam>>,
149    pub params: Vec<FunctionParameter>,
150    pub return_type: TypeAnnotation,
151}
152
153/// Optimization directive for parameter tuning
154#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
155pub struct OptimizeStatement {
156    /// Parameter name to optimize
157    pub parameter: String,
158    /// Range for the parameter [min..max]
159    pub range: (Box<Expr>, Box<Expr>),
160    /// Metric to optimize for
161    pub metric: OptimizationMetric,
162}
163
164/// Metrics that can be optimized
165#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
166pub enum OptimizationMetric {
167    Sharpe,
168    Sortino,
169    Return,
170    Drawdown,
171    WinRate,
172    ProfitFactor,
173    Custom(Box<Expr>),
174}