From 2763f09cc4747bcfe7d73381f5c8f136dd913d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Emourgeon?= Date: Sun, 12 Jul 2015 22:12:33 +0200 Subject: [PATCH 1/2] Add ^ operator for power. --- Arithmetic.pp | 6 +++++- Test/Unit/Arithmetic.pp | 6 +++++- Visitor/Arithmetic.php | 10 ++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Arithmetic.pp b/Arithmetic.pp index 2e25e1f..fd4abf7 100644 --- a/Arithmetic.pp +++ b/Arithmetic.pp @@ -52,6 +52,7 @@ %token minus \-|− %token times \*|× %token div /|÷ +%token pow \^ %token constant [A-Z_]+[A-Z0-9_]+ %token id \w+ @@ -65,7 +66,10 @@ ternary() ( ::times:: #multiplication expression() )? ternary: - term() ( ::div:: #division expression() )? + quaternary() ( ::div:: #division expression() )? + +quaternary: + term() ( ::pow:: #power expression() )? term: ( ::bracket_:: expression() ::_bracket:: #group ) diff --git a/Test/Unit/Arithmetic.pp b/Test/Unit/Arithmetic.pp index a615516..fd2743b 100644 --- a/Test/Unit/Arithmetic.pp +++ b/Test/Unit/Arithmetic.pp @@ -51,6 +51,7 @@ %token minus \- %token times \* %token div / +%token pow \^ %token constant [A-Z_]+[A-Z0-9_]+ %token id \w+ @@ -64,7 +65,10 @@ ternary() ( ::times:: #multiplication expression() )? ternary: - term() ( ::div:: #division expression() )? + quaternary() ( ::div:: #division expression() )? + +quaternary: + term() ( ::pow:: #power expression() )? term: ( ::bracket_:: expression() ::_bracket:: #group ) diff --git a/Visitor/Arithmetic.php b/Visitor/Arithmetic.php index 1534192..901da74 100644 --- a/Visitor/Arithmetic.php +++ b/Visitor/Arithmetic.php @@ -209,6 +209,16 @@ public function visit( break; + case '#power': + $children[0]->accept($this, $a, $eldnah); + + $acc = function ($b) use ($a, $acc) { + return $acc(pow($a(), $b)); + }; + + $children[1]->accept($this, $acc, $eldnah); + break; + case '#fakegroup': case '#group': $children[0]->accept($this, $a, $eldnah); From d08ba387cf5d5ac40021585fe3e0a208d117ba7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Emourgeon?= Date: Mon, 13 Jul 2015 09:22:52 +0200 Subject: [PATCH 2/2] Fix coding style. --- Visitor/Arithmetic.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Visitor/Arithmetic.php b/Visitor/Arithmetic.php index 901da74..e9253bc 100644 --- a/Visitor/Arithmetic.php +++ b/Visitor/Arithmetic.php @@ -217,6 +217,7 @@ public function visit( }; $children[1]->accept($this, $acc, $eldnah); + break; case '#fakegroup':