Commit 2f8bb457

2f8bb45747b7Merged

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

Description

Merge pull request #291 from ryanseys/rs-node-call-writes

feat(parse,codegen): support Call{Operator,And,Or}WriteNode (3 nodes)

Details

Commit
2f8bb45747b7
Parent
0b18fb801892
Merge parent
83536b81e8fe

spinel_codegen.rbmodifiedView file

@@ -25644,6 +25644,18 @@
2564425644 compile_index_or_assign(nid)
2564525645 return
2564625646 end
25647+ if t == "CallOperatorWriteNode"
25648+ compile_call_assign_typed(nid, "Operator")
25649+ return
25650+ end
25651+ if t == "CallAndWriteNode"
25652+ compile_call_assign_typed(nid, "And")
25653+ return
25654+ end
25655+ if t == "CallOrWriteNode"
25656+ compile_call_assign_typed(nid, "Or")
25657+ return
25658+ end
2564725659 if t == "IfNode"
2564825660 compile_if_stmt(nid)
2564925661 return
@@ -29311,6 +29323,67 @@
2931129323 end
2931229324 end
2931329325
29326+ # `obj.attr <op>= val` family helper -- builds the
29327+ # temp-receiver-once C block for typed-instance receivers backed by
29328+ # an attr_accessor (or struct field). For non-attr receivers, we
29329+ # exit with a precise error rather than fall through to an
29330+ # incorrect emission. The caller passes a fragment that takes the
29331+ # temp variable name and returns the C body to execute.
29332+ #
29333+ # Spec note: even for the typed-attr-accessor case the receiver is
29334+ # evaluated exactly ONCE -- the source `obj.bar += val` is
29335+ # `tmp = obj; tmp.bar = tmp.bar + val`, NOT `obj.bar = obj.bar + val`.
29336+ # The temp pattern matters when the receiver expression has side
29337+ # effects (e.g. `next_holder().attr += 1`).
29338+ def compile_call_assign_typed(nid, kind)
29339+ recv = @nd_receiver[nid]
29340+ rt = infer_type(recv)
29341+ bname = @nd_name[nid] # parser emits read_name as "name"
29342+ if rt.length <= 4 || rt[0, 4] != "obj_"
29343+ $stderr.puts "Spinel: Call" + kind + "WriteNode requires a typed instance receiver with attr_accessor; got receiver type \"" + rt + "\""
29344+ exit(1)
29345+ end
29346+ cname = rt[4, rt.length - 4]
29347+ ci = find_class_idx(cname)
29348+ if ci < 0 || cls_has_attr_writer(ci, bname) == 0
29349+ $stderr.puts "Spinel: Call" + kind + "WriteNode for `." + bname + "` requires attr_accessor (or struct field) on class " + cname
29350+ exit(1)
29351+ end
29352+ rc = compile_expr_gc_rooted(recv)
29353+ val = compile_expr(@nd_expression[nid])
29354+ tt = new_temp
29355+ field = "iv_" + bname
29356+ if kind == "Operator"
29357+ op = @nd_binop[nid]
29358+ ivar_t = cls_ivar_type(ci, "@" + bname)
29359+ if op == "+" && ivar_t == "string" && infer_type(@nd_expression[nid]) == "string"
29360+ # String-valued attr with `+=` becomes sp_str_concat, mirroring
29361+ # the LocalVariableOperatorWriteNode arm at line 21786.
29362+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29363+ tt + "->" + field + " = sp_str_concat(" + tt + "->" + field + ", (" + val + ")); }")
29364+ return
29365+ end
29366+ if op == "%"
29367+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29368+ tt + "->" + field + " = sp_imod(" + tt + "->" + field + ", (" + val + ")); }")
29369+ else
29370+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29371+ tt + "->" + field + " = " + tt + "->" + field + " " + op + " (" + val + "); }")
29372+ end
29373+ return
29374+ end
29375+ if kind == "And"
29376+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29377+ "if (" + tt + "->" + field + ") " + tt + "->" + field + " = (" + val + "); }")
29378+ return
29379+ end
29380+ if kind == "Or"
29381+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29382+ "if (!" + tt + "->" + field + ") " + tt + "->" + field + " = (" + val + "); }")
29383+ return
29384+ end
29385+ end
29386+
2931429387 # `a[i] &&= val` -- read once, conditionally write once. Mirrors
2931529388 # compile_index_op_assign's per-receiver-type dispatch but routes
2931629389 # the new value through a C `if (cur)` guard instead of an