forked from elves/elvish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboilerplate.py
More file actions
executable file
·40 lines (32 loc) · 1.14 KB
/
Copy pathboilerplate.py
File metadata and controls
executable file
·40 lines (32 loc) · 1.14 KB
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
#!/usr/bin/python2.7
import re
import os
def put_compile_s(out, name, intype, extraargs, outtype):
extranames = ', '.join(a.split(' ')[0] for a in extraargs.split(', ')) if extraargs else ''
print >>out, '''
func (cp *compiler) {name}Op(n {intype}{extraargs}) {outtype} {{
cp.compiling(n)
return {outtype}{{cp.{name}(n{extranames}), n.Range().From, n.Range().To}}
}}
func (cp *compiler) {name}Ops(ns []{intype}{extraargs}) []{outtype} {{
ops := make([]{outtype}, len(ns))
for i, n := range ns {{
ops[i] = cp.{name}Op(n{extranames})
}}
return ops
}}
'''.format(name=name, intype=intype, outtype=outtype, extraargs=extraargs,
extranames=extranames)
def main():
out = open('boilerplate.go', 'w')
print >>out, '''package eval
import "github.com/elves/elvish/parse"'''
for fname in 'compile_effect.go', 'compile_value.go':
for line in file(fname):
m = re.match(r'^func \(cp \*compiler\) (\w+)\(\w+ ([^,\[\]]+)(.*)\) (\w*Op)Body {$', line)
if m:
put_compile_s(out, *m.groups())
out.close()
os.system('gofmt -w boilerplate.go')
if __name__ == '__main__':
main()