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