Commit c8272dac
c8272dacd68eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge PR #653: pattern matching for ^var / ^(expr) pin patterns
feat(parse,codegen): pattern matching for ^var / ^(expr) pin patterns
Details
- Commit
- c8272dacd68e
- Parent
- c2b696d6466b
- Merge parent
- e67eb2434d86
spinel_codegen.rbmodifiedView file
@@ -27901,22 +27901,29 @@
2790127901
pred_type = infer_type(pred)2790227902
pred_val = compile_expr(pred)2790327903
tmp = new_temp27904+
# For typed containers (int_array, sym_array, str_array, hash27905+
# variants, range, time, user-class obj_*, ...) the temp must be27906+
# declared with the receiver's actual C type so the pattern arms27907+
# can dereference `tmp->len` / `tmp->data[i]` and per-variant hash27908+
# lookups can pass the right pointer type. Without this the temp27909+
# was declared as `mrb_int` for everything non-poly/string/float27910+
# and any pattern shape touching the scrutinee structurally27911+
# crashed at cc time. Primitive shapes (int/bool/symbol/nil) keep27912+
# the historic `mrb_int` slot.2790427913
if pred_type == "poly"2790527914
emit(" sp_RbVal " + tmp + " = " + pred_val + ";")27915+
elsif pred_type == "string"27916+
emit(" const char *" + tmp + " = " + pred_val + ";")27917+
elsif pred_type == "float"27918+
emit(" mrb_float " + tmp + " = " + pred_val + ";")27919+
elsif pred_type == "bigint"27920+
@needs_bigint = 127921+
emit(" mrb_int " + tmp + " = sp_bigint_to_int((sp_Bigint *)" + pred_val + ");")27922+
pred_type = "int"27923+
elsif pred_type == "int" || pred_type == "bool" || pred_type == "symbol" || pred_type == "nil" || pred_type == ""27924+
emit(" mrb_int " + tmp + " = " + pred_val + ";")2790627925
else27907-
if pred_type == "string"27908-
emit(" const char *" + tmp + " = " + pred_val + ";")27909-
else27910-
if pred_type == "float"27911-
emit(" mrb_float " + tmp + " = " + pred_val + ";")27912-
elsif pred_type == "bigint"27913-
@needs_bigint = 127914-
emit(" mrb_int " + tmp + " = sp_bigint_to_int((sp_Bigint *)" + pred_val + ");")27915-
pred_type = "int"27916-
else27917-
emit(" mrb_int " + tmp + " = " + pred_val + ";")27918-
end27919-
end27926+
emit(" " + c_type(pred_type) + " " + tmp + " = " + pred_val + ";")2792027927
end2792127928
conds = parse_id_list(@nd_conditions[nid])2792227929
k = 0@@ -28008,11 +28015,66 @@
2800828015
end2800928016
return tmp + " == 0"2801028017
end28018+
if pt == "SymbolNode"28019+
# `case sym in :foo` -- literal symbol pattern. Without this arm28020+
# the dispatch fell through to the trailing "1" fallback and any28021+
# symbol pattern silently always matched.28022+
sym_lit = compile_symbol_literal(@nd_content[pat_id])28023+
if pred_type == "poly"28024+
return "(" + tmp + ".tag == SP_TAG_SYM && (sp_sym)" + tmp + ".v.i == (" + sym_lit + "))"28025+
end28026+
return "(sp_sym)(" + tmp + ") == (" + sym_lit + ")"28027+
end28028+
if pt == "FloatNode"28029+
# `case f in 3.14` -- literal float pattern. Same fallthrough28030+
# silent-match bug as SymbolNode.28031+
fval = @nd_value[pat_id]28032+
if pred_type == "poly"28033+
return "(" + tmp + ".tag == SP_TAG_FLT && " + tmp + ".v.f == " + fval + ")"28034+
end28035+
return tmp + " == " + fval28036+
end2801128037
if pt == "AlternationPatternNode"2801228038
left = compile_in_pattern(@nd_left[pat_id], tmp, pred_type)2801328039
right = compile_in_pattern(@nd_right[pat_id], tmp, pred_type)2801428040
return "(" + left + " || " + right + ")"2801528041
end28042+
# PinnedVariableNode / PinnedExpressionNode: `case x in ^var` /28043+
# `in ^(expr)`. Both share the `@nd_expression` slot -- the parse28044+
# layer routes the variable into the expression field so codegen28045+
# treats them uniformly. Match by `==` against the pinned value;28046+
# poly-tagged scrutinees get both tag and value checks so a pinned28047+
# Integer doesn't spuriously match against a string with the same28048+
# underlying byte pattern.28049+
if pt == "PinnedVariableNode" || pt == "PinnedExpressionNode"28050+
pinned = compile_expr(@nd_expression[pat_id])28051+
pinned_type = infer_type(@nd_expression[pat_id])28052+
if pred_type == "poly"28053+
if pinned_type == "int"28054+
return "(" + tmp + ".tag == SP_TAG_INT && " + tmp + ".v.i == (" + pinned + "))"28055+
end28056+
if pinned_type == "string"28057+
return "(" + tmp + ".tag == SP_TAG_STR && strcmp(" + tmp + ".v.s, " + pinned + ") == 0)"28058+
end28059+
if pinned_type == "symbol"28060+
return "(" + tmp + ".tag == SP_TAG_SYM && (sp_sym)" + tmp + ".v.i == (" + pinned + "))"28061+
end28062+
if pinned_type == "float"28063+
return "(" + tmp + ".tag == SP_TAG_FLT && " + tmp + ".v.f == (" + pinned + "))"28064+
end28065+
if pinned_type == "bool"28066+
return "(" + tmp + ".tag == SP_TAG_BOOL && " + tmp + ".v.b == (" + pinned + "))"28067+
end28068+
if pinned_type == "nil"28069+
return tmp + ".tag == SP_TAG_NIL"28070+
end28071+
return "0"28072+
end28073+
if pinned_type == "string" && pred_type == "string"28074+
return "strcmp(" + tmp + ", " + pinned + ") == 0"28075+
end28076+
return "(" + tmp + ") == (" + pinned + ")"28077+
end2801628078
"1"2801728079
end2801828080
spinel_parse.cmodifiedView file
Loading diff…
test/pattern_pin.rbaddedView file
Loading diff…
test/pattern_pin.rb.expectedaddedView file
Loading diff…