diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-28 13:57:10 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-28 13:57:10 +0200 |
commit | 137cfeb08caedde56200bd1d1a7e91dd6b8620cc (patch) | |
tree | a566d1b2f39ee9b93af4095231def8b354006e2c | |
parent | 0795e76ae45b0479289acb2ee4be5ab29980f7a2 (diff) | |
download | rebel-137cfeb08caedde56200bd1d1a7e91dd6b8620cc.tar rebel-137cfeb08caedde56200bd1d1a7e91dd6b8620cc.zip |
rebel-lang: typing: add support for typechecking body statements
-rw-r--r-- | crates/rebel-lang/examples/repl.rs | 35 | ||||
-rw-r--r-- | crates/rebel-lang/src/typing.rs | 13 |
2 files changed, 20 insertions, 28 deletions
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<Type> { + 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<Type> { use ast::Expr::*; |