devalang_wasm/language/preprocessor/loader/
mod.rs1use 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
8pub fn load_module_from_path(_path: &Path) -> Result<()> {
10 Ok(())
12}
13
14pub fn inject_dependencies() -> Result<()> {
16 Ok(())
18}
19
20pub 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 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}