devalang_core/core/preprocessor/
processor.rs

1use std::{ collections::HashMap, path::Path };
2
3use crate::core::{
4    parser::statement::StatementKind,
5    preprocessor::loader::ModuleLoader,
6    shared::value::Value,
7    store::global::GlobalStore,
8    utils::path::{ normalize_path, resolve_relative_path },
9};
10
11pub fn process_modules(_module_loader: &ModuleLoader, global_store: &mut GlobalStore) {
12    for module in global_store.modules.values_mut() {
13        for stmt in &module.statements {
14            match &stmt.kind {
15                StatementKind::Let { name } => {
16                    if let Value::Null = stmt.value {
17                        eprintln!("❌ Variable '{}' is declared but not initialized.", name);
18                        
19                        module.variable_table.variables.insert(
20                            name.clone(),
21                            Value::StatementKind(Box::new(stmt.kind.clone()))
22                        );
23
24                        continue;
25                    }
26
27                    if module.variable_table.get(name).is_some() {
28                        eprintln!("❌ Variable '{}' is already defined in this scope.", name);
29                        continue;
30                    }
31
32                    if let Some(module_variable) = module.variable_table.variables.get(name) {
33                        eprintln!(
34                            "❌ Variable '{}' is already defined globally with value: {:?}",
35                            name,
36                            module_variable
37                        );
38                        continue;
39                    }
40
41                    module.variable_table.variables.insert(name.clone(), stmt.value.clone());
42                }
43
44                StatementKind::Load { source, alias } => {
45                    let module_dir = Path::new(&module.path).parent().unwrap_or(Path::new(""));
46
47                    let resolved_path = normalize_path(&module_dir.join(source));
48
49                    module.variable_table.variables.insert(
50                        alias.clone(),
51                        Value::Sample(resolved_path)
52                    );
53                }
54
55                StatementKind::Export { names, source: _ } => {
56                    for name in names {
57                        if let Some(val) = module.variable_table.get(name) {
58                            module.export_table.add_export(name.clone(), val.clone());
59                        }
60                    }
61                }
62
63                StatementKind::Import { names, source } => {
64                    let resolved = resolve_relative_path(&module.path, source);
65                    for name in names {
66                        module.import_table.add_import(
67                            name.clone(),
68                            Value::String(resolved.clone())
69                        );
70                    }
71                }
72
73                StatementKind::Group => {
74                    if let Value::Map(map) = &stmt.value {
75                        if
76                            let (Some(Value::String(name)), Some(Value::Block(body))) = (
77                                map.get("identifier"),
78                                map.get("body"),
79                            )
80                        {
81                            let mut stored_map = HashMap::new();
82
83                            stored_map.insert(
84                                "identifier".to_string(),
85                                Value::String(name.clone())
86                            );
87
88                            stored_map.insert("body".to_string(), Value::Block(body.clone()));
89
90                            module.variable_table.set(name.to_string(), Value::Map(stored_map));
91                        } else {
92                            eprintln!("❌ Invalid group definition: {:?}", stmt.value);
93                        }
94                    }
95                }
96
97                _ => {}
98            }
99        }
100    }
101}