Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented set-at!, unset-at!, exists-at?.

  • Loading branch information...
commit c582efc68e8e3a4173d3ef26cd5302f4827c6cc6 1 parent 2767e02
@dahlia dahlia authored
View
3  Lisphp/Environment.php
@@ -24,6 +24,9 @@ static function sandbox() {
$scope['car'] = new Lisphp_Runtime_List_Car;
$scope['cdr'] = new Lisphp_Runtime_List_Cdr;
$scope['at'] = new Lisphp_Runtime_List_At;
+ $scope['set-at!'] = new Lisphp_Runtime_List_SetAt;
+ $scope['unset-at!'] = new Lisphp_Runtime_List_UnsetAt;
+ $scope['exists-at?'] = new Lisphp_Runtime_List_ExistsAt;
$scope['count'] = new Lisphp_Runtime_List_Count;
$scope['map'] = new Lisphp_Runtime_List_Map;
$scope['filter'] = new Lisphp_Runtime_List_Filter;
View
35 Lisphp/Runtime/List.php
@@ -53,6 +53,41 @@ protected function execute(array $arguments) {
}
}
+final class Lisphp_Runtime_List_SetAt extends Lisphp_Runtime_BuiltinFunction {
+ protected function execute(array $arguments) {
+ list($list, $offset) = $arguments;
+ $list = array_shift($arguments);
+ if (count($arguments) < 2) {
+ $list[] = $value = array_shift($arguments);
+ } else {
+ list($key, $value) = $arguments;
+ $list[$key] = $value;
+ }
+ return $value;
+ }
+}
+
+final class Lisphp_Runtime_List_UnsetAt extends Lisphp_Runtime_BuiltinFunction {
+ protected function execute(array $arguments) {
+ list($list, $key) = $arguments;
+ if (isset($list[$key])) {
+ $value = $list[$key];
+ unset($list[$key]);
+ return $value;
+ }
+ $key = var_export($key, true);
+ throw new OutOfRangeException("no index $key of the list");
+ }
+}
+
+final class Lisphp_Runtime_List_ExistsAt
+ extends Lisphp_Runtime_BuiltinFunction {
+ protected function execute(array $arguments) {
+ list($list, $key) = $arguments;
+ return isset($list[$key]);
+ }
+}
+
final class Lisphp_Runtime_List_Count extends Lisphp_Runtime_BuiltinFunction {
protected function execute(array $arguments) {
list($list) = $arguments;
View
3  Lisphp/Test/EnvironmentTest.php
@@ -30,6 +30,9 @@ function testSandbox($scope = null) {
$this->assertType('Lisphp_Runtime_List_Car', $scope['car']);
$this->assertType('Lisphp_Runtime_List_Cdr', $scope['cdr']);
$this->assertType('Lisphp_Runtime_List_At', $scope['at']);
+ $this->assertType('Lisphp_Runtime_List_SetAt', $scope['set-at!']);
+ $this->assertType('Lisphp_Runtime_List_UnsetAt', $scope['unset-at!']);
+ $this->assertType('Lisphp_Runtime_List_ExistsAt', $scope['exists-at?']);
$this->assertType('Lisphp_Runtime_List_Count', $scope['count']);
$this->assertType('Lisphp_Runtime_List_Map', $scope['map']);
$this->assertType('Lisphp_Runtime_List_Filter', $scope['filter']);
View
33 Lisphp/Test/RuntimeTest.php
@@ -544,6 +544,39 @@ function testAt() {
}
}
+ function testSetAt() {
+ $setAt = new Lisphp_Runtime_List_SetAt;
+ $array = new ArrayObject(array('a', 'b'));
+ $this->assertFunction('c', $setAt, $array, 'c');
+ $this->assertEquals(new ArrayObject(array('a', 'b', 'c')), $array);
+ $this->assertFunction('C', $setAt, $array, 2, 'C');
+ $this->assertEquals(new ArrayObject(array('a', 'b', 'C')), $array);
+ $this->assertFunction('d', $setAt, $array, 3, 'd');
+ $this->assertEquals(new ArrayObject(array('a', 'b', 'C', 'd')), $array);
+ }
+
+ function testUnsetAt() {
+ $unsetAt = new Lisphp_Runtime_List_UnsetAt;
+ $array = new ArrayObject(array('a', 'b'));
+ $this->assertFunction('b', $unsetAt, $array, 1);
+ $this->assertEquals(new ArrayObject(array('a')), $array);
+ $array = new ArrayObject(array('a', 'b'));
+ $this->assertFunction('a', $unsetAt, $array, 0);
+ $this->assertEquals(new ArrayObject(array(1 => 'b')), $array);
+ try {
+ $this->applyFunction($unsetAt, array('a', 'b'), 3);
+ $this->fail();
+ } catch (OutOfRangeException $e) {
+ # pass.
+ }
+ }
+
+ function testExistsAt() {
+ $existsAt = new Lisphp_Runtime_List_ExistsAt;
+ $this->assertFunction(true, $existsAt, array(1, 2, 3), 0);
+ $this->assertFunction(false, $existsAt, array(1, 2, 3), 5);
+ }
+
function testCount() {
$count = new Lisphp_Runtime_List_Count;
$this->assertFunction(0, $count, array());
Please sign in to comment.
Something went wrong with that request. Please try again.