From cccd60f37a31113a42372672670ce2a2bc529684 Mon Sep 17 00:00:00 2001 From: Shevek Date: Thu, 11 Sep 2014 12:20:05 -0700 Subject: Handle conditionals in preprocessor statements. --- src/main/java/org/anarres/cpp/Preprocessor.java | 15 ++++++++++++--- 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(); -- cgit v1.2.3