aboutsummaryrefslogtreecommitdiff
path: root/tests/test/cpp/preprocessor2.scm
blob: 75e29834fbbea11ba96567a44de06df6d275df70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
(define-module (test cpp preprocessor2)
  :use-module (srfi srfi-64)
  :use-module (srfi srfi-64 util)
  :use-module (srfi srfi-64 test-error)
  :use-module (srfi srfi-71)
  :use-module (srfi srfi-88)
  :use-module ((hnh util) :select (-> unval))
  :use-module (c preprocessor2)
  :use-module (c cpp-environment)
  :use-module (c cpp-environment function-like-macro)
  :use-module (c cpp-environment object-like-macro)
  :use-module (c lex2))


;; arbitrary tokens useful in tests for checking that values are returned correctly
(define before (car (lex "before")))
(define after (car (lex "after")))

(define tokens-until-eol (@@ (c preprocessor2) tokens-until-eol))
(test-group "Tokens until End Of Line"
  (call-with-values
      (lambda ()
        (tokens-until-eol
         (list before '(whitespace "\n") after)))
    (lambda (bef aft)
      (test-equal (list before) bef)
      (test-equal (list '(whitespace "\n") after) aft))))



(define squeeze-whitespace (@@ (c preprocessor2) squeeze-whitespace))
(test-equal "Squeeze whitespace"
  '(bef (whitespace " ") aft)
  (squeeze-whitespace
   '(bef
     (whitespace a)
     (whitespace b)
     aft)))



(define stringify-token (@@ (c preprocessor2) stringify-token))
(define stringify-tokens (@@ (c preprocessor2) stringify-tokens))

(test-group "Stringify"
  (test-equal "("
    (stringify-token '(punctuator "(")))
  ;; TODO more cases

  (test-equal (car (lex "\"(a, b)\""))
    (stringify-tokens (lex "(a,    b)")))
  )


(define parse-identifier-list (@@ (c preprocessor2) parse-identifier-list))

(test-group "Parse identifier list"
  (test-group "Single argument"
   (let ((rest args (parse-identifier-list (lex "x"))))
     (test-assert (not rest))
     (test-equal '("x") args)))

  (test-group "Multiple parameters"
    (let ((rest args (parse-identifier-list (lex "x, y"))))
      (test-assert (not rest))
      (test-equal '("x" "y") args)))


  (test-group "Rest args after regular"
    (let ((rest args (parse-identifier-list (lex "x, ..."))))
      (test-assert rest)
      (test-equal '("x") args)))

  (test-group "Only rest args"
    (let ((rest args (parse-identifier-list (lex "..."))))
      (test-assert rest)
      (test-equal '() args)))

  (test-group "Errors"
    (test-error "Compound forms are invalid"
      'cpp-error (parse-identifier-list (lex "(y)")))

    (test-error "Non-identifier atoms are invalid"
      'cpp-error (parse-identifier-list (lex "1")))

    (test-error "Rest args not at end is invalid"
      'cpp-error (parse-identifier-list (lex "..., y")))))



(define expand-stringifiers (@@ (c preprocessor2) expand-stringifiers))
(define build-parameter-map (@@ (c preprocessor2) build-parameter-map))
(define parse-parameter-list (@@ (c preprocessor2) parse-parameter-list))
(define cleanup-whitespace (@@ (c preprocessor2) cleanup-whitespace))

(test-equal "Clean up whitespace"
  (lex "( 2 , 4 )")
  (cleanup-whitespace (lex "  \n (   2   , \n 4    )  \t   ")))


;; Parameter lists (the callsite arguments to the macro)
(test-group "Parameter list"
  (test-group "Empty parameter list"
    (let ((containing remaining nls (parse-parameter-list (lex "()"))))
      (test-equal '() containing)
      (test-equal '() remaining)
      (test-equal 0 nls)))

  (test-group "Single value in parameter list"
    (let ((containing remaining nls (parse-parameter-list (lex "(x)"))))
      (test-equal (list (lex "x")) containing)
      (test-equal '() remaining)
      (test-equal 0 nls)))

  (test-group "Two values in parameter list"
    (let ((containing remaining nls (parse-parameter-list (lex "(x, y)"))))
      (test-equal (list (lex "x")
                        (lex "y"))
        containing)
      (test-equal '() remaining)
      (test-equal 0 nls)))

  (test-group "Three values in parameter list"
    (let ((containing remaining nls (parse-parameter-list (lex "(x, y, z)"))))
      (test-equal (list (lex "x")
                        (lex "y")
                        (lex "z"))
        containing)
      (test-equal '() remaining)
      (test-equal 0 nls)))

  (test-group "Numeric parameter"
    (let ((containing remaining nls (parse-parameter-list (lex "(1)"))))
      (test-equal (list (lex "1")) containing)
      (test-equal '() remaining)
      (test-equal 0 nls))
    )

  (test-group "Two values, one of which is a paretheseed pair"
    (let ((containing remaining nls
                      (parse-parameter-list (lex "(x, (y, z))"))))
      (test-equal (list (lex "x") (lex "(y, z)"))
        containing)
      (test-equal '() remaining)
      (test-equal 0 nls))))

(test-group "Build parameter map"
  (test-equal "Simplest case, zero arguments"
    '()
    (let ((m (function-like-macro
              identifier: "str"
              identifier-list: '()
              body: (lex "#x"))))
      (build-parameter-map
       m '() #; (list (lex "x"))
       )))

  (test-equal "Single (simple) argument"
    `(("x" . ,(lex "x")))
    (let ((m (function-like-macro
              identifier: "str"
              identifier-list: '("x")
              body: '())))
      (build-parameter-map
       m
       (list (lex "x")))))

  (test-equal "Single advanced argument"
    `(("x" . ,(lex "(x)")))
    (let ((m (function-like-macro
              identifier: "str"
              identifier-list: '("x")
              body: '())))
      (build-parameter-map
       m (list (lex "(x)")))))

  (test-group "Rest arguments"
    (test-equal "Single simple"
      `(("__VA_ARGS__" . ,(lex "x")))
      (let ((m (function-like-macro
                identifier: "str"
                identifier-list: '()
                variadic?: #t
                body: '())))
        (build-parameter-map
         m (list (lex "x")))))

    (test-equal "Two simple"
      `(("__VA_ARGS__" . ,(lex "x,y")))
      (let ((m (function-like-macro
                identifier: "str"
                identifier-list: '()
                variadic?: #t
                body: '())))
        (build-parameter-map
         m (list (lex "x,y")))))))


(test-group "Expand stringifiers"
  (let ((m (function-like-macro
            identifier: "str"
            identifier-list: '("x")
            body: (lex "#x"))))
    (test-equal "Correct stringification of one param"
      (lex "\"10\"")
      (expand-stringifiers
       m (build-parameter-map
          m (list (lex "10"))))))

  (let ((m (function-like-macro
            identifier: "str"
            identifier-list: '()
            body: (lex "#x"))))
    (test-error "Stringification fails for non-parameters"
      'macro-expand-error
      (expand-stringifiers
       m (build-parameter-map
          m (list (lex "x")))))))

;; TODO expand-join
;; token ## token2

(define join-file-line (@@ (c preprocessor2) join-file-line))

(let ((e (join-file-line (make-environment))))
  (test-equal (object-like-macro identifier: "__FILE__"
                                 body: '((preprocessing-token (string-literal "*outside*"))))
    (get-identifier e "__FILE__"))
  (test-equal (object-like-macro identifier: "__LINE__"
                                 body: '((preprocessing-token (pp-number "1"))))
    (get-identifier e "__LINE__")))

(define resolve-token-stream (@@ (c preprocessor2) resolve-token-stream))

(test-group "Token streams"
  (test-group "Non-expanding"
    (test-equal "Null stream"
      '() (resolve-token-stream (make-environment) '()))
    (test-equal "Constant resolve to themselves"
      (lex "1") (resolve-token-stream (make-environment) (lex "1")))
    (test-equal "Identifier-likes not in environment stay put"
      (lex "x") (resolve-token-stream (make-environment) (lex "x")))
    (test-equal "Identifier-likes with stuff after keep stuff after"
      (lex "x 1") (resolve-token-stream (make-environment) (lex "x 1"))))

  (test-group "Object likes"
    (test-equal "Expansion of single token"
      (lex "10")
      (resolve-token-stream (extend-environment (make-environment)
                                                (list (object-like-macro
                                                       identifier: "x"
                                                       body: (lex "10"))))
                            (lex "x")))

    (test-equal "Expansion keeps stuff after"
      (lex "10 1")
      (resolve-token-stream (extend-environment (make-environment)
                                                (list (object-like-macro
                                                       identifier: "x"
                                                       body: (lex "10"))))
                            (lex "x 1")))

    (test-equal "Multiple object like macros in one stream"
      (lex "10 20")
      (resolve-token-stream (extend-environment (make-environment)
                                                (list (object-like-macro
                                                       identifier: "x"
                                                       body: (lex "10"))
                                                      (object-like-macro
                                                       identifier: "y"
                                                       body: (lex "20"))))
                            (lex "x y"))))

  ;; TODO

  ;; (test-group "Function likes")

  ;; (test-group "Mix of object and function likes")

  )

(define expand-macro (@@ (c preprocessor2) expand-macro))
(define resolve-define (@@ (c preprocessor2) resolve-define))
(define apply-macro (@@ (c preprocessor2) apply-macro))
(define maybe-extend-identifier (@@ (c preprocessor2) maybe-extend-identifier))

(test-group "Macro expansion"
  (test-group "Expand macro part 1"
    ;; Expand object like macros
    ;; apply-macro depends on this, but expand macro with function like macros
    ;; depend on apply-macro, thereby the two parter
    (test-group "Object like macros"
      (call-with-values
          (lambda () (expand-macro (make-environment)
                              (object-like-macro
                               identifier: "x" body: (lex "1 + 2"))
                              '()))
        (lambda (_ tokens) (test-equal "Simplest case" (lex "1 + 2") tokens)))

      (call-with-values
          (lambda () (expand-macro (make-environment)
                              (object-like-macro
                               identifier: "x" body: (lex "1+2"))
                              (cdr (lex "x something else"))))
        (lambda (_ tokens) (test-equal "Expansion with stuff after"
                        (lex "1+2 something else") tokens)))

      ;; (call-with-values (expand-macro (make-environment)))

      ))


  (test-group "Maybe extend identifier"
    (test-equal "Non-identifier returns remaining"
      (lex "x")
      ((unval maybe-extend-identifier 1)
       (make-environment) "x" '()))

    (test-equal "Non-identifiers remaining tokens are returned verbatim"
      (append (lex "x") (list after))
      ((unval maybe-extend-identifier 1)
       (make-environment) "x" (list after)))

    (test-equal "Object like identifier expands"
      (lex "1 + 2")
      ((unval maybe-extend-identifier 1)
       (extend-environment (make-environment)
                           (list
                            (object-like-macro
                             identifier: "x"
                             body: (lex "1 + 2"))))
       "x"
       '()))

    (test-equal "Object like macro still returns remaining verbatim"
      (append (lex "1 + 2") (list after))
      ((unval maybe-extend-identifier 1)
       (extend-environment (make-environment)
                           (list
                            (object-like-macro
                             identifier: "x"
                             body: (lex "1 + 2"))))
       "x"
       (list after)))

    )

  (test-group "Apply macro"
    (test-equal "zero arg macro on nothing"
      (lex "1")
      (apply-macro
       (make-environment)
       (function-like-macro identifier: "f"
                            identifier-list: '()
                            body: (lex "1"))
       '()))

    (test-equal "Single arg macro"
      (lex "10")
      (apply-macro
       (make-environment)
       (function-like-macro identifier: "f"
                            identifier-list: '("x")
                            body: (lex "x"))
       ((unval parse-parameter-list) (lex "(10)"))))

    (test-equal "Two arg macro"
      (lex "10 + 20")
      (apply-macro
       (make-environment)
       (function-like-macro identifier: "f"
                            identifier-list: '("x" "y")
                            body: (lex "x + y"))
       ((unval parse-parameter-list) (lex "(10, 20)")))))

  (test-group "Expand macro part 2"
    (test-group "Function like macros"
      (let ((e (make-environment)))
        (let ((m (function-like-macro
                  identifier: "f"
                  identifier-list: '()
                  body: (lex "1"))))
          (call-with-values (lambda () (expand-macro e m (lex "()")))
            (lambda (_ tokens*) (test-equal (lex "1") 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")
                  variadic?: #t
                  body: (lex "__VA_ARGS__ x"))))
          (call-with-values (lambda () (expand-macro e m (lex "(1)")))
            (lambda (_ tokens*) (test-equal (lex " 1") tokens*)))
          (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)")))
            (lambda (_ tokens*) (test-equal (lex "2,3 1") tokens*)))
          )
        ))))

(let ((e (make-environment)))
  (test-group "Resolve token stream with function likes"
    (test-equal "Macro expanding to its parameter"
      (lex "0")
      (resolve-token-stream
       (extend-environment
        e (list (function-like-macro identifier: "f"
                                     identifier-list: '("x")
                                     body: (lex "x"))))
       (lex "f(0)")))

    (test-equal "Macro expanding parameter multiple times"
      (lex "(2) * (2)")
      (resolve-token-stream
       (extend-environment
        e (list (function-like-macro identifier: "f"
                                     identifier-list: '("x")
                                     body: (lex "(x) * (x)"))))
       (lex "f(2)"))
      )

    (test-equal "Object like contains another object like"
      (lex "z")
      (resolve-token-stream
       (extend-environment
        e (list (object-like-macro identifier: "x"
                                   body: (lex "y"))
                (object-like-macro identifier: "y"
                                   body: (lex "z"))))
       (lex "x")))

    (test-equal "function like contains another macro"
      (lex "10")
      (resolve-token-stream
       (extend-environment
        e (list (function-like-macro identifier: "f"
                                     identifier-list: '("x")
                                     body: (lex "g(x)"))
                (function-like-macro identifier: "g"
                                     identifier-list: '("y")
                                     body: (lex "y"))))
       (lex "f(10)")))

    "
#define f(x) g(x)
#define g(y) y
f(10)
"

    (test-equal "function like containing another macro using the same parameter name"
      (lex "10")
      (resolve-token-stream
       (extend-environment
        e (list (function-like-macro identifier: "f"
                                     identifier-list: '("x")
                                     body: (lex "g(x)"))
                (function-like-macro identifier: "g"
                                     identifier-list: '("x")
                                     body: (lex "x"))))
       (lex "f(10)")))



    (test-equal "function like contains another macro"
      (lex "10 * 2 + 20 * 2 + 30")
      (resolve-token-stream
       (extend-environment
        e (list (function-like-macro identifier: "f"
                                     identifier-list: '("x" "y")
                                     body: (lex "g(x) + g(y)"))
                (function-like-macro identifier: "g"
                                     identifier-list: '("x")
                                     body: (lex "x * 2"))))
       (lex "f(10, 20) + 30")))))

(let ((e (extend-environment
          (make-environment)
          (list (@@ (c preprocessor2) defined-macro)))))
  (test-group "defined() macro"
    (test-equal "defined(NOT_DEFINED)"
      (lex "0") (resolve-token-stream e (lex "defined(X)")))
    (test-equal "defined(DEFINED)"
      (lex "1") (resolve-token-stream
                 (extend-environment
                  e (list (object-like-macro identifier: "X"
                                             body: (lex "10"))))
                 (lex "defined(X)")))))


(let ((env (resolve-define (make-environment)
                           (lex "f(x) x+1"))))
  (test-assert "New binding added" (in-environment? env "f"))
  (let ((m (get-identifier env "f")))
    (test-equal "Macro parameters" '("x") (macro-identifier-list m))
    (test-equal "Macro body" (lex "x+1") (macro-body m))))

;; This should issue a warning, since the standard requires a space after the ending parenthe here (6.10.3)
;; (resolve-define (make-environment)
;;                 (lex "f(x)x+1"))

;; (let ((env (resolve-define (make-environment)
;;                            (lex "x x"))))
;;   (test-equal "Macro expanding to itself leaves the token"
;;     (lex "x")
;;     (resolve-token-stream env (lex "x"))))

(let ((env (-> (make-environment)
               (resolve-define (lex "f(a) a*g"))
               (resolve-define (lex "g(a) f(a)")))))
  (test-equal '()
   (resolve-token-stream env (lex "f(2)(9)"))))



;; resolve-h-file
;; resolve-q-file
;; handle-pragma