Permalink
Browse files

Made and, or evaluated lazily.

  • Loading branch information...
1 parent 9e2ece2 commit cd8084867250175c962cddf87b81740f196be831 @dahlia dahlia committed Nov 25, 2009
Showing with 31 additions and 10 deletions.
  1. +8 −8 Lisphp/Runtime/Logical.php
  2. +23 −2 Lisphp/Test/RuntimeTest.php
View
16 Lisphp/Runtime/Logical.php
@@ -7,19 +7,19 @@ protected function execute(array $arguments) {
}
}
-class Lisphp_Runtime_Logical_And extends Lisphp_Runtime_BuiltinFunction {
- protected function execute(array $arguments) {
- foreach ($arguments as $value) {
- if (!$value) return $value;
+class Lisphp_Runtime_Logical_And implements Lisphp_Applicable {
+ function apply(Lisphp_Scope $scope, Lisphp_List $operands) {
+ foreach ($operands as $form) {
+ if (!$value = $form->evaluate($scope)) return $value;
}
return $value;
}
}
-class Lisphp_Runtime_Logical_Or extends Lisphp_Runtime_BuiltinFunction {
- protected function execute(array $arguments) {
- foreach ($arguments as $value) {
- if ($value) return $value;
+class Lisphp_Runtime_Logical_Or implements Lisphp_Applicable {
+ function apply(Lisphp_Scope $scope, Lisphp_List $operands) {
+ foreach ($operands as $form) {
+ if ($value = $form->evaluate($scope)) return $value;
}
return $value;
}
View
25 Lisphp/Test/RuntimeTest.php
@@ -141,7 +141,7 @@ function testIf() {
$this->assertEquals(2, $scope['b']);
}
- function applyFunction(Lisphp_Runtime_Function $function) {
+ function applyFunction(Lisphp_Applicable $function) {
$args = func_get_args();
array_shift($args);
$scope = new Lisphp_Scope;
@@ -159,7 +159,7 @@ function applyFunction(Lisphp_Runtime_Function $function) {
return $function->apply($scope, new Lisphp_List($args));
}
- function assertFunction($expected, Lisphp_Runtime_Function $function) {
+ function assertFunction($expected, Lisphp_Applicable $function) {
$args = func_get_args();
array_shift($args);
$this->assertEquals(
@@ -277,6 +277,16 @@ function testAnd() {
$this->assertFunction('', $and, 'a', 'b', '');
$this->assertFunction(null, $and, 'a', 'b', null);
$this->assertFunction('c', $and, 'a', 'b', 'c');
+ $env = Lisphp_Environment::sandbox();
+ $scope = new Lisphp_Scope($env);
+ $scope['a'] = 1;
+ $retval = $and->apply($scope, Lisphp_Parser::parseForm('{
+ (define a 2)
+ (define b 0)
+ (define a 3)
+ }', $_));
+ $this->assertEquals(0, $retval);
+ $this->assertEquals(2, $scope['a']);
}
function testOr() {
@@ -300,6 +310,17 @@ function testOr() {
$this->assertFunction('b', $or, '', 'b');
$this->assertFunction('a', $or, 'a', 'b', 'c');
$this->assertFunction('c', $or, false, null, 'c');
+ $env = Lisphp_Environment::sandbox();
+ $scope = new Lisphp_Scope($env);
+ $scope['a'] = 1;
+ $retval = $or->apply($scope, Lisphp_Parser::parseForm('{
+ (define b 0)
+ (define a 2)
+ (define a 3)
+ }', $_));
+ $this->assertEquals(2, $retval);
+ $this->assertEquals(2, $scope['a']);
+ $this->assertEquals(0, $scope['b']);
}
function testEq() {

0 comments on commit cd80848

Please sign in to comment.