speedmax/h2o-php — pull request #74 by jonathanverner (2014-05-19 22:33:30+00:00)
Title: Feature: Implement complex expressions

I found it annoying that the `{% if %}` tag only allowed simple expressions (no `and`, `or`, ...)
which forced me to nest if blocks. Similarly I found it convenient to be able to write

{{ year-1 }}

instead of populating the scope with a new variable `$prev_year`. These two commits are trying to implement the two features.

The first commit implements a simple stack-based expression parser/evaluator which allows compound expressions in the `{% if %}` tag. The second commit, which I am less sure of, implements expression evaluation for variable blocks.

I am not quite sure the second commit doesn't break something (although all unit tests pass and I didn't notice any breakage) because it changes the way the `parseArguments` method works. That method is not used in many places, thought.

The way it works now is as follows:

When parsing a variable block in `parseArguments` the code iterates over the list of tokens until it reach a `filter_start` token or the end. At that point it adds an `expression_end` token to the result and continue exactly as in the original code. So the difference in `parseArguments` is only that a new `expression_end` token is added to the returned array.

The rest of the work is done in the `render` method of the `VariableNode` where we first call `Evaluator::eval_expression` (the expression parser/evaluator) to evaluate the expression and then
we apply any potential filters.