I want to evaluate a conditional expression string, I defined following BNF:
X ::= <value>
COND ::= X | X '==' X | '!{' COND '}' | '{' COND '&&' COND '}' | '{' COND '||' COND '}'
Based on this structur I wrote this evaluating-function, returning a boolean value:
-- Bedingung auswerten
function eval(exp, valueTab, loopValue)
-- !{COND} - Negierung
if string.find(exp, '^!{(.+)}$') then
return not eval(string.gsub(exp, '^!{(.+)}$', '%1'))
-- {COND&&COND} - AND
elseif string.find(exp, '^{(.+)&&(.+)}$') then
return (eval(string.gsub(exp, '^{(.+)&&(.+)}$', '%1')) and eval(string.gsub(exp, '^{(.+)&&(.+)}$', '%2')))
-- {COND||COND} - OR
elseif string.find(exp, '^{(.+)||(.+)}$') then
return (eval(string.gsub(exp, '^{(.+)||(.+)}$', '%1')) or eval(string.gsub(exp, '^{(.+)||(.+)}$', '%2')))
-- X==X - Gleichheit -> true/false
elseif string.find(exp, '^(.+)==(.+)$') then
return (getValue(string.gsub(exp, '^(.+)==(.+)$', '%1'), valueTab, loopValue) == getValue(string.gsub(exp, '^(.+)==(.+)$', '%2'), valueTab, loopValue))
-- X -> false wenn X nil/false auswertet ansonsten true
else
return (getValue(exp, valueTab, loopValue) and true or false)
end
end
But it's not working for some nested conditions like
exp = '{{1||boolean:false}&&{boolean:true&&!{boolean:false}}}'
The first recursive step splits the expression into
eval('{1||boolean:false}&&{boolean:true') and
eval('!{boolean:false}}'
Any idea how I can check, if the number of '{' is equal to the number of '}'? It should split the expression like
eval('{1||boolean:false}') and
eval('{boolean:true&&!{boolean:false}}')
I hope u understand my question, if u have any further questions let me know. I'm also willing to change my syntax, if u have a better idea. But negation, AND- and OR-Clauses should be supported.
%b{}, which matches balanced{}.'^{(!?%b{})&&(!?%b{})}$'looks pretty good, but i doesnt fetch the values. any idea how to get them without editing the BNF grammar? (for instance to'!'? '{' X }'and'!'? '{'X '==' X'}')