aboutsummaryrefslogtreecommitdiff
path: root/src/NXP/Classes/Token
diff options
context:
space:
mode:
Diffstat (limited to 'src/NXP/Classes/Token')
-rw-r--r--src/NXP/Classes/Token/AbstractContainerToken.php46
-rw-r--r--src/NXP/Classes/Token/AbstractOperator.php20
-rw-r--r--src/NXP/Classes/Token/InterfaceFunction.php23
-rw-r--r--src/NXP/Classes/Token/InterfaceOperator.php33
-rw-r--r--src/NXP/Classes/Token/InterfaceToken.php22
-rw-r--r--src/NXP/Classes/Token/TokenComma.php25
-rw-r--r--src/NXP/Classes/Token/TokenDegree.php54
-rw-r--r--src/NXP/Classes/Token/TokenDivision.php54
-rw-r--r--src/NXP/Classes/Token/TokenFunction.php41
-rw-r--r--src/NXP/Classes/Token/TokenLeftBracket.php25
-rw-r--r--src/NXP/Classes/Token/TokenMinus.php54
-rw-r--r--src/NXP/Classes/Token/TokenMultiply.php54
-rw-r--r--src/NXP/Classes/Token/TokenNumber.php25
-rw-r--r--src/NXP/Classes/Token/TokenPlus.php54
-rw-r--r--src/NXP/Classes/Token/TokenRightBracket.php25
15 files changed, 555 insertions, 0 deletions
diff --git a/src/NXP/Classes/Token/AbstractContainerToken.php b/src/NXP/Classes/Token/AbstractContainerToken.php
new file mode 100644
index 0000000..12d49d2
--- /dev/null
+++ b/src/NXP/Classes/Token/AbstractContainerToken.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+abstract class AbstractContainerToken implements InterfaceToken
+{
+ /**
+ * @var string
+ */
+ protected $value;
+
+ /**
+ * @param string $value
+ */
+ public function __construct($value)
+ {
+ $this->value = $value;
+ }
+
+ /**
+ * @param string $value
+ */
+ public function setValue($value)
+ {
+ $this->value = $value;
+ }
+
+ /**
+ * @return string
+ */
+ public function getValue()
+ {
+ return $this->value;
+ }
+}
diff --git a/src/NXP/Classes/Token/AbstractOperator.php b/src/NXP/Classes/Token/AbstractOperator.php
new file mode 100644
index 0000000..6cdfe99
--- /dev/null
+++ b/src/NXP/Classes/Token/AbstractOperator.php
@@ -0,0 +1,20 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+abstract class AbstractOperator implements InterfaceToken, InterfaceOperator
+{
+ const RIGHT_ASSOC = 'RIGHT';
+ const LEFT_ASSOC = 'LEFT';
+}
diff --git a/src/NXP/Classes/Token/InterfaceFunction.php b/src/NXP/Classes/Token/InterfaceFunction.php
new file mode 100644
index 0000000..be867b0
--- /dev/null
+++ b/src/NXP/Classes/Token/InterfaceFunction.php
@@ -0,0 +1,23 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+interface InterfaceFunction
+{
+ /**
+ * @param array $stack
+ * @return $this
+ */
+ public function execute(&$stack);
+}
diff --git a/src/NXP/Classes/Token/InterfaceOperator.php b/src/NXP/Classes/Token/InterfaceOperator.php
new file mode 100644
index 0000000..da6cdf0
--- /dev/null
+++ b/src/NXP/Classes/Token/InterfaceOperator.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+interface InterfaceOperator
+{
+ /**
+ * @return int
+ */
+ public function getPriority();
+
+ /**
+ * @return string
+ */
+ public function getAssociation();
+
+ /**
+ * @param array $stack
+ * @return TokenNumber
+ */
+ public function execute(&$stack);
+}
diff --git a/src/NXP/Classes/Token/InterfaceToken.php b/src/NXP/Classes/Token/InterfaceToken.php
new file mode 100644
index 0000000..86fec91
--- /dev/null
+++ b/src/NXP/Classes/Token/InterfaceToken.php
@@ -0,0 +1,22 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+interface InterfaceToken
+{
+ /**
+ * @return string
+ */
+ public static function getRegex();
+}
diff --git a/src/NXP/Classes/Token/TokenComma.php b/src/NXP/Classes/Token/TokenComma.php
new file mode 100644
index 0000000..f590e32
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenComma.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+class TokenComma implements InterfaceToken
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '\,';
+ }
+}
diff --git a/src/NXP/Classes/Token/TokenDegree.php b/src/NXP/Classes/Token/TokenDegree.php
new file mode 100644
index 0000000..8488dcd
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenDegree.php
@@ -0,0 +1,54 @@
+<?php
+/**
+* This file is part of the MathExecutor package
+*
+* (c) Alexander Kiryukhin
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code
+*/
+
+namespace NXP\Classes\Token;
+
+/**
+* @author Alexander Kiryukhin <alexander@symdev.org>
+*/
+class TokenDegree extends AbstractOperator
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '\^';
+ }
+
+ /**
+ * @return int
+ */
+ public function getPriority()
+ {
+ return 3;
+ }
+
+ /**
+ * @return string
+ */
+ public function getAssociation()
+ {
+ return self::RIGHT_ASSOC;
+ }
+
+ /**
+ * @param InterfaceToken[] $stack
+ * @return $this
+ */
+ public function execute(&$stack)
+ {
+ $op2 = array_pop($stack);
+ $op1 = array_pop($stack);
+ $result = $op1->getValue() ^ $op2->getValue();
+
+ return new TokenNumber($result);
+ }
+}
diff --git a/src/NXP/Classes/Token/TokenDivision.php b/src/NXP/Classes/Token/TokenDivision.php
new file mode 100644
index 0000000..479a4ec
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenDivision.php
@@ -0,0 +1,54 @@
+<?php
+/**
+* This file is part of the MathExecutor package
+*
+* (c) Alexander Kiryukhin
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code
+*/
+
+namespace NXP\Classes\Token;
+
+/**
+* @author Alexander Kiryukhin <alexander@symdev.org>
+*/
+class TokenDivision extends AbstractOperator
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '\/';
+ }
+
+ /**
+ * @return int
+ */
+ public function getPriority()
+ {
+ return 2;
+ }
+
+ /**
+ * @return string
+ */
+ public function getAssociation()
+ {
+ return self::LEFT_ASSOC;
+ }
+
+ /**
+ * @param InterfaceToken[] $stack
+ * @return $this
+ */
+ public function execute(&$stack)
+ {
+ $op2 = array_pop($stack);
+ $op1 = array_pop($stack);
+ $result = $op1->getValue() / $op2->getValue();
+
+ return new TokenNumber($result);
+ }
+}
diff --git a/src/NXP/Classes/Token/TokenFunction.php b/src/NXP/Classes/Token/TokenFunction.php
new file mode 100644
index 0000000..23f64bd
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenFunction.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+class TokenFunction extends AbstractContainerToken implements InterfaceFunction
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
+ }
+
+ /**
+ * @param array $stack
+ * @return $this
+ */
+ public function execute(&$stack)
+ {
+ $args = array();
+ list($places, $function) = $this->value;
+ for ($i = 0; $i < $places; $i++) {
+ array_push($args, array_pop($stack)->getValue());
+ }
+ $result = call_user_func_array($function, $args);
+
+ return new TokenNumber($result);
+ }
+}
diff --git a/src/NXP/Classes/Token/TokenLeftBracket.php b/src/NXP/Classes/Token/TokenLeftBracket.php
new file mode 100644
index 0000000..0cfdc1a
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenLeftBracket.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+class TokenLeftBracket implements InterfaceToken
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '\(';
+ }
+}
diff --git a/src/NXP/Classes/Token/TokenMinus.php b/src/NXP/Classes/Token/TokenMinus.php
new file mode 100644
index 0000000..0463d4c
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenMinus.php
@@ -0,0 +1,54 @@
+<?php
+/**
+* This file is part of the MathExecutor package
+*
+* (c) Alexander Kiryukhin
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code
+*/
+
+namespace NXP\Classes\Token;
+
+/**
+* @author Alexander Kiryukhin <alexander@symdev.org>
+*/
+class TokenMinus extends AbstractOperator
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '\-';
+ }
+
+ /**
+ * @return int
+ */
+ public function getPriority()
+ {
+ return 1;
+ }
+
+ /**
+ * @return string
+ */
+ public function getAssociation()
+ {
+ return self::LEFT_ASSOC;
+ }
+
+ /**
+ * @param InterfaceToken[] $stack
+ * @return $this
+ */
+ public function execute(&$stack)
+ {
+ $op2 = array_pop($stack);
+ $op1 = array_pop($stack);
+ $result = $op1->getValue() - $op2->getValue();
+
+ return new TokenNumber($result);
+ }
+}
diff --git a/src/NXP/Classes/Token/TokenMultiply.php b/src/NXP/Classes/Token/TokenMultiply.php
new file mode 100644
index 0000000..e6fd960
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenMultiply.php
@@ -0,0 +1,54 @@
+<?php
+/**
+* This file is part of the MathExecutor package
+*
+* (c) Alexander Kiryukhin
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code
+*/
+
+namespace NXP\Classes\Token;
+
+/**
+* @author Alexander Kiryukhin <alexander@symdev.org>
+*/
+class TokenMultiply extends AbstractOperator
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '\*';
+ }
+
+ /**
+ * @return int
+ */
+ public function getPriority()
+ {
+ return 2;
+ }
+
+ /**
+ * @return string
+ */
+ public function getAssociation()
+ {
+ return self::LEFT_ASSOC;
+ }
+
+ /**
+ * @param InterfaceToken[] $stack
+ * @return $this
+ */
+ public function execute(&$stack)
+ {
+ $op2 = array_pop($stack);
+ $op1 = array_pop($stack);
+ $result = $op1->getValue() * $op2->getValue();
+
+ return new TokenNumber($result);
+ }
+}
diff --git a/src/NXP/Classes/Token/TokenNumber.php b/src/NXP/Classes/Token/TokenNumber.php
new file mode 100644
index 0000000..763ca04
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenNumber.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+class TokenNumber extends AbstractContainerToken
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '\-?\d+\.?\d*';
+ }
+}
diff --git a/src/NXP/Classes/Token/TokenPlus.php b/src/NXP/Classes/Token/TokenPlus.php
new file mode 100644
index 0000000..f9562e7
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenPlus.php
@@ -0,0 +1,54 @@
+<?php
+/**
+* This file is part of the MathExecutor package
+*
+* (c) Alexander Kiryukhin
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code
+*/
+
+namespace NXP\Classes\Token;
+
+/**
+* @author Alexander Kiryukhin <alexander@symdev.org>
+*/
+class TokenPlus extends AbstractOperator
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '\+';
+ }
+
+ /**
+ * @return int
+ */
+ public function getPriority()
+ {
+ return 1;
+ }
+
+ /**
+ * @return string
+ */
+ public function getAssociation()
+ {
+ return self::LEFT_ASSOC;
+ }
+
+ /**
+ * @param InterfaceToken[] $stack
+ * @return $this
+ */
+ public function execute(&$stack)
+ {
+ $op2 = array_pop($stack);
+ $op1 = array_pop($stack);
+ $result = $op1->getValue() + $op2->getValue();
+
+ return new TokenNumber($result);
+ }
+}
diff --git a/src/NXP/Classes/Token/TokenRightBracket.php b/src/NXP/Classes/Token/TokenRightBracket.php
new file mode 100644
index 0000000..c68473a
--- /dev/null
+++ b/src/NXP/Classes/Token/TokenRightBracket.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * This file is part of the MathExecutor package
+ *
+ * (c) Alexander Kiryukhin
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code
+ */
+
+namespace NXP\Classes\Token;
+
+/**
+ * @author Alexander Kiryukhin <alexander@symdev.org>
+ */
+class TokenRightBracket implements InterfaceToken
+{
+ /**
+ * @return string
+ */
+ public static function getRegex()
+ {
+ return '\)';
+ }
+}