melodeon/
lib.rs

1pub mod codegen;
2pub mod containers;
3pub mod context;
4pub mod demod;
5pub mod grammar;
6pub mod typed_ast;
7pub mod typesys;
8
9use std::path::Path;
10
11use demod::Demodularizer;
12use typesys::Type;
13
14/// Compiles a melodeon program by its literal string, resolving dependencies assuming that the string was read from a file at the given module path. Returns the Mil representation, as well as the type of the
15pub fn compile(melo_code: &str, module_path: &Path) -> context::CtxResult<(String, Type)> {
16    let mut root_path = module_path
17        .canonicalize()
18        .unwrap_or_else(|_| module_path.to_owned());
19    if root_path.is_file() {
20        root_path.pop();
21    }
22    let fname = module_path.file_name();
23
24    let mut demod = Demodularizer::new_at_fs(&root_path, &root_path.join("melo-libs"));
25    let modid = if let Some(f) = fname {
26        context::ProjectRoot(root_path.clone()).module_from_root(f.as_ref())
27    } else {
28        context::ProjectRoot(root_path.clone()).module_from_root(Path::new("."))
29    };
30    demod.module_override(modid, melo_code.to_string());
31
32    let raw = demod.demod(modid, &root_path)?;
33    let prgrm = typesys::typecheck_program(raw)?;
34    let t = prgrm.body.itype.clone();
35    Ok((codegen::codegen_program(prgrm), t))
36}