hoaproject/Ruler — pull request #10 by Hywan (2014-03-02 10:12:06+00:00)
Title: Add object access support

So here is a script to test:
```php
f(5)';

// Compile and dump the AST.
echo (new Hoa\Compiler\Visitor\Dump())->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.
class C {

public function foo ( $h ) {

return [2.2 * $h, 6 * $h];
}
}

$context = new Hoa\Ruler\Context();
$context['x'] = [3 => new C()];
$context['y'] = 3;

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

return $x - 1;
});

// 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->variable('x')
->index(
$model->variable('y')
)
->call(
$model->func(
'foo',
2
)
)
->index(
0
),
$model->func(
'f',
5
)
);

// 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 the result:
```php
> #expression
> > #operation
> > > #variable_access
> > > > token(identifier, x)
> > > > #array_access
> > > > > token(identifier, y)
> > > > #object_access
> > > > > #function_call
> > > > > > token(identifier, foo)
> > > > > > token(integer, 2)
> > > > #array_access
> > > > > token(integer, 0)
> > > token(identifier, >)
> > > #function_call
> > > > token(identifier, f)
> > > > token(integer, 5)

$model = new \Hoa\Ruler\Model();
$model->expression =
$model->{'>'}(
$model->variable('x')
->index(
$model->variable('y')
)
->call(
$model->func(
'foo',
2
)
)
->index(
0
),
$model->func(
'f',
5
)
);

bool(true)
bool(true)
(x[y].foo(2)[0] > f(5))
(x[y].foo(2)[0] > f(5))
```
That's it :-).

Should close the issue https://github.com/hoaproject/Ruler/issues/8.