Skip to content
Browse files

Implemented is-a?(isa?) predicate.

  • Loading branch information...
1 parent 75d6089 commit bc962584251b1ebdff812cccbd117e1f8b22b2fd @dahlia dahlia committed
View
1 Lisphp/Environment.php
@@ -37,6 +37,7 @@ static function sandbox() {
foreach (Lisphp_Runtime_Predicate_Type::getFunctions() as $n => $f) {
$scope[$n] = $f;
}
+ $scope['isa?'] = $scope['is-a?'] = new Lisphp_Runtime_Predicate_IsA;
$scope['+'] = new Lisphp_Runtime_Arithmetic_Addition;
$scope['-'] = new Lisphp_Runtime_Arithmetic_Subtraction;
$scope['*'] = new Lisphp_Runtime_Arithmetic_Multiplication;
View
4 Lisphp/Runtime/PHPClass.php
@@ -27,5 +27,9 @@ function getStaticMethods() {
}
return $methods;
}
+
+ function isClassOf($instance) {
+ return is_object($instance) && $this->class->isInstance($instance);
+ }
}
View
17 Lisphp/Runtime/Predicate.php
@@ -78,3 +78,20 @@ protected function execute(array $arguments) {
}
}
+final class Lisphp_Runtime_Predicate_IsA
+ extends Lisphp_Runtime_BuiltinFunction {
+ protected function execute(array $arguments) {
+ $object = array_shift($arguments);
+ if (!isset($arguments[0])) {
+ throw new InvalidArgumentException('too few arguments');
+ }
+ foreach ($arguments as $class) {
+ if (!($class instanceof Lisphp_Runtime_PHPClass)) {
+ throw new InvalidArgumentException('only classes are accepted');
+ }
+ if ($class->isClassOf($object)) return true;
+ }
+ return false;
+ }
+}
+
View
2 Lisphp/Test/EnvironmentTest.php
@@ -55,6 +55,8 @@ function testSandbox($scope = null) {
}
$this->assertType('Lisphp_Runtime_Predicate_Type', $scope['nil?']);
$this->assertEquals('null', $scope['nil?']->type);
+ $this->assertType('Lisphp_Runtime_Predicate_IsA', $scope['is-a?']);
+ $this->assertType('Lisphp_Runtime_Predicate_IsA', $scope['isa?']);
$this->assertType('Lisphp_Runtime_Arithmetic_Addition', $scope['+']);
$this->assertType('Lisphp_Runtime_Arithmetic_Subtraction', $scope['-']);
$this->assertType('Lisphp_Runtime_Arithmetic_Multiplication',
View
35 Lisphp/Test/RuntimeTest.php
@@ -459,6 +459,38 @@ function testTypePredicate() {
$this->assertFunction(false, $env['nil?'], 123);
}
+ function testIsA() {
+ $isa = new Lisphp_Runtime_Predicate_IsA;
+ $this->assertFunction(true, $isa,
+ new stdClass,
+ new Lisphp_Runtime_PHPClass('stdClass'));
+ $this->assertFunction(false, $isa,
+ new stdClass,
+ new Lisphp_Runtime_PHPClass('ArrayObject'));
+ $this->assertFunction(false, $isa,
+ 1, new Lisphp_Runtime_PHPClass('stdClass'));
+ $this->assertFunction(true, $isa,
+ new stdClass,
+ new Lisphp_Runtime_PHPClass('ArrayObject'),
+ new Lisphp_Runtime_PHPClass('stdClass'));
+ $this->assertFunction(false, $isa,
+ new stdClass,
+ new Lisphp_Runtime_PHPClass('ArrayObject'),
+ new Lisphp_Runtime_PHPClass('ArrayIterator'));
+ try {
+ $this->applyFunction($isa);
+ $this->fail();
+ } catch (InvalidArgumentException $e) {
+ # pass.
+ }
+ try {
+ $this->applyFunction($isa, 1);
+ $this->fail();
+ } catch (InvalidArgumentException $e) {
+ # pass.
+ }
+ }
+
function testList() {
$list = new Lisphp_Runtime_List;
$this->assertFunction(new Lisphp_List, $list);
@@ -601,6 +633,9 @@ function testPHPClass() {
$this->assertType('Lisphp_Runtime_PHPFunction', $methods['b']);
$this->assertEquals(array('Lisphp_Test_SampleClass', 'b'),
$methods['b']->callback);
+ $this->assertTrue($class->isClassOf(new Lisphp_Test_SampleClass));
+ $this->assertFalse($class->isClassOf(new stdClass));
+ $this->assertFalse($class->isClassOf(213));
}
function testUse() {

0 comments on commit bc96258

Please sign in to comment.
Something went wrong with that request. Please try again.