codegen+runtime: render LuzDict contents in to_str/write (#102)#108
Merged
Conversation
to_cstr fell through for LuzDict* and printf("%s", ptr) read from
the pointer address, producing garbage. Add luz_to_str_dict to the
runtime, route to_str and write through it, and fix to_str's inferred
return type so the result is printed with %s instead of %lld.
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.
Fixes #102.
to_cstrfalls through forLuzDict*, returning the raw pointer. Whento_str(list)orwrite(list)then formats it with%s/%lld, it dereferences the pointer as a C string, producing garbage. Same for dicts and class instances since all three areLuzDict*at runtime.Adds
luz_to_str_dict(LuzDict*)to the runtime. It walks the entries and renders the value as a list[1, 2, 3]when keys are the contiguous range"0".."size-1", otherwise as a dict{"k": v, ...}. Class instances render with their__class__-keyed fields (or{}if empty).ccodegenchanges:to_cstrroutesLuzDict*throughluz_to_str_dict.write/printof aLuzDict*formats with%sand the new helper instead of%lldon the pointer.infer_type(Call("to_str"))now returnschar*sowrite(to_str(list))prints with%srather than%lld.Repro from the issue:
Before: garbage / undefined.
After:
[1, 2, 3].Also covered:
write(nums)directly,write({"a": 1}), and lists of strings (["hello", "world"]).Adds three regression tests in
test_ccodegen.cpp. Full ctest suite passes (184/184).