Skip to content

Commit 692c9f2

Browse files
Basic dispatch table to hold first class citizens
1 parent d85790f commit 692c9f2

File tree

4 files changed

+150
-11
lines changed

4 files changed

+150
-11
lines changed

src/Core/DispatchTable.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Rawr\Core;
4+
5+
final class DispatchTable {
6+
private static $table = [];
7+
8+
static function tableExists($table) {
9+
return array_key_exists($table, static::$table);
10+
}
11+
12+
static function createTable($table) {
13+
if (!static::tableExists($table)) {
14+
static::$table[$table] = [];
15+
} else {
16+
throw new \Exception("[rawr-core] Table [{$table}] already exists");
17+
}
18+
}
19+
20+
static function clearTable($table) {
21+
if (static::tableExists($table)) {
22+
static::$table[$table] = [];
23+
} else {
24+
throw new \Exception("[rawr-core] Unable to clear inexistent table [{$table}]");
25+
}
26+
}
27+
28+
static function deleteTable($table) {
29+
if (static::tableExists($table)) {
30+
unset(static::$table[$table]);
31+
} else {
32+
throw new \Exception("[rawr-core] Unable to delete inexistent table [{$table}]");
33+
}
34+
}
35+
36+
static function tableHasKey($table, $key) {
37+
return static::tableExists($table) && array_key_exists($key, static::$table[$table]);
38+
}
39+
40+
static function addToTable($table, $key, $value) {
41+
if (static::tableExists($table)) {
42+
if (!static::tableHasKey($table, $key)) {
43+
static::$table[$table][$key] = $value;
44+
} else {
45+
throw new \Exception("[rawr-core] Duplicated key [{$key}] on table [{$table}]");
46+
}
47+
} else {
48+
throw new \Exception("[rawr-core] Inexistent table [{$table}]");
49+
}
50+
}
51+
52+
static function getFromTable($table, $key) {
53+
// TODO
54+
}
55+
56+
static function removeFromTable($table, $key) {
57+
// TODO
58+
}
59+
60+
static function debugTable($table) {
61+
if (array_key_exists($table, static::$table)) {
62+
var_dump(static::$table[$table]);
63+
} else {
64+
throw new \Exception("[rawr-core] Table [{$table}] not found");
65+
}
66+
}
67+
68+
static function __GET__() {
69+
return static::$table;
70+
}
71+
}

src/DataType/Action.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ public function boundPointer()
131131
/**
132132
* Returns the number of remaining parameters for this action.
133133
* @author Marcelo Camargo
134-
* @return Int
134+
* @return rawr<Int>
135135
*/
136136
public function getRemainingParameters()
137137
{
138-
return new Int($this->remaining_size);
138+
return rawr_from_primitive($this->remaining_size, rawr_integer);
139139
}
140140

141141
/**
@@ -175,5 +175,56 @@ public function ·($fn)
175175
return call_user_func_array($left, [call_user_func_array($right, $arguments)]);
176176
});
177177
}
178+
179+
/**
180+
* Returns the documentation comment above the function.
181+
* @author Marcelo Camargo
182+
* @return rawr<String>
183+
*/
184+
public function getDocComment()
185+
{
186+
return rawr_from_primitive($this->reflection->getDocComment(), rawr_string);
187+
}
188+
189+
/**
190+
* The line number where the function ends.
191+
* @author Marcelo Camargo
192+
* @return rawr<Int>
193+
*/
194+
public function getEndLine()
195+
{
196+
return rawr_from_primitive($this->reflection->getEndLine(), rawr_integer);
197+
}
198+
199+
/**
200+
* Returns informations about the extension of a function.
201+
* @author Marcelo Camargo
202+
* @return ReflectionExtension
203+
*/
204+
public function getExtension()
205+
{
206+
return $this->reflection->getExtension();
207+
}
208+
209+
/**
210+
* Returns the name of the function's extension.
211+
* @author Marcelo Camargo
212+
* @return rawr<String>
213+
*/
214+
public function getExtensionName()
215+
{
216+
return rawr_from_primitive($this->reflection->getExtensionName(), rawr_string);
217+
}
218+
219+
/**
220+
* Gets the filename from a user-defined function.
221+
* @author Marcelo Camargo
222+
* @return rawr<String>
223+
*/
224+
public function getFileName()
225+
{
226+
return rawr_from_primitive($this->reflection->getFileName(), rawr_string);
227+
}
178228
}
179229

230+

src/DataType/BaseType.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace Rawr\DataType;
4+
use \Rawr\Core\DispatchTable;
45

56
class BaseType
67
{
@@ -10,4 +11,19 @@ final public function value()
1011
{
1112
return $this->value;
1213
}
14+
15+
static function __callStatic($name, $arguments)
16+
{
17+
var_dump(["name" => $name, "args" => $arguments]);
18+
}
19+
20+
static function defStatic($name, $closure) {
21+
$table = get_called_class();
22+
23+
if (!DispatchTable::tableExists($table)) {
24+
DispatchTable::createTable($table);
25+
}
26+
27+
DispatchTable::addToTable($table, $name, rawr_reduce($closure, rawr_callable));
28+
}
1329
}

test.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
<?php
22

33
require_once 'src/Core/rawr_core_functions.php';
4+
require_once 'src/Core/DispatchTable.php';
5+
46
require_once 'src/DataType/BaseType.php';
57
require_once 'src/DataType/Action.php';
68
require_once 'src/DataType/Bool.php';
79
require_once 'src/DataType/Int.php';
810

11+
12+
use \Rawr\Core\DispatchTable;
13+
14+
use \Rawr\DataType\BaseType;
915
use \Rawr\DataType\Action;
1016
use \Rawr\DataType\Bool;
1117
use \Rawr\DataType\Int;
1218

13-
$add10 = new Action(function($x, Int $y) {
14-
return $x + $y->value();
15-
}, 10);
16-
17-
$multiply = new Action(function($x, $y) {
18-
return $x + $y * 3;
19-
}, 5);
2019

21-
$add_and_double = $add10->·($multiply);
20+
Action::defStatic("helloWorld", function($name) {
21+
echo "Hello {$name}", PHP_EOL;
22+
});
2223

23-
var_dump($add_and_double(20)); // => Int(70)
24+
var_dump(DispatchTable::__GET__());

0 commit comments

Comments
 (0)