shape_value/closure.rs
1//! Closure type definition (implementations in shape-runtime)
2//!
3//! This module contains only the DATA STRUCTURE for Closure.
4//! All method implementations live in shape-runtime to avoid circular dependencies.
5
6use crate::value_word::ValueWord;
7use shape_ast::ast::VarKind;
8use std::collections::HashMap;
9
10/// A closure captures a function definition along with its environment
11#[derive(Debug, Clone)]
12pub struct Closure {
13 /// The function ID (bytecode index)
14 pub function_id: u16,
15
16 /// Captured environment (variable bindings from enclosing scope)
17 pub captured_env: CapturedEnvironment,
18}
19
20impl PartialEq for Closure {
21 fn eq(&self, other: &Self) -> bool {
22 // Closures are equal if they have the same function ID
23 // Captured environment comparison is complex and not needed for now
24 self.function_id == other.function_id
25 }
26}
27
28/// Captured environment for a closure
29#[derive(Debug, Clone)]
30pub struct CapturedEnvironment {
31 /// Captured variable bindings
32 pub bindings: HashMap<String, CapturedBinding>,
33 /// Parent environment (for nested closures)
34 pub parent: Option<Box<CapturedEnvironment>>,
35}
36
37/// A captured variable binding
38#[derive(Debug, Clone)]
39pub struct CapturedBinding {
40 /// The captured value
41 pub value: ValueWord,
42 /// The kind of variable (let, var, const)
43 pub kind: VarKind,
44 /// Whether this binding is mutable (for 'var' declarations)
45 pub is_mutable: bool,
46}