devalang_core/core/preprocessor/resolver/
call.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_call(
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    match &stmt.kind {
22        StatementKind::Call { .. } => {
23            // Check if it's a function
24            if let Some(func) = global_store.functions.functions.get(&name) {
25                let mut call_map = std::collections::HashMap::new();
26                call_map.insert("name".to_string(), Value::Identifier(name.clone()));
27                call_map.insert(
28                    "parameters".to_string(),
29                    Value::Array(
30                        func.parameters
31                            .iter()
32                            .map(|p| Value::Identifier(p.clone()))
33                            .collect(),
34                    ),
35                );
36                call_map.insert("args".to_string(), Value::Array(args.clone()));
37                call_map.insert("body".to_string(), Value::Block(func.body.clone()));
38
39                return Statement {
40                    kind: StatementKind::Call { name, args },
41                    value: Value::Map(call_map),
42                    ..stmt.clone()
43                };
44            }
45
46            // Otherwise, check if it's a variable (e.g. group)
47            if let Some(variable) = global_store.variables.variables.get(&name) {
48                if let Value::Statement(stmt_box) = variable {
49                    if let StatementKind::Group = stmt_box.kind {
50                        if let Value::Map(map) = &stmt_box.value {
51                            if let Some(Value::Block(body)) = map.get("body") {
52                                let mut resolved_map = std::collections::HashMap::new();
53                                resolved_map
54                                    .insert("identifier".to_string(), Value::String(name.clone()));
55                                resolved_map.insert("args".to_string(), Value::Array(args.clone()));
56                                resolved_map.insert("body".to_string(), Value::Block(body.clone()));
57
58                                return Statement {
59                                    kind: StatementKind::Call { name, args },
60                                    value: Value::Map(resolved_map),
61                                    ..stmt.clone()
62                                };
63                            }
64                        }
65                    }
66                }
67            }
68
69            // Otherwise, log an error
70            logger.log_message(
71                LogLevel::Error,
72                &format!("Function or group '{}' not found", name),
73            );
74            Statement {
75                kind: StatementKind::Error {
76                    message: format!("Function or group '{}' not found", name),
77                },
78                value: Value::Null,
79                ..stmt.clone()
80            }
81        }
82        _ => {
83            let stacktrace = format!("{}:{}:{}", module.path, stmt.line, stmt.column);
84            logger.log_message(
85                LogLevel::Error,
86                &format!("Expected StatementKind::Call in resolve_call()\n  → at {stacktrace}"),
87            );
88
89            Statement {
90                kind: StatementKind::Error {
91                    message: "Expected StatementKind::Call in resolve_call()".to_string(),
92                },
93                value: Value::Null,
94                ..stmt.clone()
95            }
96        }
97    }
98}
99
100// (removed unused helpers get_group_body, error_stmt)