devalang_core/core/preprocessor/resolver/
function.rs1use crate::core::{
2 parser::statement::{Statement, StatementKind},
3 preprocessor::{module::Module, resolver::driver::resolve_statement},
4 store::{function::FunctionDef, global::GlobalStore},
5};
6use devalang_types::Value;
7
8pub fn resolve_function(
9 stmt: &Statement,
10 module: &Module,
11 path: &str,
12 global_store: &mut GlobalStore,
13) -> Statement {
14 if let StatementKind::Function {
15 name,
16 parameters,
17 body,
18 } = &stmt.kind
19 {
20 let resolved_body = resolve_block_statements(body, module, path, global_store);
21
22 global_store.functions.add_function(FunctionDef {
23 name: name.clone(),
24 parameters: parameters.clone(),
25 body: resolved_body.clone(),
26 });
27
28 if let Some(current_mod) = global_store.modules.get_mut(path) {
29 current_mod.function_table.add_function(FunctionDef {
30 name: name.clone(),
31 parameters: parameters.clone(),
32 body: resolved_body.clone(),
33 });
34 } else {
35 eprintln!("[resolve_statement] ❌ Module path not found: {path}");
36 }
37
38 Statement {
39 kind: StatementKind::Function {
40 name: name.clone(),
41 parameters: parameters.clone(),
42 body: resolved_body,
43 },
44 value: Value::Null,
45 ..stmt.clone()
46 }
47 } else {
48 Statement {
49 kind: StatementKind::Error {
50 message: "Expected a function statement".to_string(),
51 },
52 value: Value::Null,
53 ..stmt.clone()
54 }
55 }
56}
57
58fn resolve_block_statements(
59 body: &[Statement],
60 module: &Module,
61 path: &str,
62 global_store: &mut GlobalStore,
63) -> Vec<Statement> {
64 body.iter()
65 .map(|stmt| resolve_statement(stmt, module, path, global_store))
66 .collect()
67}
68
69