-
-
Save sebastiankessel/838773f98b33d9d24eeb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static String simplify(String expression) | |
{ | |
expression = expression.trim(); | |
while (expression.contains('(')) | |
{ | |
string parentLogic = expression.substringAfter('(').substringBeforeLast(')'); | |
string result = evaluate(parentLogic) ? 'true' : 'false'; | |
expression = expression.replace('(' + parentLogic + ')', result); | |
} | |
return expression; | |
} | |
static Boolean evaluate(String expression) | |
{ | |
expression = simplify(expression); | |
if (!isSimpleExpression(expression)) | |
{ | |
if (expression.contains('&&')) | |
{ | |
return andJoin( | |
expression.substringBefore('&&'), | |
expression.substringAfter('&&') | |
); | |
} | |
if (expression.contains('||')) | |
{ | |
return orJoin( | |
expression.substringBefore('||'), | |
expression.substringAfter('||') | |
); | |
} | |
if (expression.startsWith('!')) return !evaluate(expression.substring(1)); | |
} | |
return Boolean.valueOf(expression); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment