1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Demonstrated re2c and Zephyr ASDL.
|
4 | #
|
5 | # Note: this is a "TASK FILE"! (Cleaner than Makefile)
|
6 | #
|
7 | # Demo
|
8 | # - show-oils
|
9 | # - show static type error, caught by MyPy
|
10 | # - show generated Python code
|
11 | # - generated C++
|
12 | #
|
13 | # Usage:
|
14 | # demo/houston-fp/run.sh <function name>
|
15 |
|
16 | set -o nounset
|
17 | set -o pipefail
|
18 | set -o errexit
|
19 |
|
20 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
|
21 |
|
22 | source build/dev-shell.sh
|
23 |
|
24 | readonly BASE_DIR=_tmp/houston-fp
|
25 |
|
26 | #
|
27 | # Utilities
|
28 | #
|
29 |
|
30 | # Note: not showing metaprogramming from frontend/lexer_def.py - only re2c
|
31 |
|
32 | re2c-gen() {
|
33 | local name=$1
|
34 | shift
|
35 | # Rest are flags
|
36 |
|
37 | #re2c --help
|
38 | #return
|
39 |
|
40 | set -x
|
41 | # Generate C switch and goto
|
42 | # Flags copied from Oils
|
43 |
|
44 | local more_flags=''
|
45 | # Extra flags for number
|
46 | #local more_flags='-i --case-ranges'
|
47 |
|
48 | # -i to debug the generated source directly? Doesn't work
|
49 | # --no-debug-info
|
50 | #local more_flags='-i'
|
51 | #local more_flags='--debug-output'
|
52 | #local more_flags='--no-debug-info'
|
53 |
|
54 | re2c \
|
55 | $more_flags \
|
56 | -W -Werror \
|
57 | -o $BASE_DIR/$name-re2c.cc demo/houston-fp/$name.re2c.cc
|
58 |
|
59 | # Generate DOT graph (text)
|
60 | re2c --emit-dot \
|
61 | -o $BASE_DIR/$name-re2c.dot demo/houston-fp/$name.re2c.cc
|
62 |
|
63 | # Only works locally right now
|
64 | if command -v dot; then
|
65 | # Generate image
|
66 | dot -Tpng \
|
67 | -o $BASE_DIR/$name-re2c.png $BASE_DIR/$name-re2c.dot
|
68 | fi
|
69 |
|
70 | set +x
|
71 | }
|
72 |
|
73 | compile() {
|
74 | local name=$1
|
75 |
|
76 | c++ -std=c++11 -g \
|
77 | -o $BASE_DIR/$name-re2c $BASE_DIR/$name-re2c.cc
|
78 | }
|
79 |
|
80 | # TUI debugger!
|
81 | debug() {
|
82 | local name=${1:-favorite}
|
83 | shift
|
84 |
|
85 | gdb --tui --args _tmp/houston-fp/$name-re2c "$@"
|
86 | }
|
87 |
|
88 | number() {
|
89 | re2c-gen number
|
90 | compile number
|
91 |
|
92 | $BASE_DIR/number-re2c ''
|
93 | $BASE_DIR/number-re2c 'z'
|
94 | $BASE_DIR/number-re2c '123'
|
95 | }
|
96 |
|
97 | favorite() {
|
98 | re2c-gen favorite
|
99 | echo
|
100 |
|
101 | wc -l demo/houston-fp/favorite.re2c.cc
|
102 | echo
|
103 |
|
104 | wc -l $BASE_DIR/favorite*.{dot,cc}
|
105 | echo
|
106 |
|
107 | compile favorite
|
108 |
|
109 | $BASE_DIR/favorite-re2c '"hello world"'
|
110 | $BASE_DIR/favorite-re2c '""'
|
111 | $BASE_DIR/favorite-re2c '"foo \n bar"'
|
112 | $BASE_DIR/favorite-re2c '"bad \"'
|
113 | $BASE_DIR/favorite-re2c '"unclosed '
|
114 | $BASE_DIR/favorite-re2c 'unquoted'
|
115 |
|
116 | echo
|
117 | }
|
118 |
|
119 | show-oils() {
|
120 | # 68 instances
|
121 | egrep '%[a-zA-Z]+' */*.asdl
|
122 | echo
|
123 |
|
124 | egrep -C 1 '%[a-zA-Z]+' */*.asdl
|
125 | }
|
126 |
|
127 | check-types() {
|
128 | time MYPYPATH=".:pyext:$BASE_DIR" python3 -m mypy \
|
129 | --py2 \
|
130 | --follow-imports=silent \
|
131 | demo/houston-fp/demo_main.py
|
132 | }
|
133 |
|
134 | #
|
135 | # ASDL
|
136 | #
|
137 |
|
138 | readonly SCHEMA=demo/houston-fp/demo.asdl
|
139 |
|
140 | asdl-main() {
|
141 | PYTHONPATH='.:vendor/' asdl/asdl_main.py "$@"
|
142 | }
|
143 |
|
144 | count-lines() {
|
145 | wc -l $SCHEMA demo/houston-fp/demo_main.py
|
146 | echo
|
147 |
|
148 | wc -l $BASE_DIR/demo*
|
149 | echo
|
150 |
|
151 | wc -l demo/houston-fp/run.sh
|
152 | echo
|
153 | }
|
154 |
|
155 | gen-asdl() {
|
156 | asdl-main mypy $SCHEMA > $BASE_DIR/demo_asdl.py
|
157 |
|
158 | asdl-main cpp $SCHEMA $BASE_DIR/demo.asdl # out prefix
|
159 | }
|
160 |
|
161 | asdl-demo() {
|
162 | PYTHONPATH=".:vendor/:$BASE_DIR" demo/houston-fp/demo_main.py
|
163 | }
|
164 |
|
165 | asdl-case-classes() {
|
166 | gen-asdl
|
167 |
|
168 | count-lines
|
169 |
|
170 | check-types
|
171 |
|
172 | asdl-demo
|
173 | }
|
174 |
|
175 | banner() {
|
176 | echo
|
177 | echo ---
|
178 | echo "$@"
|
179 | echo ---
|
180 | echo
|
181 | }
|
182 |
|
183 | soil-run() {
|
184 | # For local testing
|
185 | rm -r -f $BASE_DIR/*
|
186 | mkdir -p $BASE_DIR
|
187 |
|
188 | banner 're2c'
|
189 |
|
190 | favorite
|
191 |
|
192 | banner 'ASDL'
|
193 |
|
194 | asdl-case-classes
|
195 | }
|
196 |
|
197 | "$@"
|