OILS / core / optview_gen.py View on Github | oilshell.org

94 lines, 22 significant
1#!/usr/bin/env python2
2"""optview_gen.py."""
3from __future__ import print_function
4
5import sys
6
7from frontend import option_def
8#from core import optview
9
10
11def GenMethods(opt_names, f):
12 for n in opt_names:
13 f.write(' bool %s() { return _Get(option_i::%s); }\n' % (n, n))
14
15
16def main(argv):
17 f = sys.stdout
18
19 f.write("""\
20#ifndef OPTVIEW_H
21#define OPTVIEW_H
22
23#include "_gen/frontend/option.asdl.h"
24#include "mycpp/runtime.h"
25
26namespace optview {
27
28using option_asdl::option_i;
29
30class _View {
31 public:
32 _View(List<bool>* opt0_array, List<List<bool>*>* opt_stacks)
33 : opt0_array(opt0_array), opt_stacks(opt_stacks) {
34 }
35
36 bool _Get(int opt_num) {
37 List<bool>* overlay = opt_stacks->at(opt_num);
38 if ((overlay == nullptr) or len(overlay) == 0) {
39 return opt0_array->at(opt_num);
40 } else {
41 return overlay->at(-1);
42 }
43 }
44
45 static constexpr ObjHeader obj_header() {
46 return ObjHeader::ClassFixed(field_mask(), sizeof(_View));
47 }
48
49 List<bool>* opt0_array;
50 List<List<bool>*>* opt_stacks;
51
52 static constexpr uint32_t field_mask() {
53 return
54 maskbit(offsetof(_View, opt0_array))
55 | maskbit(offsetof(_View, opt_stacks));
56 }
57};
58
59class Parse : public _View {
60 public:
61 Parse(List<bool>* opt0_array, List<List<bool>*>* opt_stacks)
62 : _View(opt0_array, opt_stacks) {
63 }
64""")
65
66 GenMethods(option_def.ParseOptNames(), f)
67
68 f.write("""\
69};
70
71class Exec : public _View {
72 public:
73 Exec(List<bool>* opt0_array, List<List<bool>*>* opt_stacks)
74 : _View(opt0_array, opt_stacks) {
75 }
76""")
77
78 GenMethods(option_def.ExecOptNames(), f)
79
80 f.write("""\
81};
82
83} // namespace optview
84
85#endif // OPTVIEW_H
86""")
87
88
89if __name__ == '__main__':
90 try:
91 main(sys.argv)
92 except RuntimeError as e:
93 print('FATAL: %s' % e, file=sys.stderr)
94 sys.exit(1)