aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-07-13 11:47:22 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2022-07-13 11:47:22 +0200
commiteb5f5dd3f3f408ccb9f62303f031efc8454fd5de (patch)
tree500c176dc33dbfd6a099d774c71a9d0b32fc4f28
parentFix _Pragma, and non-standard pragma directives. (diff)
downloadcalp-eb5f5dd3f3f408ccb9f62303f031efc8454fd5de.tar.gz
calp-eb5f5dd3f3f408ccb9f62303f031efc8454fd5de.tar.xz
Resolve #ifdef, #ifndef, #else, #endif.
-rw-r--r--module/c/preprocessor2.scm14
-rw-r--r--tests/test/cpp/preprocessor2.scm76
2 files changed, 72 insertions, 18 deletions
diff --git a/module/c/preprocessor2.scm b/module/c/preprocessor2.scm
index 2677c42e..28d11901 100644
--- a/module/c/preprocessor2.scm
+++ b/module/c/preprocessor2.scm
@@ -633,12 +633,18 @@
((if) resolve-for-if)
((ifdef)
(lambda (env body)
- (if (in-environment? env (identifier-token? (car body)))
- enter-active-if enter-inactive-if)))
+ ((if (in-environment? env (identifier-token? (car body)))
+ enter-active-if enter-inactive-if)
+ env)))
((ifndef)
(lambda (env body)
- (if (in-environment? env (identifier-token? (car body)))
- enter-inactive-if enter-active-if)))
+ ((if (in-environment? env (identifier-token? (car body)))
+ enter-inactive-if enter-active-if)
+ env)))
+ ;; NOTE possibly validate that body is empty for endif and else
+ ((endif) (lambda (env _) (leave-if env)))
+ ((else) (lambda (env _) (flip-flop-if env)))
+ ;; ((elif) (lambda ))
((define) resolve-define)
((undef) (lambda (env body) (remove-identifier env (identifier-token? (car body)))))
((line) handle-line-directive)
diff --git a/tests/test/cpp/preprocessor2.scm b/tests/test/cpp/preprocessor2.scm
index 5e1bb736..b4825907 100644
--- a/tests/test/cpp/preprocessor2.scm
+++ b/tests/test/cpp/preprocessor2.scm
@@ -592,17 +592,11 @@
(lex "x"))))))
+;; TODO
;; resolve-h-file
;; resolve-q-file
-;; handle-pragma
;; include
-;; if
-;; else
-;; ifdef
-;; ifndef
-;; elif
-
(call-with-values (lambda ()
@@ -803,13 +797,6 @@ LISTING(..\\listing.dir)
" e))))))
))
-;; TODO
-;; if
-;; else
-;; ifdef
-;; ifndef
-;; elif
-
(test-group "Next token matches?"
(test-assert "Zero tokens never match" (not (next-token-matches? (const #t) '())))
@@ -990,3 +977,64 @@ debug(\"X = %d\\n\", x);
showlist(The first, second, and third items.);
report(x>y, \"x is %d but y is %d\", x, y);
"))))
+
+
+
+(test-group "Conditionals"
+ (test-group "ifdef"
+ (test-equal "#ifdef on non-defined"
+ (lex "")
+ (run "
+#ifdef X
+x
+#endif"))
+
+ (test-equal "#ifdef on defined"
+ (lex "x")
+ (run "
+#define X
+#ifdef X
+x
+#endif")))
+
+ (test-group "ifndef"
+ (test-equal "#ifndef on non-defined"
+ (lex "x")
+ (run "
+#ifndef X
+x
+#endif"))
+
+ (test-equal "#ifndef on defined"
+ (lex "")
+ (run "
+#define X
+#ifndef X
+x
+#endif
+")))
+
+ (test-group "else"
+ (test-equal "else from active to inactive"
+ (lex "1")
+ (run "
+#ifndef X
+1
+#else
+2
+#endif"))
+
+ (test-equal "else from inactive to active"
+ (lex "2")
+ (run "
+#ifdef X
+1
+#else
+2
+#endif")))
+
+;; TODO
+;; if
+;; elif
+
+ )