From ccc7848110b06479cf8a38ee843d4d3adc01a27c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 12 Jul 2022 02:37:49 +0200 Subject: work --- tests/test/cpp/preprocessor2.scm | 185 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 182 insertions(+), 3 deletions(-) (limited to 'tests/test/cpp/preprocessor2.scm') diff --git a/tests/test/cpp/preprocessor2.scm b/tests/test/cpp/preprocessor2.scm index 01c46dff..2d95269a 100644 --- a/tests/test/cpp/preprocessor2.scm +++ b/tests/test/cpp/preprocessor2.scm @@ -30,6 +30,8 @@ stringify-tokens ) ) + :use-module ((c cpp-types) + :select (punctuator-token? identifier-token?)) :use-module (c lex2) ) @@ -61,7 +63,7 @@ (define* (run str optional: (env (make-environment))) (let ((env tokens (handle-preprocessing-tokens env (tokenize str)))) - (remove-noexpand tokens))) + (drop-whitespace-both (remove-noexpand tokens)))) (test-group "Tokens until End Of Line" @@ -133,7 +135,7 @@ (test-group "Parameter list" (test-group "Empty parameter list" (let ((containing remaining nls (parse-parameter-list (lex "()")))) - (test-equal '() containing) + (test-equal '(()) containing) (test-equal '() remaining) (test-equal 0 nls))) @@ -426,6 +428,7 @@ (lambda (_ tokens*) (test-equal (lex "1") (remove-noexpand tokens*)))) (test-error "Arity error for to many args" 'cpp-arity-error (expand-macro e m '() (lex "(10)")))) + (let ((m (function-like-macro identifier: "f" identifier-list: '("x") @@ -433,6 +436,8 @@ body: (lex "__VA_ARGS__ x")))) (call-with-values (lambda () (expand-macro e m '() (lex "(1)"))) (lambda (_ tokens*) (test-equal (lex " 1") (remove-noexpand tokens*)))) + ;; This doesn't fail, since a single required argument is satisfied by the default nothing + #; (test-error "Arity error on too few args (with variadic)" 'cpp-arity-error (expand-macro e m '() (lex "()"))) (call-with-values (lambda () (expand-macro e m '() (lex "(1,2,3)"))) @@ -745,7 +750,7 @@ wf(x, y) #define X 100")) (test-equal "#undef" - (append (lex "X") (lex "10") (lex "X")) + (lex "X\n10\nX") (run " X #define X 10 @@ -771,3 +776,177 @@ X ;; ifdef ;; ifndef ;; elif + +(define next-token-matches? (@@ (c preprocessor2) next-token-matches?)) + +(test-group "Next token matches?" + (test-assert "Zero tokens never match" (not (next-token-matches? (const #t) '()))) + + (test-assert "Non-matching token" + (not (next-token-matches? punctuator-token? (lex "x+y")))) + + (test-assert "Maching token" + (next-token-matches? identifier-token? (lex "x+y"))) + + (test-assert "Matching token, after whitespace" + (next-token-matches? identifier-token? (lex " \n x + y")))) + + +(test-equal "Function likes aren't expanded if not followed by a left parenthese" + (lex "f") + (run " +#define f(x) +f")) + +(test-equal "Parameter expansion times" + (lex "fx fy") (run " +#define fw(x) f ## x +#define ffw(x) fw(x) +#define x y +fw(x) ffw(x) +")) + +(test-equal (lex "(5 + 10)") (run " +#define x 10 +#define f(a) a +#define g h +#define h(x) (x + 10) +f(g)(5)")) + + +;; (expand-macro +;; (extend-environment +;; (make-environment) +;; (list (object-like-macro identifier: "g" +;; body: (lex "h")) +;; (function-like-macro identifier: "h" +;; identifier:-list '("x") +;; body: (lex "(x + 10)")))) +;; (function-like-macro identifier: "f" +;; identifier:-list '("a") +;; body: (lex "a")) +;; '() +;; (lex "(g)(5)")) + +;; ;; ⇒ #< cpp-if-status: (outside) cpp-variables: # cpp-file-stack: (("*outside*" . 1))> +;; ⇒ (#< type: preprocessing-token body: (identifier "h") noexpand: ("f" "h")> +;; #< type: preprocessing-token body: (punctuator "(") noexpand: ()> +;; #< type: preprocessing-token body: (pp-number "5") noexpand: ()> +;; #< type: preprocessing-token body: (punctuator ")") noexpand: ()>) + +(define unlex (@ (c unlex) unlex)) + +(test-group "6.10.3.5 Scope of macro definitions" + (test-equal "Example3" + (unlex (lex "f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1); +f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1); +int i[] = { 1, 23, 4, 5, }; +char c[2][6] = { \"hello\", \"\" };")) + (unlex (drop-whitespace-both (run " +#define x 3 +#define f(a) f(x * (a)) +#undef x +#define x 2 +#define g f +#define z z[0] +#define h g(~ +#define m(a) a(w) +#define w 0,1 +#define t(a) a +#define p() int +#define q(x) x +#define r(x,y) x ## y +#define str(x) # x + +f(y+1) + f(f(z)) % t(t(g)(0) + t)(1); +g(x+(3,4)-w) | h 5) & m + (f)^m(m); +p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) }; +char c[2][6] = { str(hello), str() };")))) + ) + +;; (tokenize " +;; f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1); +;; f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1); +;; int i[] = { 1, 23, 4, 5, }; +;; char c[2][6] = { \"hello\", \"\" };") + + +;; (define env +;; (handle-preprocessing-tokens (make-environment) (tokenize " +;; #define x 3 +;; #define f(a) f(x * (a)) +;; #undef x +;; #define x 2 +;; #define g f +;; #define z z[0] +;; #define h g(~ +;; #define m(a) a(w) +;; #define w 0,1 +;; #define t(a) a +;; #define p() int +;; #define q(x) x +;; #define r(x,y) x ## y +;; #define str(x) # x +;; "))) + + +;; (handle-preprocessing-tokens +;; env (tokenize +;; "f(y+1) + f(f(z)) % t(t(g)(0) + t)(1); +;; g(x+(3,4)-w) | h 5) & m +;; (f)^m(m); +;; p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) }; +;; char c[2][6] = { str(hello), str() };")) + + + +;; (let ((env tokens +;; (handle-preprocessing-tokens +;; (make-environment) +;; (tokenize " +;; #define x 3 +;; #define f(a) f(x * (a)) +;; #undef x +;; #define x 2 +;; #define g f +;; #define z z[0] +;; #define h g(~ +;; #define m(a) a(w) +;; #define w 0,1 +;; #define t(a) a +;; #define p() int +;; #define q(x) x +;; #define r(x,y) x ## y +;; #define str(x) # x + +;; f(y+1) + f(f(z)) % t(t(g)(0) + t)(1); +;; g(x+(3,4)-w) | h 5) & m +;; (f)^m(m); +;; p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) }; +;; char c[2][6] = { str(hello), str() };")))) +;; (remove-noexpand tokens)) + +;; (test-equal "anything" +;; (run +;; " +;; #define x 3 +;; #define f(a) f(x * (a)) +;; #undef x +;; #define x 2 +;; #define g f +;; #define z z[0] +;; #define h g(~ +;; #define m(a) a(w) +;; #define w 0,1 +;; #define t(a) a +;; #define p() int +;; #define q(x) x +;; #define r(x,y) x ## y +;; #define str(x) # x + +;; f(y+1) + f(f(z)) % t(t(g)(0) + t)(1); +;; g(x+(3,4)-w) | h 5) & m +;; (f)^m(m); +;; p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) }; +;; char c[2][6] = { str(hello), str() };")) -- cgit v1.2.3