diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-28 00:47:33 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-28 13:18:32 +0200 |
commit | decbf0f8318d0918d9455c2524bfc40a89cbc76c (patch) | |
tree | cc3bcdb5a5f4b0e81fa58f44fd7a594175b5aa28 | |
parent | f70b2b4c6c96b315c69f5de65eb7194062980bd0 (diff) | |
download | rebel-decbf0f8318d0918d9455c2524bfc40a89cbc76c.tar rebel-decbf0f8318d0918d9455c2524bfc40a89cbc76c.zip |
rebel-lang: context: typecheck interpolated expressions
-rw-r--r-- | crates/rebel-lang/src/typing.rs | 23 |
1 files changed, 22 insertions, 1 deletions
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<Type> { 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() |