OILS / opy / lib / opcode_gen.py View on Github | oilshell.org

48 lines, 29 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3"""
4opcode_gen.py
5"""
6
7import sys
8
9from lib import opcode
10
11
12def log(msg, *args):
13 if args:
14 msg = msg % args
15 print(msg, file=sys.stderr)
16
17
18def main(argv):
19 opcode_nums = set(opcode.opmap.itervalues())
20
21 # Print opcodes in numerical order. They're not contiguous integers.
22 for num in sorted(opcode_nums):
23 # SLICE+1 -> SLICE_1
24 name = opcode.opname[num].replace('+', '_')
25 print('#define %s %d' % (name, num))
26
27 print('')
28 print('#define HAVE_ARGUMENT %d' % opcode.HAVE_ARGUMENT)
29
30 #log('%s', opcode.opname)
31
32 print('')
33 print('const char* const kOpcodeNames[] = {')
34 n = max(opcode_nums)
35 for i in xrange(n+1):
36 if i in opcode_nums:
37 print('"%s",' % opcode.opname[i])
38 else:
39 print('"",') # empty value
40 print('};')
41
42
43if __name__ == '__main__':
44 try:
45 main(sys.argv)
46 except RuntimeError as e:
47 print('FATAL: %s' % e, file=sys.stderr)
48 sys.exit(1)