Skip to content

Commit 812b206

Browse files
committed
Registry fixes: postfix++→prefix, float f-suffix, final→constexpr, color var shadowing
CodeGen: - PostfixExpr ++/-- normalized to prefix form universally (Rule C) - Float literals without f/F suffix get f appended (Rule D) Parser: - Java 'final' qualifier consumed and mapped to constexpr (Rule E) CppBuild: - fixColorTypes: skip single-letter variables and for-loop counters from int→color rewrite (fixes Problem 1.1: c/r shadowing by color type) - E0006: single-letter and two-letter-lowercase identifiers skipped
1 parent 6e00c77 commit 812b206

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

mode/CppMode.jar

460 Bytes
Binary file not shown.

src/java/CodeGen.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,8 @@ public static String renderTypeRef(TypeRef t) {
836836
if (nt.baseName().startsWith("decltype(")) return nt.baseName();
837837
StringBuilder sb = new StringBuilder();
838838
if (nt.isConst()) sb.append("const ");
839+
// Dependent type: "typename T::value_type" -- prepend typename
840+
if (nt.baseName().contains("::") && !nt.baseName().startsWith("std::") && !nt.baseName().contains("(")) sb.append("typename ");
839841
sb.append(nt.baseName());
840842
if (!nt.templateArgs().isEmpty()) {
841843
sb.append('<');
@@ -878,7 +880,17 @@ public static String renderTypeRef(TypeRef t) {
878880
// =====================================================================
879881

880882
public static String renderExpr(Expr e) {
881-
if (e instanceof Literal lit) return lit.text();
883+
if (e instanceof Literal lit) {
884+
String txt = lit.text();
885+
// Append f suffix to float literals without one -- prevents
886+
// double/float ambiguity on overloaded Processing API functions
887+
if (lit.kind() == Literal.Kind.FLOAT
888+
&& !txt.endsWith("f") && !txt.endsWith("F")
889+
&& !txt.endsWith("d") && !txt.endsWith("D")) {
890+
txt = txt + "f";
891+
}
892+
return txt;
893+
}
882894
if (e instanceof Identifier id) return id.name();
883895
if (e instanceof ScopedName sn) return sn.joined();
884896

@@ -946,7 +958,11 @@ public static String renderExpr(Expr e) {
946958
return u.op() + renderExpr(u.operand());
947959
}
948960
if (e instanceof PostfixExpr p) {
949-
return renderExpr(p.operand()) + p.op();
961+
// Normalize postfix ++/-- to prefix: better C++ style, avoids
962+
// postfix operator resolution issues inside user class bodies
963+
if (p.op().equals("++") || p.op().equals("--"))
964+
return p.op() + renderExpr(p.operand());
965+
return renderExpr(p.operand()) + p.op(); // "..." pack expansion etc.
950966
}
951967
if (e instanceof AssignExpr a) {
952968
return renderExpr(a.target()) + " = " + renderExpr(a.value());

src/java/CppBuild.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,16 @@ private String fixColorTypes(String code) {
22122212
code = sb3.toString();
22132213

22142214
// Pass 4: replace int varname declarations with color varname for all colorVars
2215+
// Skip variables declared as loop counters or with explicit int literals
2216+
// to avoid mistyping loop vars like "for (int c = 0; ...)" as color
2217+
java.util.regex.Pattern forLoopDecl = java.util.regex.Pattern.compile(
2218+
"\\bfor\\s*\\(\\s*int\\s+(\\w+)");
2219+
java.util.Set<String> loopVars = new java.util.HashSet<>();
2220+
java.util.regex.Matcher lm = forLoopDecl.matcher(code);
2221+
while (lm.find()) loopVars.add(lm.group(1));
22152222
for (String v : colorVars) {
2223+
if (loopVars.contains(v)) continue; // skip loop counters
2224+
if (v.length() == 1) continue; // skip single-letter vars (likely loop counters or params)
22162225
code = code.replaceAll("\\bint\\s+(" + Pattern.quote(v) + ")\\b", "color $1");
22172226
}
22182227

@@ -2418,6 +2427,10 @@ private void checkForJavaStaticCallSyntax(String code, RunnerListener listener)
24182427
String firstName = t0.text();
24192428
if (firstName.isEmpty() || !Character.isUpperCase(firstName.charAt(0))) continue;
24202429
if (instanceTypes.contains(firstName)) continue; // known instance type
2430+
// Single-letter uppercase = almost certainly a template param (T, S, U, K, V, etc.)
2431+
if (firstName.length() == 1) continue;
2432+
// Two-letter uppercase combos commonly used as template params: Ts, Vs, etc.
2433+
if (firstName.length() == 2 && Character.isLowerCase(firstName.charAt(1))) continue;
24212434
// Only fire for known static classes OR if preceded by nothing
24222435
// (i.e. not "obj.UpperMethod()" which is a method call)
24232436
// Check context: if previous token is ")", "]", or identifier, skip

src/java/Parser.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,10 +1117,13 @@ private List<TopLevelItem> parseFunctionOrVariable(List<CppLexerToken> leadingCo
11171117

11181118
boolean isVirtual = matchKeyword("virtual");
11191119
boolean isStatic = matchKeyword("static");
1120+
// Java "final" → C++ constexpr (for literals) or const
1121+
boolean isFinal = check(CppLexerTokenType.IDENTIFIER) && peek().text().equals("final");
1122+
if (isFinal) advance();
11201123
matchKeyword("inline");
11211124
matchKeyword("volatile"); // consume volatile qualifier
1122-
boolean isConst = matchKeyword("const");
1123-
boolean isConstexprFn = matchKeyword("constexpr") || matchKeyword("consteval");
1125+
boolean isConst = matchKeyword("const") || isFinal;
1126+
boolean isConstexprFn = matchKeyword("constexpr") || matchKeyword("consteval") || isFinal;
11241127
if (isConstexprFn && !isConst) isConst = true;
11251128
matchKeyword("constinit");
11261129
if (!isVirtual) isVirtual = matchKeyword("virtual"); // constexpr virtual
@@ -2904,7 +2907,8 @@ private Expr parseInitializerElement() {
29042907
advance(); // consume "="
29052908
}
29062909
Expr e = parseExpr();
2907-
matchPunct("..."); // pack expansion in brace-init: "{args...}"
2910+
if (matchPunct("...")) // pack expansion in brace-init: "{args...}"
2911+
e = new PostfixExpr("...", e, e.line(), e.col(), List.of());
29082912
return e;
29092913
}
29102914

0 commit comments

Comments
 (0)