use common::logging::log;

use crate::model::{CompilerError, Program};
use parser::Parser;

pub mod lexer;
pub mod parser;

pub fn generate_ast(input: &str) -> Result<Program, CompilerError> {
    log("Tokenising Input...");

    let mut lexer = lexer::Lexer::new(&input);
    let tokens = lexer.tokenize().map_err(|e| CompilerError::Generic(e))?;
    // println!("{tokens:?}");

    log(&format!("Parsing {} Tokens...", tokens.len()));

    let mut parser = Parser::new(tokens);
    let ast = match parser.parse() {
        Ok(ast) => ast,
        Err(e) => return Err(CompilerError::Generic(e)),
    };

    Ok(ast)
}
