-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.php
executable file
·57 lines (53 loc) · 1.73 KB
/
compiler.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
#!/usr/bin/php
<?php
require_once(__DIR__ . '/core/SplClassLoader.php');
$autoloader = new SplClassLoader('vendor', __DIR__ . '/src');
$autoloader->register();
use vendor\TokenParser\Scanner;
use vendor\Utility\Console;
use vendor\Exception\CompilerException;
use vendor\SyntaxParser\Nodes\Program;
use vendor\Visualisator\TreeViewer;
$params = Console::parseArgs();
$scanner = new Scanner(file_get_contents($params['filename']));
$tokens = [];
if ($params['lex']) {
try {
while ($scanner->next()) {
$tokens[] = $scanner->get();
}
} catch (CompilerException $e) {
Console::write($e);
Console::closeStream();
exit;
}
foreach ($tokens as &$token) {
Console::write($token->getStr());
}
} else if ($params['syntax-only']) {
try {
$program = new Program($scanner);
} catch (CompilerException $e) {
Console::write($e);
Console::closeStream();
exit(1);
}
if ($params['html']) {
$syntaxTree = json_encode($program->toIdArray());
// echo $syntaxTree;
TreeViewer::genSyntaxHtml($params['html'], "result", $syntaxTree);
} else {
$syntaxTree = $program->toIdArray();
Console::write(json_encode($syntaxTree, JSON_PRETTY_PRINT));
}
} else if ($params['table-only']) {
try {
$program = new Program($scanner);
} catch (CompilerException $e) {
Console::write($e);
Console::closeStream();
exit(1);
}
$program->symTable->printInfo('');
}
Console::closeStream();