-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmitter.class.php
More file actions
executable file
·190 lines (173 loc) · 5.33 KB
/
Copy pathEmitter.class.php
File metadata and controls
executable file
·190 lines (173 loc) · 5.33 KB
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
<?php namespace lang\ast;
use io\streams\OutputStream;
use lang\ast\{Node, Error, Errors};
use lang\{IllegalArgumentException, IllegalStateException, ClassLoader, XPClass};
abstract class Emitter {
private $transformations= [];
/**
* Selects the correct emitter for a given runtime
*
* @param string $runtime E.g. "php:".PHP_VERSION
* @param string[]|lang.XPClass[] $emitters Optional
* @return lang.XPClass
* @throws lang.IllegalArgumentException
*/
public static function forRuntime($runtime, $emitters= []) {
sscanf($runtime, '%[^.:]%*[.:]%d.%d', $engine, $major, $minor);
$cl= ClassLoader::getDefault();
$engine= strtoupper($engine);
do {
$impl= "lang.ast.emit.{$engine}{$major}{$minor}";
if ($cl->providesClass($impl)) {
if (empty($emitters)) return $cl->loadClass($impl);
// Extend loaded class, including all given emitters
$extended= ['kind' => 'class', 'extends' => [$cl->loadClass($impl)], 'implements' => [], 'use' => []];
foreach ($emitters as $class) {
if ($class instanceof XPClass) {
$impl.= '⋈'.strtr($class->getName(), ['.' => '·']);
$extended['use'][]= $class;
} else {
$impl.= '⋈'.strtr($class, ['.' => '·', '\\' => '·']);
$extended['use'][]= XPClass::forName($class);
}
}
return ClassLoader::defineType($impl, $extended, '{}');
}
} while ($minor-- > 0);
throw new IllegalArgumentException('XP Compiler does not support '.$runtime.' yet');
}
/**
* Raises an exception
*
* @param string $error
* @param ?Throwable $cause
* @return never
*/
public function raise($error, $cause= null) {
throw new IllegalStateException($error, $cause);
}
/**
* Transforms nodes of a certain kind using the given function, which
* may return either single node, which will be then emitted, or an
* iterable producing nodes, which will then be emitted as statements.
* Returns a handle to remove the transformation again
*
* @param string $kind
* @param function(lang.ast.Node): lang.ast.Node|iterable $function
* @return var
*/
public function transform($kind, $function) {
if (isset($this->transformations[$kind])) {
$i= sizeof($this->transformations[$kind]);
$this->transformations[$kind][]= $function;
} else {
$i= 0;
$this->transformations[$kind]= [$function];
}
return ['kind' => $kind, 'id' => $i];
}
/**
* Removes a transformation added with transform()
*
* @param var $transformation
* @return void
*/
public function remove($transformation) {
$kind= $transformation['kind'];
array_splice($this->transformations[$kind], $transformation['id'], 1);
if (empty($this->transformations[$kind])) unset($this->transformations[$kind]);
}
/**
* Returns all transformations
*
* @return [:var[]]
*/
public function transformations() {
return $this->transformations;
}
/**
* Catch-all, should `$node->kind` be empty in `"emit{$node->kind}"`.
*
* @return void
*/
protected function emit() {
throw new IllegalStateException('Called without node kind');
}
/**
* Standalone operators
*
* @param lang.ast.emit.Result $result
* @param lang.ast.Token $operator
* @return void
*/
protected function emitOperator($result, $operator) {
throw new IllegalStateException('Unexpected operator '.$operator->value.' at line '.$operator->line);
}
/**
* Emit nodes seperated as statements
*
* @param lang.ast.emit.Result $result
* @param iterable $nodes
* @return void
*/
public function emitAll($result, $nodes) {
foreach ($nodes as $node) {
$this->emitOne($result, $node);
$result->out->write(';');
}
}
/**
* Emit single nodes
*
* @param lang.ast.emit.Result $result
* @param lang.ast.Node $node
* @return void
*/
public function emitOne($result, $node) {
if ($transformations= $this->transformations[$node->kind] ?? null) {
foreach ($transformations as $transformation) {
$r= $transformation($result->codegen, $node);
if ($r instanceof Node) {
if ($r->kind === $node->kind) continue;
$this->{'emit'.$r->kind}($result->at($r->line), $r);
return;
} else if ($r) {
foreach ($r as $s => $n) {
$this->{'emit'.$n->kind}($result->at($n->line), $n);
null === $s || $result->out->write(';');
}
return;
}
}
// Fall through, use default
}
$this->{'emit'.$node->kind}($result->at($node->line), $node);
}
/**
* Creates result
*
* @param io.streams.OutputStream $target
* @return lang.ast.emit.Result
*/
protected abstract function result($target);
/**
* Emitter entry point, takes nodes and emits them to the given target.
*
* @param iterable $nodes
* @param io.streams.OutputStream $target
* @param ?string $source
* @return io.streams.OutputStream
* @throws lang.ast.Errors
*/
public function write($nodes, OutputStream $target, $source= null) {
$result= $this->result($target)->from($source);
try {
$this->emitAll($result, $nodes);
return $target;
} catch (Error $e) {
throw new Errors([$e], $source);
} finally {
$result->close();
}
}
}