Library for evaluating prerequisites writen in simple infix language.
This library evaluates simple c-like logical expressions with given variables. It was made for MineHub academys prerequisites.
As previously mentioned, it uses c-like logical expression syntax.
Variable names can contain every character except those: ()|&!
- these are reserved for other syntax. Variables can be only boolean and will be loaded from eval function. See evaluation
In examples we would use true and false to make it simpler, but you can use any variable name you want, as long as it doesn't contain reserved words.
You can use operators or (||
) with and (&&
), where and has bigger priority, meaning that
true || true && false
would evaluate as true
, instead of false
.
To change priority, you can use groups (just put it in brackets). So this
(true || false ) && false
would evaluate as false
We can also negate variables and groups:
!true
would evaluate as false
, and
!(true || false)
would also evaluate as false
.
Everything is expression, so you can do stuff like:
(true || (false || true)) && (!(true || (false || true)) && false)
TODO maybe its not correct
<variable> ::= [^\s\(\)\|&!]+
<expression> ::= <variable>
<expression> ::= <expression>"||"<expression>
<expression> ::= <expression>"&&"<expression>
<expression> ::= "("<expression>")"
<expression> ::= "!"<expression>
To evaluate string, simply create instance of \MineHub\Prerequisities\Evaluator
and use eval
method. This method takes code and array of variables:
$evaluator = new Evaluator();
$evaluator->eval('!(!foo || bar) && (foo && !bar)', ['foo' => true, 'bar' => false]); // returns true