I have a CLI calculator, and I am adding square root function. I have this regex that parses the users input:
string.scan(/\d*\.?\d+\^?|[-+\/*%()]|sqrt\(\d*\.?\d+\)/)
It works with these inputs as expected:
calc -o "sqrt(9)" #=> ["sqrt(9)"]
calc -o "sqrt(9) + sqrt(9)" #=> ["sqrt(9)", "+", "sqrt(9)"]
However, my regex has not accounted for nested sqrt. With this,
calc -0 "sqrt(6+3)"
I desire the output:
["sqrt(6+3)"]
because when the program finds sqrt
while searching, it will simply recursively apply the scan
method with the regex until it gets into the deepest nested formula and work its way back. But I get:
["(", "6", "+", "3", ")"]
I have tried to capture all but within the sqrt brackets, but it also captures everything in every other bracket as well. So I am having trouble capturing sqrt(9)
and sqrt(6+3)
without one messing with the other.
Any guidance is much appreciated.
UPDATE: So following on from an answer provided, perhaps I need to explain my program more so you get an idea of what is going on.
Say I have the input 2 * (3 + 5)
, this will be interpreted into the following array:
["2", "*", "(", "3", "+", "5", ")"]
So program conforms to PEDMAS so will first look for parenthesis, in this situation it finds them. A basic loop is basically like this:
function find_backets
start_i, end_i
for i in array do
if i == "("
start_i = index
find_brackets
end
if i == ")"
end_i = index
# end of nest
end
end
I can then pass my start and end locations within the array to a function that will iteration over each nested operation. So the above can interpret this just fine:
calc -o "2 * (6 + (2 * 2))"
#=> ["2", "*", "(", "6", "+", "(", "2", "*", "2", ")", ")"]
My idea is that when it comes across the sqrt function, it will simply just reuse the same regex used for the users input, and create a whole new array and do the above on it. Then once it's done, I take index 0 and place it where the sqrt used to be.
EDIT: So yeah didn't actually mentioned, I at to basically capture the entirely of a sqrt. So anything and everything in something like sqrt(5+5*(6/2+sqrt(9))
UPDATE: I think I have found a solution
So I done some more reading to learn how * + ?
and that worked a bit more and I think (at least so far) this is working
string.scan(/\d*\.?\d+\^?|[-+\/*%()^]|sqrt\(.+?\)+|pi/)
calc -o "sqrt(9)" #=> ["sqrt(9)"]
calc -o "sqrt(3+6)" #=> ["sqrt(3+6)"]
calc -o "sqrt(9) + sqrt(9)" #=> ["sqrt(9)", "+", "sqrt(9)"]
calc -o "sqrt(9) + 2" #=> ["sqrt(9)", "+", "2"]
Will update in a bit