aboutsummaryrefslogtreecommitdiff
path: root/module/c/parse.scm
blob: d407f5ac6714bd13119f9541b9151a2366dfd561 (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
(define-module (c parse)
  :use-module (hnh util)
  :use-module (srfi srfi-1)
  :use-module (ice-9 match)
  :export (parse-lexeme-tree))

;;; Rename this
(define (perms set)
  (concatenate
   (map (lambda (key)
          (map (lambda (o) (cons key o))
               (delete key set)))
        set)))

(define (symbol-concat pair)
  (cond [(null? (car pair)) (cdr pair)]
        [(null? (cdr pair)) (car pair)]
        [else (symbol-append (car pair) (cdr pair))]))

(define (parse-integer-suffix str)
  (define valid-sequences
    (delete 'dummy
            (lset-union eq? '(dummy)
                        (map symbol-concat (perms '(() U L)))
                        (map symbol-concat (perms '(() U LL))))))

  ;; => (LLU ULL LL LU UL L U)

  (aif (memv (string->symbol (string-upcase str))
          valid-sequences)
       (case (car it)
         [(LLU ULL) '(unsigned long-long)]
         [(LU UL) '(unsigned long)]
         [(LL) '(long-long)]
         [(L) '(long)]
         [(U) '(unsigned)])
       (scm-error 'c-parse-error "parse-integer-suffix"
                  "Invalid integer suffix ~s"
                  (list str) #f)))

(define (group-body->type vars)
  (concatenate
   (map
    (match-lambda (('variable var) (list (parse-lexeme-tree `(variable ,var))))
                  (('postfix ('variable var)
                             ('postfix-operator "*"))
                   (list (parse-lexeme-tree `(variable ,var))
                         '*))
                  (else (scm-error 'c-parse-error "parse-lexeme-tree"
                                   "Invalid token ~s in typecast form: ~s"
                                   (list else vars) #f)))
    vars)))

(define (parse-lexeme-tree tree)
  (match tree
    ['() '()]

    ;; Number constants
    [('base-10 n) (string->number n 10)]
    [('base-8  n) (string->number n  8)]
    [('base-16 n) (string->number n 16)]

    [('integer n ('integer-suffix suffix))
     `(as-type
       ,(parse-integer-suffix suffix)
       ,(parse-lexeme-tree n))
     ]
    [('integer n)
     (parse-lexeme-tree n)]

    ;; Character literals, stored as raw integers
    ;; so mathematical operations keep working on them.
    [('char ('escaped-char ('base-8-char n)))
     (-> n (string->number 8) #; integer->char)]
    [('char ('escaped-char ('base-16-char n)))
     (-> n (string->number 16) #; integer->char)]
    [('char ('escaped-char c))
     (char->integer
      (case (string-ref c 0)
        ((#\a) #\alarm)
        ((#\b) #\backspace)
        ((#\e) #\esc)
        ((#\f) #\page)
        ((#\n) #\newline)
        ((#\r) #\return)
        ((#\t) #\tab)
        ((#\v) #\vtab)
        ((#\\) #\\)
        ((#\') #\')))]

    [('char c) (char->integer (string-ref c 0))]

    [('variable var) (string->symbol var)]

    [('operator "&&") 'and]
    [('operator "&=") 'and_eq]
    [('operator "&")  'bitand]
    [('operator "|")  'bitor]
    [('operator "!=") 'not_eq]
    [('operator "||") 'or]
    [('operator "|=") 'or_eq]
    [('operator "^")  'xor]
    [('operator "^=") 'xor_eq]
    [('operator op)  (string->symbol op)]

    [('prefix-operator op)
     (case (string->symbol op)
       ((!) 'not)
       ((~) 'compl)
       ((*) 'dereference)
       ((&) 'pointer)
       ((++) 'pre-increment)
       ((--) 'pre-decrement)
       (else (scm-error 'c-parse-error "parse-lexeme-tree"
                        "Unknown prefix operator ~s"
                        (list op) #f)))]
    [('postfix-operator op)
     (case (string->symbol op)
       [(++) 'post-increment]
       [(--) 'post-decrement]
       [else (scm-error 'c-parse-error "parse-lexeme-tree"
                        "Unknown postfix operator ~s"
                        (list op) #f)])]

    ;; Parenthesis grouping
    [('group args ...)
     (parse-lexeme-tree args)]

    ;; Atomic item. Used by flatten-infix
    [('atom body)
     (parse-lexeme-tree body)]

    [('prefix op arg)
     `(,(parse-lexeme-tree op)
       ,(parse-lexeme-tree arg))]

    [('postfix arg op)
     `(,(parse-lexeme-tree op)
       ,(parse-lexeme-tree arg))]

    [('infix args ...)
     (let ((r
            (resolve-order-of-operations
             (flatten-infix (cons 'infix args)))))
       (match r
         (`(: (? ,op ,true)
              ,false)
          `(ternary ,op ,true ,false))
         (_ r)))]

    [('funcall function ('group arguments))
     `(funcall ,(parse-lexeme-tree function)
               ,(parse-lexeme-tree arguments))]

    [(('variable "struct") ('variable value) ..1)
     `(struct-type ,@(map string->symbol value))
     ]

    ;; A list of variables. Most likely a type signature
    ;; [(('variable value) ..1)
    ;;  ]

    ;; A typecast with only variables must (?) be a typecast?
    [(('group groups ..1) ... value)
     (fold-right (lambda (type done) `(as-type ,type ,done))
                 (parse-lexeme-tree value)
                 (map group-body->type groups))]

    ;; Type name resolution?
    ;; https://en.wikipedia.org/wiki/C_data_types
    ;; base types with spaces:
    ;; =======================
    ;; [[un]signed] char
    ;; [[un]signed] short [int]
    ;; [[un]signed] int
    ;; [un]signed [int]
    ;; [[un]signed] long [int]
    ;; [[un]signed] long long [int]
    ;; float
    ;; [long] double

    ;; https://en.wikipedia.org/wiki/Type_qualifier
    ;; qualifiers
    ;; const
    ;; volatile
    ;; restrict
    ;; _Atomic


    ;; Storage specifiers
    ;; auto
    ;; register
    ;; static
    ;; extern

    ;; struct <typename>
    ;; enum <typename>
    ;; union <typename>

    ;; https://en.wikipedia.org/wiki/C_syntax
    ;; int (*ptr_to_array)[100]


    [(? symbol? bare)
     (scm-error 'c-parse-error
                "parse-lexeme-tree"
                "Naked literal in lex-tree: ~s"
                (list bare)
                #f)]

    [form
     (scm-error 'c-parse-error
                "parse-lexeme-tree"
                "Unknown form in lex-tree: ~s"
                (list form) #f)
     ]))

;; https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
;; https://en.cppreference.com/w/c/language/operator_precedence

(define order-of-operations
  (reverse
   (concatenate
    ;; This is only for binary operations
    `((-> ,(symbol #\.))
      ;; All unary procedures go here, incnluding typecasts, and sizeof
      (* / %)
      (+ -)
      (<< >>)
      (< <= > >=)
      (== != not_eq)
      (& bitand)
      (^ xorg)
      (,(symbol #\|) bitor)
      (&& and)
      (,(symbol #\| #\|) or)
      (? :)
      (= += -= *= /= %= <<= >>= &= ^= ,(symbol #\| #\=)
         and_eq or_eq xor_eq)
      (,(symbol #\,))
      ))))

(define (mark-other form)
  (if (list? form) (cons '*other* form) form))

(define* (resolve-order-of-operations
          tree optional: (order order-of-operations))

  (format (current-error-port) "~s~%" tree)
  (if (null? order)
      (scm-error 'c-parse-error
                 "resolve-order-of-operations"
                 "Out of operations to resolve when resolving expression ~s"
                 (list tree) #f)
      (match tree
        [('*other* body ...) body]
        [(form) (resolve-order-of-operations form order)]
        [(forms ...)
         (match (split-by forms (car order))
           [(group) (resolve-order-of-operations group (cdr order))]
           [groups
            (cons (car order)
                  (map (lambda (form) (resolve-order-of-operations
                                  form order-of-operations))
                       groups))])]
        [a a])))

;; Flatens a tree of infix triples. Stops when it should.
;; (parenthesis, function calls, ...)
(define (flatten-infix form)
  (match form
    [('infix left op ('infix right ...))
     (cons* (parse-lexeme-tree left)
            (parse-lexeme-tree op)
            (flatten-infix (cons 'infix right)))]

    [('infix left op right)
     (list (mark-other (parse-lexeme-tree left))
           (parse-lexeme-tree op)
           (mark-other (parse-lexeme-tree right)))]

    [other (scm-error 'c-parse-error
                      "flatten-infix"
                      "Not an infix tree ~a"
                      (list other)
                      #f)]))