diff options
author | Shevek <[email protected]> | 2014-09-11 12:20:05 -0700 |
---|---|---|
committer | Shevek <[email protected]> | 2014-09-11 12:20:05 -0700 |
commit | cccd60f37a31113a42372672670ce2a2bc529684 (patch) | |
tree | 479b0bb354da25cdb8707e8b27baa49e2f9b12c5 | |
parent | 2db1eafd535d8db92f76db8ccf45b1b80f6152e3 (diff) |
Handle conditionals in preprocessor statements.
-rw-r--r-- | src/main/java/org/anarres/cpp/Preprocessor.java | 15 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/PreprocessorTest.java | 4 |
2 files changed, 16 insertions, 3 deletions
diff --git a/src/main/java/org/anarres/cpp/Preprocessor.java b/src/main/java/org/anarres/cpp/Preprocessor.java index 0d14b53..192ac45 100644 --- a/src/main/java/org/anarres/cpp/Preprocessor.java +++ b/src/main/java/org/anarres/cpp/Preprocessor.java @@ -1514,7 +1514,7 @@ public class Preprocessor implements Closeable { tok = expr_token(); if (tok.getType() != ')') { expr_untoken(tok); - error(tok, "missing ) in expression"); + error(tok, "Missing ) in expression. Got " + tok.getText()); return 0; } break; @@ -1628,8 +1628,17 @@ public class Preprocessor implements Closeable { lhs = (lhs != 0) || (rhs != 0) ? 1 : 0; break; - case '?': - /* XXX Handle this? */ + case '?': { + tok = expr_token(); + if (tok.getType() != ':') { + expr_untoken(tok); + error(tok, "Missing : in conditional expression. Got " + tok.getText()); + return 0; + } + long falseResult = expr(0); + lhs = (lhs != 0) ? rhs : falseResult; + } + break; default: error(op, diff --git a/src/test/java/org/anarres/cpp/PreprocessorTest.java b/src/test/java/org/anarres/cpp/PreprocessorTest.java index 8a40823..84e7e31 100644 --- a/src/test/java/org/anarres/cpp/PreprocessorTest.java +++ b/src/test/java/org/anarres/cpp/PreprocessorTest.java @@ -153,6 +153,10 @@ public class PreprocessorTest { testInput("LStr(x);\n", NL, I("L"), "x", ';'); testInput("'foo'\n", NL, SQSTRING); + testInput("#if 1 ? 2 : 0\nTEXT\n#endif\n", NL, NL, I("TEXT"), NL); + testInput("#if 1 ? 0 : 2\nTEXT\n#endif\n", NL, NL, NL); + testInput("#if 0 ? 0 : 2\nTEXT\n#endif\n", NL, NL, I("TEXT"), NL); + testInput("#if 0 ? 2 : 0\nTEXT\n#endif\n", NL, NL, NL); writer.close(); |