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