restructure parsing and typing modules
* parsing backend submodules * move typing to its own module
This commit is contained in:
parent
43df8c4b0a
commit
99434748fa
16 changed files with 1315 additions and 316 deletions
40
src/main.rs
40
src/main.rs
|
|
@ -1,8 +1,10 @@
|
|||
mod ast;
|
||||
mod parsing;
|
||||
mod typing;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::fs;
|
||||
|
||||
use crate::ast::module::Module;
|
||||
|
||||
/// Experimental compiler for krone
|
||||
#[derive(Parser, Debug)]
|
||||
|
|
@ -16,8 +18,8 @@ struct Cli {
|
|||
#[derive(Subcommand, Debug)]
|
||||
enum Commands {
|
||||
Parse {
|
||||
/// Path to the source file
|
||||
file: String,
|
||||
/// Path to the source files
|
||||
files: Vec<String>,
|
||||
|
||||
/// Dump the AST to stdout
|
||||
#[arg(long)]
|
||||
|
|
@ -25,7 +27,7 @@ enum Commands {
|
|||
|
||||
/// Add missing return types in the AST
|
||||
#[arg(long)]
|
||||
complete_ast: bool,
|
||||
type_check: bool,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -34,25 +36,31 @@ fn main() {
|
|||
|
||||
match &cli.command {
|
||||
Commands::Parse {
|
||||
file,
|
||||
files,
|
||||
dump_ast,
|
||||
complete_ast,
|
||||
type_check,
|
||||
} => {
|
||||
let source = fs::read_to_string(&file).expect("could not read the source file");
|
||||
let mut ast = match parsing::parse(&source) {
|
||||
Ok(ast) => ast,
|
||||
Err(e) => panic!("Parsing error: {:#?}", e),
|
||||
};
|
||||
let paths = files.iter().map(std::path::Path::new);
|
||||
let modules: Vec<Module> = paths
|
||||
.map(|path| match parsing::parse_file(&path) {
|
||||
Ok(module) => module,
|
||||
Err(e) => panic!("Parsing error: {:#?}", e),
|
||||
})
|
||||
.collect();
|
||||
|
||||
if *complete_ast {
|
||||
if let Err(e) = ast.check_return_types() {
|
||||
eprintln!("{:#?}", e);
|
||||
return;
|
||||
if *type_check {
|
||||
for module in &modules {
|
||||
if let Err(e) = module.type_check() {
|
||||
eprintln!("{}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if *dump_ast {
|
||||
println!("{:#?}", &ast);
|
||||
for module in &modules {
|
||||
println!("{:#?}", &module);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue