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_temp
27904+ # For typed containers (int_array, sym_array, str_array, hash
27905+ # variants, range, time, user-class obj_*, ...) the temp must be
27906+ # declared with the receiver's actual C type so the pattern arms
27907+ # can dereference `tmp->len` / `tmp->data[i]` and per-variant hash
27908+ # lookups can pass the right pointer type. Without this the temp
27909+ # was declared as `mrb_int` for everything non-poly/string/float
27910+ # and any pattern shape touching the scrutinee structurally
27911+ # crashed at cc time. Primitive shapes (int/bool/symbol/nil) keep
27912+ # 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 = 1
27921+ 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 else
27907- if pred_type == "string"
27908- emit(" const char *" + tmp + " = " + pred_val + ";")
27909- else
27910- if pred_type == "float"
27911- emit(" mrb_float " + tmp + " = " + pred_val + ";")
27912- elsif pred_type == "bigint"
27913- @needs_bigint = 1
27914- emit(" mrb_int " + tmp + " = sp_bigint_to_int((sp_Bigint *)" + pred_val + ");")
27915- pred_type = "int"
27916- else
27917- emit(" mrb_int " + tmp + " = " + pred_val + ";")
27918- end
27919- end
27926+ emit(" " + c_type(pred_type) + " " + tmp + " = " + pred_val + ";")
2792027927 end
2792127928 conds = parse_id_list(@nd_conditions[nid])
2792227929 k = 0
@@ -28008,11 +28015,66 @@
2800828015 end
2800928016 return tmp + " == 0"
2801028017 end
28018+ if pt == "SymbolNode"
28019+ # `case sym in :foo` -- literal symbol pattern. Without this arm
28020+ # the dispatch fell through to the trailing "1" fallback and any
28021+ # 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+ end
28026+ return "(sp_sym)(" + tmp + ") == (" + sym_lit + ")"
28027+ end
28028+ if pt == "FloatNode"
28029+ # `case f in 3.14` -- literal float pattern. Same fallthrough
28030+ # 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+ end
28035+ return tmp + " == " + fval
28036+ end
2801128037 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 end
28042+ # PinnedVariableNode / PinnedExpressionNode: `case x in ^var` /
28043+ # `in ^(expr)`. Both share the `@nd_expression` slot -- the parse
28044+ # layer routes the variable into the expression field so codegen
28045+ # treats them uniformly. Match by `==` against the pinned value;
28046+ # poly-tagged scrutinees get both tag and value checks so a pinned
28047+ # Integer doesn't spuriously match against a string with the same
28048+ # 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+ end
28056+ if pinned_type == "string"
28057+ return "(" + tmp + ".tag == SP_TAG_STR && strcmp(" + tmp + ".v.s, " + pinned + ") == 0)"
28058+ end
28059+ if pinned_type == "symbol"
28060+ return "(" + tmp + ".tag == SP_TAG_SYM && (sp_sym)" + tmp + ".v.i == (" + pinned + "))"
28061+ end
28062+ if pinned_type == "float"
28063+ return "(" + tmp + ".tag == SP_TAG_FLT && " + tmp + ".v.f == (" + pinned + "))"
28064+ end
28065+ if pinned_type == "bool"
28066+ return "(" + tmp + ".tag == SP_TAG_BOOL && " + tmp + ".v.b == (" + pinned + "))"
28067+ end
28068+ if pinned_type == "nil"
28069+ return tmp + ".tag == SP_TAG_NIL"
28070+ end
28071+ return "0"
28072+ end
28073+ if pinned_type == "string" && pred_type == "string"
28074+ return "strcmp(" + tmp + ", " + pinned + ") == 0"
28075+ end
28076+ return "(" + tmp + ") == (" + pinned + ")"
28077+ end
2801628078 "1"
2801728079 end
2801828080