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