1 | # examples.sh: Hooks for specific files
|
2 |
|
3 | # COPIED FROM DEFUNCT run.sh. Because some examples require it. NOT
|
4 | # TESTED. TODO: Delete after it runs under Ninja.
|
5 |
|
6 | # -I with ASDL files.
|
7 | compile-with-asdl() {
|
8 | local name=$1
|
9 | local variant=$2
|
10 | local src=_gen/$name.cc
|
11 | shift 2
|
12 |
|
13 | local flags
|
14 | case $variant in
|
15 | (asan)
|
16 | flags="$BASE_CXXFLAGS $ASAN_FLAGS"
|
17 | ;;
|
18 | (opt)
|
19 | flags="$BASE_CXXFLAGS -O2 -g"
|
20 | ;;
|
21 | (*)
|
22 | flags="$BASE_CXXFLAGS"
|
23 | ;;
|
24 | esac
|
25 |
|
26 | # TODO: Use $REPO_ROOT, etc.
|
27 | $CXX -o _bin/$name.$variant $flags \
|
28 | -I . -I .. -I ../_devbuild/gen -I ../_build/cpp -I _gen -I ../cpp \
|
29 | switchy_containers.cc $src "$@" -lstdc++
|
30 | }
|
31 |
|
32 | asdl-gen() {
|
33 | PYTHONPATH="$REPO_ROOT:$REPO_ROOT/vendor" $REPO_ROOT/asdl/asdl_main.py "$@"
|
34 | }
|
35 |
|
36 | # Type check, with some relaxations for Oil
|
37 | typecheck-oil() {
|
38 | local name=$1
|
39 | local flags='--no-strict-optional'
|
40 |
|
41 | MYPYPATH="$REPO_ROOT:$REPO_ROOT/native" \
|
42 | mypy --py2 --strict $flags examples/$name.py | tee _tmp/err.txt
|
43 | }
|
44 |
|
45 | #
|
46 | # examples/varargs
|
47 | #
|
48 |
|
49 | translate-varargs() {
|
50 | # Need this otherwise we get type errors
|
51 | codegen-parse
|
52 |
|
53 | local snippet='
|
54 | #include "leaky_preamble.h"
|
55 | #include "asdl_runtime.h"
|
56 |
|
57 | '
|
58 | translate-ordered varargs "$snippet" \
|
59 | $REPO_ROOT/asdl/runtime.py \
|
60 | examples/varargs.py
|
61 | }
|
62 |
|
63 | compile-varargs() {
|
64 | local variant=$1
|
65 | # need -I flag
|
66 | compile-with-asdl varargs $variant
|
67 | }
|
68 |
|
69 | #
|
70 | # examples/parse
|
71 | #
|
72 |
|
73 | typecheck-parse() {
|
74 | typecheck-oil parse
|
75 | }
|
76 |
|
77 | codegen-parse() {
|
78 | mkdir -p _gen
|
79 | local out=_gen/expr_asdl.py
|
80 | touch _gen/__init__.py
|
81 | asdl-gen mypy examples/expr.asdl > $out
|
82 | }
|
83 |
|
84 | # build ASDL schema and run it
|
85 | pyrun-parse() {
|
86 | codegen-parse
|
87 |
|
88 | PYTHONPATH="$REPO_ROOT/mycpp:$REPO_ROOT/vendor:$REPO_ROOT" examples/parse.py
|
89 | }
|
90 |
|
91 | # classes and ASDL
|
92 | translate-parse() {
|
93 | # Need this otherwise we get type errors
|
94 | codegen-parse
|
95 |
|
96 | local snippet='
|
97 |
|
98 | #include "expr_asdl.h"
|
99 |
|
100 | Str* repr(void* obj) {
|
101 | return StrFromC("TODO: repr()");
|
102 | }
|
103 |
|
104 | '
|
105 | # TODO: This is similar to prebuilt/translate.sh ASDL_FILES
|
106 | translate-ordered parse "$snippet" \
|
107 | $REPO_ROOT/pylib/cgi.py \
|
108 | $REPO_ROOT/asdl/runtime.py \
|
109 | $REPO_ROOT/asdl/format.py \
|
110 | $REPO_ROOT/core/ansi.py \
|
111 | $REPO_ROOT/data_lang/j8_lite.py \
|
112 | examples/parse.py
|
113 | }
|
114 |
|
115 | # Because it depends on ASDL
|
116 | compile-parse() {
|
117 | local variant=$1
|
118 | mkdir -p _gen
|
119 | asdl-gen cpp examples/expr.asdl _gen/expr_asdl
|
120 |
|
121 | compile-with-asdl parse $variant \
|
122 | _gen/expr_asdl.cc \
|
123 | ../_gen/asdl/hnode.asdl.cc
|
124 | }
|
125 |
|
126 | ### parse
|
127 | # Good news! Parsing is 10x faster.
|
128 | # 198 ms in C++ vs 1,974 in Python! Is that because of the method calls?
|
129 | benchmark-parse() {
|
130 | export BENCHMARK=1
|
131 |
|
132 | local name=parse
|
133 |
|
134 | echo
|
135 | echo $'\t[ C++ ]'
|
136 | time _bin/$name
|
137 |
|
138 | # TODO: Consolidate this with the above.
|
139 | # We need 'asdl'
|
140 | export PYTHONPATH="$REPO_ROOT/mycpp:$REPO_ROOT"
|
141 |
|
142 | echo
|
143 | echo $'\t[ Python ]'
|
144 | time examples/${name}.py
|
145 | }
|
146 |
|
147 | #
|
148 | # Other
|
149 | #
|
150 |
|
151 | lexer-main() {
|
152 | local variant=${1:-opt}
|
153 |
|
154 | local name='lexer_main'
|
155 | PYTHONPATH=$REPO_ROOT examples/lexer_main.py
|
156 | #mypy --py2 --strict examples/$name.py
|
157 |
|
158 | local snippet='
|
159 | #include "id_kind_asdl.h" // syntax.asdl depends on this
|
160 | using id_kind_asdl::Id_t; // TODO: proper ASDL modules
|
161 |
|
162 | #include "syntax_asdl.h"
|
163 | #include "types_asdl.h"
|
164 |
|
165 | //#include "match.h"
|
166 |
|
167 | #include "mycpp/runtime.h"
|
168 |
|
169 | // Stub
|
170 | void p_die(Str* s, syntax_asdl::Token* blame_token) {
|
171 | throw AssertionError();
|
172 | }
|
173 |
|
174 | // Hack for now. Every sum type should have repr()?
|
175 | Str* repr(syntax_asdl::source_t* obj) {
|
176 | return StrFromC("TODO");
|
177 | }
|
178 | '
|
179 | translate-ordered lexer_main "$snippet" \
|
180 | $REPO_ROOT/asdl/runtime.py \
|
181 | $REPO_ROOT/frontend/reader.py \
|
182 | $REPO_ROOT/core/alloc.py \
|
183 | $REPO_ROOT/frontend/lexer.py \
|
184 | examples/lexer_main.py
|
185 |
|
186 | compile-with-asdl $name $variant # ../cpp/match.cc
|
187 | }
|
188 |
|
189 | #
|
190 | # alloc_main
|
191 | #
|
192 |
|
193 | typecheck-alloc_main() {
|
194 | typecheck-oil alloc_main
|
195 | }
|
196 |
|
197 | # TODO: pyrun-alloc_main could set PYTHONPATH for syntax_asdl
|
198 |
|
199 | compile-alloc_main() {
|
200 | local variant=${1:-opt}
|
201 | local name='alloc_main'
|
202 |
|
203 | #mypy --py2 --strict examples/$name.py
|
204 |
|
205 | PYTHONPATH=$REPO_ROOT examples/alloc_main.py
|
206 |
|
207 | # NOTE: We didn't import source_e because we're using isinstance().
|
208 | local snippet='
|
209 | #include "id_kind_asdl.h" // syntax.asdl depends on this
|
210 | using id_kind_asdl::Id_t; // TODO: proper ASDL modules
|
211 | #include "syntax_asdl.h"
|
212 |
|
213 | // Hack for now. Every sum type should have repr()?
|
214 | Str* repr(syntax_asdl::source_t* obj) {
|
215 | return StrFromC("TODO");
|
216 | }
|
217 | '
|
218 | translate-ordered alloc_main "$snippet" \
|
219 | $REPO_ROOT/asdl/runtime.py \
|
220 | $REPO_ROOT/core/alloc.py \
|
221 | examples/alloc_main.py
|
222 |
|
223 | local out=_gen/syntax_asdl
|
224 | asdl-gen cpp ../frontend/syntax.asdl $out
|
225 |
|
226 | compile-with-asdl alloc_main $variant \
|
227 | _gen/syntax_asdl.cc \
|
228 | ../_gen/asdl/hnode.asdl.cc \
|
229 | ../_gen/frontend/id_kind.asdl.cc
|
230 | }
|
231 |
|
232 | #
|
233 | # pgen2_demo
|
234 | #
|
235 |
|
236 | # build ASDL schema and run it
|
237 | pyrun-pgen2_demo() {
|
238 | #codegen-pgen2_demo
|
239 | pushd ..
|
240 | build/py.sh demo-grammar
|
241 | popd
|
242 |
|
243 | PYTHONPATH="$REPO_ROOT/mycpp:$REPO_ROOT/vendor:$REPO_ROOT" examples/pgen2_demo.py
|
244 | }
|
245 |
|
246 | typecheck-pgen2_demo() {
|
247 | typecheck-oil pgen2_demo
|
248 | }
|
249 |
|
250 | # These files compile
|
251 | FILES=(
|
252 | $REPO_ROOT/asdl/runtime.py
|
253 | $REPO_ROOT/core/alloc.py
|
254 | $REPO_ROOT/frontend/reader.py
|
255 | $REPO_ROOT/frontend/lexer.py
|
256 | $REPO_ROOT/pgen2/grammar.py
|
257 | $REPO_ROOT/pgen2/parse.py
|
258 | $REPO_ROOT/ysh/expr_parse.py
|
259 | $REPO_ROOT/ysh/expr_to_ast.py
|
260 | )
|
261 |
|
262 | readonly PGEN2_DEMO_FILES=("${FILES[@]}")
|
263 |
|
264 | # NOTE: Doesn't compile anymore. Moved onto bin/osh_parse.py
|
265 | translate-pgen2_demo() {
|
266 | local name='pgen2_demo'
|
267 |
|
268 | translate-ordered $name "$(cat ../cpp/leaky_preamble.h)" \
|
269 | "${PGEN2_DEMO_FILES[@]}" examples/$name.py
|
270 |
|
271 | compile-pgen2_demo
|
272 | }
|
273 |
|
274 | compile-pgen2_demo() {
|
275 | local variant=$1
|
276 | local name='pgen2_demo'
|
277 |
|
278 | compile-with-asdl $name $variant \
|
279 | ../cpp/leaky_frontend_match.cc \
|
280 | ../cpp/leaky_osh_arith_parse.cc \
|
281 | ../_devbuild/gen-cpp/syntax_asdl.cc \
|
282 | ../_devbuild/gen-cpp/hnode_asdl.cc \
|
283 | ../_devbuild/gen-cpp/id_kind_asdl.cc \
|
284 | ../_devbuild/gen-cpp/lookup.cc
|
285 | }
|