-
Notifications
You must be signed in to change notification settings - Fork 4
/
plox.php
60 lines (49 loc) · 913 Bytes
/
plox.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
<?php
spl_autoload_register(function ($class)
{
$class = strtolower($class);
$class = str_replace('\\', '/', $class);
require($class.'.php');
});
require_once('tokentype.php');
require_once('ast.php');
if ($argc > 2)
{
print("Usage: plox [script]\n");
}
else if ($argc == 2)
{
runFile($argv[1]);
}
else
{
runPrompt();
}
function runFile($filename)
{
$str = file_get_contents($filename);
run($str);
}
function runPrompt()
{
while (true)
{
print ('> ');
$str = trim(fgets(STDIN));
run($str);
}
}
function run($source)
{
$interpreter = new Interpreter();
$scanner = new Scanner($source);
$tokens = $scanner->scanTokens();
$parser = new Parser($tokens);
$statements = $parser->parse();
if (EPlox::$hadError) return;
$resolver = new Resolver($interpreter);
$resolver->resolve($statements);
unset($resolver);
if (EPlox::$hadError) return;
$interpreter->interpret($statements);
}