devalang_core/core/preprocessor/resolver/
call.rs

1use crate::core::{
2    parser::statement::{Statement, StatementKind},
3    preprocessor::module::Module,
4    store::global::GlobalStore,
5};
6use devalang_types::Value;
7use devalang_utils::logger::{LogLevel, Logger};
8
9pub fn resolve_call(
10    stmt: &Statement,
11    name: String,
12    args: Vec<Value>,
13    module: &Module,
14    _path: &str,
15    global_store: &mut GlobalStore,
16) -> Statement {
17    let logger = Logger::new();
18
19    match &stmt.kind {
20        StatementKind::Call { .. } => {
21            // Check if it's a function
22            if let Some(func) = global_store.functions.functions.get(&name) {
23                let mut call_map = std::collections::HashMap::new();
24                call_map.insert("name".to_string(), Value::Identifier(name.clone()));
25                call_map.insert(
26                    "parameters".to_string(),
27                    Value::Array(
28                        func.parameters
29                            .iter()
30                            .map(|p| Value::Identifier(p.clone()))
31                            .collect(),
32                    ),
33                );
34                call_map.insert("args".to_string(), Value::Array(args.clone()));
35                call_map.insert("body".to_string(), Value::Block(func.body.clone()));
36
37                return Statement {
38                    kind: StatementKind::Call { name, args },
39                    value: Value::Map(call_map),
40                    ..stmt.clone()
41                };
42            }
43
44            // Otherwise, check if it's a variable (e.g. group)
45            if let Some(Value::Statement(stmt_box)) = global_store.variables.variables.get(&name) {
46                if let StatementKind::Group = stmt_box.kind {
47                    if let Value::Map(map) = &stmt_box.value {
48                        if let Some(Value::Block(body)) = map.get("body") {
49                            let mut resolved_map = std::collections::HashMap::new();
50                            resolved_map
51                                .insert("identifier".to_string(), Value::String(name.clone()));
52                            resolved_map.insert("args".to_string(), Value::Array(args.clone()));
53                            resolved_map.insert("body".to_string(), Value::Block(body.clone()));
54
55                            return Statement {
56                                kind: StatementKind::Call { name, args },
57                                value: Value::Map(resolved_map),
58                                ..stmt.clone()
59                            };
60                        }
61                    }
62                }
63            }
64
65            // Otherwise, log an error
66            logger.log_message(
67                LogLevel::Error,
68                &format!("Function or group '{}' not found", name),
69            );
70            Statement {
71                kind: StatementKind::Error {
72                    message: format!("Function or group '{}' not found", name),
73                },
74                value: Value::Null,
75                ..stmt.clone()
76            }
77        }
78        _ => {
79            let stacktrace = format!("{}:{}:{}", module.path, stmt.line, stmt.column);
80            logger.log_message(
81                LogLevel::Error,
82                &format!("Expected StatementKind::Call in resolve_call()\n  → at {stacktrace}"),
83            );
84
85            Statement {
86                kind: StatementKind::Error {
87                    message: "Expected StatementKind::Call in resolve_call()".to_string(),
88                },
89                value: Value::Null,
90                ..stmt.clone()
91            }
92        }
93    }
94}
95
96// (removed unused helpers get_group_body, error_stmt)