Commit 28e52f24

28e52f2452aaMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

fix(codegen): dispatch regex held in a local variable, propagate /i /x /m

Stage 4 of issue #61. Builds on stage 3 (constant-resolution) by extending find_regexp_index to also resolve a LocalVariableReadNode when scan_features observed exactly one write of a regex literal to that name. Multi-write or non-regex-write names stay ambiguous and fall through.

scan_features now records each var = /lit/ into parallel arrays @local_regex_names / @local_regex_idx. The pre-scan calls scan_features on the regex value first to ensure the pattern is registered before the index is read; the existing pattern-push site gains an idempotence check so the recursive descent that follows doesn't double-register.

A latent bug surfaces once the local-receiver =~ actually reaches the engine: regex flags were never propagated.

  • spinel_parse.c didn't emit flags for RegularExpressionNode.
  • spinel_codegen.rb's bit translation expected pre-Prism flag values (1, 2, 8) but Prism uses 4 (i), 8 (x), 16 (m).

Both fixed. test/regex_multiline_pattern.rb (a /x literal on a local) was passing only because =~ fell through to the (-1) literal, which is truthy in C; with the engine actually running, the test needed /x to be honored.

test/regex_local_var.rb covers re.match?(s), s =~ re, re =~ s, the same shapes inside a method body, and a multi-write local that correctly stays out of scope for static dispatch.

Refs #61

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
28e52f2452aa
Parent
7e3308dccc78

spinel_codegen.rbmodifiedView file

@@ -188,6 +188,13 @@
188188 @needs_rand = 0
189189 @regexp_patterns = "".split(",")
190190 @regexp_flags = "".split(",")
191+ # `var = /lit/` resolution. Parallel arrays: `@local_regex_names`
192+ # holds the local-variable name and `@local_regex_idx` holds the
193+ # corresponding `@regexp_patterns` index, or -1 when the same name
194+ # has any other (non-regex or different-regex) write anywhere in
195+ # the program — in which case the dispatcher must fall through.
196+ @local_regex_names = "".split(",")
197+ @local_regex_idx = []
191198
192199 # Cache for parse_id_list: AST list fields never change once loaded,
193200 # so the parsed IntArray can be shared across callers. The `[[0]]`
@@ -928,6 +935,19 @@
928935 end
929936 end
930937 end
938+ # A local variable with exactly one write to a regex literal is
939+ # also resolvable. Multi-write or non-regex-write names were
940+ # marked ambiguous (-1) by scan_features.
941+ if @nd_type[nid] == "LocalVariableReadNode"
942+ lname = @nd_name[nid]
943+ i = 0
944+ while i < @local_regex_names.length
945+ if @local_regex_names[i] == lname
946+ return @local_regex_idx[i]
947+ end
948+ i = i + 1
949+ end
950+ end
931951 -1
932952 end
933953
@@ -7077,10 +7097,14 @@
70777097 if @nd_flags[nid] != 0
70787098 f = @nd_flags[nid]
70797099 parts = "".split(",")
7080- if f & 1 != 0
7100+ # Prism flag bits → engine `RE_FLAG_*` values (see re_internal.h).
7101+ # Prism: IGNORE_CASE=4, EXTENDED=8, MULTI_LINE=16.
7102+ # Engine: IGNORECASE=1, MULTILINE=2, DOTALL=4, EXTENDED=8.
7103+ # Ruby's /m (dot-matches-newline) maps to MULTILINE|DOTALL = 6.
7104+ if f & 4 != 0
70817105 parts.push("1")
70827106 end
7083- if f & 2 != 0
7107+ if f & 16 != 0
70847108 parts.push("6")
70857109 end
70867110 if f & 8 != 0
@@ -7090,8 +7114,59 @@
70907114 flags = parts.join("|")
70917115 end
70927116 end
7093- @regexp_patterns.push(pat)
7094- @regexp_flags.push(flags)
7117+ # Idempotent: identical patterns share the same compiled global,
7118+ # so a second visit (e.g. via the LocalVariableWriteNode pre-scan
7119+ # below) is a no-op.
7120+ already = 0
7121+ ri0 = 0
7122+ while ri0 < @regexp_patterns.length
7123+ if @regexp_patterns[ri0] == pat
7124+ already = 1
7125+ end
7126+ ri0 = ri0 + 1
7127+ end
7128+ if already == 0
7129+ @regexp_patterns.push(pat)
7130+ @regexp_flags.push(flags)
7131+ end
7132+ end
7133+ # Track `var = /lit/` so a regex held in a local can be dispatched
7134+ # by find_regexp_index. A name with multiple writes (any kind, any
7135+ # regex literal) is marked ambiguous (-1) and falls through.
7136+ if t == "LocalVariableWriteNode"
7137+ lname = @nd_name[nid]
7138+ vid = @nd_expression[nid]
7139+ this_idx = -1
7140+ if vid >= 0 && @nd_type[vid] == "RegularExpressionNode"
7141+ # Register the pattern up front (the recursive scan after this
7142+ # block would do it too, but we need the index now to record
7143+ # the local-name → pattern mapping).
7144+ scan_features(vid)
7145+ rpat = @nd_unescaped[vid]
7146+ ri = 0
7147+ while ri < @regexp_patterns.length
7148+ if @regexp_patterns[ri] == rpat
7149+ this_idx = ri
7150+ end
7151+ ri = ri + 1
7152+ end
7153+ end
7154+ i2 = 0
7155+ found = 0
7156+ while i2 < @local_regex_names.length
7157+ if @local_regex_names[i2] == lname
7158+ found = 1
7159+ # Any second write (regex or not) marks ambiguous.
7160+ if @local_regex_idx[i2] != this_idx
7161+ @local_regex_idx[i2] = -1
7162+ end
7163+ end
7164+ i2 = i2 + 1
7165+ end
7166+ if found == 0
7167+ @local_regex_names.push(lname)
7168+ @local_regex_idx.push(this_idx)
7169+ end
70957170 end
70967171 if t == "XStringNode"
70977172 @needs_file_io = 1