devalang_core/core/preprocessor/resolver/
spawn.rs

1use crate::{
2    core::{
3        parser::statement::{Statement, StatementKind},
4        preprocessor::module::Module,
5        shared::value::Value,
6        store::global::GlobalStore,
7    },
8    utils::logger::{LogLevel, Logger},
9};
10
11pub fn resolve_spawn(
12    stmt: &Statement,
13    name: String,
14    args: Vec<Value>,
15    module: &Module,
16    _path: &str,
17    global_store: &mut GlobalStore,
18) -> Statement {
19    let logger = Logger::new();
20
21    // If it's a function
22    if let Some(func) = global_store.functions.functions.get(&name) {
23        let mut resolved_map = std::collections::HashMap::new();
24        resolved_map.insert("name".to_string(), Value::String(name.clone()));
25        resolved_map.insert("args".to_string(), Value::Array(args.clone()));
26        resolved_map.insert("body".to_string(), Value::Block(func.body.clone()));
27
28        return Statement {
29            kind: StatementKind::Spawn { name, args },
30            value: Value::Map(resolved_map),
31            ..stmt.clone()
32        };
33    }
34
35    // If it's a group stored in variables
36    if let Some(variable) = global_store.variables.variables.get(&name) {
37        if let Value::Statement(stmt_box) = variable {
38            if let StatementKind::Group = stmt_box.kind {
39                if let Value::Map(map) = &stmt_box.value {
40                    if let Some(Value::Block(body)) = map.get("body") {
41                        let mut resolved_map = std::collections::HashMap::new();
42                        resolved_map.insert("identifier".to_string(), Value::String(name.clone()));
43                        resolved_map.insert("args".to_string(), Value::Array(args.clone()));
44                        resolved_map.insert("body".to_string(), Value::Block(body.clone()));
45
46                        return Statement {
47                            kind: StatementKind::Spawn { name, args },
48                            value: Value::Map(resolved_map),
49                            ..stmt.clone()
50                        };
51                    }
52                }
53            }
54        }
55    }
56
57    // Otherwise, log an error
58    let stacktrace = format!("{}:{}:{}", module.path, stmt.line, stmt.column);
59    logger.log_message(
60        LogLevel::Error,
61        &format!(
62            "Function or group '{}' not found for spawn\n  → at {stacktrace}",
63            name
64        ),
65    );
66
67    Statement {
68        kind: StatementKind::Error {
69            message: format!("Function or group '{}' not found for spawn", name),
70        },
71        value: Value::Null,
72        ..stmt.clone()
73    }
74}
75
76// (removed unused helpers get_group_body, error_stmt)