-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.php
388 lines (325 loc) · 12.3 KB
/
repl.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
declare(strict_types=1);
error_reporting(E_ALL);
use Che\SimpleLisp\HashMap;
use Che\SimpleLisp\HashMapInterface;
use Che\SimpleLisp\Symbol;
use function Che\SimpleLisp\Env\_defaultEnv;
use function Che\SimpleLisp\Eval\_eval;
use function Che\SimpleLisp\Parse\parseTokens;
use function Che\SimpleLisp\Parse\tokenize;
require_once './vendor/autoload.php';
const HISTORY_FILE_PATH = '.repl_history';
const STD_LIB = 'std_lib.lisp';
enum OutputMode: string
{
case ESCAPE = '?> ';
case UNESCAPE = 'unescape?> ';
}
class ReplMode
{
private ?OutputMode $disposableMode = null;
public function __construct(private OutputMode $mode)
{
}
public function setMode(OutputMode $mode): self
{
$this->mode = $mode;
return $this;
}
public function setDisposableMode(OutputMode $mode): self
{
$this->disposableMode = $mode;
return $this;
}
public function getMode(): OutputMode
{
if($this->disposableMode !== null) {
$result = $this->disposableMode;
$this->disposableMode = null;
return $result;
}
return $this->mode;
}
}
function makeIndent(int $indent): string
{
return str_repeat('.', $indent * 2);
}
class BraceCounter
{
private int $count = 0;
public function inc(): self
{
$this->count++;
return $this;
}
public function dec(): self
{
if($this->count > 0) {
$this->count--;
}
return $this;
}
public function count(): int
{
return $this->count;
}
}
/**
* @todo не учитывает что скобки могут быть в составе строки
*
* @throws Exception
*/
function consumeLine(BraceCounter $coutner, string $line): void
{
$clipErrorString = static function (string $line, int $errorPosition): string {
$leftPadding = 100;
$rightPadding = 100;
$startPos = $errorPosition - $leftPadding;
$startPos = ($startPos < 0) ? 0 : $startPos;
$endPos = $errorPosition + $rightPadding;
$pointerLine = str_repeat('-', $errorPosition) . '^' . str_repeat('-', strlen($line) - $errorPosition - 1);
return sprintf(
"%s\n%s",
substr($line, $startPos, $endPos),
substr($pointerLine, $startPos, $endPos)
);
};
foreach (str_split($line) as $k => $char) {
if ($char === '(') {
$coutner->inc();
continue;
}
if($char === ')') {
if($coutner->count() === 0) {
throw new \Exception("Лишняя закрывающая скобка:\n" . $clipErrorString($line, $k));
}
$coutner->dec();
}
}
}
function multiInput(OutputMode $mode): string
{
$counter = new BraceCounter();
$prompt = $mode->value;
$result = '';
while (true) {
$line = readline($prompt);
consumeLine($counter, $line);
$prompt = makeIndent($counter->count());
$result .= $line . ' ';
if($counter->count() === 0) {
break;
}
}
return trim($result);
}
enum MessageType: string
{
case REGULAR = '';
case INFO = 'Info';
case WARNING = 'Warning';
case ERROR = 'Error';
}
$env = new class (_defaultEnv()) extends HashMap {
#[\Override] public function getIterator(): \Traversable
{
$iterate = static function (HashMapInterface $map, int $level = 0, int $number = 0) use (&$iterate): \Generator {
yield [$level, $number] => $level === 0 ? $map->iterateMap() : $map->getIterator();
$level += 1;
foreach ($map->childList() as $objItem) {
foreach ($iterate($objItem, $level, $number) as $key => $item) {
yield $key => $item;
}
$number += 1;
}
};
return $iterate($this);
}
private function iterateMap(): \Traversable
{
return parent::getIterator();
}
};
$std_lib = file_get_contents(implode(DIRECTORY_SEPARATOR, ['.', 'src', STD_LIB]));
__eval($std_lib, $env);
if(file_exists(HISTORY_FILE_PATH)) {
readline_read_history(HISTORY_FILE_PATH);
}
writeMessage("\n" . str_repeat('=', 53));
writeMessage("Наберите \033[0;33m:help\033[0m для просмотра списка доступных комманд");
writeMessage(str_repeat('=', 53) . "\n");
$replMode = new ReplMode(OutputMode::ESCAPE);
while (true) {
$command = multiInput($replMode->getMode());
if(empty(str_replace(' ', '', $command))) {
continue;
}
readline_add_history($command);
try {
if($command[0] === ':') {
switch (true) {
// режим экранированного вывода
case $command === ':esc':
$replMode->setMode(OutputMode::ESCAPE);
continue 2;
// режим экранированного вывода
case $command === ':unesc':
$replMode->setMode(OutputMode::UNESCAPE);
continue 2;
// выход
case $command === ':quit':
case $command === ':q':
break 2;
// переменные окружения
case str_starts_with($command, ':e'):
$c = explode(' ', $command);
$toInt = static function (array $arr, int $key): ?int {
return isset($arr[$key])
? (int)$arr[$key]
: null;
};
writeMessage(hmToString($env, $toInt($c, 1), $toInt($c, 2)), MessageType::INFO);
continue 2;
// очистка истории
case $command === ':ch':
readline_clear_history();
@unlink(HISTORY_FILE_PATH);
continue 2;
// загрузка файла
case str_starts_with($command, ':load'):
case str_starts_with($command, ':l'):
$fileName = str_replace([':load ', ':l '], '', $command);
if(!file_exists($fileName)) {
throw new Exception(sprintf('File Not Found: "%s"', $fileName));
}
$command = file_get_contents($fileName);
writeMessage("OK");
break;
// показать токены команды
case str_starts_with($command, ':t'):
$command = trim(substr($command, 2));
writeMessage(toString(tokenize($command)), MessageType::INFO);
continue 2;
// показать "скомпилированную" команду
case str_starts_with($command, ':pt'):
$command = trim(substr($command, 3));
writeMessage(toString(parseTokens(tokenize($command))), MessageType::INFO);
continue 2;
case $command === ':gc':
gc_collect_cycles();
writeMessage('GC launched', MessageType::INFO);
continue 2;
case $command === ':help':
writeMessage("Список доступных комманд:");
writeMessage("\033[0;32m:esc\033[0m - режим экранированного вывода в \033[0;33mREPL\033[0m");
writeMessage("\033[0;32m:unesc \033[0m - режим неэкранированного вывода в \033[0;33mREPL\033[0m");
writeMessage("\033[0;32m:q, :quit \033[0m - выход");
writeMessage("\033[0;32m:e\033[0m - рекурсивный вывод переменных окружения - \033[0;33m:e 1 2\033[0m, где \033[0;33m1\033[0m - уровень вложенности, \033[0;33m2\033[0m - порядковый номер на уровне");
writeMessage("\033[0;32m:l, :load\033[0m - загрузка и исполнение файла - \033[0;33m:load programs/fib\033[0m");
writeMessage("\033[0;32m:t\033[0m - показать токены строки - \033[0;33m:t (def a 123)\033[0m");
writeMessage("\033[0;32m:pt\033[0m - показать сформированную команду из строки - \033[0;33m:pt (def a 123)\033[0m");
writeMessage("\033[0;32m:gc\033[0m - принудительный запуск сборщика мусора");
writeMessage("\033[0;32m:help\033[0m - отображение этой справки");
continue 2;
default:
writeMessage(sprintf("Unknown command '%s'\n", $command), MessageType::WARNING);
continue 2;
}
}
$result = __eval($command, $env);
$messageType = $result === null ? MessageType::INFO : MessageType::REGULAR;
$result = $result ?? 'OK';
} catch (\Throwable $e) {
$prePrint = static function (string $s, int $maxLength): string {
$length = strlen($s);
return substr($s, 0, $maxLength) . ($length > $maxLength ? ' ...' : '');
};
$result = sprintf(
"%s\nTrace:\n================\n%s",
$prePrint($e->getMessage(), 1000),
$prePrint($e->getTraceAsString(), 5000)
);
$messageType = MessageType::ERROR;
$replMode->setDisposableMode(OutputMode::UNESCAPE);
}
writeMessage(toString($result, $replMode->getMode()), $messageType);
}
readline_write_history(HISTORY_FILE_PATH);
exit(0);
function toString(mixed $value, OutputMode $mode = OutputMode::ESCAPE): string
{
if(is_iterable($value)) {
$acc = '';
foreach ($value as $item) {
$acc .= ' ';
$acc .= toString($item);
}
return sprintf('(%s)', substr($acc, 1));
}
if(is_bool($value)) {
$value = new Symbol($value ? 'true' : 'false');
}
if(is_object($value) && (!str_starts_with(get_class($value), 'Che\SimpleLisp'))) {
$value = sprintf('class %s: %s', get_class($value), substr(serialize($value), 0, 100));
}
if($mode === OutputMode::UNESCAPE) {
return (string)$value;
}
if(is_string($value)) {
$value = sprintf('"%s"', $value);
}
return str_replace(["\r", "\n", "\t"], ['\r', '\n', '\t'], (string)$value);
}
function decorateMessage(string $message, MessageType $type): string
{
[$color, $nc] = match ($type) {
MessageType::REGULAR => [null, null],
MessageType::INFO => ["\033[0;37m", "\033[0m"],
MessageType::WARNING => ["\033[0;33m", "\033[0m"],
MessageType::ERROR => ["\033[0;31m", "\033[0m"],
};
return implode('', array_filter([
$color,
($type === MessageType::REGULAR ? null : $type->value . '! '),
$nc,
$message
]));
}
function writeMessage(string $message, MessageType $type = MessageType::REGULAR): void
{
echo decorateMessage($message, $type). "\n";
}
function hmToString(\Traversable $map, ?int $showLevel = null, ?int $showNumber = null): string
{
$hideEnv = fn (?int $filterArg, int $filteredArg): bool => $filterArg !== null && $filterArg !== $filteredArg;
$acc = [];
foreach ($map as $hmKey => $iterable) {
[$level, $number] = $hmKey;
if($hideEnv($showLevel, $level)) {
continue;
}
if($hideEnv($showNumber, $number)) {
continue;
}
$buf = [];
foreach ($iterable as $key => $value) {
$buf[] = sprintf("\033[0;32m%s\033[0m => \033[0;35m%s\033[0m", $key, toString($value));
}
$acc[] = sprintf("===level: %d | number: %d===\n%s", $level, $number, implode("\n", $buf));
}
return implode("\n", $acc);
}
function __eval(string $command, HashMapInterface $env): mixed
{
$tokens = new \SplDoublyLinkedList();
foreach (tokenize($command) as $tokenItem) {
$tokens->push($tokenItem);
}
$result = null;
while (!$tokens->isEmpty()) {
$result = _eval(parseTokens($tokens), $env);
}
return $result;
}