-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcalc
executable file
·255 lines (237 loc) · 8.64 KB
/
bcalc
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
#!/usr/bin/env bash
## A calculator in pure-bash.
## I even made a pure-bash lexer and parser for this.
## Not gonna lie I feel pretty accomplished right now.
## ---------------------------------------------------
## Created by: Phate6660 [https://github.com/Phate6660]
## Dependencies: Only bash ;)
DEBUG=false
FORMATTED=false
if [ -n "${2}" ]; then
if [ "${2}" == "--debug" ]; then
DEBUG=true
fi
if [ "${2}" == "--formatted" ] || [ "${3}" == "--formatted" ]; then
FORMATTED=true
fi
fi
## Argument Gathering and Preparation
# Get the first arg (since that's all we need), and add a space between each character.
# Prints a debug message.
debug() {
local message="${1}"
if ${DEBUG}; then
printf "%s\n" "${message}"
fi
}
# Debugs an array by looping over it and printing each element.
debug_array() {
local name="${1}"
local count="${2}"
declare -a array
for arg in "${@:3}"; do
array+=("${arg}")
done
for ((index=0;index<=count;index++)); do
debug "${name}[${index}] = ${array[${index}]}"
done
}
args="${1}"
args_count="${#args}"
for ((char_index=0; char_index<args_count; char_index++)); do
chars_spaces+="${args:${char_index}:1} "
done
# Read each space-separated character into the array.
# shellcheck disable=SC2162
read -a chars_array <<< "${chars_spaces}"
## Lexing
# Iterate over the argument array.
tmp_counter=0
for char in "${chars_array[@]}"; do
# Check if "$char" is a parenthesis, and if so append to final_array.
if [ "${char}" == "(" ] || [ "${char}" == ")" ]; then
final_array+=("${char}")
continue
# Check if "$char" is a number, and if so append to final_array.
elif [[ "${char}" =~ ^[0-9]+$ ]]; then
final_array+=("${char}")
continue
# Check if "$char" is a math operation, and if so append to final_array.
elif [ "${char}" == "^" ] \
|| [ "${char}" == "*" ] \
|| [ "${char}" == "/" ] \
|| [ "${char}" == "%" ] \
|| [ "${char}" == "+" ] \
|| [ "${char}" == "-" ]; then
# Up n by 1 before and after appending the math operation to account for array index offsets.
tmp_counter=$((tmp_counter + 1))
final_array+=("${char}")
tmp_counter=$((tmp_counter + 1))
continue
else
# If the character isn't a digit, math operation, or parenthesis, display an error message and exit.
printf "Only digits, parenthesis, '^', '*', '%%', '/', '+', and '-', are supported.\n'%s' is currently unsupported.\n" "${char}"
exit 1
fi
done
final_array_count="${#final_array[@]}"
debug_array "final_array" "${final_array_count}" "${final_array[@]}"
## Parsing
for ((index=0;index<=final_array_count;index++)); do
if [ "${index}" == "${final_array_count}" ]; then
if [ -n "${mn}" ]; then
debug "mn = ${mn}"
numbers="${mn[*]}"
unset mn
numbers="${numbers// /}"
debug "numbers = ${numbers}"
number_array+=("${numbers}")
unset numbers
break
else
# No more looping is needed. Break!
break
fi
else
# Check if the character is an opening parenthesis...
if [ "${final_array[${index}]}" == "(" ]; then
unset op_found
paren=true
continue
# Check if the character is a closing parenthesis...
elif [ "${final_array[${index}]}" == ")" ]; then
unset op_found
paren=false
ip="${ip[*]}"
ip="${ip// /}"
ip="${ip/\^/\*\*}"
if [[ "${ip}" == *-* ]]; then
# BASH isn't fully respecting PEMDAS,
# so I split the input based on '-' as a delimiter.
# I then calculate each side of the '-' before subtracting
# the right side from the left site.
IFS='-' read -ra ip_split <<< "${ip}"
debug "ip_split = ${ip_split[*]}"
first=$((ip_split[0]))
second=$((ip_split[1]))
ip=$((first - second))
unset first second
else
ip=$(("${ip}"))
fi
debug "ip = ${ip}"
number_array+=("${ip}")
unset ip
continue
# If the element is an operation, append to operation_array.
elif [ "${final_array[${index}]}" == "^" ] \
|| [ "${final_array[${index}]}" == "*" ] \
|| [ "${final_array[${index}]}" == "/" ] \
|| [ "${final_array[${index}]}" == "%" ] \
|| [ "${final_array[${index}]}" == "+" ] \
|| [ "${final_array[${index}]}" == "-" ]; then
if [ "${index}" == 0 ]; then
if [ "${final_array[${index}]}" == "-" ]; then
# Do nothing, skipping the error. It's a negative number so it's all good.
negative_first=true
else
printf "Currently other notations are unsupported, please start with a number."
exit 1
fi
fi
if [ -n "${op_found}" ]; then
printf "You may not have more than one operation between numbers." && exit 1
fi
op_found=true
if ${paren}; then
ip+=("${final_array[${index}]}")
else
operation_array+=("${final_array[${index}]}")
fi
if [ -n "${mn}" ]; then
debug "mn = ${mn[*]}"
numbers="${mn[*]}"
unset mn
numbers="${numbers// /}"
debug "numbers = ${numbers}"
number_array+=("${numbers}")
unset numbers
continue
else
continue
fi
# If the element is a number...
elif [[ "${final_array[${index}]}" =~ ^[0-9]+$ ]]; then
unset op_found
# If "$paren" is set...
if [ "${paren}" == true ]; then
ip+=("${final_array[${index}]}")
continue
else
mn+=("${final_array[${index}]}")
continue
fi
fi
fi
done
number_count="${#number_array[@]}"
operation_count="${#operation_array[@]}"
# Example for this snippet:
# `./bcalc 1+1+` would break the script.
# Adding this outputs an error message and gracefully exits.
#
# If the first number is a negative, the amount of operations shouldn't be more than the amount of numbers.
if ${negative_first} && ((operation_count > number_count)); then
printf "The count of operations shouldn't be greater than the count of numbers."
exit 1
# If the first number is NOT is a negative number, then the amount of operations shouldn't be
# greater than or equal to the amount of numbers.
elif [ -z "${negative_first}" ] && ((operation_count >= number_count)); then
printf "The count of operations shouldn't be greater than or equal to the count of numbers."
exit 1
fi
# This is useful for seeing the contents of each index of the number_array and operation_array variables.
debug_array "number_array" "${number_count}" "${number_array[@]}"
debug_array "operation_array" "${operation_count}" "${operation_array[@]}"
# This is useful to make sure that the counts in the arrays are accurate to what they should be.
debug "number_count = ${number_count}"
debug "operation_count = ${operation_count}"
## Finishing Touches
# Assemble the final calculation variable.
index=0
for ((loop_count=0;loop_count<=final_array_count;loop_count++)); do
if [ "${loop_count}" = "${number_count}" ]; then
break
else
if [ -n "${negative_first}" ]; then
calculation_array+=("${operation_array[${index}]}")
if [ "${loop_count}" == "${operation_count}" ]; then
continue
else
calculation_array+=("${number_array[${index}]}")
index=$((index + 1))
continue
fi
else
calculation_array+=("${number_array[${index}]}")
if [ "${loop_count}" == "${operation_count}" ]; then
continue
else
calculation_array+=("${operation_array[${index}]}")
index=$((index + 1))
continue
fi
fi
fi
done
calculation="${calculation_array[*]}"
calculation="${calculation// /}"
calculation="${calculation/\^/\*\*}"
debug "calculation = ${calculation}"
## Output
# Output the result of the calculation without any formatting.
if ${FORMATTED}; then
printf "%'d\n" $((calculation))
else
printf "%d\n" $((calculation))
fi