restructure parsing and typing modules

* parsing backend submodules
* move typing to its own module
This commit is contained in:
Romain Paquet 2023-07-05 16:14:30 +02:00
parent 43df8c4b0a
commit 99434748fa
16 changed files with 1315 additions and 316 deletions

452
src/typing/mod.rs Normal file
View file

@ -0,0 +1,452 @@
use std::collections::HashMap;
use crate::ast::{
module::{Module, ModulePath},
*,
};
#[derive(Debug, PartialEq, Clone)]
pub enum Type {
Bool,
Int,
Float,
Unit,
Str,
Custom(Identifier),
}
impl From<&str> for Type {
fn from(value: &str) -> Self {
match value {
"int" => Type::Int,
"float" => Type::Float,
_ => Type::Custom(Identifier::from(value)),
}
}
}
impl FunctionDefinition {
fn signature(&self) -> (Vec<Type>, Type) {
let return_type = self.return_type.unwrap_or(Type::Unit);
let params_types = self.parameters.iter().map(|p| p.typ).collect();
(params_types, return_type)
}
}
impl Module {
pub fn type_check(&self) -> Result<(), TypeError> {
let mut ctx = TypeContext::new(self.path);
ctx.file = self.file.clone();
// Register all function signatures
for Definition::FunctionDefinition(func) in &self.definitions {
if let Some(previous) = ctx.functions.insert(func.name.clone(), func.signature()) {
todo!("handle redefinition of function or identical function names across different files");
}
}
// TODO: add signatures of imported functions (even if they have not been checked)
// Type-check the function bodies
for Definition::FunctionDefinition(func) in &self.definitions {
func.typ(&mut ctx)?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct TypeError {
file: Option<std::path::PathBuf>,
module: ModulePath,
function: Option<String>,
kind: TypeErrorKind,
}
impl std::fmt::Display for TypeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Error\n")?;
if let Some(path) = &self.file {
f.write_fmt(format_args!(" in file {}\n", path.display()))?;
}
f.write_fmt(format_args!(" in module {}\n", self.module))?;
if let Some(name) = &self.function {
f.write_fmt(format_args!(" in function {}\n", name))?;
}
f.write_fmt(format_args!("{:#?}", self.kind))?;
Ok(())
}
}
#[derive(Default)]
struct TypeErrorBuilder {
file: Option<std::path::PathBuf>,
module: Option<ModulePath>,
function: Option<String>,
kind: Option<TypeErrorKind>,
}
impl TypeError {
fn builder() -> TypeErrorBuilder {
TypeErrorBuilder::default()
}
}
impl TypeErrorBuilder {
fn context(mut self, ctx: &TypeContext) -> Self {
self.file = ctx.file.clone();
self.module = Some(ctx.module.clone());
self.function = ctx.function.clone();
self
}
fn kind(mut self, kind: TypeErrorKind) -> Self {
self.kind = Some(kind);
self
}
fn build(self) -> TypeError {
TypeError {
file: self.file,
module: self.module.expect("TypeError builder is missing module"),
function: self.function,
kind: self.kind.expect("TypeError builder is missing kind"),
}
}
}
#[derive(Debug)]
pub enum TypeErrorKind {
InvalidBinaryOperator {
operator: BinaryOperator,
lht: Type,
rht: Type,
},
BlockTypeDoesNotMatchFunctionType {
block_type: Type,
function_type: Type,
},
ReturnTypeDoesNotMatchFunctionType {
function_type: Type,
return_type: Type,
},
UnknownIdentifier {
identifier: String,
},
AssignmentMismatch {
lht: Type,
rht: Type,
},
AssignUndeclared,
VariableRedeclaration,
ReturnStatementsMismatch,
UnknownFunctionCalled(Identifier),
WrongFunctionArguments,
ConditionIsNotBool,
IfElseMismatch,
}
pub struct TypeContext {
pub file: Option<std::path::PathBuf>,
pub module: ModulePath,
pub function: Option<Identifier>,
pub functions: HashMap<Identifier, (Vec<Type>, Type)>,
pub variables: HashMap<Identifier, Type>,
}
impl TypeContext {
pub fn new(path: ModulePath) -> Self {
TypeContext {
file: None,
module: path,
function: None,
functions: Default::default(),
variables: Default::default(),
}
}
}
/// Trait for nodes which have a deducible type.
pub trait Typ {
/// Try to resolve the type of the node.
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError>;
}
impl Typ for FunctionDefinition {
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError> {
let func = self;
ctx.function = Some(func.name.clone());
for param in &func.parameters {
ctx.variables.insert(param.name.clone(), param.typ.clone());
}
let body_type = &func.body.typ(ctx)?;
// If the return type is not specified, it is unit.
let func_return_type = match &func.return_type {
Some(typ) => typ,
None => &Type::Unit,
};
// Check coherence with the body's type.
if *func_return_type != *body_type {
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::BlockTypeDoesNotMatchFunctionType {
block_type: body_type.clone(),
function_type: func_return_type.clone(),
})
.build());
}
// Check coherence with return statements.
for statement in &func.body.statements {
if let Statement::ReturnStatement(value) = statement {
let ret_type = match value {
Some(expr) => expr.typ(ctx)?,
None => Type::Unit,
};
if ret_type != *func_return_type {
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::ReturnTypeDoesNotMatchFunctionType {
function_type: func_return_type.clone(),
return_type: ret_type,
})
.build());
}
}
}
Ok(func_return_type.clone())
}
}
impl Typ for Block {
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError> {
let mut return_typ: Option<Type> = None;
// Check declarations and assignments.
for statement in &self.statements {
match statement {
Statement::DeclareStatement(ident, expr) => {
let typ = expr.typ(ctx)?;
if let Some(_typ) = ctx.variables.insert(ident.clone(), typ) {
// TODO: Shadowing? (illegal for now)
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::VariableRedeclaration)
.build());
}
}
Statement::AssignStatement(ident, expr) => {
let rhs_typ = expr.typ(ctx)?;
let Some(lhs_typ) = ctx.variables.get(ident) else {
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::AssignUndeclared)
.build());
};
// Ensure same type on both sides.
if rhs_typ != *lhs_typ {
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::AssignmentMismatch {
lht: lhs_typ.clone(),
rht: rhs_typ.clone(),
})
.build());
}
}
Statement::ReturnStatement(maybe_expr) => {
let expr_typ = if let Some(expr) = maybe_expr {
expr.typ(ctx)?
} else {
Type::Unit
};
if let Some(typ) = &return_typ {
if expr_typ != *typ {
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::ReturnStatementsMismatch)
.build());
}
} else {
return_typ = Some(expr_typ);
}
}
Statement::CallStatement(call) => {
call.typ(ctx)?;
}
Statement::UseStatement(_path) => {
// TODO: import the signatures (and types)
}
Statement::IfStatement(cond, block) => {
if cond.typ(ctx)? != Type::Bool {
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::ConditionIsNotBool)
.build());
}
block.typ(ctx)?;
}
Statement::WhileStatement(cond, block) => {
if cond.typ(ctx)? != Type::Bool {
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::ConditionIsNotBool)
.build());
}
block.typ(ctx)?;
}
}
}
// Check if there is an expression at the end of the block.
if let Some(expr) = &self.value {
expr.typ(ctx)
} else {
Ok(Type::Unit)
}
// TODO/FIXME: find a way to return `return_typ` so that the
// top-level block (the function) can check if this return type
// (and eventually those from other block) matches the type of
// the function.
}
}
impl Typ for Call {
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError> {
match &self.callee {
Expr::Identifier(ident) => {
let signature = match ctx.functions.get(ident) {
Some(sgn) => sgn.clone(),
None => {
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::UnknownFunctionCalled(ident.clone()))
.build())
}
};
let (params_types, func_type) = signature;
// Collect arg types.
let mut args_types: Vec<Type> = vec![];
for arg in &self.args {
let typ = arg.typ(ctx)?;
args_types.push(typ.clone());
}
if args_types == *params_types {
Ok(func_type.clone())
} else {
Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::WrongFunctionArguments)
.build())
}
}
_ => unimplemented!("cannot call on expression other than identifier"),
}
}
}
impl Typ for Expr {
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError> {
match self {
Expr::Identifier(identifier) => {
if let Some(typ) = ctx.variables.get(identifier) {
Ok(typ.clone())
} else {
Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::UnknownIdentifier {
identifier: identifier.clone(),
})
.build())
}
}
Expr::BooleanLiteral(_) => Ok(Type::Bool),
Expr::IntegerLiteral(_) => Ok(Type::Int),
Expr::FloatLiteral(_) => Ok(Type::Float),
Expr::BinaryExpression(lhs, op, rhs) => match op {
BinaryOperator::Add
| BinaryOperator::Sub
| BinaryOperator::Mul
| BinaryOperator::Div => {
let left_type = &lhs.typ(ctx)?;
let right_type = &rhs.typ(ctx)?;
match (left_type, right_type) {
(Type::Int, Type::Int) => Ok(Type::Int),
(Type::Float, Type::Float) => Ok(Type::Float),
(_, _) => Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::InvalidBinaryOperator {
operator: op.clone(),
lht: left_type.clone(),
rht: right_type.clone(),
})
.build()),
}
}
BinaryOperator::Equal | BinaryOperator::NotEqual => {
let lhs_type = lhs.typ(ctx)?;
let rhs_type = rhs.typ(ctx)?;
if lhs_type != rhs_type {
return Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::InvalidBinaryOperator {
operator: op.clone(),
lht: lhs_type.clone(),
rht: rhs_type.clone(),
})
.build());
}
Ok(Type::Bool)
}
BinaryOperator::Modulo => {
let lhs_type = lhs.typ(ctx)?;
let rhs_type = lhs.typ(ctx)?;
match (&lhs_type, &rhs_type) {
(Type::Int, Type::Int) => Ok(Type::Int),
_ => Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::InvalidBinaryOperator {
operator: op.clone(),
lht: lhs_type.clone(),
rht: rhs_type.clone(),
})
.build()),
}
}
},
Expr::StringLiteral(_) => Ok(Type::Str),
Expr::Call(call) => call.typ(ctx),
Expr::Block(block) => block.typ(ctx),
Expr::IfExpr(cond, true_block, else_value) => {
if cond.typ(ctx)? != Type::Bool {
Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::ConditionIsNotBool)
.build())
} else {
let true_block_type = true_block.typ(ctx)?;
let else_type = else_value.typ(ctx)?;
if true_block_type != else_type {
Err(TypeError::builder()
.context(ctx)
.kind(TypeErrorKind::IfElseMismatch)
.build())
} else {
Ok(true_block_type.clone())
}
}
}
}
}
}