From cd407ee2b575565e821c8758c6efaf6476ccc5e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rcio=20Almada?= Date: Wed, 17 Apr 2019 02:16:19 -0300 Subject: [PATCH] Fix bug when expanding an Ast leaf which is a single token cc https://github.com/ircmaxell/php-compiler/pull/29 --- src/Expansion.php | 3 ++ .../issues/ircmaxell-php-compiler#29.phpt | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tests/phpt/issues/ircmaxell-php-compiler#29.phpt diff --git a/src/Expansion.php b/src/Expansion.php index aad5b3b..8d1d1cd 100644 --- a/src/Expansion.php +++ b/src/Expansion.php @@ -232,6 +232,9 @@ private function mutate(TokenStream $ts, Ast $context, Engine $engine) : TokenSt $delimiters = $result->{'delimiters'}; + // normalize when context is a single token + if ($context instanceof Token) $context = [[$context]]; + // normalize associative arrays if (array_values($context) !== $context) $context = [$context]; diff --git a/tests/phpt/issues/ircmaxell-php-compiler#29.phpt b/tests/phpt/issues/ircmaxell-php-compiler#29.phpt new file mode 100644 index 0000000..4e3fb43 --- /dev/null +++ b/tests/phpt/issues/ircmaxell-php-compiler#29.phpt @@ -0,0 +1,51 @@ +--TEST-- +Test for bug found at https://github.com/ircmaxell/php-compiler/pull/29 --pretty-print +--FILE-- +> { + $(stmts ... { + $(assignop ? ... { + $(binary ? ... { + $(binary_op ... { + $(binary_xor ? ... { + $(result) = $this->context->builder->bitwiseXor($(binary_left), $__right); + }) + }) + }) + }) + }) +} + +compile { + $result = $value ^ 1; +} + +?> +--EXPECTF-- +context->builder->bitwiseXor($value, $__right); + +?>