Skip to content

Instantly share code, notes, and snippets.

@sebastiankessel
Created March 8, 2016 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastiankessel/838773f98b33d9d24eeb to your computer and use it in GitHub Desktop.
Save sebastiankessel/838773f98b33d9d24eeb to your computer and use it in GitHub Desktop.
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