-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_expr.php
184 lines (158 loc) · 4.42 KB
/
eval_expr.php
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
182
183
184
<?php
require_once(__DIR__ . '/eval_require.php');
function eval_expr ($expression, $base, $op) {
$verb = "Converting expression to RPN...";
verbose($verb);
$A = 0;
$operators = getOperators($op);
// dd($operators);
$output = [];
$operations = [];
while (count($expression) > 0)
{
$char = array_shift($expression);
if (!isOperator($char, $op))
array_unshift($output, $char);
else
{
if (isset($operations[0]))
{
if ($operators[$char]['p'] < $operations[0]["p"] && $operations[0]['c'] !== '(')
{
while (count($operations) > 0 && (isset($operations[0]) && $operations[0]['c'] !== '('))
array_unshift($output, array_shift($operations)['c']);
}
elseif ($operators[$char]['p'] === $operations[0]["p"] && $operations[0]['c'] !== '(')
array_unshift($output, array_shift($operations)['c']);
if ($char === ')' && $operations[0]['c'] === '(')
$del = array_shift($operations);
}
if($char !== ")")
array_unshift($operations, ['c' => $char, 'p' => $operators[$char]['p']]);
}
}
while (count($operations) > 0)
array_unshift($output, array_shift($operations)['c']);
verbose($verb."\033[3D\033[92m".str_repeat(' ', 42-strlen($verb))."[done]\033[m\n", ["A" => $A]);
// verbose("\033[3D\033[92m".str_repeat(' ', 42-strlen($verb))."[done]\033[m\n");
return (array_reverse($output));
}
function npi ($npi, $base, $op, $pile = [], $verboseFlag = true) {
$verb = "Calculating...";
if ($verboseFlag)
verbose($verb);
$A = 0;
$operators = getOperators($op);
//loop on npi array until [0] is an operator
while(count($npi) > 0 && !isOperator($npi[0], $op))
array_unshift($pile, array_shift($npi));
//get necessary values
$operator = array_shift($npi);
$depile1 = array_shift($pile);
$depile2 = array_shift($pile);
if (isset($operator))
$result = $operators[$operator]['f']($depile2, $depile1, $base, $op);
else
$result = $depile1;
array_unshift($pile, $result);
if (count($npi) > 0)
$result = npi($npi, $base, $op, $pile, false);
else
$result = $pile[0];
if ($verboseFlag)
verbose($verb."\033[3D\033[92m".str_repeat(' ', 42-strlen($verb))."[done]\033[m\n", ["A" => $A]);
// verbose("\033[3D\033[92m".str_repeat(' ', 42-strlen($verb))."[done]\033[m\n");
return ($result);
}
function zeroTrim ($result) {
while ($result[0] === "0" && $result[1] !== ".")
$result = substr($result, 1);
if (strpos($result, ".") === false)
return ($result);
$result = strrev($result);
while ($result[0] === "0")
$result = substr($result, 1);
if ($result[0] === ".")
$result = substr($result, 1);
$result = strrev($result);
return ($result);
}
/**
* @return array All operators by default or according to the given ones,
* their priority, and now many depiled they need
*/
function getOperators ($op = null) {
return ([
$op[0] ?? '+' => [
'p' => 1,
'o' => 2,
'f' => 'add'
],
$op[1] ?? '-' => [
'p' => 1,
'o' => 2,
'f' => 'sub'
],
$op[2] ?? '*' => [
'p' => 2,
'o' => 2,
'f' => 'mul'
],
$op[3] ?? '/' => [
'p' => 2,
'o' => 2,
'f' => 'div'
],
$op[4] ?? '%' => [
'p' => 2,
'o' => 2,
'f' => 'mod'
],
$op[5] ?? "(" => [
'p' => 3,
'o' => 2
],
$op[6] ?? ")" => null
]);
}
/**
* Check if the given char is an operator
*
* @return bool
*/
function isOperator ($char, $operators = null) {
return (array_key_exists($char, getOperators($operators)));
}
function fillEnv () {
$tmpenv = file(".env");
foreach ($tmpenv as $key => $line) {
$line = str_replace(' ', '', trim(stripcslashes($line)));
if ($line[0] !== "#")
putenv($line);
}
}
function main ($argv) {
verbose("\033[92mVerbose: enabled\033[m\n");
verbose("\033[1;92mStarting\033[m\n");
if (!isset($argv[1]))
displayError("Please enter an expression");
if (count($argv) > 4)
displayWarning("This script only need 3 arguments", false, false);
$givenExpr = str_replace(' ', '', trim(stripcslashes($argv[1])));
checkIsSetBaseOpe($givenExpr, $argv);
$base = $argv[2] ?? "0123456789";
$operators = $argv[3] ?? "+-*/%()";
$expr = my_split($givenExpr, $base, $operators);
$npi = eval_expr($expr, $base, $operators);
$result = npi($npi, $base, $operators);
$result = zeroTrim($result);
if (getenv("VERBOSE") === "true")
echo "\033[1;92m".$givenExpr." = ".($result ?? $expr[0])."\033[m\n";
else
echo ($result ?? $expr[0]);
// echo ($result ?? $expr[0])."\n";
// exit($result ?? $expr[0]);
// echo $result ?? $expr[0];
}
//fillEnv();
main(checkVerbose($argv));