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.
91    /// When VarKind::Var, mutability is inferred from usage.
92    #[serde(default)]
93    pub is_mut: bool,
94    pub pattern: DestructurePattern,
95    pub type_annotation: Option<TypeAnnotation>,
96    pub value: Option<Expr>,
97    /// Explicit ownership modifier on the initializer: `let x = move y` or `let x = clone y`
98    #[serde(default)]
99    pub ownership: OwnershipModifier,
100}
101
102/// Explicit ownership transfer modifier on variable initialization.
103#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
104pub enum OwnershipModifier {
105    /// No explicit modifier — inferred from context.
106    /// For `var`: smart inference (move if dead, clone if live).
107    /// For `let`: always move.
108    #[default]
109    Inferred,
110    /// `move` — explicitly force a move, invalidating the source.
111    Move,
112    /// `clone` — explicitly clone the source value.
113    Clone,
114}
115
116#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
117pub enum VarKind {
118    Let,
119    Var,
120    Const,
121}
122
123#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
124pub struct Assignment {
125    pub pattern: DestructurePattern,
126    pub value: Expr,
127}
128
129/// Declaration-only intrinsic type in std/core metadata.
130#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
131pub struct BuiltinTypeDecl {
132    pub name: String,
133    pub name_span: Span,
134    #[serde(default)]
135    pub doc_comment: Option<DocComment>,
136    pub type_params: Option<Vec<TypeParam>>,
137}
138
139/// Declaration-only intrinsic function in std/core metadata.
140#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
141pub struct BuiltinFunctionDecl {
142    pub name: String,
143    pub name_span: Span,
144    #[serde(default)]
145    pub doc_comment: Option<DocComment>,
146    pub type_params: Option<Vec<TypeParam>>,
147    pub params: Vec<FunctionParameter>,
148    pub return_type: TypeAnnotation,
149}
150
151/// Optimization directive for parameter tuning
152#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
153pub struct OptimizeStatement {
154    /// Parameter name to optimize
155    pub parameter: String,
156    /// Range for the parameter [min..max]
157    pub range: (Box<Expr>, Box<Expr>),
158    /// Metric to optimize for
159    pub metric: OptimizationMetric,
160}
161
162/// Metrics that can be optimized
163#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
164pub enum OptimizationMetric {
165    Sharpe,
166    Sortino,
167    Return,
168    Drawdown,
169    WinRate,
170    ProfitFactor,
171    Custom(Box<Expr>),
172}