Permalink
Browse files

Wrote four comparison operators: `<`, `<=`, `>`, `>=`.

  • Loading branch information...
1 parent a576698 commit 41132799f6baac06e5c9ad9cfabe1e46a503c6da @dahlia dahlia committed Oct 28, 2010
Showing with 33 additions and 1 deletion.
  1. +4 −0 Lisphp/Environment.php
  2. +29 −1 Lisphp/Runtime/Predicate.php
View
4 Lisphp/Environment.php
@@ -40,6 +40,10 @@ static function sandbox() {
= new Lisphp_Runtime_Predicate_NotEq;
$scope['!='] = $scope['/='] = $scope['not-equal'] = $scope['not-equal?']
= new Lisphp_Runtime_Predicate_NotEqual;
+ $scope['<'] = new Lisphp_Runtime_Predicate_LessThan;
+ $scope['>'] = new Lisphp_Runtime_Predicate_GreaterThan;
+ $scope['<='] = new Lisphp_Runtime_Predicate_LessEqual;
+ $scope['>='] = new Lisphp_Runtime_Predicate_GreaterEqual;
foreach (Lisphp_Runtime_Predicate_Type::getFunctions() as $n => $f) {
$scope[$n] = $f;
}
View
30 Lisphp/Runtime/Predicate.php
@@ -11,7 +11,7 @@
$fst = array_shift($operands);
$or = $this->logicalOr;
foreach ($operands as $val) {
- if ($or xor !$this->compare($val, $fst)) return $or;
+ if ($or xor !$this->compare($fst, $val)) return $or;
$fst = $val;
}
return !$or;
@@ -40,6 +40,34 @@ protected function compare($a, $b) {
}
}
+final class Lisphp_Runtime_Predicate_LessThan
+ extends Lisphp_Runtime_ComparingPredicate {
+ protected function compare($a, $b) {
+ return $a < $b;
+ }
+}
+
+final class Lisphp_Runtime_Predicate_GreaterThan
+ extends Lisphp_Runtime_ComparingPredicate {
+ protected function compare($a, $b) {
+ return $a > $b;
+ }
+}
+
+final class Lisphp_Runtime_Predicate_LessEqual
+ extends Lisphp_Runtime_ComparingPredicate {
+ protected function compare($a, $b) {
+ return $a <= $b;
+ }
+}
+
+final class Lisphp_Runtime_Predicate_GreaterEqual
+ extends Lisphp_Runtime_ComparingPredicate {
+ protected function compare($a, $b) {
+ return $a >= $b;
+ }
+}
+
class Lisphp_Runtime_Predicate_NotEqual
extends Lisphp_Runtime_ComparingPredicate {
protected $logicalOr = true;

0 comments on commit 4113279

Please sign in to comment.