1 | #!/usr/bin/env python2
|
2 | """Arith_parse_gen.py."""
|
3 | from __future__ import print_function
|
4 |
|
5 | import collections
|
6 | import sys
|
7 |
|
8 | from osh import arith_parse
|
9 | from mycpp.mylib import log
|
10 |
|
11 | _ = log
|
12 |
|
13 |
|
14 | def main(argv):
|
15 | spec = arith_parse.Spec()
|
16 |
|
17 | print("""\
|
18 | #include "cpp/osh.h"
|
19 |
|
20 | using syntax_asdl::arith_expr_t;
|
21 | using syntax_asdl::word_t;
|
22 | using tdop::TdopParser;
|
23 | """)
|
24 |
|
25 | to_declare = collections.defaultdict(set)
|
26 |
|
27 | for row in spec.nud_lookup.itervalues():
|
28 | mod_name, func_name = row.ModuleAndFuncName()
|
29 | to_declare[mod_name].add(('N', func_name))
|
30 |
|
31 | # TODO: namespace are arith_parse or tdop
|
32 | for row in spec.led_lookup.itervalues():
|
33 | mod_name, func_name = row.ModuleAndFuncName()
|
34 | to_declare[mod_name].add(('L', func_name))
|
35 |
|
36 | # main program has no headers, so here are prototypes
|
37 | for mod_name in to_declare:
|
38 | print('namespace %s { ' % mod_name)
|
39 | for typ, func in sorted(to_declare[mod_name]):
|
40 | if typ == 'N':
|
41 | # tdop::NullFunc
|
42 | fmt = 'arith_expr_t* %s(TdopParser*, word_t*, int);'
|
43 | else:
|
44 | # tdop::LeftFunc
|
45 | fmt = 'arith_expr_t* %s(TdopParser*, word_t*, arith_expr_t*, int);'
|
46 | print(fmt % func)
|
47 |
|
48 | print('}')
|
49 | print('')
|
50 |
|
51 | print("""\
|
52 | namespace arith_parse {
|
53 |
|
54 | tdop::LeftInfo kLeftLookup[] = {
|
55 | { nullptr, 0, 0 }, // empty
|
56 | """,
|
57 | end='')
|
58 |
|
59 | n = max(spec.led_lookup)
|
60 | m = max(spec.nud_lookup)
|
61 | assert n == m
|
62 | #log('arith_parse_gen.py: precedence table has %d entries', n)
|
63 |
|
64 | for i in xrange(1, n):
|
65 | row = spec.led_lookup.get(i)
|
66 | if row is None:
|
67 | assert False, 'No empty rows anymore'
|
68 | print(' { nullptr, 0, 0 }, // empty')
|
69 | else:
|
70 | print(' %s' % row)
|
71 |
|
72 | print("""\
|
73 | };
|
74 |
|
75 | tdop::NullInfo kNullLookup[] = {
|
76 | { nullptr, 0 }, // empty
|
77 | """,
|
78 | end='')
|
79 |
|
80 | for i in xrange(1, n):
|
81 | row = spec.nud_lookup.get(i)
|
82 | if row is None:
|
83 | assert False, 'No empty rows anymore'
|
84 | print(' { nullptr, 0 }, // empty')
|
85 | else:
|
86 | print(' %s' % row)
|
87 |
|
88 | print("""\
|
89 | };
|
90 |
|
91 | };
|
92 | """)
|
93 |
|
94 |
|
95 | if __name__ == '__main__':
|
96 | try:
|
97 | main(sys.argv)
|
98 | except RuntimeError as e:
|
99 | print('FATAL: %s' % e, file=sys.stderr)
|
100 | sys.exit(1)
|