-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_calculator_v1.lua
More file actions
74 lines (60 loc) · 2 KB
/
Copy pathstring_calculator_v1.lua
File metadata and controls
74 lines (60 loc) · 2 KB
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
local gsub = string.gsub
local find = string.find
local sub = string.sub
local reverse = string.reverse
local operations = {" ^ "," / "," * "," - "," + "}
local getmath = {
[" ^ "] = function (a,b) return a^b end,
[" / "] = function (a,b) return a/b end,
[" * "] = function (a,b) return a*b end,
[" - "] = function (a,b) return a-b end,
[" + "] = function (a,b) return a+b end
}
local len_operations = #operations
local function expr(val)
if find(val, "[0-9]") == nil then
return val
end
val = gsub(gsub(gsub(gsub(gsub(gsub(gsub(gsub(val,
" ", ""),
"(%d+%.*%d*)", " %1 "),
"%(", " %("),
"%)", "%) "),
"([^ ])%- ", "%1 -"),
"([^ ])%+ ", "%1 "),
"^%- ", " -"),
"^%+ ", " ")
while true do
local temp = val
if find(val, ")",1,true) ~= nil then
temp = sub(val, 1, find(val, ")",1,true) - 1)
temp = sub(temp, 1 - find(reverse(temp), "(", 1, true))
end
local temp2 = temp
for i = 1, len_operations do
local operation = operations[i]
while true do
if find(temp2, operation, 1, true) == nil then
break
end
local switcher = sub(temp2, 1, find(temp2, operation, 1, true) - 1)
local switcher = sub(switcher, 1 - (find(reverse(switcher), " ", 1 , true) or 0))
local switcher2 = sub(temp2, find(temp2, operation, 1, true) + 3)
local switcher2 = sub(switcher2, 1, (find(switcher2, " ", 1, true) or 0) - 1)
local switcher3 = getmath[operation](switcher,switcher2)
local switcher = gsub(switcher, "([%.%-])", "%%%1")
local switcher2 = gsub(switcher2, "([%.%-])", "%%%1")
temp2 = gsub(temp2, " " .. switcher .. " %".. sub(operation,2) .. switcher2 .. " ", " " .. switcher3 .. " ")
end
end
if find(val, ")", 1, true) == nil and find(val, "(", 1, true) == nil then
val = temp2
break
end
temp = gsub(temp, "([%.%-%+%*%/%^])", "%%%1")
val = gsub(val, " %(" .. temp .. "%) ", temp2)
end
if val * 2 ~= nil then
return gsub(val, " ", "")
end
end