added support for DSA libraries to compiler and made some optimisations.
provided an API for the editor to use.
This commit is contained in:
+3
-47
@@ -1,15 +1,6 @@
|
||||
#![feature(try_trait_v2)]
|
||||
use std::path::Path;
|
||||
|
||||
use std::{fs, path::Path};
|
||||
|
||||
pub mod lexer;
|
||||
pub mod parser;
|
||||
use parser::Parser;
|
||||
pub mod codegen;
|
||||
mod registers;
|
||||
mod semantic_analyser;
|
||||
|
||||
use crate::{codegen::CodeGenerator, parser::ParseResult, semantic_analyser::Analyser};
|
||||
use compiler;
|
||||
|
||||
fn main() {
|
||||
// read from input file: syntax "c_compiler <src.c> [output.dsa]"
|
||||
@@ -26,40 +17,5 @@ fn main() {
|
||||
"output.dsa"
|
||||
};
|
||||
|
||||
// read input
|
||||
let input = std::fs::read_to_string(input_file).expect("Failed to read input file");
|
||||
|
||||
let lexer = lexer::Lexer::new(&input);
|
||||
let tokens = lexer.collect::<Vec<_>>();
|
||||
println!("{tokens:?}");
|
||||
|
||||
let mut parser = Parser::new(tokens);
|
||||
let ast = match parser.parse() {
|
||||
ParseResult::Accept(ast) => ast,
|
||||
ParseResult::Reject(e) => {
|
||||
eprintln!("Error: {e:?}");
|
||||
return;
|
||||
}
|
||||
ParseResult::Deny => {
|
||||
panic!("Parser denied parsing")
|
||||
}
|
||||
};
|
||||
println!("{ast:#?}");
|
||||
|
||||
let analyser = Analyser::new();
|
||||
analyser.analyse(ast.clone()).unwrap();
|
||||
|
||||
// Code Gen
|
||||
let mut generator = CodeGenerator::new(ast);
|
||||
let result = match generator.generate() {
|
||||
Ok(code) => code,
|
||||
Err(e) => {
|
||||
eprintln!("Parsing error: {:?}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
println!("{result}");
|
||||
std::fs::write(output_file, &result).expect("Failed to write output");
|
||||
println!("Result written to {}", output_file);
|
||||
compiler::compile_file(Path::new(input_file), Path::new(output_file)).unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user