From 57896643822d2c63bebd95c3c9622f2f37d2f780 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Thu, 22 Oct 2026 00:45:58 +0530 Subject: [PATCH] interpreter: match a bare regex against $0 --- interpreter/src/ir/lower.rs | 5 ++++- tests/by-util/test_awk.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/interpreter/src/ir/lower.rs b/interpreter/src/ir/lower.rs index 20fbe42..af1160d 100644 --- a/interpreter/src/ir/lower.rs +++ b/interpreter/src/ir/lower.rs @@ -513,9 +513,12 @@ impl<'a> CodeGen<'a> { TypedArg::new_cnt(self, val(buf.into())) } Atom::Regex(r) => { + // A bare `/re/` is `$0 ~ /re/` + let (arg, ty) = TypedArg::new_imm(0).into_arg(); + self.emit(Instruction::LoadF { dest, arg, ty }); let buf = &*self.arena.alloc_slice_copy(r.as_ref()); let (rhs, tyr) = TypedArg::new_cnt(self, Value::new_regex(buf.into())).into_arg(); - let (lhs, tyl) = TypedArg::new_imm(0).into_arg(); + let (lhs, tyl) = TypedArg::new_reg(dest).into_arg(); self.emit(Instruction::Matches { dest, rhs, lhs, tyr, tyl }); TypedArg::new_reg(dest) } diff --git a/tests/by-util/test_awk.rs b/tests/by-util/test_awk.rs index 3e19faf..9f56c9f 100644 --- a/tests/by-util/test_awk.rs +++ b/tests/by-util/test_awk.rs @@ -881,3 +881,21 @@ fn mdim_in_array() { .succeeds() .stdout_is("1 1 1 1 0\n"); } + +#[test] +fn bare_regex_pattern_matches_record() { + ucmd() + .args(&["/b/ { print NR \": \" $0 } !/a/ { print \"no a\" }"]) + .pipe_in("a b\nc\nab\n") + .succeeds() + .stdout_is("1: a b\nno a\n3: ab\n"); +} + +#[test] +fn bare_regex_in_expression_matches_record() { + ucmd() + .arg(r#"{ n += /x/; if (/^y/) print "y", NR } END { print n }"#) + .pipe_in("x\ny\nxx\n") + .succeeds() + .stdout_is("y 2\n2\n"); +}