devalang_wasm/language/preprocessor/loader/
mod.rs

1use crate::language::syntax::ast::{Statement, StatementKind, Value};
2use crate::language::syntax::parser::driver::SimpleParser;
3/// Module loader - handles file loading and module dependencies
4use anyhow::Result;
5use std::collections::HashMap;
6use std::path::Path;
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 {
39                exports.push(n.clone());
40            }
41        }
42    }
43
44    for s in &stmts {
45        match &s.kind {
46            StatementKind::Let { name, value }
47            | StatementKind::Var { name, value }
48            | StatementKind::Const { name, value } => {
49                if exports.contains(name) {
50                    if let Some(v) = value {
51                        variables.insert(name.clone(), v.clone());
52                    }
53                }
54            }
55            StatementKind::Group { name, body } => {
56                if exports.contains(name) {
57                    groups.insert(name.clone(), body.clone());
58                }
59            }
60            StatementKind::Pattern { name, .. } => {
61                if exports.contains(name) {
62                    patterns.insert(name.clone(), s.clone());
63                }
64            }
65            _ => {}
66        }
67    }
68
69    Ok(ModuleExports {
70        variables,
71        groups,
72        patterns,
73    })
74}