From 137cfeb08caedde56200bd1d1a7e91dd6b8620cc Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 28 Apr 2024 13:57:10 +0200 Subject: rebel-lang: typing: add support for typechecking body statements --- crates/rebel-lang/examples/repl.rs | 35 +++++++---------------------------- crates/rebel-lang/src/typing.rs | 13 +++++++++++++ 2 files changed, 20 insertions(+), 28 deletions(-) (limited to 'crates') diff --git a/crates/rebel-lang/examples/repl.rs b/crates/rebel-lang/examples/repl.rs index 914f318..db82ba6 100644 --- a/crates/rebel-lang/examples/repl.rs +++ b/crates/rebel-lang/examples/repl.rs @@ -85,34 +85,13 @@ fn main() { } }; - match stmt.validate() { - Ok(()) => (), - Err(err) => { - println!("Validation error: {err:?}"); - continue; - } - }; - - match &stmt { - rebel_parse::ast::BodyStmt::Assign { dest: _, expr } => { - match Type::ast_expr_type(&ctx, expr) { - Ok(_) => (), - Err(err) => { - println!("Type error: {err:?}"); - continue; - } - }; - } - rebel_parse::ast::BodyStmt::Expr { expr } => { - match Type::ast_expr_type(&ctx, expr) { - Ok(_) => (), - Err(err) => { - println!("Type error: {err:?}"); - continue; - } - }; - } - rebel_parse::ast::BodyStmt::Empty => {} + if let Err(err) = stmt.validate() { + println!("Validation error: {err:?}"); + continue; + } + if let Err(err) = Type::ast_stmt_type(&ctx, &stmt) { + println!("Type error: {err:?}"); + continue; } let value = match ctx.run(&stmt) { diff --git a/crates/rebel-lang/src/typing.rs b/crates/rebel-lang/src/typing.rs index 59c161b..5309e76 100644 --- a/crates/rebel-lang/src/typing.rs +++ b/crates/rebel-lang/src/typing.rs @@ -78,6 +78,19 @@ impl Type { }) } + pub fn ast_stmt_type(ctx: &Context, expr: &ast::BodyStmt<'_>) -> Result { + match expr { + ast::BodyStmt::Assign { dest: _, expr } => { + // TODO: Assignability, dest type + let dest_type = Type::Free; + let expr_type = Self::ast_expr_type(ctx, expr)?; + dest_type.unify(expr_type, Coerce::Assign) + } + ast::BodyStmt::Expr { expr } => Self::ast_expr_type(ctx, expr), + ast::BodyStmt::Empty => Ok(Type::Unit), + } + } + pub fn ast_expr_type(ctx: &Context, expr: &ast::Expr<'_>) -> Result { use ast::Expr::*; -- cgit v1.2.3