added error handling to type checker

This commit is contained in:
Andrew Segavac
2021-09-12 14:36:36 -06:00
parent ff691ebc49
commit 8c131c035b
5 changed files with 558 additions and 451 deletions

36
src/errors.rs Normal file
View File

@@ -0,0 +1,36 @@
use crate::ast;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum TypingError {
#[error("unknown named type")]
TypeDoesNotExist { identifier: ast::Identifier },
#[error("identifier is not type")]
IdentifierIsNotType { identifier: ast::Identifier },
#[error("argument length mismatch")]
ArgumentLengthMismatch {
// TODO: add position
},
#[error("type mismatch")]
TypeMismatch {
type_one: ast::TypeUsage,
type_two: ast::TypeUsage,
},
#[error("unknown field name")]
UnknownFieldName { identifier: ast::Identifier },
#[error("attribute gotten of non-struct")]
AttributeOfNonstruct { identifier: ast::Identifier },
#[error("name is not a struct, cannot instaniate")]
NotAStructLiteral { identifier: ast::Identifier },
#[error("struct literal fields mismatch")]
StructLiteralFieldsMismatch {
struct_name: ast::Identifier,
struct_definition_name: ast::Identifier,
},
#[error("function call used with non-function")]
FunctionCallNotAFunction {
// TODO: add position
},
#[error("multiple errors")]
MultipleErrors { errors: Vec<TypingError> },
}

View File

@@ -1,5 +1,6 @@
// mod types;
mod ast;
mod errors;
mod type_alias_resolution;
mod type_checking;
#[macro_use]
@@ -60,9 +61,16 @@ fn main() {
let resolved_ast = alias_resolver.with_module(&module_ast);
println!("resolved ast: {:#?}", &resolved_ast);
let type_checker = type_checking::TypeChecker {};
let (checked_ast, subst) = type_checker.with_module(&resolved_ast);
println!("checked ast: {:#?}", &checked_ast);
println!("substitutions: {:#?}", &subst);
let type_checking_result = type_checker.with_module(&resolved_ast);
match &type_checking_result {
Ok((checked_ast, subst)) => {
println!("checked ast: {:#?}", &checked_ast);
println!("substitutions: {:#?}", &subst);
}
Err(err) => {
println!("type checking error: {:#?}", &err);
}
}
// let context = Context::create();
// let mut code_gen = compiler::ModuleCodeGen::new(&context, "main".to_string());
@@ -137,5 +145,10 @@ fn grammar() {
assert!(grammar::ModuleParser::new()
.parse(&id_gen, "fn add(a: i32, b: i32): i32 { a + b }")
.is_ok());
assert!(grammar::ModuleParser::new().parse(&id_gen, "fn add(a: i32, b: i32): i32 { a + b } fn subtract(a: i32, b: i32): i32 { a - b }").is_ok());
assert!(grammar::ModuleParser::new()
.parse(
&id_gen,
"fn add(a: i32, b: i32): i32 { a + b } fn subtract(a: i32, b: i32): i32 { a - b }"
)
.is_ok());
}

File diff suppressed because it is too large Load Diff