1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! Main execution methods for Shape engine
use super::types::{ExecutionMetrics, ExecutionResult, ExecutionType};
use crate::type_schema::with_async_scope;
use shape_ast::error::Result;
use shape_ast::parser;
impl super::ShapeEngine {
/// Execute a Shape program from source code (sync mode)
///
/// This method allows blocking data loads (REPL mode).
/// For scripts/backtests, use execute_async() instead.
pub fn execute(
&mut self,
executor: &mut impl super::ProgramExecutor,
source: &str,
) -> Result<ExecutionResult> {
// Set REPL/sync mode - allow blocking loads
if let Some(ctx) = self.runtime.persistent_context_mut() {
ctx.set_data_load_mode(crate::context::DataLoadMode::Sync);
}
self.execute_with_options(executor, source, false)
}
/// Execute a Shape program with async prefetching (Phase 6/8)
///
/// This method:
/// 1. Sets Async data mode (runtime data requests use cache, not blocking)
/// 2. Parses and analyzes the program
/// 3. Determines required data (symbols/timeframes)
/// 4. Prefetches data concurrently
/// 5. Executes synchronously using cached data
///
/// # Example
///
/// ```ignore
/// let provider = DataFrameAdapter::new(...);
/// let mut engine = ShapeEngine::with_async_provider(provider)?;
/// let result = engine.execute_async(&interpreter, "let sma = close.sma(20)").await?;
/// ```
pub async fn execute_async(
&mut self,
executor: &mut impl super::ProgramExecutor,
source: &str,
) -> Result<ExecutionResult> {
// Set async mode - runtime data requests must use cache
if let Some(ctx) = self.runtime.persistent_context_mut() {
ctx.set_data_load_mode(crate::context::DataLoadMode::Async);
}
// Install this runtime's TypeSchemaRegistry as the task-local
// ambient for the duration of this execution.
let schema_registry = self.runtime.schema_registry_arc();
with_async_scope(schema_registry, self.execute_async_inner(executor, source)).await
}
async fn execute_async_inner(
&mut self,
executor: &mut impl super::ProgramExecutor,
source: &str,
) -> Result<ExecutionResult> {
let start_time = std::time::Instant::now();
// Parse the source
let parse_start = std::time::Instant::now();
let mut program = parser::parse_program(source)?;
let parse_time_ms = parse_start.elapsed().as_millis() as u64;
// Desugar high-level syntax (e.g., from-queries to method chains) before analysis
shape_ast::transform::desugar_program(&mut program);
let analysis_start = std::time::Instant::now();
let analysis_time_ms = analysis_start.elapsed().as_millis() as u64;
// Prefetch data if using async provider
let has_cache = self
.runtime
.persistent_context()
.map(|ctx| ctx.has_data_cache())
.unwrap_or(false);
if has_cache {
// Extract symbols/timeframes from program
let queries = self.extract_data_queries(&program)?;
// Prefetch all required data concurrently
if let Some(ctx) = self.runtime.persistent_context_mut() {
ctx.prefetch_data(queries).await?;
}
}
// Store source text for error messages during execution
self.set_source(source);
// Execute synchronously using cached data
let runtime_start = std::time::Instant::now();
let result = executor.execute_program(self, &program)?;
let runtime_time_ms = runtime_start.elapsed().as_millis() as u64;
let total_time_ms = start_time.elapsed().as_millis() as u64;
let memory_used_bytes = self.estimate_memory_usage();
let rows_processed = Some(self.default_data.row_count());
let messages = self.collect_messages();
Ok(ExecutionResult {
value: result.wire_value,
type_info: result.type_info,
execution_type: result.execution_type,
metrics: ExecutionMetrics {
execution_time_ms: total_time_ms,
parse_time_ms,
analysis_time_ms,
runtime_time_ms,
memory_used_bytes,
rows_processed,
},
messages,
content_json: result.content_json,
content_html: result.content_html,
content_terminal: result.content_terminal,
})
}
/// Execute a REPL command with persistent state
///
/// Unlike `execute_async`, this uses incremental analysis where variables
/// and functions persist across commands. Call `init_repl()` once before
/// the first call to this method.
pub async fn execute_repl(
&mut self,
executor: &mut impl super::ProgramExecutor,
source: &str,
) -> Result<ExecutionResult> {
// Set async mode - runtime data requests must use cache
if let Some(ctx) = self.runtime.persistent_context_mut() {
ctx.set_data_load_mode(crate::context::DataLoadMode::Async);
}
// Install this runtime's TypeSchemaRegistry as the task-local
// ambient for the duration of this REPL command.
let schema_registry = self.runtime.schema_registry_arc();
with_async_scope(schema_registry, self.execute_repl_inner(executor, source)).await
}
async fn execute_repl_inner(
&mut self,
executor: &mut impl super::ProgramExecutor,
source: &str,
) -> Result<ExecutionResult> {
let start_time = std::time::Instant::now();
// Parse the source
let parse_start = std::time::Instant::now();
let mut program = parser::parse_program(source)?;
let parse_time_ms = parse_start.elapsed().as_millis() as u64;
// Desugar high-level syntax (e.g., from-queries to method chains) before analysis
shape_ast::transform::desugar_program(&mut program);
let analysis_start = std::time::Instant::now();
let analysis_time_ms = analysis_start.elapsed().as_millis() as u64;
// Prefetch data if using async provider
let has_cache = self
.runtime
.persistent_context()
.map(|ctx| ctx.has_data_cache())
.unwrap_or(false);
if has_cache {
let queries = self.extract_data_queries(&program)?;
if let Some(ctx) = self.runtime.persistent_context_mut() {
ctx.prefetch_data(queries).await?;
}
}
// Process imports and declarations before execution
self.runtime.load_program(&program, &self.default_data)?;
// Store source text for error messages during execution
self.set_source(source);
// Execute
let runtime_start = std::time::Instant::now();
let result = executor.execute_program(self, &program)?;
let runtime_time_ms = runtime_start.elapsed().as_millis() as u64;
let total_time_ms = start_time.elapsed().as_millis() as u64;
let memory_used_bytes = self.estimate_memory_usage();
let rows_processed = Some(self.default_data.row_count());
let messages = self.collect_messages();
Ok(ExecutionResult {
value: result.wire_value,
type_info: result.type_info,
execution_type: ExecutionType::Repl,
metrics: ExecutionMetrics {
execution_time_ms: total_time_ms,
parse_time_ms,
analysis_time_ms,
runtime_time_ms,
memory_used_bytes,
rows_processed,
},
messages,
content_json: result.content_json,
content_html: result.content_html,
content_terminal: result.content_terminal,
})
}
/// Parse and analyze source code without executing it.
///
/// Returns the analyzed AST `Program`, ready for compilation.
/// Used by the recompile-and-resume flow.
pub fn parse_and_analyze(&mut self, source: &str) -> Result<shape_ast::Program> {
// Parse/desugar may touch ambient schema state (e.g. comptime
// builtins). Install this runtime's registry for the call.
let _scope = self.runtime.enter_schema_scope();
if let Some(ctx) = self.runtime.persistent_context_mut() {
ctx.reset_for_new_execution();
}
let mut program = parser::parse_program(source)?;
shape_ast::transform::desugar_program(&mut program);
self.set_source(source);
Ok(program)
}
/// Execute a Shape program with options
pub(super) fn execute_with_options(
&mut self,
executor: &mut impl super::ProgramExecutor,
source: &str,
_is_stdlib: bool,
) -> Result<ExecutionResult> {
// Install this runtime's TypeSchemaRegistry as the thread-local
// ambient for the duration of synchronous execution.
let _scope = self.runtime.enter_schema_scope();
let start_time = std::time::Instant::now();
// Always reset variable scopes before each execution
if let Some(ctx) = self.runtime.persistent_context_mut() {
ctx.reset_for_new_execution();
}
// Check for deprecated APIs before parsing (the deprecated syntax may not parse cleanly)
Self::check_deprecated_apis(source)?;
// Parse the source
let parse_start = std::time::Instant::now();
let mut program = parser::parse_program(source)?;
let parse_time_ms = parse_start.elapsed().as_millis() as u64;
// Desugar high-level syntax (e.g., from-queries to method chains) before analysis
shape_ast::transform::desugar_program(&mut program);
let analysis_start = std::time::Instant::now();
let analysis_time_ms = analysis_start.elapsed().as_millis() as u64;
// Store source text for error messages during execution
self.set_source(source);
// Execute the program
let runtime_start = std::time::Instant::now();
let result = executor.execute_program(self, &program)?;
let runtime_time_ms = runtime_start.elapsed().as_millis() as u64;
let total_time_ms = start_time.elapsed().as_millis() as u64;
// Get memory usage estimate (heap allocation approximation)
let memory_used_bytes = self.estimate_memory_usage();
// Get rows processed count from market data
let rows_processed = Some(self.default_data.row_count());
// Collect any messages from the runtime
let messages = self.collect_messages();
Ok(ExecutionResult {
value: result.wire_value,
type_info: result.type_info,
execution_type: result.execution_type,
metrics: ExecutionMetrics {
execution_time_ms: total_time_ms,
parse_time_ms,
analysis_time_ms,
runtime_time_ms,
memory_used_bytes,
rows_processed,
},
messages,
content_json: result.content_json,
content_html: result.content_html,
content_terminal: result.content_terminal,
})
}
/// Execute a REPL command
pub fn execute_repl_command(
&mut self,
executor: &mut impl super::ProgramExecutor,
command: &str,
) -> Result<ExecutionResult> {
let mut result = self.execute(executor, command)?;
result.execution_type = ExecutionType::Repl;
Ok(result)
}
/// Check for deprecated APIs before parsing. Some deprecated call syntax may
/// not parse cleanly (e.g. escaped quotes in raw strings), so we detect them
/// via pattern matching on the source text and produce helpful diagnostics.
fn check_deprecated_apis(source: &str) -> Result<()> {
let trimmed = source.trim();
if trimmed.starts_with("csv.load") || trimmed.contains("csv.load(") {
return Err(shape_ast::error::ShapeError::SemanticError {
message: "csv.load has been removed. Use the csv package instead: import { read } from \"csv\""
.to_string(),
location: None,
});
}
// Check for bare load(provider, params) — the global load function was removed
if trimmed.starts_with("load(") || trimmed.starts_with("load (") {
return Err(shape_ast::error::ShapeError::SemanticError {
message: "load(provider, params) has been removed. Use typed data access instead: data(\"source\", { ... })"
.to_string(),
location: None,
});
}
Ok(())
}
/// Estimate memory usage based on runtime state
pub(super) fn estimate_memory_usage(&self) -> Option<usize> {
// Estimate based on known allocations
let mut total = 0usize;
// Market data rows (each row ~48 bytes for 6 f64 values)
total += self.default_data.row_count() * 48;
// Variable storage estimate (rough approximation)
// This is a simplified estimate - real tracking would require custom allocator
total += 1024; // Base overhead for runtime structures
Some(total)
}
/// Collect messages from runtime execution
pub(super) fn collect_messages(&self) -> Vec<super::types::Message> {
// Currently the runtime doesn't track messages, but this provides the interface
// for future implementation
vec![]
}
}