Skip to main content

typr_cli/
metaprogramming.rs

1//! Metaprogramming utilities for TypR CLI
2//!
3//! Handles module imports and file expansion.
4
5use crate::io::{get_os_file, read_file_from_name};
6use nom_locate::LocatedSpan;
7use typr_core::components::context::config::Environment;
8use typr_core::components::language::Lang;
9use typr_core::processes::parsing::parse;
10
11fn import_file_module_code(line: &Lang, environment: Environment) -> Lang {
12    match line {
13        Lang::ModuleImport(name, _h) => {
14            let file = get_os_file(&format!("{}.ty", name));
15            let parse_result = parse(LocatedSpan::new_extra(
16                &read_file_from_name(&name, environment),
17                file,
18            ));
19            // TODO: propagate errors from imported modules
20            metaprogrammation(parse_result.ast.to_module(name, environment), environment)
21        }
22        n => n.clone(),
23    }
24}
25
26fn import_file_modules_code(adt: Lang, environment: Environment) -> Lang {
27    match adt {
28        Lang::Module(name, lines, position, config, h) => {
29            let new_lines = lines
30                .iter()
31                .map(|x| import_file_module_code(x, environment))
32                .collect::<Vec<_>>();
33            Lang::Module(name, new_lines, position, config, h)
34        }
35        Lang::Lines(lines, h) => {
36            let new_lines = lines
37                .iter()
38                .map(|x| import_file_module_code(x, environment))
39                .collect::<Vec<_>>();
40            Lang::Lines(new_lines, h)
41        }
42        s => s,
43    }
44}
45
46pub fn metaprogrammation(adt: Lang, environment: Environment) -> Lang {
47    import_file_modules_code(adt, environment)
48}