Repository navigation
Add support for function-like $define macros - #554
Conversation
Greptile SummaryThis PR implements function-like Key areas reviewed:
Confidence Score: 5/5Safe to merge — no P0 or P1 issues found; the single finding is a minor consistency nit on an error-recovery path that does not affect correctness. All previously raised concerns (done_set chain expansion depth, trailing-comma double-put) have been resolved. The remaining comment is P2-only: an inconsistent but harmless omission of &pos restoration on the arity-error path. The feature logic, recursion guard, argument scanning, and table integration are all correct. uni/unicon/preproce.icn — specifically the arity-mismatch path in preproc_expand_fmacro_call (lines 687–690), though it is not a blocker. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["preproc_scan_directive()\n$define NAME..."] --> B["preproc_fmacro_parse_error := 0"]
B --> C{"preproc_scan_define_fmacro()"}
C -->|"succeeds: NAME(p1,p2) body"| D["Store in preproc_fun_table\nDelete from preproc_sym_table"]
C -->|"fails, no parse error\n(no leading '(')"| E{"preproc_scan_define_value()"}
C -->|"fails, parse error\n(malformed params)"| F["Error already reported\nNo fallback"]
E -->|"succeeds"| G["Store in preproc_sym_table\nDelete from preproc_fun_table"]
E -->|"fails"| H["No definition created"]
subgraph Expansion ["Text Expansion - preproc_scan_text()"]
I["Scan identifier 'IDENT'"] --> J{"In preproc_fun_table?"}
J -->|yes| K{"Check done_set\n(recursion guard)"}
K -->|"not recursive"| L["preproc_expand_fmacro_call()"]
L --> M["preproc_opt_space() + ='('"]
M -->|"no '(' found"| N["Restore &pos, fail\n→ try object-like"]
M -->|"'(' found"| O["preproc_scan_macro_args()"]
O --> P{"arity matches?"}
P -->|no| Q["Error: wrong number of args\nfail"]
P -->|yes| R["preproc_subst_fmacro()"]
R --> S["expanded ? preproc_scan_text(done_set + ident)"]
K -->|"recursive"| T["skip fmacro\n→ try object-like"]
J -->|no| U{"In preproc_sym_table?"}
U -->|yes| V["Object-like expansion"]
U -->|no| W["Leave as plain text"]
end
Prompt To Fix All With AIThis is a comment left during a code review.
Path: uni/unicon/preproce.icn
Line: 687-690
Comment:
**`&pos` not restored before `fail` on arity mismatch**
The other two early-exit paths in this function both restore `&pos := start_pos` before failing, making it easy to reason that the caller's scanner position is always in a known state when the function returns `fail`. The arity-mismatch path skips that restore:
```icn
if *args ~== *fmacro.params then {
preproc_error("wrong number of args in macro call to " || name)
fail -- &pos is still pointing after the closing ')'
}
```
This is not a runtime bug — `preproc_scan_text` does `&pos := save_pos` after any failure, so error recovery still works correctly. However, adding the restore here would make all failure paths consistent and reduce the cognitive load for anyone auditing the function later:
```suggestion
if *args ~== *fmacro.params then {
preproc_error("wrong number of args in macro call to " || name)
&pos := start_pos
fail
}
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (10): Last reviewed commit: "tests/unicon: add macro preprocessor cov..." | Re-trigger Greptile |
|
The code looks awesome to me. I will review more when Greptile is happy, or when its issues are debunked. I would prefer not to have empty then-parts if they are not necessary, by reversing logic and using "not", etc. I would imagine that this has been tested with too many and too few parameters, omitted parameters, etc but I didn't see those tests. I was glad to see tests with macro calls inside parameters of other macro calls, as that is a common source of macro trouble. Actually, one might check out the worst examples of macro hell in C and see what this code does with similar cases. I would suggest that trickier parts of the code, if there are any, might deserve a bit more commenting. Usually something as complex as macro parameters has some trickier parts. |
c10738a to
232c555
Compare
f67b37f to
78b851c
Compare
I fixed a few rounds of greptile reviews, I think it is happy now. I added a few comments, and expanded the test to cover more cases. |
|
@greptile, your assessment is wrong in this case: This is a comment left during a code review. Comment: In Icon/Unicon,
expands to This means that if the preprocessor were to silently accept invalid input instead of emitting the expected diagnostic, all five error-path tests would still "pass" and "arity-checks-ok" would still be printed. The fix is to add Correction: In Unicon, a procedure doesn't |
|
You're absolutely right - I apologize for the incorrect assessment. In Unicon, procedures that fall off the end fail implicitly rather than returning
The assertions properly catch cases where the compiler silently accepts invalid input instead of producing the expected diagnostic. Thank you for the correction on Unicon's semantics. |
bc6142c to
6d1d720
Compare
|
@greptile, I did push an update to fix the last two minor issues you pointed out. |
Extend the Unicon preprocessor to support function-style macro definitions and calls (for example `$define sum(x,y) x+y` and `sum(1,2)`). Keep object-like macro behavior unchanged while adding argument parsing, substitution, recursion guards, and consistent handling across: `define`, `undef`, `ifdef`, and `ifndef`. Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Add a dedicated regression test for function-like $define macros, including nested expansion, whitespace around invocation, zero-argument macros, and ifdef/undef behavior. Cover tricky argument/nesting cases and add dedicated arity-failure fixtures that verify too-few, too-many, omitted, empty-middl, and duplicate-parameter cases fail with the expected error. Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
|
@greptile, I fixed the two-char \^x caret-escape sequence. |
No description provided.