summaryrefslogtreecommitdiff
path: root/src/ste/Calc.php
blob: 660943d0e2864fbba22315c1d1fb782dd5b0e172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

namespace kch42\ste;

/**
 * Class Calc contains static methods needed by <ste:calc />
 */
class Calc
{
    private function __construct()
    {
    }

    /**
     * Parse a mathematical expression with the shunting yard algorithm (https://en.wikipedia.org/wiki/Shunting-yard_algorithm)
     *
     * We could also just eval() the $infix_math code, but this is much cooler :-D (Parser inception)

     * @param string $infix_math
     * @return array
     * @throws RuntimeError
     */
    private static function shunting_yard(string $infix_math): array
    {
        $operators = [
            "+" => ["l", 2],
            "-" => ["l", 2],
            "*" => ["l", 3],
            "/" => ["l", 3],
            "^" => ["r", 4],
            "_" => ["r", 5],
            "(" => ["", 0],
            ")" => ["", 0]
        ];

        preg_match_all("/\s*(?:(?:[+\\-\\*\\/\\^\\(\\)])|(\\d*[\\.]?\\d*))\\s*/s", $infix_math, $tokens, PREG_PATTERN_ORDER);
        $tokens_raw   = array_filter(array_map('trim', $tokens[0]), function ($x) {
            return ($x === "0") || (!empty($x));
        });
        $output_queue = [];
        $op_stack     = [];

        $lastpriority = null;
        /* Make - unary, if neccessary */
        $tokens = [];
        foreach ($tokens_raw as $token) {
            $priority = isset($operators[$token]) ? $operators[$token][1] : -1;
            if (($token == "-") && (($lastpriority === null) || ($lastpriority >= 0))) {
                $priority = $operators["_"][1];
                $tokens[] = "_";
            } else {
                $tokens[] = $token;
            }
            $lastpriority = $priority;
        }

        while (!empty($tokens)) {
            $token = array_shift($tokens);
            if (is_numeric($token)) {
                $output_queue[] = $token;
            } elseif ($token == "(") {
                $op_stack[] = $token;
            } elseif ($token == ")") {
                $lbr_found = false;
                while (!empty($op_stack)) {
                    $op = array_pop($op_stack);
                    if ($op == "(") {
                        $lbr_found = true;
                        break;
                    }
                    $output_queue[] = $op;
                }
                if (!$lbr_found) {
                    throw new RuntimeError("Bracket mismatch.");
                }
            } elseif (!isset($operators[$token])) {
                throw new RuntimeError("Invalid token ($token): Not a number, bracket or operator. Stop.");
            } else {
                $priority = $operators[$token][1];
                if ($operators[$token][0] == "l") {
                    while (
                        !empty($op_stack)
                        && $priority <= $operators[$op_stack[count($op_stack)-1]][1]
                    ) {
                        $output_queue[] = array_pop($op_stack);
                    }
                } else {
                    while (
                        !empty($op_stack)
                        && $priority < $operators[$op_stack[count($op_stack)-1]][1]
                    ) {
                        $output_queue[] = array_pop($op_stack);
                    }
                }
                $op_stack[] = $token;
            }
        }

        while (!empty($op_stack)) {
            $op = array_pop($op_stack);
            if ($op == "(") {
                throw new RuntimeError("Bracket mismatch...");
            }
            $output_queue[] = $op;
        }

        return $output_queue;
    }

    /**
     * @param array $array
     * @return array
     * @throws RuntimeError
     */
    private static function pop2(array &$array): array
    {
        $rv = [array_pop($array), array_pop($array)];
        if (array_search(null, $rv, true) !== false) {
            throw new RuntimeError("Not enough numbers on stack. Invalid formula.");
        }
        return $rv;
    }

    /**
     * @param array $rpn A mathematical expression in reverse polish notation
     * @return int|float
     * @throws RuntimeError
     */
    private static function calc_rpn(array $rpn)
    {
        $stack = [];
        foreach ($rpn as $token) {
            switch ($token) {
            case "+":
                list($b, $a) = self::pop2($stack);
                $stack[] = $a + $b;
                break;
            case "-":
                list($b, $a) = self::pop2($stack);
                $stack[] = $a - $b;
                break;
            case "*":
                list($b, $a) = self::pop2($stack);
                $stack[] = $a * $b;
                break;
            case "/":
                list($b, $a) = self::pop2($stack);
                $stack[] = $a / $b;
                break;
            case "^":
                list($b, $a) = self::pop2($stack);
                $stack[] = pow($a, $b);
                break;
            case "_":
                $a = array_pop($stack);
                if ($a === null) {
                    throw new RuntimeError("Not enough numbers on stack. Invalid formula.");
                }
                $stack[] = -$a;
                break;
            default:
                $stack[] = $token;
                break;
            }
        }
        return array_pop($stack);
    }

    /**
     * Calculate a simple mathematical expression. Supported operators are +, -, *, /, ^.
     * You can use ( and ) to group expressions together.
     *
     * @param string $expr
     * @return float|int
     * @throws RuntimeError
     */
    public static function calc(string $expr)
    {
        return self::calc_rpn(self::shunting_yard($expr));
    }
}