Expression::__construct()   A
last analyzed

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 1
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_numeric($value)) {
47
            return new Expressions\Number($value);
48
        } elseif (preg_match('/^\$?[a-z]+$/', $value)) {
49
            return new Expressions\Variable($value);
50
        }
51
52
        switch ($value) {
53
            case '+':
54
                return new Expressions\Addition($value);
55
            case '-':
56
                return new Expressions\Subtraction($value);
57
            case '*':
58
                return new Expressions\Multiplication($value);
59
            case '/':
60
                return new Expressions\Division($value);
61
            case '%':
62
                return new Expressions\Modulo($value);
63
            case '?':
64
            case ':':
65
                return new Expressions\Ternary($value);
66
            case '(':
67
            case ')':
68
                return new Expressions\Parenthesis($value);
69
            case '==':
70
                return new Expressions\ComparisonEQ($value);
71
            case '<':
72
                return new Expressions\ComparisonLT($value);
73
            case '>':
74
                return new Expressions\ComparisonGT($value);
75
            case '<=':
76
                return new Expressions\ComparisonLTE($value);
77
            case '>=':
78
                return new Expressions\ComparisonGTE($value);
79
            case '!=':
80
                return new Expressions\ComparisonNE($value);
81
            case '||':
82
                return new Expressions\OperatorOr($value);
83
            case '&&':
84
                return new Expressions\OperatorAnd($value);
85
        }
86
87
        throw new \RuntimeException('Undefined Value ' . $value);
88
    }
89
90
    abstract public function operate(Stack $stack, $variables=array());
91
92
    public function isOperator() {
93
        return false;
94
    }
95
96
    public function isParenthesis() {
97
        return false;
98
    }
99
100
    public function render() {
101
        return $this->value;
102
    }
103
}
104