Completed
Push — master ( a65034...5a3549 )
by Michal
03:28
created

Expression::isOperator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
    Copyright (c) 2016 Michal Čihař <[email protected]>
4
5
    This file is part of SimpleMath.
6
7
    This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 2 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License along
18
    with this program; if not, write to the Free Software Foundation, Inc.,
19
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
*/
21
namespace SimpleMath;
22
23
abstract class Expression {
24
25
    protected $value = '';
26
    protected $precidence = 0;
27
    protected $leftAssoc = true;
28
29
    public function __construct($value) {
30
        $this->value = $value;
31
    }
32
33
    public function getPrecidence() {
34
        return $this->precidence;
35
    }
36
37
    public function isLeftAssoc() {
38
        return $this->leftAssoc;
39
    }
40
41
    public function isOpen() {
42
        return false;
43
    }
44
45
    public static function factory($value) {
46
        if (is_object($value) && $value instanceof Expression) {
47
            return $value;
48
        } elseif (is_numeric($value)) {
49
            return new Expressions\Number($value);
50
        } elseif (preg_match('/^\$?[a-z]+$/', $value)) {
51
            return new Expressions\Variable($value);
52
        }
53
54
        switch ($value) {
55
            case '+':
56
                return new Expressions\Addition($value);
57
            case '-':
58
                return new Expressions\Subtraction($value);
59
            case '*':
60
                return new Expressions\Multiplication($value);
61
            case '/':
62
                return new Expressions\Division($value);
63
            case '%':
64
                return new Expressions\Modulo($value);
65
            case '?':
66
            case ':':
67
                return new Expressions\Ternary($value);
68
            case '(':
69
            case ')':
70
                return new Expressions\Parenthesis($value);
71
            case '==':
72
                return new Expressions\ComparisonEQ($value);
73
            case '<':
74
                return new Expressions\ComparisonLT($value);
75
            case '>':
76
                return new Expressions\ComparisonGT($value);
77
            case '<=':
78
                return new Expressions\ComparisonLTE($value);
79
            case '>=':
80
                return new Expressions\ComparisonGTE($value);
81
            case '!=':
82
                return new Expressions\ComparisonNE($value);
83
            case '||':
84
                return new Expressions\OperatorOr($value);
85
            case '&&':
86
                return new Expressions\OperatorAnd($value);
87
        }
88
89
        throw new \Exception('Undefined Value ' . $value);
90
    }
91
92
    abstract public function operate(Stack $stack, $variables=array());
93
94
    public function isOperator() {
95
        return false;
96
    }
97
98
    public function isParenthesis() {
99
        return false;
100
    }
101
102
    public function render() {
103
        return $this->value;
104
    }
105
}
106