OILS / core / optview_gen.py View on Github | oils.pub

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