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