Skip to content

codegen+runtime: render LuzDict contents in to_str/write (#102)#108

Merged
Elabsurdo984 merged 1 commit into
luz-lang:masterfrom
SAY-5:fix-issue-102-tostr-dict
May 9, 2026
Merged

codegen+runtime: render LuzDict contents in to_str/write (#102)#108
Elabsurdo984 merged 1 commit into
luz-lang:masterfrom
SAY-5:fix-issue-102-tostr-dict

Conversation

@SAY-5

@SAY-5 SAY-5 commented May 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #102.

to_cstr falls through for LuzDict*, returning the raw pointer. When to_str(list) or write(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 are LuzDict* 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).

ccodegen changes:

  • to_cstr routes LuzDict* through luz_to_str_dict.
  • write/print of a LuzDict* formats with %s and the new helper instead of %lld on the pointer.
  • infer_type(Call("to_str")) now returns char* so write(to_str(list)) prints with %s rather than %lld.

Repro from the issue:

nums = [1, 2, 3]
write(to_str(nums))

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).

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.
@Elabsurdo984
Elabsurdo984 merged commit 8930476 into luz-lang:master May 9, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[luzc] to_str() / write() on dict, list, or class instance produces garbage output

2 participants