basic jit
This commit is contained in:
parent
374daaff7f
commit
511be952aa
16 changed files with 971 additions and 495 deletions
|
|
@ -1,4 +1,6 @@
|
|||
use crate::typing::{BinaryOperator, Identifier, ModulePath, Type, TypeContext};
|
||||
use crate::typing::{BinaryOperator, Identifier, ModulePath, Type, TypingContext};
|
||||
|
||||
use super::UnaryOperator;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TypeError {
|
||||
|
|
@ -38,7 +40,7 @@ impl TypeError {
|
|||
}
|
||||
|
||||
impl TypeErrorBuilder {
|
||||
pub fn context(mut self, ctx: &TypeContext) -> Self {
|
||||
pub fn context(mut self, ctx: &TypingContext) -> Self {
|
||||
self.file = ctx.file.clone();
|
||||
self.module = Some(ctx.module.clone());
|
||||
self.function = ctx.function.clone();
|
||||
|
|
@ -89,4 +91,8 @@ pub enum TypeErrorKind {
|
|||
WrongFunctionArguments,
|
||||
ConditionIsNotBool,
|
||||
IfElseMismatch,
|
||||
InvalidUnaryOperator {
|
||||
operator: UnaryOperator,
|
||||
inner: Type,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,47 +1,88 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::ast::untyped::*;
|
||||
use crate::ast::untyped::module::Module;
|
||||
use crate::ast::ModulePath;
|
||||
use crate::ast::*;
|
||||
|
||||
mod error;
|
||||
use crate::typing::error::{TypeError, TypeErrorKind};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Type {
|
||||
/// Not a real type, used for parsing pass
|
||||
Undefined,
|
||||
Bool,
|
||||
Int,
|
||||
Float,
|
||||
Unit,
|
||||
Str,
|
||||
Function {
|
||||
params: Vec<Type>,
|
||||
returns: Box<Type>,
|
||||
},
|
||||
Custom(Identifier),
|
||||
}
|
||||
|
||||
impl Display for Type {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Type::Undefined => f.write_str("UNDEFINED"),
|
||||
Type::Bool => f.write_str("Bool"),
|
||||
Type::Int => f.write_str("Int"),
|
||||
Type::Float => f.write_str("Float"),
|
||||
Type::Unit => f.write_str("Unit"),
|
||||
Type::Str => f.write_str("Str"),
|
||||
Type::Custom(identifier) => f.write_str(identifier),
|
||||
Type::Function { params, returns } => {
|
||||
f.write_str("Fn(")?;
|
||||
for param in params {
|
||||
f.write_fmt(format_args!("{}, ", param))?;
|
||||
}
|
||||
f.write_str(") -> ")?;
|
||||
f.write_fmt(format_args!("{}", returns))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Type {
|
||||
fn from(value: &str) -> Self {
|
||||
match value {
|
||||
"int" => Type::Int,
|
||||
"float" => Type::Float,
|
||||
"bool" => Type::Bool,
|
||||
_ => Type::Custom(Identifier::from(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl untyped::FunctionDefinition {
|
||||
fn signature(&self) -> (Vec<Type>, Type) {
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Signature(Vec<Type>, Type);
|
||||
|
||||
impl Into<Type> for Signature {
|
||||
fn into(self) -> Type {
|
||||
Type::Function {
|
||||
params: self.0,
|
||||
returns: Box::new(self.1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FunctionDefinition {
|
||||
fn signature(&self) -> Signature {
|
||||
let return_type = self.return_type.clone().unwrap_or(Type::Unit);
|
||||
let params_types = self.parameters.iter().map(|p| p.typ.clone()).collect();
|
||||
(params_types, return_type)
|
||||
Signature(params_types, return_type)
|
||||
}
|
||||
}
|
||||
|
||||
impl Module {
|
||||
pub fn type_check(&self) -> Result<(), TypeError> {
|
||||
let mut ctx = TypeContext::new(self.path.clone());
|
||||
pub fn type_check(&mut self) -> Result<(), TypeError> {
|
||||
let mut ctx = TypingContext::new(self.path.clone());
|
||||
ctx.file = self.file.clone();
|
||||
|
||||
// Register all function signatures
|
||||
for Definition::FunctionDefinition(func) in &self.definitions {
|
||||
for func in &self.functions {
|
||||
if let Some(_previous) = ctx.functions.insert(func.name.clone(), func.signature()) {
|
||||
todo!("handle redefinition of function or identical function names across different files");
|
||||
}
|
||||
|
|
@ -49,8 +90,8 @@ impl Module {
|
|||
|
||||
// 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 {
|
||||
// Type-check the function bodies and complete all type placeholders
|
||||
for func in &mut self.functions {
|
||||
func.typ(&mut ctx)?;
|
||||
ctx.variables.clear();
|
||||
}
|
||||
|
|
@ -59,18 +100,20 @@ impl Module {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct TypeContext {
|
||||
pub struct TypingContext {
|
||||
pub file: Option<std::path::PathBuf>,
|
||||
pub module: ModulePath,
|
||||
pub function: Option<Identifier>,
|
||||
pub functions: HashMap<Identifier, (Vec<Type>, Type)>,
|
||||
pub functions: HashMap<Identifier, Signature>,
|
||||
pub variables: HashMap<Identifier, Type>,
|
||||
}
|
||||
|
||||
impl TypeContext {
|
||||
impl TypingContext {
|
||||
pub fn new(path: ModulePath) -> Self {
|
||||
let builtin_functions =
|
||||
HashMap::from([(String::from("println"), (vec![Type::Str], Type::Unit))]);
|
||||
let builtin_functions = HashMap::from([(
|
||||
String::from("println"),
|
||||
Signature(vec![Type::Str], Type::Unit),
|
||||
)]);
|
||||
|
||||
Self {
|
||||
file: None,
|
||||
|
|
@ -84,70 +127,72 @@ impl TypeContext {
|
|||
|
||||
/// Trait for nodes which have a deducible type.
|
||||
pub trait TypeCheck {
|
||||
/// Try to resolve the type of the node.
|
||||
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError>;
|
||||
/// Try to resolve the type of the node and complete its type placeholders.
|
||||
fn typ(&mut self, ctx: &mut TypingContext) -> Result<Type, TypeError>;
|
||||
}
|
||||
|
||||
impl TypeCheck for FunctionDefinition {
|
||||
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError> {
|
||||
fn typ(&mut self, ctx: &mut TypingContext) -> Result<Type, TypeError> {
|
||||
ctx.function = Some(self.name.clone());
|
||||
|
||||
for param in &self.parameters {
|
||||
// XXX: Parameter types should be checked
|
||||
// when they are not builtin
|
||||
ctx.variables.insert(param.name.clone(), param.typ.clone());
|
||||
}
|
||||
|
||||
let body_type = &self.body.typ(ctx)?;
|
||||
let body_type = self.body.typ(ctx)?;
|
||||
|
||||
// If the return type is not specified, it is unit.
|
||||
let func_return_type = match &self.return_type {
|
||||
Some(typ) => typ,
|
||||
None => &Type::Unit,
|
||||
};
|
||||
if self.return_type.is_none() {
|
||||
self.return_type = Some(Type::Unit)
|
||||
}
|
||||
|
||||
// Check coherence with the body's type.
|
||||
if *func_return_type != *body_type {
|
||||
if *self.return_type.as_ref().unwrap() != body_type {
|
||||
return Err(TypeError::builder()
|
||||
.context(ctx)
|
||||
.kind(TypeErrorKind::BlockTypeDoesNotMatchFunctionType {
|
||||
block_type: body_type.clone(),
|
||||
function_type: func_return_type.clone(),
|
||||
function_type: self.return_type.as_ref().unwrap().clone(),
|
||||
})
|
||||
.build());
|
||||
}
|
||||
|
||||
// Check coherence with return statements.
|
||||
for statement in &self.body.statements {
|
||||
|
||||
for statement in &mut self.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 {
|
||||
if ret_type != *self.return_type.as_ref().unwrap() {
|
||||
return Err(TypeError::builder()
|
||||
.context(ctx)
|
||||
.kind(TypeErrorKind::ReturnTypeDoesNotMatchFunctionType {
|
||||
function_type: func_return_type.clone(),
|
||||
return_type: ret_type,
|
||||
function_type: self.return_type.as_ref().unwrap().clone(),
|
||||
return_type: ret_type.clone(),
|
||||
})
|
||||
.build());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(func_return_type.clone())
|
||||
Ok(self.return_type.clone().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeCheck for Block {
|
||||
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError> {
|
||||
fn typ(&mut self, ctx: &mut TypingContext) -> Result<Type, TypeError> {
|
||||
let mut return_typ: Option<Type> = None;
|
||||
|
||||
// Check declarations and assignments.
|
||||
for statement in &self.statements {
|
||||
for statement in &mut self.statements {
|
||||
match statement {
|
||||
Statement::DeclareStatement(ident, expr) => {
|
||||
let typ = expr.typ(ctx)?;
|
||||
if let Some(_typ) = ctx.variables.insert(ident.clone(), typ) {
|
||||
if let Some(_typ) = ctx.variables.insert(ident.clone(), typ.clone()) {
|
||||
// TODO: Shadowing? (illegal for now)
|
||||
return Err(TypeError::builder()
|
||||
.context(ctx)
|
||||
|
|
@ -159,9 +204,9 @@ impl TypeCheck for Block {
|
|||
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());
|
||||
.context(ctx)
|
||||
.kind(TypeErrorKind::AssignUndeclared)
|
||||
.build());
|
||||
};
|
||||
|
||||
// Ensure same type on both sides.
|
||||
|
|
@ -189,7 +234,7 @@ impl TypeCheck for Block {
|
|||
.build());
|
||||
}
|
||||
} else {
|
||||
return_typ = Some(expr_typ);
|
||||
return_typ = Some(expr_typ.clone());
|
||||
}
|
||||
}
|
||||
Statement::CallStatement(call) => {
|
||||
|
|
@ -220,9 +265,11 @@ impl TypeCheck for Block {
|
|||
}
|
||||
|
||||
// Check if there is an expression at the end of the block.
|
||||
if let Some(expr) = &self.value {
|
||||
expr.typ(ctx)
|
||||
if let Some(expr) = &mut self.value {
|
||||
self.typ = expr.typ(ctx)?.clone();
|
||||
Ok(self.typ.clone())
|
||||
} else {
|
||||
self.typ = Type::Unit;
|
||||
Ok(Type::Unit)
|
||||
}
|
||||
|
||||
|
|
@ -234,29 +281,34 @@ impl TypeCheck for Block {
|
|||
}
|
||||
|
||||
impl TypeCheck for Call {
|
||||
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError> {
|
||||
match &*self.callee {
|
||||
Expr::Identifier(ident) => {
|
||||
let signature = match ctx.functions.get(ident) {
|
||||
fn typ(&mut self, ctx: &mut TypingContext) -> Result<Type, TypeError> {
|
||||
match &mut *self.callee {
|
||||
Expr::Identifier { name, typ } => {
|
||||
let signature = match ctx.functions.get(name) {
|
||||
Some(sgn) => sgn.clone(),
|
||||
None => {
|
||||
return Err(TypeError::builder()
|
||||
.context(ctx)
|
||||
.kind(TypeErrorKind::UnknownFunctionCalled(ident.clone()))
|
||||
.kind(TypeErrorKind::UnknownFunctionCalled(name.clone()))
|
||||
.build())
|
||||
}
|
||||
};
|
||||
let (params_types, func_type) = signature;
|
||||
|
||||
*typ = signature.clone().into();
|
||||
|
||||
let Signature(params_types, func_type) = signature;
|
||||
|
||||
self.typ = func_type.clone();
|
||||
|
||||
// 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());
|
||||
for arg in &mut self.args {
|
||||
let arg_typ = arg.typ(ctx)?;
|
||||
args_types.push(arg_typ.clone());
|
||||
}
|
||||
|
||||
if args_types == *params_types {
|
||||
Ok(func_type.clone())
|
||||
Ok(self.typ.clone())
|
||||
} else {
|
||||
Err(TypeError::builder()
|
||||
.context(ctx)
|
||||
|
|
@ -270,16 +322,17 @@ impl TypeCheck for Call {
|
|||
}
|
||||
|
||||
impl TypeCheck for Expr {
|
||||
fn typ(&self, ctx: &mut TypeContext) -> Result<Type, TypeError> {
|
||||
fn typ(&mut self, ctx: &mut TypingContext) -> Result<Type, TypeError> {
|
||||
match self {
|
||||
Expr::Identifier(identifier) => {
|
||||
if let Some(typ) = ctx.variables.get(identifier) {
|
||||
Expr::Identifier { name, typ } => {
|
||||
if let Some(ty) = ctx.variables.get(name) {
|
||||
*typ = ty.clone();
|
||||
Ok(typ.clone())
|
||||
} else {
|
||||
Err(TypeError::builder()
|
||||
.context(ctx)
|
||||
.kind(TypeErrorKind::UnknownIdentifier {
|
||||
identifier: identifier.clone(),
|
||||
identifier: name.clone(),
|
||||
})
|
||||
.build())
|
||||
}
|
||||
|
|
@ -287,192 +340,107 @@ impl TypeCheck for Expr {
|
|||
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()),
|
||||
}
|
||||
Expr::UnaryExpression { op, inner } => {
|
||||
let inner_type = &inner.typ(ctx)?;
|
||||
match (&op, inner_type) {
|
||||
(UnaryOperator::Not, Type::Bool) => Ok(Type::Bool),
|
||||
_ => Err(TypeError::builder()
|
||||
.context(ctx)
|
||||
.kind(TypeErrorKind::InvalidUnaryOperator {
|
||||
operator: *op,
|
||||
inner: inner_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());
|
||||
}
|
||||
Expr::BinaryExpression { lhs, op, rhs, typ } => {
|
||||
let ty = match op {
|
||||
BinaryOperator::Add
|
||||
| BinaryOperator::Sub
|
||||
| BinaryOperator::Mul
|
||||
| BinaryOperator::Div
|
||||
| BinaryOperator::And
|
||||
| BinaryOperator::Or => {
|
||||
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),
|
||||
(Type::Bool, Type::Bool) => Ok(Type::Bool),
|
||||
(_, _) => Err(TypeError::builder()
|
||||
.context(ctx)
|
||||
.kind(TypeErrorKind::InvalidBinaryOperator {
|
||||
operator: op.clone(),
|
||||
lht: left_type.clone(),
|
||||
rht: right_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()),
|
||||
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()),
|
||||
}
|
||||
}
|
||||
};
|
||||
*typ = ty?;
|
||||
Ok(typ.clone())
|
||||
}
|
||||
Expr::StringLiteral(_) => Ok(Type::Str),
|
||||
Expr::UnitLiteral => Ok(Type::Unit),
|
||||
Expr::Call(call) => call.typ(ctx),
|
||||
Expr::Block(block) => block.typ(ctx),
|
||||
Expr::IfExpr(cond, true_block, else_value) => {
|
||||
Expr::IfExpr {
|
||||
cond,
|
||||
then_body,
|
||||
else_body,
|
||||
typ,
|
||||
} => {
|
||||
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 {
|
||||
let then_body_type = then_body.typ(ctx)?;
|
||||
let else_type = else_body.typ(ctx)?;
|
||||
if then_body_type != else_type {
|
||||
Err(TypeError::builder()
|
||||
.context(ctx)
|
||||
.kind(TypeErrorKind::IfElseMismatch)
|
||||
.build())
|
||||
} else {
|
||||
Ok(true_block_type.clone())
|
||||
// XXX: opt: return ref to avoid cloning
|
||||
*typ = then_body_type.clone();
|
||||
Ok(then_body_type)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Typed<T: TypeCheck> {
|
||||
inner: T,
|
||||
typ: Type,
|
||||
}
|
||||
|
||||
trait IntoTyped<T: TypeCheck> {
|
||||
fn into_typed(self: Self, ctx: &mut TypeContext) -> Result<Typed<T>, TypeError>;
|
||||
}
|
||||
|
||||
impl IntoTyped<Block> for Block {
|
||||
fn into_typed(self: Block, ctx: &mut TypeContext) -> Result<Typed<Block>, 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) {
|
||||
// XXX: 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.
|
||||
let typ = if let Some(expr) = &self.value {
|
||||
expr.typ(ctx)?
|
||||
} else {
|
||||
Type::Unit
|
||||
};
|
||||
|
||||
Ok(Typed { inner: self, typ })
|
||||
|
||||
// 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.
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue