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                // Pattern case
64                if let StatementKind::Pattern { .. } = stmt_box.kind {
65                    // pattern value may be a string or a map stored on the statement
66                    let mut resolved_map = std::collections::HashMap::new();
67                    resolved_map.insert("identifier".to_string(), Value::String(name.clone()));
68                    // pattern value
69                    match &stmt_box.value {
70                        Value::String(s) => {
71                            resolved_map.insert("pattern".to_string(), Value::String(s.clone()));
72                        }
73                        Value::Map(m) => {
74                            if let Some(val) = m.get("pattern") {
75                                resolved_map.insert("pattern".to_string(), val.clone());
76                            }
77                            if let Some(val) = m.get("target") {
78                                resolved_map.insert("target".to_string(), val.clone());
79                            }
80                        }
81                        _ => {}
82                    }
83                    resolved_map.insert("args".to_string(), Value::Array(args.clone()));
84
85                    return Statement {
86                        kind: StatementKind::Call { name, args },
87                        value: Value::Map(resolved_map),
88                        ..stmt.clone()
89                    };
90                }
91            }
92
93            // Otherwise, log an error
94            logger.log_message(
95                LogLevel::Error,
96                &format!("Function or group '{}' not found", name),
97            );
98            Statement {
99                kind: StatementKind::Error {
100                    message: format!("Function or group '{}' not found", name),
101                },
102                value: Value::Null,
103                ..stmt.clone()
104            }
105        }
106        _ => {
107            let stacktrace = format!("{}:{}:{}", module.path, stmt.line, stmt.column);
108            logger.log_message(
109                LogLevel::Error,
110                &format!("Expected StatementKind::Call in resolve_call()\n  → at {stacktrace}"),
111            );
112
113            Statement {
114                kind: StatementKind::Error {
115                    message: "Expected StatementKind::Call in resolve_call()".to_string(),
116                },
117                value: Value::Null,
118                ..stmt.clone()
119            }
120        }
121    }
122}
123
124// (removed unused helpers get_group_body, error_stmt)