github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/elvish/eval/boilerplate.py (about) 1 #!/usr/bin/python2.7 2 import re 3 import os 4 5 6 def put_compile_s(out, name, intype, extraargs, outtype): 7 if not outtype.endswith('Func'): 8 return 9 outtype = outtype[:-4] 10 extranames = ', '.join(a.split(' ')[0] for a in extraargs.split(', ')) if extraargs else '' 11 print >>out, ''' 12 func (cp *compiler) {name}Op(n {intype}{extraargs}) {outtype} {{ 13 cp.compiling(n) 14 return {outtype}{{cp.{name}(n{extranames}), n.Begin(), n.End()}} 15 }} 16 17 func (cp *compiler) {name}Ops(ns []{intype}{extraargs}) []{outtype} {{ 18 ops := make([]{outtype}, len(ns)) 19 for i, n := range ns {{ 20 ops[i] = cp.{name}Op(n{extranames}) 21 }} 22 return ops 23 }} 24 '''.format(name=name, intype=intype, outtype=outtype, extraargs=extraargs, 25 extranames=extranames) 26 27 28 def main(): 29 out = open('boilerplate.go', 'w') 30 print >>out, '''package eval 31 32 import "github.com/u-root/u-root/cmds/elvish/parse"''' 33 for fname in 'compile_op.go', 'compile_value.go': 34 for line in file(fname): 35 m = re.match(r'^func \(cp \*compiler\) (\w+)\(\w+ ([^,\[\]]+)(.*)\) (\w*OpFunc) {$', line) 36 if m: 37 put_compile_s(out, *m.groups()) 38 out.close() 39 os.system('gofmt -w boilerplate.go') 40 41 42 if __name__ == '__main__': 43 main()