hoaproject/Ruler — pull request #12 by Hywan (2014-03-03 22:26:14+00:00)
Title: Support arimethical expressions

See https://github.com/hoaproject/Ruler/issues/11 for more details.

You can test with the following program:
```php
visit(
Hoa\Ruler\Ruler::getCompiler()->parse($rule)
), "\n\n";

// Get the model directly (use the helper, just because I'm tired :-)).
$imodel = Hoa\Ruler\Ruler::interprete($rule);

echo $imodel, "\n\n";

// Context.
$context = new Hoa\Ruler\Context();
$context['y'] = [5];

// Prepare some functions.
$asserter = new Hoa\Ruler\Visitor\Asserter($context);
$asserter->setOperator('f', function ( $x ) {

return $x;
});

// First assert.
var_dump($asserter->visit($imodel));

// Here copy the result of the output of $imodel (which is the result of the
// compiler) to compare the result (test the model).
$model = new \Hoa\Ruler\Model();
$model->expression =
$model->{'='}(
$model->func(
'-',
$model->{'-'}(
10,
$model->{'-'}(
2,
$model->{'-'}(
2,
1,
-1
),
-1
),
1
)
),
$model->func(
'-',
$model->variable('y')
->index(
0
)
)
);

// Assert again.
var_dump($asserter->visit($model));

// Compare the disassembler of both models (from the rule and from the model).
$disassembly = new Hoa\Ruler\Visitor\Disassembly();
echo $disassembly->visit($imodel), "\n",
$disassembly->visit($model), "\n";
```
and here is the result:
```php
> #expression
> > #operation
> > > #negative
> > > > #substraction
> > > > > token(number, 10)
> > > > > #substraction
> > > > > > token(number, 2)
> > > > > > #substraction
> > > > > > > token(number, 2)
> > > > > > > token(number, 1)
> > > token(identifier, =)
> > > #negative
> > > > #variable_access
> > > > > token(identifier, y)
> > > > > #array_access
> > > > > > token(number, 0)

$model = new \Hoa\Ruler\Model();
$model->expression =
$model->{'='}(
$model->func(
'-',
$model->{'-'}(
10,
$model->{'-'}(
2,
$model->{'-'}(
2,
1,
-1
),
-1
),
1
)
),
$model->func(
'-',
$model->variable('y')
->index(
0
)
)
);

bool(true)
bool(true)
(-((10 - (2 - (2 - 1)))) = -(y[0]))
(-((10 - (2 - (2 - 1)))) = -(y[0]))
```
The only BC break introduced is the array declaration syntax (`(1, 2, 3)` to `[1, 2, 3]`). This is the first BC break of Hoa :'-), but since the library is still in the RC state, we can do this.

Also, the `README` is updated consequently.