use qala_compiler::chunk::Program;
use qala_compiler::codegen;
use qala_compiler::errors::QalaError;
use qala_compiler::lexer::Lexer;
use qala_compiler::parser::Parser;
use qala_compiler::typechecker::{self, QalaWarning};
use qala_compiler::typed_ast::TypedAst;
pub struct Compiled {
pub program: Program,
pub warnings: Vec<QalaWarning>,
}
pub fn compile_source(src: &str) -> Result<Compiled, Vec<QalaError>> {
let tokens = Lexer::tokenize(src).map_err(|e| vec![e])?;
let ast = Parser::parse(&tokens).map_err(|e| vec![e])?;
let (typed, errors, warnings) = typechecker::check_program(&ast, src);
if !errors.is_empty() {
return Err(errors);
}
let mut program = codegen::compile_program(&typed, src)?;
program.optimize();
Ok(Compiled { program, warnings })
}
pub fn check_source(src: &str) -> (Vec<QalaError>, Vec<QalaWarning>) {
let tokens = match Lexer::tokenize(src) {
Ok(t) => t,
Err(e) => return (vec![e], Vec::new()),
};
let ast = match Parser::parse(&tokens) {
Ok(a) => a,
Err(e) => return (vec![e], Vec::new()),
};
let (_typed, errors, warnings) = typechecker::check_program(&ast, src);
(errors, warnings)
}
pub fn typecheck_source(src: &str) -> Result<TypedAst, Vec<QalaError>> {
let tokens = Lexer::tokenize(src).map_err(|e| vec![e])?;
let ast = Parser::parse(&tokens).map_err(|e| vec![e])?;
let (typed, errors, _warnings) = typechecker::check_program(&ast, src);
if !errors.is_empty() {
return Err(errors);
}
Ok(typed)
}