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
return2564625646
end25647+
if t == "CallOperatorWriteNode"25648+
compile_call_assign_typed(nid, "Operator")25649+
return25650+
end25651+
if t == "CallAndWriteNode"25652+
compile_call_assign_typed(nid, "And")25653+
return25654+
end25655+
if t == "CallOrWriteNode"25656+
compile_call_assign_typed(nid, "Or")25657+
return25658+
end2564725659
if t == "IfNode"2564825660
compile_if_stmt(nid)2564925661
return@@ -29311,6 +29323,67 @@
2931129323
end2931229324
end2931329325
29326+
# `obj.attr <op>= val` family helper -- builds the29327+
# temp-receiver-once C block for typed-instance receivers backed by29328+
# an attr_accessor (or struct field). For non-attr receivers, we29329+
# exit with a precise error rather than fall through to an29330+
# incorrect emission. The caller passes a fragment that takes the29331+
# temp variable name and returns the C body to execute.29332+
#29333+
# Spec note: even for the typed-attr-accessor case the receiver is29334+
# evaluated exactly ONCE -- the source `obj.bar += val` is29335+
# `tmp = obj; tmp.bar = tmp.bar + val`, NOT `obj.bar = obj.bar + val`.29336+
# The temp pattern matters when the receiver expression has side29337+
# 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+
end29346+
cname = rt[4, rt.length - 4]29347+
ci = find_class_idx(cname)29348+
if ci < 0 || cls_has_attr_writer(ci, bname) == 029349+
$stderr.puts "Spinel: Call" + kind + "WriteNode for `." + bname + "` requires attr_accessor (or struct field) on class " + cname29350+
exit(1)29351+
end29352+
rc = compile_expr_gc_rooted(recv)29353+
val = compile_expr(@nd_expression[nid])29354+
tt = new_temp29355+
field = "iv_" + bname29356+
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, mirroring29361+
# the LocalVariableOperatorWriteNode arm at line 21786.29362+
emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +29363+
tt + "->" + field + " = sp_str_concat(" + tt + "->" + field + ", (" + val + ")); }")29364+
return29365+
end29366+
if op == "%"29367+
emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +29368+
tt + "->" + field + " = sp_imod(" + tt + "->" + field + ", (" + val + ")); }")29369+
else29370+
emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +29371+
tt + "->" + field + " = " + tt + "->" + field + " " + op + " (" + val + "); }")29372+
end29373+
return29374+
end29375+
if kind == "And"29376+
emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +29377+
"if (" + tt + "->" + field + ") " + tt + "->" + field + " = (" + val + "); }")29378+
return29379+
end29380+
if kind == "Or"29381+
emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +29382+
"if (!" + tt + "->" + field + ") " + tt + "->" + field + " = (" + val + "); }")29383+
return29384+
end29385+
end29386+
2931429387
# `a[i] &&= val` -- read once, conditionally write once. Mirrors2931529388
# compile_index_op_assign's per-receiver-type dispatch but routes2931629389
# the new value through a C `if (cur)` guard instead of anspinel_parse.cmodifiedView file
Loading diff…
test/call_and_write.rbaddedView file
Loading diff…
test/call_and_write.rb.expectedaddedView file
Loading diff…
test/call_op_write.rbaddedView file
Loading diff…
test/call_op_write.rb.expectedaddedView file
Loading diff…
test/call_or_write.rbaddedView file
Loading diff…
test/call_or_write.rb.expectedaddedView file
Loading diff…