added logging to builder trait and implemented for compiler and
assembler crates.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use common::logging::log;
|
||||
|
||||
use crate::model::{CompilerError, Program};
|
||||
use common::logging::Logger;
|
||||
use parser::{ParseResult, Parser};
|
||||
// use semantic_analyser::Analyser;
|
||||
|
||||
@@ -8,14 +7,14 @@ pub mod lexer;
|
||||
pub mod parser;
|
||||
// pub mod semantic_analyser;
|
||||
|
||||
pub fn generate_ast(input: &str) -> Result<Program, CompilerError> {
|
||||
log("Tokenising Input...");
|
||||
pub fn generate_ast(input: &str, logger: &Logger) -> Result<Program, CompilerError> {
|
||||
logger.info("Tokenising Input...");
|
||||
|
||||
let lexer = lexer::Lexer::new(&input);
|
||||
let tokens = lexer.collect::<Vec<_>>();
|
||||
println!("{tokens:#?}");
|
||||
|
||||
log(&format!("Parsing {} Tokens...", tokens.len()));
|
||||
logger.info(&format!("Parsing {} Tokens...", tokens.len()));
|
||||
|
||||
let mut parser = Parser::new(tokens);
|
||||
let ast = match parser.parse() {
|
||||
@@ -27,12 +26,12 @@ pub fn generate_ast(input: &str) -> Result<Program, CompilerError> {
|
||||
};
|
||||
// println!("{ast:#?}");
|
||||
|
||||
log("Analyzing AST...");
|
||||
log("Checking Type Information...");
|
||||
logger.info("Analyzing AST...");
|
||||
logger.info("Checking Type Information...");
|
||||
|
||||
// let mut analyser = Analyser::new();
|
||||
// analyser.analyse(ast.clone()).unwrap();
|
||||
|
||||
log("Type Checking Complete...");
|
||||
logger.info("Type Checking Complete...");
|
||||
Ok(ast)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
use common::logging::Logger;
|
||||
|
||||
use crate::model::{CompilerError, Program};
|
||||
|
||||
// mod c;
|
||||
mod dsc;
|
||||
|
||||
pub fn compiler_frontend(ext: &str, data: &str) -> Result<Program, CompilerError> {
|
||||
pub fn compiler_frontend(
|
||||
ext: &str,
|
||||
data: &str,
|
||||
logger: &Logger,
|
||||
) -> Result<Program, CompilerError> {
|
||||
match ext {
|
||||
"dsc" => Ok(dsc::generate_ast(&data)?),
|
||||
"dsc" => Ok(dsc::generate_ast(&data, &logger)?),
|
||||
// "c" => Ok(c::generate_ast(&data)?),
|
||||
_ => Err(CompilerError::Generic(format!(
|
||||
"File type {} not supported",
|
||||
|
||||
+12
-5
@@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use common::{
|
||||
build::{BuildError, Builder},
|
||||
logging::log,
|
||||
logging::LogReceiver,
|
||||
};
|
||||
|
||||
use crate::{model::CompilerError, specialised::build_specialised};
|
||||
@@ -17,6 +17,7 @@ mod specialised;
|
||||
pub struct Compiler {
|
||||
src_path: PathBuf,
|
||||
result: Option<Result<String, BuildError>>,
|
||||
logger: LogReceiver,
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
@@ -36,10 +37,11 @@ impl Compiler {
|
||||
}
|
||||
|
||||
// Parse the input using the frontend, providing the file extension and data.
|
||||
let ast = match frontend::compiler_frontend(input_ext, &input) {
|
||||
Ok(ast) => ast,
|
||||
Err(err) => return Err(format!("Compilation failed: {err:?}").into()),
|
||||
};
|
||||
let ast =
|
||||
match frontend::compiler_frontend(input_ext, &input, &self.logger.logger()) {
|
||||
Ok(ast) => ast,
|
||||
Err(err) => return Err(format!("Compilation failed: {err:?}").into()),
|
||||
};
|
||||
|
||||
// println!("Parsed AST: {:#?}", ast);
|
||||
|
||||
@@ -60,6 +62,7 @@ impl Builder for Compiler {
|
||||
Self {
|
||||
src_path: src_path.into(),
|
||||
result: None,
|
||||
logger: LogReceiver::new(true),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +82,10 @@ impl Builder for Compiler {
|
||||
"Compiler was never started",
|
||||
)))?
|
||||
}
|
||||
|
||||
fn logs(&self) -> Vec<String> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error(msg: impl Into<String>) -> CompilerError {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use common::{build::Builder, logging::log};
|
||||
use common::{build::Builder, logging::info};
|
||||
use compiler::Compiler;
|
||||
|
||||
fn main() {
|
||||
@@ -25,7 +25,7 @@ fn main() {
|
||||
|
||||
std::fs::write(output_file, &result).expect("Failed to write output");
|
||||
|
||||
log(&format!(
|
||||
info(&format!(
|
||||
"Compilation Successful ✅ \n\tSource: {}\n\tOutput: {}\n",
|
||||
input_file, output_file,
|
||||
));
|
||||
|
||||
Reference in New Issue
Block a user