OILS / mycpp / examples.sh View on Github | oils.pub

277 lines, 152 significant
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.
7compile-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
32asdl-gen() {
33 PYTHONPATH="$REPO_ROOT:$REPO_ROOT/vendor" $REPO_ROOT/asdl/asdl_main.py "$@"
34}
35
36# Type check, with some relaxations for Oil
37typecheck-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
49translate-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
63compile-varargs() {
64 local variant=$1
65 # need -I flag
66 compile-with-asdl varargs $variant
67}
68
69#
70# examples/parse
71#
72
73typecheck-parse() {
74 typecheck-oil parse
75}
76
77codegen-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
85pyrun-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# TODO: it needs deps.txt?
93translate-parse() {
94 # Need this otherwise we get type errors
95 codegen-parse
96
97 # TODO: This is similar to prebuilt/translate.sh ASDL_FILES
98 translate-ordered parse '' \
99 $REPO_ROOT/pylib/cgi.py \
100 $REPO_ROOT/asdl/runtime.py \
101 $REPO_ROOT/asdl/format.py \
102 $REPO_ROOT/core/ansi.py \
103 $REPO_ROOT/data_lang/j8_lite.py \
104 examples/parse.py
105}
106
107# Because it depends on ASDL
108compile-parse() {
109 local variant=$1
110 mkdir -p _gen
111 asdl-gen cpp examples/expr.asdl _gen/expr_asdl
112
113 compile-with-asdl parse $variant \
114 _gen/expr_asdl.cc \
115 ../_gen/asdl/hnode.asdl.cc
116}
117
118### parse
119# Good news! Parsing is 10x faster.
120# 198 ms in C++ vs 1,974 in Python! Is that because of the method calls?
121benchmark-parse() {
122 export BENCHMARK=1
123
124 local name=parse
125
126 echo
127 echo $'\t[ C++ ]'
128 time _bin/$name
129
130 # TODO: Consolidate this with the above.
131 # We need 'asdl'
132 export PYTHONPATH="$REPO_ROOT/mycpp:$REPO_ROOT"
133
134 echo
135 echo $'\t[ Python ]'
136 time examples/${name}.py
137}
138
139#
140# Other
141#
142
143lexer-main() {
144 local variant=${1:-opt}
145
146 local name='lexer_main'
147 PYTHONPATH=$REPO_ROOT examples/lexer_main.py
148 #mypy --py2 --strict examples/$name.py
149
150 local snippet='
151#include "id_kind_asdl.h" // syntax.asdl depends on this
152using id_kind_asdl::Id_t; // TODO: proper ASDL modules
153
154#include "syntax_asdl.h"
155#include "types_asdl.h"
156
157//#include "match.h"
158
159#include "mycpp/runtime.h"
160
161// Stub
162void p_die(Str* s, syntax_asdl::Token* blame_token) {
163 throw AssertionError();
164}
165
166// Hack for now. Every sum type should have repr()?
167Str* repr(syntax_asdl::source_t* obj) {
168 return StrFromC("TODO");
169}
170'
171 translate-ordered lexer_main "$snippet" \
172 $REPO_ROOT/asdl/runtime.py \
173 $REPO_ROOT/frontend/reader.py \
174 $REPO_ROOT/core/alloc.py \
175 $REPO_ROOT/frontend/lexer.py \
176 examples/lexer_main.py
177
178 compile-with-asdl $name $variant # ../cpp/match.cc
179}
180
181#
182# alloc_main
183#
184
185typecheck-alloc_main() {
186 typecheck-oil alloc_main
187}
188
189# TODO: pyrun-alloc_main could set PYTHONPATH for syntax_asdl
190
191compile-alloc_main() {
192 local variant=${1:-opt}
193 local name='alloc_main'
194
195 #mypy --py2 --strict examples/$name.py
196
197 PYTHONPATH=$REPO_ROOT examples/alloc_main.py
198
199 # NOTE: We didn't import source_e because we're using isinstance().
200 local snippet='
201#include "id_kind_asdl.h" // syntax.asdl depends on this
202using id_kind_asdl::Id_t; // TODO: proper ASDL modules
203#include "syntax_asdl.h"
204
205// Hack for now. Every sum type should have repr()?
206Str* repr(syntax_asdl::source_t* obj) {
207 return StrFromC("TODO");
208}
209'
210 translate-ordered alloc_main "$snippet" \
211 $REPO_ROOT/asdl/runtime.py \
212 $REPO_ROOT/core/alloc.py \
213 examples/alloc_main.py
214
215 local out=_gen/syntax_asdl
216 asdl-gen cpp ../frontend/syntax.asdl $out
217
218 compile-with-asdl alloc_main $variant \
219 _gen/syntax_asdl.cc \
220 ../_gen/asdl/hnode.asdl.cc \
221 ../_gen/frontend/id_kind.asdl.cc
222}
223
224#
225# pgen2_demo
226#
227
228# build ASDL schema and run it
229pyrun-pgen2_demo() {
230 #codegen-pgen2_demo
231 pushd ..
232 build/py.sh demo-grammar
233 popd
234
235 PYTHONPATH="$REPO_ROOT/mycpp:$REPO_ROOT/vendor:$REPO_ROOT" examples/pgen2_demo.py
236}
237
238typecheck-pgen2_demo() {
239 typecheck-oil pgen2_demo
240}
241
242# These files compile
243FILES=(
244 $REPO_ROOT/asdl/runtime.py
245 $REPO_ROOT/core/alloc.py
246 $REPO_ROOT/frontend/reader.py
247 $REPO_ROOT/frontend/lexer.py
248 $REPO_ROOT/pgen2/grammar.py
249 $REPO_ROOT/pgen2/parse.py
250 $REPO_ROOT/ysh/expr_parse.py
251 $REPO_ROOT/ysh/expr_to_ast.py
252)
253
254readonly PGEN2_DEMO_FILES=("${FILES[@]}")
255
256# NOTE: Doesn't compile anymore. Moved onto bin/osh_parse.py
257translate-pgen2_demo() {
258 local name='pgen2_demo'
259
260 translate-ordered $name "$(cat ../cpp/leaky_preamble.h)" \
261 "${PGEN2_DEMO_FILES[@]}" examples/$name.py
262
263 compile-pgen2_demo
264}
265
266compile-pgen2_demo() {
267 local variant=$1
268 local name='pgen2_demo'
269
270 compile-with-asdl $name $variant \
271 ../cpp/leaky_frontend_match.cc \
272 ../cpp/leaky_osh_arith_parse.cc \
273 ../_devbuild/gen-cpp/syntax_asdl.cc \
274 ../_devbuild/gen-cpp/hnode_asdl.cc \
275 ../_devbuild/gen-cpp/id_kind_asdl.cc \
276 ../_devbuild/gen-cpp/lookup.cc
277}