Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented Lisphp_Runtime_PHPFunction and Lisphp_Runtime_PHPClass.

  • Loading branch information...
commit 7727779bb2f9796bcc1e627c76012aa85beaee1d 1 parent 07f180e
@dahlia dahlia authored
View
1  .gitignore
@@ -0,0 +1 @@
+*.swp
View
3  Lisphp/Runtime.php
@@ -8,3 +8,6 @@
require_once 'Runtime/Logical.php';
require_once 'Runtime/Arithmetic.php';
require_once 'Runtime/List.php';
+require_once 'Runtime/PHPFunction.php';
+require_once 'Runtime/PHPClass.php';
+
View
15 Lisphp/Runtime/PHPClass.php
@@ -0,0 +1,15 @@
+<?php
+require_once 'Lisphp/Runtime/Function.php';
+
+final class Lisphp_Runtime_PHPClass extends Lisphp_Runtime_Function {
+ public $class;
+
+ function __construct($class) {
+ $this->class = new ReflectionClass($class);
+ }
+
+ function execute(array $arguments) {
+ return $this->class->newInstanceArgs($arguments);
+ }
+}
+
View
15 Lisphp/Runtime/PHPFunction.php
@@ -0,0 +1,15 @@
+<?php
+require_once 'Lisphp/Runtime/Function.php';
+
+final class Lisphp_Runtime_PHPFunction extends Lisphp_Runtime_Function {
+ public $callback;
+
+ function __construct($callback) {
+ $this->callback = $callback;
+ }
+
+ function execute(array $arguments) {
+ return call_user_func_array($this->callback, $arguments);
+ }
+}
+
View
36 Lisphp/Test/RuntimeTest.php
@@ -87,10 +87,9 @@ function testIf() {
$this->assertEquals(2, $scope['b']);
}
- function assertFunction($expected, Lisphp_Runtime_Function $function) {
+ function applyFunction(Lisphp_Runtime_Function $function) {
$args = func_get_args();
array_shift($args);
- array_shift($args);
$scope = new Lisphp_Scope;
$symbol = 0;
foreach ($args as &$value) {
@@ -103,8 +102,16 @@ function assertFunction($expected, Lisphp_Runtime_Function $function) {
$value = new Lisphp_Literal($value);
}
}
- $retval = $function->apply($scope, new Lisphp_List($args));
- $this->assertEquals($expected, $retval);
+ return $function->apply($scope, new Lisphp_List($args));
+ }
+
+ function assertFunction($expected, Lisphp_Runtime_Function $function) {
+ $args = func_get_args();
+ array_shift($args);
+ $this->assertEquals(
+ $expected,
+ call_user_func_array(array($this, 'applyFunction'), $args)
+ );
}
function testFunction() {
@@ -216,9 +223,8 @@ function testOr() {
function testCar() {
$car = new Lisphp_Runtime_List_Car;
$this->assertFunction(1, $car, array(1, 2, 3));
- $args = Lisphp_Parser::parseForm('(:[])', $_);
try {
- $car->apply(new Lisphp_Scope, $args);
+ $this->applyFunction($car, new Lisphp_List);
$this->fails();
} catch (UnexpectedValueException $e) {
# pass.
@@ -232,5 +238,23 @@ function testCdr() {
$this->assertFunction(null, $cdr, array());
$this->assertFunction(new Lisphp_List, $cdr, array(1));
}
+
+ function methodTest($a) {
+ return array($this, $a);
+ }
+
+ function testPHPFunction() {
+ $substr = new Lisphp_Runtime_PHPFunction('substr');
+ $this->assertFunction('world', $substr, 'hello world', 6);
+ $method = new Lisphp_Runtime_PHPFunction(array($this, 'methodTest'));
+ $this->assertFunction(array($this, 123), $method, 123);
+ }
+
+ function testPHPClass() {
+ $class = new Lisphp_Runtime_PHPClass('ArrayObject');
+ $obj = $this->applyFunction($class, array(1, 2, 3));
+ $this->assertType('ArrayObject', $obj);
+ $this->assertEquals(array(1, 2, 3), $obj->getArrayCopy());
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.