-
Notifications
You must be signed in to change notification settings - Fork 0
/
grovel.scm
62 lines (57 loc) · 2.54 KB
/
grovel.scm
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
#!
(import (scheme base))
(import (gambit match))
(import (c-groveler))
(define (deb x)
(parameterize ((current-output-port (current-error-port)))
(write x)
(newline)))
(define (symbol-middle symbol prefix-string suffix-string)
(let ((string (symbol->string symbol)))
(and (>= (string-length string)
(+ (string-length prefix-string)
(string-length suffix-string)))
(substring string
(string-length prefix-string)
(- (string-length string)
(string-length suffix-string))))))
(define (main input-filename)
(let ((input (with-input-from-file input-filename read-all)))
(let ((g (make-c-groveler)))
(grovel-c-include g "errno.h")
(for-each (lambda (form)
(match form
((include ,header)
(cond ((and (symbol? header)
(symbol-middle header "<" ">"))
=> (lambda (filename)
(grovel-c-include g filename)))
(else
(error "Huh include?" form))))
((quote-c . ,strings)
(apply grovel-c-quote g strings))
((constant signed ,constant)
(grovel-c-constant-signed g constant))
((constant unsigned ,constant)
(grovel-c-constant-unsigned g constant))
((constant string ,constant)
(grovel-c-constant-string g constant))
((constant-ifdef signed ,constant)
(grovel-c-constant-ifdef-signed g constant))
((constant-ifdef unsigned ,constant)
(grovel-c-constant-ifdef-unsigned g constant))
((call-constant string ,function ,constant)
(grovel-c-call-constant-string g function constant))
((call-constant-ifdef string ,function ,constant)
(grovel-c-call-constant-ifdef-string g function constant))
((type-signedness ,type)
(grovel-c-type-signedness g type))
((type-size ,type)
(grovel-c-type-size g type))
((type-slot ,type ,slot)
(grovel-c-type-slot g type slot))
(,_
(error "Huh?" form))))
input)
(write-string (c-groveler->string g)))))
(apply main (cdr (command-line)))