devalang_wasm/language/preprocessor/loader/
mod.rs

1/// Module loader - handles file loading and module dependencies
2use anyhow::Result;
3use std::path::Path;
4use crate::language::syntax::parser::driver::SimpleParser;
5use crate::language::syntax::ast::{StatementKind, Value, Statement};
6use std::collections::HashMap;
7
8/// Load a module from a file path
9pub fn load_module_from_path(_path: &Path) -> Result<()> {
10    // TODO: legacy placeholder
11    Ok(())
12}
13
14/// Inject dependencies into a module
15pub fn inject_dependencies() -> Result<()> {
16    // TODO: Implement dependency injection
17    Ok(())
18}
19
20/// Read a module file and return a map of exported variable names -> Value
21pub struct ModuleExports {
22    pub variables: HashMap<String, Value>,
23    pub groups: HashMap<String, Vec<Statement>>,
24    pub patterns: HashMap<String, Statement>,
25}
26
27pub fn load_module_exports(path: &Path) -> Result<ModuleExports> {
28    let mut variables: HashMap<String, Value> = HashMap::new();
29    let mut groups: HashMap<String, Vec<Statement>> = HashMap::new();
30    let mut patterns: HashMap<String, Statement> = HashMap::new();
31
32    let stmts = SimpleParser::parse_file(path)?;
33
34    // collect exported names
35    let mut exports: Vec<String> = Vec::new();
36    for s in &stmts {
37        if let StatementKind::Export { names, .. } = &s.kind {
38            for n in names { exports.push(n.clone()); }
39        }
40    }
41
42    for s in &stmts {
43        match &s.kind {
44            StatementKind::Let { name, value } | StatementKind::Var { name, value } | StatementKind::Const { name, value } => {
45                if exports.contains(name) {
46                    if let Some(v) = value {
47                        variables.insert(name.clone(), v.clone());
48                    }
49                }
50            }
51            StatementKind::Group { name, body } => {
52                if exports.contains(name) {
53                    groups.insert(name.clone(), body.clone());
54                }
55            }
56            StatementKind::Pattern { name, .. } => {
57                if exports.contains(name) {
58                    patterns.insert(name.clone(), s.clone());
59                }
60            }
61            _ => {}
62        }
63    }
64
65    Ok(ModuleExports { variables, groups, patterns })
66}