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
flagsforRegularExpressionNode. - 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
@needs_rand = 0 @regexp_patterns = "".split(",") @regexp_flags = "".split(",") # `var = /lit/` resolution. Parallel arrays: `@local_regex_names` # holds the local-variable name and `@local_regex_idx` holds the # corresponding `@regexp_patterns` index, or -1 when the same name # has any other (non-regex or different-regex) write anywhere in # the program — in which case the dispatcher must fall through. @local_regex_names = "".split(",") @local_regex_idx = [] # Cache for parse_id_list: AST list fields never change once loaded, # so the parsed IntArray can be shared across callers. The `[[0]]` end end end # A local variable with exactly one write to a regex literal is # also resolvable. Multi-write or non-regex-write names were # marked ambiguous (-1) by scan_features. if @nd_type[nid] == "LocalVariableReadNode" lname = @nd_name[nid] i = 0 while i < @local_regex_names.length if @local_regex_names[i] == lname return @local_regex_idx[i] end i = i + 1 end end -1 end if @nd_flags[nid] != 0 f = @nd_flags[nid] parts = "".split(",") if f & 1 != 0 # Prism flag bits → engine `RE_FLAG_*` values (see re_internal.h). # Prism: IGNORE_CASE=4, EXTENDED=8, MULTI_LINE=16. # Engine: IGNORECASE=1, MULTILINE=2, DOTALL=4, EXTENDED=8. # Ruby's /m (dot-matches-newline) maps to MULTILINE|DOTALL = 6. if f & 4 != 0 parts.push("1") end if f & 2 != 0 if f & 16 != 0 parts.push("6") end if f & 8 != 0 flags = parts.join("|") end end @regexp_patterns.push(pat) @regexp_flags.push(flags) # Idempotent: identical patterns share the same compiled global, # so a second visit (e.g. via the LocalVariableWriteNode pre-scan # below) is a no-op. already = 0 ri0 = 0 while ri0 < @regexp_patterns.length if @regexp_patterns[ri0] == pat already = 1 end ri0 = ri0 + 1 end if already == 0 @regexp_patterns.push(pat) @regexp_flags.push(flags) end end # Track `var = /lit/` so a regex held in a local can be dispatched # by find_regexp_index. A name with multiple writes (any kind, any # regex literal) is marked ambiguous (-1) and falls through. if t == "LocalVariableWriteNode" lname = @nd_name[nid] vid = @nd_expression[nid] this_idx = -1 if vid >= 0 && @nd_type[vid] == "RegularExpressionNode" # Register the pattern up front (the recursive scan after this # block would do it too, but we need the index now to record # the local-name → pattern mapping). scan_features(vid) rpat = @nd_unescaped[vid] ri = 0 while ri < @regexp_patterns.length if @regexp_patterns[ri] == rpat this_idx = ri end ri = ri + 1 end end i2 = 0 found = 0 while i2 < @local_regex_names.length if @local_regex_names[i2] == lname found = 1 # Any second write (regex or not) marks ambiguous. if @local_regex_idx[i2] != this_idx @local_regex_idx[i2] = -1 end end i2 = i2 + 1 end if found == 0 @local_regex_names.push(lname) @local_regex_idx.push(this_idx) end end if t == "XStringNode" @needs_file_io = 1spinel_parse.cmodifiedView file
Loading diff…
test/regex_local_var.rbaddedView file
Loading diff…