aboutsummaryrefslogtreecommitdiff
path: root/tests/test/cpp/parse2.scm
blob: 4140448084c3dda6128d2c7f3fe904699b23903d (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
(define-module (test cpp parse2)
  :use-module (srfi srfi-64)
  :use-module (srfi srfi-88)
  :use-module ((c ast) :select (build-ast))
  :use-module ((c preprocessor2)
               :select (preprocess-string
                        make-default-environment)))


(define (run str)
  (call-with-values (lambda () (preprocess-string str (make-default-environment)))
    (lambda (_ tokens)
      (build-ast tokens))))



(test-group "primitives"
  (test-equal "Simple integer"
    '((constexpr (constant 1)))
    (run "1"))

  (test-equal "Complex integer"
    '((constexpr (constant 16)))
    (run "0x10l"))

  (test-equal "Simple character"
    '((constexpr (constant #x41)))
    (run "'A'"))

  (test-equal "String literal"
    '((constexpr (string-constant #vu8(#x48 #x65 #x6c #x6c #x6f 0))))
    (run "\"Hello\"")))



(test-equal "_Generic"
  '((constexpr
     (generic X
              ((specifier-qualifier-list (type long) (type double))
               . cbrtl)
              (default . cbrt)
              ((specifier-qualifier-list (type float)) . cbrtf))))
  (run "_Generic(X, long double: cbrtl, default: cbrt, float: cbrtf)"))

(test-group "postfix expression"
  (test-equal "array index"
    '((constexpr (idx arr i)))
    (run "arr[i]"))

  (test-equal "Funcall"
    '((constexpr (f)))
    (run "f()"))

  (test-equal "Funcall with args"
    '((constexpr (f a b c)))
    (run "f(a,b,c)"))

  (test-equal "Chained function calls"
    '((constexpr ((f a) b)))
    (run "f(a)(b)"))

  (test-equal "dot-access"
    '((constexpr (dot-access a b)))
    (run "a.b"))

  (test-equal "chained dotaccess"
    '((constexpr (dot-access (dot-access a b) c)))
    (run "a.b.c"))

  (test-equal "ptr-access"
    '((constexpr (ptr-access a b)))
    (run "a->b")))

;; unary expressions


;; cast expresions

(test-equal "Chained casts"
  '((constexpr
     (as-type (specifier-qualifier-list (type short))
              (as-type (specifier-qualifier-list (type int))
                       x))))
 (run "(short) (int) x"))


(test-equal "Ternary"
  '((constexpr (ternary (constant 1)
                        (constant 2)
                        (constant 3))))
  (run "1 ? 2 : 3"))


(test-equal "Comma operator"
  '((constexpr (begin (= x (constant 10))
                      (= y (constant 20)))))
  (run "x = 10, y = 20"))



(test-group "Declarations"
  (test-equal "Simple"
    '((translation-unit
       (define (named x ((type int)))
         <undefined-value>)))
    (run "int x;"))

  (test-equal "Simple with value"
    '((translation-unit
       (define (named x ((type int)))
         (constant 1))))
    (run "int x = 1;"))


  (test-equal "Multiple at same time"
    '((translation-unit
       (begin
        (define (named x ((type long) (type int)))
          (constant 1))
        (define (named y (pointer-to ((type long) (type int))))
          <undefined-value>))))
    (run "long int x = 1, *y;"))

  ;; TODO static_assert-declaration

  (test-group "structs"
    (test-equal "declaration"
      '((translation-unit
         (struct-like-declaration ((type (struct (named s)))))))
      (run "struct s;"))

    (test-equal "definition"
      '((translation-unit
         (struct-like-declaration
          ((type (struct (named s)
                   (struct-declaration-list
                    (struct-declarator-list
                     (named x (specifier-qualifier-list (type int)))))))))))
      (run "struct s { int x; };"))

    (test-equal "Definition with multiple fields"
      '((translation-unit
         (struct-like-declaration
          ((type (struct (named p)
                   (struct-declaration-list
                    (struct-declarator-list
                     (named x (specifier-qualifier-list (type int))))
                    (struct-declarator-list
                     (named y (specifier-qualifier-list (type int)))))))))))
      (run "struct p { int x; int y; };"))

    (test-equal "Anonymous definition"
      '((translation-unit
         (struct-like-declaration
          ((type (struct
                     (struct-declaration-list
                      (struct-declarator-list
                       (named x (specifier-qualifier-list (type int)))))))))))
      (run "struct { int x; };"))


    (test-equal "struct with inner named struct"
      '((translation-unit
         (struct-like-declaration
          ((type (struct (named p)
                   (struct-declaration-list
                    (struct-declarator-list
                     (named a (specifier-qualifier-list (type int))))
                    (specifier-qualifier-list
                     (type (struct (named inner)
                             (struct-declaration-list
                              (struct-declarator-list
                               (named x (specifier-qualifier-list (type int)))))))))))))))
      (run "struct p { int a; struct inner { int x; }; };"))

    (test-equal "struct with inner anonymous struct"
      '((translation-unit
         (struct-like-declaration
          ((type (struct (named p)
                   (struct-declaration-list
                    (struct-declarator-list
                     (named a (specifier-qualifier-list (type int))))
                    (specifier-qualifier-list
                     (type (struct
                               (struct-declaration-list
                                (struct-declarator-list
                                 (named x (specifier-qualifier-list (type int)))))))))))))))
      (run "struct p { int a; struct { int x; }; };"))

    (run "struct p { struct s; };")

    )

  (test-group "Unions"
    (test-equal
        '((translation-unit
           (struct-like-declaration
            ((type (union (named X)))))))
      (run "union X;"))

    ;; (run "union p { struct s; };")

    (test-equal
        '((translation-unit
           (struct-like-declaration
            ((type (union (named int_or_char)
                          (struct-declaration-list
                           (struct-declarator-list
                            (named i (specifier-qualifier-list (type int))))
                           (struct-declarator-list
                            (named s (specifier-qualifier-list (type char)))))))))))
      (run "union int_or_char { int i; char s; };")))

  (test-group "Typedef"
    (test-equal "Simple"
      '((translation-unit
         (define (named uint
                        ((storage typedef)
                         (type unsigned)
                         (type int)))
           <undefined-value>)))
      (run "typedef unsigned int uint;"))

    ;; Interesting since the star "binds" to the right
    (test-equal "with ptr"
      '((translation-unit
         (define (named int_ptr
                        (pointer-to
                         ((storage typedef)
                          (type int))))
           <undefined-value>)))
      (run "typedef int *int_ptr;"))

    (test-equal "Function pointer"
      '((translation-unit
         (define ((named func_ptr
                         (pointer-to
                          (procedure
                           (returning (pointer-to ((storage typedef)
                                                   (type void))))
                           (taking ((pointer-to ((type void)))))))))
           <undefined-value>)))
      (run "typedef void*(*func_ptr)(void*);")))

  )