1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Thunder\Logeek; |
4
|
|
|
|
5
|
|
|
final class Compiler |
6
|
|
|
{ |
7
|
|
|
public function compile(Board $board, $code): array |
8
|
|
|
{ |
9
|
|
|
$lines = explode("\n", trim($code)); |
10
|
|
|
$tree = $this->buildTree($lines); |
11
|
|
|
$functions = []; |
12
|
|
|
foreach($tree as $fn) { |
13
|
|
|
$tokens = explode(' ', $fn['line']); |
14
|
|
|
if(!$tokens || !$fn['line']) { |
15
|
|
|
continue; |
16
|
|
|
} |
17
|
|
|
$functions[$tokens[1]] = $this->compileTree($board, $fn['sub']); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return $functions; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function compileTree(Board $board, array $tree): array |
24
|
|
|
{ |
25
|
|
|
$code = []; |
26
|
|
|
while(true) { |
27
|
|
|
if(!$tree) { |
28
|
|
|
break; |
29
|
|
|
} |
30
|
|
|
$line = array_shift($tree); |
31
|
|
|
$tokens = explode(' ', $line['line']); |
32
|
|
|
if(!$tokens || !$line['line']) { |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
switch($tokens[0]) { |
36
|
|
|
case 'for': { |
|
|
|
|
37
|
|
|
$code[] = [ |
38
|
|
|
'action' => 'for', |
39
|
|
|
'iterations' => $tokens[1], |
40
|
|
|
'program' => $this->compileTree($board, $line['sub']), |
41
|
|
|
]; |
42
|
|
|
break; |
43
|
|
|
} |
44
|
|
|
case 'none': { break; } |
|
|
|
|
45
|
|
|
case 'if': { |
|
|
|
|
46
|
|
|
$else = array_shift($tree); |
47
|
|
|
$code[] = [ |
48
|
|
|
'action' => 'if', |
49
|
|
|
'left' => $tokens[1], |
50
|
|
|
'operator' => $tokens[2], |
51
|
|
|
'right' => $tokens[3], |
52
|
|
|
'true' => $this->compileTree($board, $line['sub']), |
53
|
|
|
'false' => $this->compileTree($board, $else['sub']), |
54
|
|
|
]; |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
case 'while': { |
|
|
|
|
58
|
|
|
$code[] = [ |
59
|
|
|
'action' => 'while', |
60
|
|
|
'left' => $tokens[1], |
61
|
|
|
'operator' => $tokens[2], |
62
|
|
|
'right' => $tokens[3], |
63
|
|
|
'program' => $this->compileTree($board, $line['sub']), |
64
|
|
|
]; |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
default: { |
|
|
|
|
68
|
|
|
$code = array_merge($code, $this->compileSimple($board, $tokens)); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $code; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function compileSimple(Board $board, array $tokens): array |
77
|
|
|
{ |
78
|
|
|
$action = array_shift($tokens); |
79
|
|
|
$number = 1; |
80
|
|
|
if(is_numeric($action)) { |
81
|
|
|
$number = $action; |
82
|
|
|
$action = array_shift($tokens); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$code = []; |
86
|
|
|
for($i = 0; $i < $number; $i++) { |
87
|
|
|
$args = $board->getAction($action)->getArguments(); |
88
|
|
|
if(\count($args) !== \count($tokens)) { |
89
|
|
|
throw new \RuntimeException(sprintf('Action args %s does not match tokens %s!', json_encode($args), json_encode($tokens))); |
90
|
|
|
} |
91
|
|
|
$code[] = array_merge(['action' => $action], array_combine($args, $tokens)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $code; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function buildTree(array $lines): array |
98
|
|
|
{ |
99
|
|
|
$tree = []; |
100
|
|
|
while($lines) { |
101
|
|
|
$line = array_shift($lines); |
102
|
|
|
$level = $this->getLineLevel($line); |
103
|
|
|
$subLines = []; |
104
|
|
|
while($lines && $this->getLineLevel($lines[0]) > $level) { |
105
|
|
|
$subLines[] = array_shift($lines); |
106
|
|
|
} |
107
|
|
|
$tree[] = [ |
108
|
|
|
'line' => trim($line), |
109
|
|
|
'level' => $level, |
110
|
|
|
'sub' => $this->buildTree($subLines), |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $tree; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getLineLevel($line): int |
118
|
|
|
{ |
119
|
|
|
return (int)((\strlen($line) - \strlen(ltrim($line))) / 2); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.