Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented predicates for types.

  • Loading branch information...
commit b7b3f170f9ea73c1ba728c0930a9b6702aac5d05 1 parent 849ac64
@dahlia dahlia authored
View
3  Lisphp/Environment.php
@@ -34,6 +34,9 @@ static function sandbox() {
= new Lisphp_Runtime_Predicate_NotEq;
$scope['!='] = $scope['/='] = $scope['not-equal'] = $scope['not-equal?']
= new Lisphp_Runtime_Predicate_NotEqual;
+ foreach (Lisphp_Runtime_Predicate_Type::getFunctions() as $n => $f) {
+ $scope[$n] = $f;
+ }
$scope['+'] = new Lisphp_Runtime_Arithmetic_Addition;
$scope['-'] = new Lisphp_Runtime_Arithmetic_Subtraction;
$scope['*'] = new Lisphp_Runtime_Arithmetic_Multiplication;
View
41 Lisphp/Runtime/Predicate.php
@@ -1,7 +1,8 @@
<?php
require_once 'Lisphp/Runtime/BuiltinFunction.php';
-abstract class Lisphp_Runtime_Predicate extends Lisphp_Runtime_BuiltinFunction {
+abstract class Lisphp_Runtime_ComparingPredicate
+ extends Lisphp_Runtime_BuiltinFunction {
protected $logicalOr = false;
final protected function execute(array $operands) {
@@ -19,19 +20,19 @@
abstract protected function compare($a, $b);
}
-class Lisphp_Runtime_Predicate_Eq extends Lisphp_Runtime_Predicate {
+class Lisphp_Runtime_Predicate_Eq extends Lisphp_Runtime_ComparingPredicate {
protected function compare($a, $b) {
return $a === $b;
}
}
-class Lisphp_Runtime_Predicate_Equal extends Lisphp_Runtime_Predicate {
+class Lisphp_Runtime_Predicate_Equal extends Lisphp_Runtime_ComparingPredicate {
protected function compare($a, $b) {
return $a == $b;
}
}
-class Lisphp_Runtime_Predicate_NotEq extends Lisphp_Runtime_Predicate {
+class Lisphp_Runtime_Predicate_NotEq extends Lisphp_Runtime_ComparingPredicate {
protected $logicalOr = true;
protected function compare($a, $b) {
@@ -39,7 +40,8 @@ protected function compare($a, $b) {
}
}
-class Lisphp_Runtime_Predicate_NotEqual extends Lisphp_Runtime_Predicate {
+class Lisphp_Runtime_Predicate_NotEqual
+ extends Lisphp_Runtime_ComparingPredicate {
protected $logicalOr = true;
protected function compare($a, $b) {
@@ -47,3 +49,32 @@ protected function compare($a, $b) {
}
}
+final class Lisphp_Runtime_Predicate_Type
+ extends Lisphp_Runtime_BuiltinFunction {
+ static $types = array('array', 'binary', 'bool', 'buffer', 'double',
+ 'float', 'int', 'integer', 'long', 'null', 'numberic',
+ 'object', 'real', 'resource', 'scalar', 'string');
+ public $type;
+
+ static function getFunctions(Lisphp_Scope $superscope = null) {
+ $scope = new Lisphp_Scope($superscope);
+ foreach (self::$types as $type) {
+ $scope["$type?"] = new self($type);
+ }
+ $scope['nil?'] = new self('null');
+ return $scope;
+ }
+
+ function __construct($type) {
+ $this->type = $type;
+ }
+
+ protected function execute(array $arguments) {
+ $function = "is_{$this->type}";
+ foreach ($arguments as $value) {
+ if (!$function($value)) return false;
+ }
+ return true;
+ }
+}
+
View
9 Lisphp/Scope.php
@@ -1,7 +1,7 @@
<?php
require_once 'Lisphp/Symbol.php';
-final class Lisphp_Scope implements ArrayAccess {
+final class Lisphp_Scope implements ArrayAccess, IteratorAggregate {
public $values = array(), $superscope;
function __construct(self $superscope = null) {
@@ -51,6 +51,13 @@ function offsetUnset($symbol) {
}
}
+ function getIterator() {
+ foreach ($this->listSymbols() as $name) {
+ $values[$name] = $this[$name];
+ }
+ return new ArrayIterator(isset($values) ? $values : array());
+ }
+
function listSymbols() {
$symbols = array_keys($this->values);
if (!$this->superscope) return $symbols;
View
7 Lisphp/Test/EnvironmentTest.php
@@ -48,6 +48,13 @@ function testSandbox($scope = null) {
$scope['not-equal']);
$this->assertType('Lisphp_Runtime_Predicate_NotEqual',
$scope['not-equal?']);
+ foreach (Lisphp_Runtime_Predicate_Type::$types as $type) {
+ $this->assertType('Lisphp_Runtime_Predicate_Type',
+ $scope["$type?"]);
+ $this->assertEquals($type, $scope["$type?"]->type);
+ }
+ $this->assertType('Lisphp_Runtime_Predicate_Type', $scope['nil?']);
+ $this->assertEquals('null', $scope['nil?']->type);
$this->assertType('Lisphp_Runtime_Arithmetic_Addition', $scope['+']);
$this->assertType('Lisphp_Runtime_Arithmetic_Subtraction', $scope['-']);
$this->assertType('Lisphp_Runtime_Arithmetic_Multiplication',
View
19 Lisphp/Test/RuntimeTest.php
@@ -433,6 +433,25 @@ function testNotEqual() {
}
}
+ function testTypePredicate() {
+ $int = new Lisphp_Runtime_Predicate_Type('int');
+ $this->assertEquals('int', $int->type);
+ $this->assertFunction(true, $int, 123);
+ $this->assertFunction(false, $int, 3.14);
+ $this->assertFunction(false, $int, 'abc');
+ $string = new Lisphp_Runtime_Predicate_Type('string');
+ $this->assertEquals('string', $string->type);
+ $this->assertFunction(true, $string, 'hello');
+ $this->assertFunction(false, $string, 123);
+ $env = Lisphp_Runtime_Predicate_Type::getFunctions();
+ foreach (Lisphp_Runtime_Predicate_Type::$types as $type) {
+ $this->assertType('Lisphp_Runtime_Predicate_Type', $env["$type?"]);
+ $this->assertEquals($type, $env["$type?"]->type);
+ }
+ $this->assertFunction(true, $env['nil?'], null);
+ $this->assertFunction(false, $env['nil?'], 123);
+ }
+
function testList() {
$list = new Lisphp_Runtime_List;
$this->assertFunction(new Lisphp_List, $list);
Please sign in to comment.
Something went wrong with that request. Please try again.