-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_constants.ty
50 lines (40 loc) · 962 Bytes
/
get_constants.ty
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
import io
import os (args)
import sh (sh)
if not let [_, header, *prefixes] = args {
os.exit(1)
}
let last = nil
let constants = []
let inEnum = false
for line, i in io.open(header, 'r') {
match line {
/#\s*define\s+(\w+)\s+(.*)$/ => {
constants.push((_1, _2))
},
/^\s*(?:typedef\s*)?enum(?:\s|$)/ => {
inEnum = true
},
/^\s*}/ => {
inEnum = false
last = nil
},
/^\s*([A-Z0-9_]+)\s*=\s*([^,]+)/ => {
if inEnum {
constants.push((_1, _2))
last = _1
}
},
/^\s*([A-Z0-9_]+)/ => {
if inEnum {
constants.push((_1, (if last { "{last} + 1" } else { '0' })))
last = _1
}
},
_ => ;
}
}
constants.filter!(${prefixes.any?([in it.0])})
for (name, value) in constants {
print("let {name} = {value}")
}