elvish/eval/boilerplate.py

43 lines
1.2 KiB
Python
Raw Normal View History

2016-01-23 01:05:15 +08:00
#!/usr/bin/python2.7
import re
2016-02-20 07:48:13 +08:00
import os
2016-01-23 01:05:15 +08:00
def put_compile_s(out, name, intype, extraargs, outtype):
2016-02-20 07:48:13 +08:00
if not outtype.endswith('Func'):
return
outtype = outtype[:-4]
extranames = ', '.join(a.split(' ')[0] for a in extraargs.split(', ')) if extraargs else ''
2016-01-23 01:05:15 +08:00
print >>out, '''
2016-02-20 07:48:13 +08:00
func (cp *compiler) {name}Op(n {intype}{extraargs}) {outtype} {{
return {outtype}{{cp.{name}(n{extranames}), n.Begin(), n.End()}}
}}
2016-02-20 07:48:13 +08:00
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)
2016-01-23 01:05:15 +08:00
def main():
out = open('boilerplate.go', 'w')
print >>out, '''package eval
2016-02-20 07:48:13 +08:00
2016-01-29 04:02:09 +08:00
import "github.com/elves/elvish/parse"'''
for fname in 'compileOp.go', 'compileValuesOp.go':
2016-02-20 00:21:51 +08:00
for line in file(fname):
2016-02-20 07:48:13 +08:00
m = re.match(r'^func \(cp \*compiler\) (\w+)\(\w+ ([^,]+)(.*)\) (\w*OpFunc) {$', line)
2016-02-20 00:21:51 +08:00
if m:
put_compile_s(out, *m.groups())
2016-02-20 07:48:13 +08:00
out.close()
os.system('gofmt -w boilerplate.go')
2016-01-23 01:05:15 +08:00
if __name__ == '__main__':
main()