adding '_ = <expr>' syntax

This commit is contained in:
Jakub Doka 2024-11-03 22:27:37 +01:00
parent 61250c906a
commit 999b25df8b
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
5 changed files with 13 additions and 2 deletions

View file

@ -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
}

View file

@ -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("$")?;

View file

@ -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 = ".(",

View file

@ -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 {

View file

@ -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))?;