devalang_core/core/preprocessor/resolver/
call.rs1use crate::{
2 core::{
3 parser::statement::{ Statement, StatementKind },
4 preprocessor::module::Module,
5 shared::value::Value,
6 store::global::GlobalStore,
7 },
8 utils::logger::{ Logger, LogLevel },
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 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 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.insert(
54 "identifier".to_string(),
55 Value::String(name.clone())
56 );
57 resolved_map.insert("args".to_string(), Value::Array(args.clone()));
58 resolved_map.insert("body".to_string(), Value::Block(body.clone()));
59
60 return Statement {
61 kind: StatementKind::Call { name, args },
62 value: Value::Map(resolved_map),
63 ..stmt.clone()
64 };
65 }
66 }
67 }
68 }
69 }
70
71 logger.log_message(LogLevel::Error, &format!("Function or group '{}' not found", name));
73 Statement {
74 kind: StatementKind::Error {
75 message: format!("Function or group '{}' not found", name),
76 },
77 value: Value::Null,
78 ..stmt.clone()
79 }
80 }
81 _ => {
82 let stacktrace = format!("{}:{}:{}", module.path, stmt.line, stmt.column);
83 logger.log_message(
84 LogLevel::Error,
85 &format!(
86 "Expected StatementKind::Call in resolve_call()\n → at {stacktrace}"
87 )
88 );
89
90 Statement {
91 kind: StatementKind::Error {
92 message: "Expected StatementKind::Call in resolve_call()".to_string(),
93 },
94 value: Value::Null,
95 ..stmt.clone()
96 }
97 },
98 }
99}
100
101