elvish/eval/boilerplate.py

41 lines
1.1 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):
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} {{
cp.compiling(n)
2019-03-10 23:24:04 +08:00
return {outtype}{{cp.{name}(n{extranames}), n.Range().From, n.Range().To}}
}}
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"'''
2019-03-10 23:24:04 +08:00
for fname in 'compile_effect.go', 'compile_value.go':
2016-02-20 00:21:51 +08:00
for line in file(fname):
2019-03-10 23:24:04 +08:00
m = re.match(r'^func \(cp \*compiler\) (\w+)\(\w+ ([^,\[\]]+)(.*)\) (\w*Op)Body {$', 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()