From 999b25df8b74e0af9c6b754ecee7bbefc093db49 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sun, 3 Nov 2024 22:27:37 +0100 Subject: [PATCH] adding '_ = ' syntax --- lang/README.md | 4 ++-- lang/src/fmt.rs | 1 + lang/src/lexer.rs | 2 ++ lang/src/parser.rs | 4 ++++ lang/src/son.rs | 4 ++++ 5 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lang/README.md b/lang/README.md index 90606ca4..1fb88210 100644 --- a/lang/README.md +++ b/lang/README.md @@ -1008,9 +1008,9 @@ create_back_buffer := fn(total_pages: int): ^u32 { remaining := total_pages - 0xFF loop if remaining <= 0 break else { if remaining < 0xFF { - _f := request_page(@intcast(remaining)) + _ = request_page(@intcast(remaining)) } else { - _f := request_page(0xFF) + _ = request_page(0xFF) } remaining -= 0xFF } diff --git a/lang/src/fmt.rs b/lang/src/fmt.rs index 57a2dcb5..773561b8 100644 --- a/lang/src/fmt.rs +++ b/lang/src/fmt.rs @@ -346,6 +346,7 @@ impl<'a> Formatter<'a> { self.fmt(val, f) } Expr::Return { val: None, .. } => f.write_str("return"), + Expr::Wildcard { .. } => f.write_str("_"), Expr::Ident { pos, is_ct, .. } => { if is_ct { f.write_str("$")?; diff --git a/lang/src/lexer.rs b/lang/src/lexer.rs index 99963571..f68a63e5 100644 --- a/lang/src/lexer.rs +++ b/lang/src/lexer.rs @@ -148,6 +148,7 @@ pub enum TokenKind { RBrack = b']', Xor = b'^', Tick = b'`', + Under = b'_', // Unused = a-z LBrace = b'{', Bor = b'|', @@ -308,6 +309,7 @@ gen_token_kind! { Null = b"null", Idk = b"idk", Die = b"die", + Under = b"_", #[punkt] Ctor = ".{", Tupl = ".(", diff --git a/lang/src/parser.rs b/lang/src/parser.rs index e8c9e571..7995eef8 100644 --- a/lang/src/parser.rs +++ b/lang/src/parser.rs @@ -392,6 +392,7 @@ impl<'a, 'b> Parser<'a, 'b> { let (id, is_first) = self.resolve_ident(token); E::Ident { pos, is_ct: token.kind == T::CtIdent, id, is_first } } + T::Under => E::Wildcard { pos }, T::If => E::If { pos, cond: self.ptr_expr()?, @@ -800,6 +801,9 @@ generate_expr! { pos: Pos, val: Option<&'a Self>, }, + Wildcard { + pos: Pos, + }, /// note: ':unicode:' is any utf-8 character except ascii /// `'[a-zA-Z_:unicode:][a-zA-Z0-9_:unicode:]*'` Ident { diff --git a/lang/src/son.rs b/lang/src/son.rs index 351ff940..b95b1bbb 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -2666,6 +2666,10 @@ impl<'a> Codegen<'a> { self.assign_pattern(left, right); Some(Value::VOID) } + Expr::BinOp { left: Expr::Wildcard { .. }, op: TokenKind::Assign, right, .. } => { + self.expr(right)?; + Some(Value::VOID) + } Expr::BinOp { left, pos, op: TokenKind::Assign, right } => { let dest = self.raw_expr(left)?; let mut value = self.expr_ctx(right, Ctx::default().with_ty(dest.ty))?;