Hi all,
I'd like to ask whether there's interest in an opt-in compiler switch that lets && and || be used as the logical operators .AND. and .OR.. I have a working implementation and would prefer to hear your view before sending a pull request.
Motivation
&& and || are the logical operators in C, C++, Java, JavaScript, C#, Python-adjacent code and so on. Developers coming to Harbour from those languages trip over the fact that && is a line comment here. The switch would make Harbour code look more familiar to them. It would be strictly opt-in.
Proposed behavior
- -kL enables it, -kL- disables it, and #pragma -kL+ / #pragma -kL- switch it per file. The default is off.
- When on, && is .AND., || is .OR., and && is no longer a comment (use // instead).
- {|| still starts an empty codeblock parameter list. {|x,y| x || y } uses || as OR.
harbour
#pragma -kL+
IF lA && ! lB || lC // same as: lA .AND. ! lB .OR. lC
...
ENDIF
Implementation (small, preprocessor-only)
- New HB_COMPFLAG_CLOGIC and a fCLogic state in the preprocessor (hb_pp_setCLogic()).
- The tokenizer (src/pp/ppcore.c) emits the existing .AND. and .OR. tokens, so the lexer and grammar are unchanged and -p output stays valid for a compiler without the switch.
- Two spots needed care: a keyword directly followed by && (a&&b, where a& would otherwise scan as macro-text), and the {|| rule above.
- Touches about 10 files: hbcomp.h, hbpp.h, ppcore.c, ppcomp.c, cmdcheck.c, hbusage.c, doc/en/compiler.txt, and utils/hbtest (new rt_clogic.prg with 19 test calls).
Verification
hbtest rebuilt with the modified compiler passes 4880 of 4880 (the previous 4861 plus the new ones). I only tested on macOS with clang, so Linux and Windows builds are untested. The standard headers in include/ contain no &&, so they are unaffected when the switch is on.
Known limitations and trade-offs
- && comments stop working under the switch, so a project has to use // consistently. Third-party .ch files that use && comments would break if included in a -kL build.
- The runtime macro compiler (&("a && b")) is not covered.
- It adds one more behavior difference between Harbour and Clipper/xHarbour source. Because it is off by default and uses a normal -k switch, existing code is unaffected.
Questions
1. Is this something you'd want upstream at all, or would you prefer it stays a local patch?
2. If yes, is -kL a good letter, and is a -k switch the right place for it?
3. Should the macro compiler be covered for consistency?
4. Would you rather see && comments kept working in some form, for example only when followed by a space and a non-expression?
The patch is a single commit and I can send it as a PR against master or as a patch file.
Lorenzo