Commit b57676d0

b57676d05e4aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Fix pattern match: add literal checks, array length check, NoMatchingPatternError

  • emit_pm_cond(): compute runtime match condition for each arm type (IntegerNode equality, StringNode eq, SymbolNode eq, ConstantReadNode static type check, ArrayPatternNode length check with/without rest)
  • ArrayPatternNode arms now guarded by length check so shorter/longer arrays fall through to subsequent arms
  • When no arm matches and no else clause: raise NoMatchingPatternError

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
b57676d05e4a
Parent
7fefd7a25fff

src/codegen.cmodifiedView file

@@ -12852,6 +12852,59 @@
1285212852 return 1;
1285312853 }
1285412854
12855+/* Emit the match condition for a pattern into buf as a C boolean expression.
12856+ Returns 1 if a condition was emitted (requires a runtime check),
12857+ 0 if the pattern always matches (no condition needed). */
12858+static int emit_pm_cond(Compiler *c, int pat, int t, TyKind pt, Buf *b) {
12859+ const NodeTable *nt = c->nt;
12860+ const char *pty = nt_type(nt, pat);
12861+ if (!pty) return 0;
12862+ if (!strcmp(pty, "IntegerNode")) {
12863+ long long v = (long long)nt_int(nt, pat, "value", 0);
12864+ buf_printf(b, "(_t%d == %lldLL)", t, v);
12865+ return 1;
12866+ }
12867+ if (!strcmp(pty, "StringNode")) {
12868+ const char *sv = nt_str(nt, pat, "content");
12869+ buf_printf(b, "sp_str_eq(_t%d, \"%s\")", t, sv ? sv : "");
12870+ return 1;
12871+ }
12872+ if (!strcmp(pty, "SymbolNode")) {
12873+ const char *sv = nt_str(nt, pat, "value");
12874+ if (sv) { buf_printf(b, "(_t%d == sp_sym_intern(\"%s\"))", t, sv); return 1; }
12875+ return 0;
12876+ }
12877+ if (!strcmp(pty, "ConstantReadNode")) {
12878+ const char *cn2 = nt_str(nt, pat, "name");
12879+ if (cn2) {
12880+ int yes = ty_matches_class(pt, cn2, 0);
12881+ buf_printf(b, "%d", yes > 0 ? 1 : 0);
12882+ return 1;
12883+ }
12884+ return 0;
12885+ }
12886+ if (!strcmp(pty, "ArrayPatternNode")) {
12887+ /* Length check */
12888+ int apn = 0;
12889+ nt_arr(nt, pat, "requireds", &apn);
12890+ int rest_nid = nt_ref(nt, pat, "rest");
12891+ int has_rest = (rest_nid >= 0 && nt_type(nt, rest_nid) &&
12892+ !strcmp(nt_type(nt, rest_nid), "SplatNode"));
12893+ if (has_rest)
12894+ buf_printf(b, "(_t%d && _t%d->len >= %dLL)", t, t, (long long)apn);
12895+ else
12896+ buf_printf(b, "(_t%d && _t%d->len == %dLL)", t, t, (long long)apn);
12897+ return 1;
12898+ }
12899+ if (!strcmp(pty, "CapturePatternNode")) {
12900+ /* Check inner pattern's condition if any */
12901+ int val = nt_ref(nt, pat, "value");
12902+ if (val >= 0) return emit_pm_cond(c, val, t, pt, b);
12903+ return 0;
12904+ }
12905+ return 0;
12906+}
12907+
1285512908 /* case/in (pattern match) -> bind pattern vars, optional guard check,
1285612909 then body; goto end_label to skip subsequent arms. */
1285712910 static void emit_case_match(Compiler *c, int id, Buf *b, int indent) {
@@ -12883,15 +12936,29 @@
1288312936
1288412937 emit_indent(b, indent); buf_puts(b, "{\n");
1288512938
12886- int guard = -1; /* IfNode guard expression */
12887- int array_pat = -1; /* ArrayPatternNode to destructure */
12939+ /* --- compute match condition --- */
12940+ Buf cond_buf = {NULL, 0, 0};
12941+ int has_cond = emit_pm_cond(c, pat, t, pt, &cond_buf);
12942+ /* For IfNode the pattern is always a binding (LV), guard is separate */
12943+ if (!strcmp(pty, "IfNode")) has_cond = 0;
12944+
12945+ int body_indent = indent + 1;
12946+ if (has_cond) {
12947+ emit_indent(b, indent + 1);
12948+ buf_printf(b, "if (%s) {\n", cond_buf.p ? cond_buf.p : "1");
12949+ body_indent = indent + 2;
12950+ }
12951+ free(cond_buf.p);
12952+
12953+ /* --- bindings --- */
12954+ int guard = -1;
12955+ int array_pat = -1;
1288812956
1288912957 if (!strcmp(pty, "LocalVariableTargetNode")) {
1289012958 const char *lnm = nt_str(nt, pat, "name");
12891- if (lnm) { emit_indent(b, indent + 1); buf_printf(b, "lv_%s = _t%d;\n", lnm, t); }
12959+ if (lnm) { emit_indent(b, body_indent); buf_printf(b, "lv_%s = _t%d;\n", lnm, t); }
1289212960 }
1289312961 else if (!strcmp(pty, "IfNode")) {
12894- /* `in x if guard`: statements contains LocalVariableTargetNode(s) */
1289512962 guard = nt_ref(nt, pat, "predicate");
1289612963 int bs = nt_ref(nt, pat, "statements");
1289712964 if (bs >= 0 && nt_type(nt, bs) && !strcmp(nt_type(nt, bs), "StatementsNode")) {
@@ -12901,18 +12968,17 @@
1290112968 const char *bty = nt_type(nt, body[k]);
1290212969 if (bty && !strcmp(bty, "LocalVariableTargetNode")) {
1290312970 const char *lnm = nt_str(nt, body[k], "name");
12904- if (lnm) { emit_indent(b, indent + 1); buf_printf(b, "lv_%s = _t%d;\n", lnm, t); }
12971+ if (lnm) { emit_indent(b, body_indent); buf_printf(b, "lv_%s = _t%d;\n", lnm, t); }
1290512972 }
1290612973 }
1290712974 }
1290812975 }
1290912976 else if (!strcmp(pty, "CapturePatternNode")) {
12910- /* `in PATTERN => var`: bind var = scrutinee */
1291112977 int tgt = nt_ref(nt, pat, "target");
1291212978 if (tgt >= 0 && nt_type(nt, tgt) &&
1291312979 !strcmp(nt_type(nt, tgt), "LocalVariableTargetNode")) {
1291412980 const char *lnm = nt_str(nt, tgt, "name");
12915- if (lnm) { emit_indent(b, indent + 1); buf_printf(b, "lv_%s = _t%d;\n", lnm, t); }
12981+ if (lnm) { emit_indent(b, body_indent); buf_printf(b, "lv_%s = _t%d;\n", lnm, t); }
1291612982 }
1291712983 int val = nt_ref(nt, pat, "value");
1291812984 if (val >= 0 && nt_type(nt, val) && !strcmp(nt_type(nt, val), "ArrayPatternNode"))
@@ -12921,23 +12987,24 @@
1292112987 else if (!strcmp(pty, "ArrayPatternNode")) {
1292212988 array_pat = pat;
1292312989 }
12990+ /* IntegerNode/StringNode/SymbolNode/ConstantReadNode: value-only, no binding */
1292412991
12925- /* Destructure ArrayPatternNode */
12992+ /* --- ArrayPatternNode destructuring --- */
1292612993 if (array_pat >= 0) {
1292712994 TyKind arr_t = ty_is_array(pt) ? pt : TY_INT_ARRAY;
1292812995 const char *k = (arr_t == TY_POLY_ARRAY) ? "Poly" : array_kind(arr_t);
1292912996 if (!k) k = "Int";
1293012997 int apn = 0;
1293112998 const int *reqs = nt_arr(nt, array_pat, "requireds", &apn);
12999+ int rest_nid = nt_ref(nt, array_pat, "rest");
1293213000 for (int i = 0; i < apn; i++) {
1293313001 const char *lty2 = nt_type(nt, reqs[i]);
1293413002 if (!lty2 || strcmp(lty2, "LocalVariableTargetNode")) continue;
1293513003 const char *lnm = nt_str(nt, reqs[i], "name");
1293613004 if (!lnm) continue;
12937- emit_indent(b, indent + 1);
13005+ emit_indent(b, body_indent);
1293813006 buf_printf(b, "lv_%s = sp_%sArray_get(_t%d, %dLL);\n", lnm, k, t, (long long)i);
1293913007 }
12940- int rest_nid = nt_ref(nt, array_pat, "rest");
1294113008 if (rest_nid >= 0 && nt_type(nt, rest_nid) &&
1294213009 !strcmp(nt_type(nt, rest_nid), "SplatNode")) {
1294313010 int inner = nt_ref(nt, rest_nid, "expression");
@@ -12945,28 +13012,29 @@
1294513012 !strcmp(nt_type(nt, inner), "LocalVariableTargetNode")) {
1294613013 const char *rnm = nt_str(nt, inner, "name");
1294713014 if (rnm) {
12948- emit_indent(b, indent + 1);
12949- buf_printf(b, "if (_t%d) lv_%s = sp_%sArray_slice(_t%d, %dLL, _t%d->len - %dLL);\n",
12950- t, rnm, k, t, (long long)apn, t, (long long)apn);
13015+ emit_indent(b, body_indent);
13016+ buf_printf(b, "lv_%s = sp_%sArray_slice(_t%d, %dLL, _t%d->len - %dLL);\n",
13017+ rnm, k, t, (long long)apn, t, (long long)apn);
1295113018 }
1295213019 }
1295313020 }
1295413021 }
1295513022
12956- /* Emit body with optional guard */
13023+ /* --- body with optional guard --- */
1295713024 if (guard >= 0) {
12958- emit_indent(b, indent + 1); buf_puts(b, "if (");
13025+ emit_indent(b, body_indent); buf_puts(b, "if (");
1295913026 emit_expr(c, guard, b);
1296013027 buf_puts(b, ") {\n");
12961- emit_stmts(c, stmts, b, indent + 2);
12962- emit_indent(b, indent + 2); buf_printf(b, "goto _pm_%d;\n", lbl);
12963- emit_indent(b, indent + 1); buf_puts(b, "}\n");
13028+ emit_stmts(c, stmts, b, body_indent + 1);
13029+ emit_indent(b, body_indent + 1); buf_printf(b, "goto _pm_%d;\n", lbl);
13030+ emit_indent(b, body_indent); buf_puts(b, "}\n");
1296413031 }
1296513032 else {
12966- emit_stmts(c, stmts, b, indent + 1);
12967- emit_indent(b, indent + 1); buf_printf(b, "goto _pm_%d;\n", lbl);
13033+ emit_stmts(c, stmts, b, body_indent);
13034+ emit_indent(b, body_indent); buf_printf(b, "goto _pm_%d;\n", lbl);
1296813035 }
1296913036
13037+ if (has_cond) { emit_indent(b, indent + 1); buf_puts(b, "}\n"); }
1297013038 emit_indent(b, indent); buf_puts(b, "}\n");
1297113039 }
1297213040
@@ -12975,6 +13043,11 @@
1297513043 emit_stmts(c, nt_ref(nt, else_clause, "statements"), b, indent + 1);
1297613044 emit_indent(b, indent); buf_puts(b, "}\n");
1297713045 }
13046+ else {
13047+ /* No matching arm and no else: raise NoMatchingPatternError */
13048+ emit_indent(b, indent);
13049+ buf_printf(b, "sp_raise_cls(\"NoMatchingPatternError\", \"no pattern matched\");\n");
13050+ }
1297813051
1297913052 emit_indent(b, indent); buf_printf(b, "_pm_%d:;\n", lbl);
1298013053 }