-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathall-expression
executable file
·214 lines (186 loc) · 3.88 KB
/
all-expression
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash -fu
# Must be /bin/bash, because we use `export -f` with `parallel`
DOCKER_ARGS="--network none --rm"
DOCKER_REPO=ghcr.io/kaitai-io/
ALL_TARGETS="
cpp
go
java
javascript
lua
nim
perl
php
python
ruby
"
print_usage()
{
cat <<EOF
Usage: $0 [OPTIONS] EXPRESSION"
Interpretes EXPRESSION in various programming languages using Docker images
and prints the result.
Options:
--target, -t Comma-separated list of targets to run (default: all)
--parallel, -p Run all targets in parallel
--help, -h Show this help message
Available targets: $ALL_TARGETS
EOF
}
expr_cpp()
{
cat > "$WORKDIR/expr_run.cpp" <<EOF
#include <iostream>
int main() {
std::cout << ($EXPRESSION) << std::endl;
return 0;
}
EOF
IMG_COORD=kaitai-cpp_stl-gcc11-linux-x86_64
echo -n "* C++ (gcc11): "
docker run $DOCKER_ARGS \
-v "$WORKDIR:/work" \
--workdir /work \
"$DOCKER_REPO$IMG_COORD" \
sh -c "g++ -Wall -o expr_run_gcc11 expr_run.cpp && ./expr_run_gcc11"
IMG_COORD=kaitai-cpp_stl-clang11-linux-x86_64
echo -n "* C++ (clang11): "
docker run $DOCKER_ARGS \
-v "$WORKDIR:/work" \
--workdir /work \
"$DOCKER_REPO$IMG_COORD" \
sh -c "clang++ -Wall -o expr_run_clang14 expr_run.cpp && ./expr_run_clang14"
}
expr_go()
{
cat > "$WORKDIR/expr_run.go" <<EOF
package main
import "fmt"
func main() {
fmt.Println($EXPRESSION)
}
EOF
IMG_COORD=kaitai-go-1.21-linux-x86_64
echo -n "* Go: "
docker run $DOCKER_ARGS \
-v "$WORKDIR:/work" \
--workdir /work \
"$DOCKER_REPO$IMG_COORD" \
sh -c "go run expr_run.go"
}
expr_java()
{
cat > "$WORKDIR/ExprRun.java" <<EOF
public class ExprRun {
public static void main(String[] args) {
System.out.println($EXPRESSION);
}
}
EOF
IMG_COORD=kaitai-java-temurin8-linux-x86_64
echo -n "* Java: "
docker run $DOCKER_ARGS \
-v "$WORKDIR:/work" \
--workdir /work \
"$DOCKER_REPO$IMG_COORD" \
sh -c "javac ExprRun.java && java ExprRun"
}
expr_javascript()
{
IMG_COORD=kaitai-javascript-nodejs20-linux-x86_64
echo -n "* JavaScript: "
docker run $DOCKER_ARGS "$DOCKER_REPO$IMG_COORD" \
nodejs -e "console.log($EXPRESSION)"
}
expr_lua()
{
IMG_COORD=kaitai-lua-5.3-linux-x86_64
echo -n "* Lua: "
docker run $DOCKER_ARGS "$DOCKER_REPO$IMG_COORD" \
lua -e "print(string.format('%q', $EXPRESSION))"
}
expr_nim()
{
cat > "$WORKDIR/expr_run.nim" <<EOF
echo $EXPRESSION
EOF
IMG_COORD=kaitai-nim-1.6.0-linux-x86_64
echo -n "* Nim: "
docker run $DOCKER_ARGS \
-v "$WORKDIR:/work" \
--workdir /work \
"$DOCKER_REPO$IMG_COORD" \
sh -c 'nim c -r --hints:off expr_run.nim'
}
expr_perl()
{
IMG_COORD=kaitai-perl-5.38-linux-x86_64
echo -n "* Perl: "
docker run $DOCKER_ARGS "$DOCKER_REPO$IMG_COORD" \
perl -MData::Dumper -e "print(Dumper($EXPRESSION));"
}
expr_php()
{
IMG_COORD=kaitai-php-8.3-linux-x86_64
echo -n "* PHP: "
docker run $DOCKER_ARGS "$DOCKER_REPO$IMG_COORD" \
php -r "var_dump($EXPRESSION);"
}
expr_python()
{
IMG_COORD=kaitai-python-3.11-linux-x86_64
echo -n "* Python: "
docker run $DOCKER_ARGS "$DOCKER_REPO$IMG_COORD" \
python -c "print(repr($EXPRESSION))"
}
expr_ruby()
{
IMG_COORD=kaitai-ruby-3.3-linux-x86_64
echo -n "* Ruby: "
docker run $DOCKER_ARGS "$DOCKER_REPO$IMG_COORD" \
ruby -e "p($EXPRESSION)"
}
# Parse command line
TARGETS="$ALL_TARGETS"
PARALLEL_FLAG=0
while [ $# -gt 1 ]; do
key="$1"
case "$key" in
--parallel|-p)
PARALLEL_FLAG=1
shift
;;
--help|-h)
print_usage
exit 0
;;
--target|-t)
TARGETS=$(echo "$2" | tr ',' ' ')
shift 2
;;
*)
echo "Unknown option: $1"
print_usage
exit 1
;;
esac
done
if [ $# -ne 1 ]; then
echo "Error: missing EXPRESSION"
print_usage
exit 1
fi
EXPRESSION="$1"
# Create temporary directory
WORKDIR=$(mktemp -d)
if [ "$PARALLEL_FLAG" = 1 ]; then
for TARGET in $TARGETS; do
export -f "expr_$TARGET"
done
export EXPRESSION WORKDIR DOCKER_ARGS DOCKER_REPO
parallel expr_{} ::: $ALL_TARGETS
else
for TARGET in $TARGETS; do
"expr_$TARGET"
done
fi