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
|
|
|
|
|
|
|
|
2016-02-01 18:42:28 +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]
|
2016-02-01 18:42:28 +08:00
|
|
|
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-07 21:13:12 +08:00
|
|
|
}}
|
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
|
2016-02-07 21:13:12 +08:00
|
|
|
}}
|
|
|
|
'''.format(name=name, intype=intype, outtype=outtype, extraargs=extraargs,
|
2016-02-01 18:42:28 +08:00
|
|
|
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"'''
|
2016-02-26 08:10:16 +08:00
|
|
|
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()
|