Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Support arimethical expressions #12

Closed
wants to merge 2 commits into from
Closed

Conversation

Hywan
Copy link
Member

@Hywan Hywan commented Mar 3, 2014

See #11 for more details.

You can test with the following program:

<?php

require '/usr/local/lib/Hoa/Core/Core.php';

Hoa\Core::enableErrorHandler();
Hoa\Core::enableExceptionHandler();

// Rule.
$rule = '-(10 - 2 - 2 - 1) = -y[0]';

// 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.
$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:

>  #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. Only the syntax (so the grammar and the disassembler) is impacted; the interpreter, the asserter and the model are not impacted (so unserializing an existing rule from a database will not break).

Also, the README is updated consequently.

Precedence order for operators:

  * logical: `not`, `and`, `or` and `xor`,
  * user-defined,
  * arithmetical: `+`, `-` (or `−`), `*` (or `×`), `/` (or `÷`), `**`
    and `%`.

Also support scientist notation (for example 4.2e-7).
@Hywan
Copy link
Member Author

Hywan commented Mar 3, 2014

I am asking the review of @stephpy and @shouze. Can you test with your programs?

@stephpy
Copy link
Member

stephpy commented Mar 4, 2014

Ok, i'll test it asap.

@flip111
Copy link

flip111 commented Mar 4, 2014

what about unit tests for stuff like this? I'm just worried because new features here would break the PHPDumper most likely :(

@stephpy
Copy link
Member

stephpy commented Mar 4, 2014

@flip111 +1 but hoa has it owns test system, i let @Hywan introduce it. :)

@Hywan
Copy link
Member Author

Hywan commented Mar 4, 2014

@flip111 Tests are under migrations. Our tests pass, I ask @stephpy and @shouze to run their own test suites to get other feedbacks.

@Hywan
Copy link
Member Author

Hywan commented Mar 4, 2014

@flip111 Also, it should not break anything in your php-dumper since the implementation of these new operators relies on the same user-defined operators.

@Hywan Hywan mentioned this pull request Mar 28, 2014
@Hywan
Copy link
Member Author

Hywan commented Mar 28, 2014

The grammar is right-associative and not left-associative. We should fix it. /cc @guiled

@guiled
Copy link
Member

guiled commented Mar 28, 2014

Thank you !

@Hywan
Copy link
Member Author

Hywan commented Sep 22, 2014

Depends on hoaproject/Math#8.
Edit: merged.

@Hywan
Copy link
Member Author

Hywan commented Sep 23, 2014

Before merging this PR, we have to use […] instead of (…) for arrays. Based on that, we will be able to finalize this library and then introduce new feature. @stephpy is agree with that.

@stephpy
Copy link
Member

stephpy commented Sep 23, 2014

👍

@Bhoat Bhoat closed this in #22 Sep 23, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

4 participants