aboutsummaryrefslogtreecommitdiff
path: root/module/c/cpp.scm
blob: 9a8245ad118ddc0681fceb6a132321bd51ade092 (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
(define-module (c cpp)
  :use-module (hnh util)
  :use-module (srfi srfi-1)
  :use-module (ice-9 popen)
  :use-module (ice-9 match)
  :use-module (ice-9 regex)
  :use-module ((rnrs io ports) :select (call-with-port))
  :use-module (ice-9 format)
  :use-module ((hnh util io) :select (read-lines))
  :use-module (hnh util graph)
  :use-module (c lex)
  :use-module (c parse)
  :use-module (c operators)
  :export (replace-symbols include#)
  )


;; input "#define F(x, y) x + y"
;; 1 full define | F(x, y)
;; 2 macro name  | F
;; 3 macro args  | (x, y)
;; 4 macro body  | x + y
(define define-re (make-regexp "^#define ((\\w+)([(][^)]*[)])?) (.*)"))

(define (tokenize-define-line header-line)
  (aif (regexp-exec define-re header-line)
    (cons (match:substring it 1)
          (match:substring it 4))
    (scm-error 'c-parse-error
               "tokenize-define-line"
               "Line dosen't match: ~s"
               (list header-line) #f)))


(define (replace-symbols tree dict)
  (if (not (list? tree))
      (or (assoc-ref dict tree) tree)
      (map (lambda (node) (replace-symbols node dict))
           tree)))

;; Direct values. Lisp also has quoted symbols in this group.
(define (immediate? x)
  (or (number? x)
      (char? x)
      (string? x)))

;; built in symbols. Should never be marked as dependencies
(define (primitive? x)
  (memv x (cons 'funcall binary-operators)))



;; (symbol . value) -> (list (dependencies . symbol . value)
(define (parse-cpp-define pair)
  (define f (compose parse-lexeme-tree lex))
  (define left (f (car pair)))
  (define proc-args
    (match (and (pair? left)
                (eq? 'funcall (car left))
                (caddr left))
      [#f '()]
      [(_ args ...) args]
      [arg (list arg)]))

  (define right (f (cdr pair)))
  (define dependencies
    (lset-difference
     eq?
     (remove primitive?
             (remove immediate?
                     (flatten (if (list? right)
                                  right (list right)))))
     proc-args))

  (cons
   dependencies
   (match left
     [('funcall name ('#{,}# args ...))
      (cons name `(lambda ,args ,right))]

     [('funcall name arg)
      (cons name `(lambda (,arg) ,right))]

     [name (cons name right)])))


(define (parse-cpp-file lines)
  (map (lambda (line)
         (catch #t
           (lambda () (parse-cpp-define line))
           (lambda (err caller fmt args data)
             (format #t "~a in ~a: ~?~%"
                     err caller fmt args)
             (format #t "~s~%" line)
             #f)))
       lines))

(define (private-c-symbol? string)
  (char=? #\_ (string-ref string 0)))

(define (tokenize-header-file header-file)
  (map tokenize-define-line
       (call-with-port
        (open-pipe* OPEN_READ "cpp" "-dM" header-file)
        read-lines)))

(define (load-cpp-file header-file)

  (define lines (tokenize-header-file header-file))
  (define forms (parse-cpp-file lines))

  (fold (lambda (node graph)
          (add-node graph (cdr node) (car node)))
        (make-graph car)
        (filter identity forms)))

(define (include% header-file)
  (define graph* (load-cpp-file header-file))
  ;; Hack for termios since this symbol isn't defined.
  ;; (including in the above removed private c symbols)
  (define graph (add-node graph* (cons '_POSIX_VDISABLE #f) '()))
  ;; TODO expand bodies
  ;; (remove (compose private-c-symbol? car))
  (resolve-dependency-graph graph))

(define-macro (include# header-file . args)

  (define define-form (if (null? args) 'define (car args)))

  `(begin
     ,@(map (lambda (pair) `(,define-form ,(car pair) ,(cdr pair)))
            (include% header-file))))