From decbf0f8318d0918d9455c2524bfc40a89cbc76c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 28 Apr 2024 00:47:33 +0200 Subject: rebel-lang: context: typecheck interpolated expressions --- crates/rebel-lang/src/typing.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/rebel-lang/src/typing.rs b/crates/rebel-lang/src/typing.rs index b7a7d34..59c161b 100644 --- a/crates/rebel-lang/src/typing.rs +++ b/crates/rebel-lang/src/typing.rs @@ -249,6 +249,22 @@ impl Type { .cloned() } + fn check_string_piece(ctx: &Context, piece: &ast::StrPiece, kind: ast::StrKind) -> Result<()> { + let typ = match piece { + ast::StrPiece::Chars(_) => return Ok(()), + ast::StrPiece::Escape(_) => return Ok(()), + ast::StrPiece::Interp(expr) => Self::ast_expr_type(ctx, expr)?, + }; + match (typ, kind) { + (Type::Bool, _) => Ok(()), + (Type::Int, _) => Ok(()), + (Type::Str, _) => Ok(()), + (Type::Tuple(_), ast::StrKind::Script) => Ok(()), + (Type::Array(_, _), ast::StrKind::Script) => Ok(()), + _ => Err(TypeError), + } + } + fn literal_type(ctx: &Context, lit: &ast::Literal<'_>) -> Result { use ast::Literal; use Type::*; @@ -257,7 +273,12 @@ impl Type { Literal::Unit => Unit, Literal::Bool(_) => Bool, Literal::Int(_) => Int, - Literal::Str { .. } => Str, + Literal::Str { pieces, kind } => { + for piece in pieces { + Self::check_string_piece(ctx, piece, *kind)?; + } + Str + } Literal::Tuple(elems) => Tuple( elems .iter() -- cgit v1.2.3