brooks_lib/mel/compiler/
mod.rs1use tree_sitter;
19
20use crate::common::TextLocation;
21use crate::mel::ast::Expr;
22use crate::mel::compiler::compile::MELCompiler;
23use crate::mel::compiler::compile::MELCompilerContext;
24use crate::mel::compiler::compile::SyntaxVisitorDriver;
25use crate::mel::compiler::error::CompileResult;
26use crate::mel::compiler::error::CompilerError;
27use crate::mel::compiler::error::CompilerLocatableError;
28
29pub mod compile;
30pub mod error;
31
32#[cfg(test)]
33mod test;
34
35pub fn compile(source: &str) -> CompileResult<Expr<()>> {
36 let mut parser = tree_sitter::Parser::new();
37 let language = tree_sitter_mel::LANGUAGE;
38 parser
39 .set_language(&language.into())
40 .map_err(|e| CompilerLocatableError {
41 error: CompilerError::SyntaxError(format!("Could not parse: {e}")),
42 location: TextLocation {
43 start: 0,
44 extent: 0,
45 },
46 })?;
47 let result = parser.parse(source, None).ok_or(CompilerLocatableError {
48 error: CompilerError::SyntaxError("Could not parse.".to_string()),
49 location: TextLocation {
50 start: 0,
51 extent: 0,
52 },
53 })?;
54
55 let vd = SyntaxVisitorDriver {};
56 let sd = MELCompiler::new(source);
57 let cc = MELCompilerContext::default();
58
59 let result = vd.visit(result.root_node(), &sd, cc)?;
60
61 match result {
62 MELCompilerContext::Expr(expr) => Ok(expr),
63 _ => Err(CompilerLocatableError {
64 error: CompilerError::EmptyContext,
65 location: TextLocation {
66 start: 0,
67 extent: 0,
68 },
69 }),
70 }
71}