Add if (var x = expr) and map[k].field op= rhs#19
Open
congwang-mk wants to merge 7 commits intomainfrom
Open
Conversation
Signed-off-by: Cong Wang <cwang@multikernel.io>
Signed-off-by: Cong Wang <cwang@multikernel.io>
Signed-off-by: Cong Wang <cwang@multikernel.io>
… and SPEC Signed-off-by: Cong Wang <cwang@multikernel.io>
Signed-off-by: Cong Wang <cwang@multikernel.io>
Signed-off-by: Cong Wang <cwang@multikernel.io>
…resence checks Signed-off-by: Cong Wang <cwang@multikernel.io>
00ad003 to
d5f25c1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two new statement forms for ergonomic map access, plus the doc and test work pinning them down.
if (var x = expr) { ... } else { ... }— declaration-as-condition. Bindsxonly inside the truthy branch; the branch is taken iffexprproduces a present value (map hit, non-null pointer return). Lowers to a singlebpf_map_lookup_elemplus a null check; for struct-valued maps the body's field operations mutate the entry in place via the underlying lookup pointer (no manual write-back).m[k].field op= rhs— compound assignment on a struct-field of a map value. Lowers to a presence-checkedif (p) { p->field = p->field op rhs; }against the same single lookup. Absent entries are a no-op (no creation), distinguishing it from the existing scalarm[k] op= rhswhich creates on first use.Bundled deliverables:
examples/maps_demo.ksandexamples/map_operations_demo.ks: rewrite the read-modify-write blocks to use the new idiom (drops 13 LoC, removes redundant write-backs).README.md: replace the misleadinglookup_or_createsnippet with paired examples; add an "Ergonomic map idioms" bullet to the solution checklist.SPEC.md: new §6.2.5 (compound assignment with map indexing — scalar and struct-field forms with full semantics: LHS shape, type rules, presence/creation semantics, lowering shape), new §7.5.1.2 (declaration-as-condition with single-evaluation, scoping, and lowering);assignment_statementandif_statementEBNF productions expanded from the prior shorthand into the seven forms the parser actually implements. Also fixes the §6.2.3 example, which usedstats.field += 1on a value-boundstats— a shape the parser has never accepted.tests/test_iflet.ml(new): 10 tests across parse, type-check, and codegen-string layers — covers struct/scalar bindings, else,else if (var ...)chains, binding-leak rejections (then-branch only), and the in-place-mutation codegen shape.tests/test_compound_index_assignment.ml: extend the Phase 2 test with a codegen-string check pinning the synthetic-pointer-binding path that the codegen-fix in this PR repaired (verified by temporarily reverting the fix — test flips red on "synthetic binding does NOT use deref-load init").84 alcotest suites pass (was 83); the new
test_ifletsuite is the +1.Test plan
eval \$(opam env) && dune build && dune runtest --force— 84 suites greencd /tmp && cp <example>.ks . && kernelscript compile <example>.ks -o out && cd out && make— example end-to-end build formaps_demo.ks(clean) and a minimalm[k].field += 1source (clean)bpf_map_lookup_elemhappens once, presence is checked againstNULL, and the mutation isptr->field = ...tests/test_iflet.ml::codegen_struct_value_binding_shape)🤖 Generated with Claude Code