-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolv.sh
executable file
·279 lines (250 loc) · 5.83 KB
/
solv.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
shopt -s extglob
solv_version="1.2.0"
example_input=" a simple sum 1 + 1
let us multiply that result LINE:-1 * 3
and sum the previous lines SUM:-2,-1
as a percentage demo 2 - 1 as % of LINE:+2
percent off demo (5 * 2) % off 512
percent of demo 10 % of 256"
if [ $# -eq 0 ] && [ -t 0 ]
then
echo "
-=[ solv ${solv_version} ]=-----------------------------------------------------------
Soulver-like calculator for the command line.
Written by Conan Theobald:
- https://github.com/shuckster/solv.sh
Find Soulver here:
- https://soulver.app/
----------------------------------------------------------------------------
Usage: solv.sh <input> | stdin
Uses bc along with some preprocessing to calculate inputs like:
$example_input
Run the above with --example
LINE and SUM work with relative line numbers, so LINE:-1 means to replace
that token with the calculated value of the line before it, and SUM:-2,-1
means to replace that token with the sum of the range specified.
As you can see, arbitrary text can appear before your expressions (and after
them too) but try to stick to the basic alphabet: punctuaction characters
might be seen an interpreted as part of the expression.
"
exit
fi
if test "$1" = "--example"
then
echo "
-=[ solv ${solv_version} ]=-----------------------------------------------------------
--example
"
echo "$example_input" | ${0}
echo ""
exit
fi
process_lines_from_stdin_or_args ()
{
if test -t 0
then
# No stdin, just echo the args
echo "$@"
else
# Read from stdin
while IFS=$'\n' read -r line
do
echo "$line"
done
fi
}
convert_sum_to_line()
{
local input="$1"
input="${input#SUM:}"
IFS=',' read -r start end <<< "$input"
local result=""
local i
if (( start <= end ))
then
for (( i=start; i<=end; i++ ))
do
if (( i != 0 ))
then
result+="LINE:$(printf '%+d' $i) + "
fi
done
else
for (( i=start; i>=end; i-- ))
do
if (( i != 0 ))
then
result+="LINE:$(printf '%+d' $i) + "
fi
done
fi
result=${result% + }
echo "($result)"
}
preprocess_sums ()
{
read -r line
local regex="SUM:[-+]?[0-9]+,[-+]?[0-9]+"
while [[ $line =~ $regex ]]
do
local pattern="${BASH_REMATCH[0]}"
local replacement
replacement=$(convert_sum_to_line "$pattern")
line="${line//$pattern/$replacement}"
done
echo "$line"
}
preprocess_percents ()
{
read -r line
# Process "as % of"
if [[ $line =~ (.*)\ as\ \%\ of\ (.*) ]]
then
local expr="${BASH_REMATCH[1]}"
local base="${BASH_REMATCH[2]}"
line="($expr) * 100 / $base"
# Process "% off"
elif [[ $line =~ (.*)\%\ off\ (.*) ]]
then
local percent="${BASH_REMATCH[1]}"
local original="${BASH_REMATCH[2]}"
line="$original - ($original * $percent / 100)"
# Process "% of"
elif [[ $line =~ (.*)\%\ of\ (.*) ]]
then
local percent="${BASH_REMATCH[1]}"
local total="${BASH_REMATCH[2]}"
line="($total * $percent / 100)"
fi
echo "$line"
}
relative_to_absolute_line ()
{
local absolute_line_number=$1
shift
local line="$1"
if [ -z "$line" ]
then
read -r line
fi
while [[ "$line" =~ (LINE:)([+-]?[0-9]+) ]]
do
adjust=${BASH_REMATCH[2]}
replacement=$((absolute_line_number + adjust))
replacement=$(printf "%05d" $replacement)
replacement="_LABS:$replacement"
line=${line//${BASH_REMATCH[1]}${BASH_REMATCH[2]}/$replacement}
done
echo "$line"
}
calculate_answer_from_line ()
{
line=$1
answer=$(\
echo "$line" |\
sed s/[^[:punct:][:digit:]]//g |\
sed s/[=,?:\|_~\']//g |\
bc -l 2>/dev/null || echo "-"\
)
echo "$answer"
}
strip_trailing_answer ()
{
line=$1
line=${line%#=*}
# shellcheck disable=2001
line=$(echo "$line" | sed 's/ *$//')
echo "$line"
}
strip_trailing_zeros ()
{
# Remove decimal point and trailing zeros if it's a whole number
line=${1%.*}
if [[ $line != "$1" ]]; then
# Trim trailing zeros, but keep decimal point if present
line=${1%%*(0)}
# Remove the decimal point if it's now redundant
line=${line%.}
fi
echo "$line"
}
source_lines=()
preprocessed_lines=()
# Read all lines and preprocess
#
line_count=1
max_line_length=0
while IFS=$'\n' read -r line
do
line=$(strip_trailing_answer "$line")
source_lines[line_count]="$line"
preprocessed_line=$(\
echo "$line" |\
preprocess_percents |\
preprocess_sums |\
relative_to_absolute_line $line_count\
)
preprocessed_lines[line_count]="$preprocessed_line"
line_count=$((line_count+1))
if [[ ${#line} -gt $max_line_length ]]
then
max_line_length=${#line}
fi
done < <(process_lines_from_stdin_or_args "$@")
max_line_length=$((max_line_length+1))
pad_to_max ()
{
line=$1
line_length=${#line}
pad_length=$((max_line_length-line_length))
for i in $(seq 1 $pad_length)
do
line="$line "
done
echo "$line"
}
answers_hash=()
# While answers are changing, keep recalculating
#
while true
do
answers_changed=false
for i in "${!preprocessed_lines[@]}"
do
# current_line_number=$i
line=${preprocessed_lines[$i]}
while [[ "$line" =~ _LABS:([0-9]+) ]]
do
line_reference=${BASH_REMATCH[1]}
answer_index=${line_reference##+(0)}
answer_index=${answer_index:-0}
replacement=${answers_hash[$answer_index]}
line=${line//_LABS:$line_reference/$replacement}
done
answer=$(calculate_answer_from_line "$line")
if [[ ${answers_hash[$i]} != "$answer" ]]
then
answers_changed=true
answers_hash[i]=$answer
fi
done
if [[ $answers_changed == false ]]
then
break
fi
done
# Print original lines with answers side-by-side
#
for i in "${!source_lines[@]}"
do
line=${source_lines[$i]}
if [[ ${line} != "" ]]
then
line=$(pad_to_max "${line}")
answer=${answers_hash[$i]}
echo "$line #= $(strip_trailing_zeros "$answer")"
else
echo ""
fi
done