-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathebash.sh
executable file
·54 lines (46 loc) · 1.36 KB
/
ebash.sh
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
#!/bin/bash
function transpile {
echo "$(cat $1)" |
awk '
/\ *function.*\(.*\)/ { match($0, /\(.*\)/);
print substr($0, 0, RSTART-1) " {";
n = split(substr($0, RSTART+1, RLENGTH-2), args, ",");
printf "\t"
for (i=1; i<=n; i++) {
gsub(/^\ */, "", args[i]);
gsub(/\ *$/, "", args[i]);
printf args[i] "=$" i ";";
}
printf "\n";
next;
} 1
' | # support for named function arguments
sed -E 's/^#!(.*)/#!\/bin\/bash/' | # changing the shebang
sed -E 's/\$([a-zA-Z_][a-zA-Z_0-9]*)\+\+/\1=\$(( \$\1+1 ))/' | # the increment operator
sed -E 's/\$\{\{(.*)\}\}/`bc -q -l <<< "pi=a(1)*4;\1"`/' | # math expressions
sed -E 's/\$\?\{\{(.*)\}\}/`bc -q -l <<< "pi=a(1)*4;\1"` == "1"/' # math conditionals
}
input_file=$1
if [[ -z $input_file ]]
then
echo "You should provide the input file as the first argument!"
exit 1
elif [[ ! -f $input_file ]]
then
echo "The input file $input_file does not exist!"
exit 1
fi
code=`transpile $input_file`
output_file=$2
if [[ -z $output_file ]]
then
eval "$code"
else
if [[ -e $output_file ]]
then
echo "The output file $output_file already exists!"
read -p "Do you want to overwrite $output_file? (y/n): "
[[ ! $REPLY =~ ^[Yy](es)?$ ]] && echo "Aborting..." && exit 1
fi
echo "$code" > $output_file
fi