Skip to content
Browse files

Implemented the from macro.

  • Loading branch information...
1 parent ec871cd commit 2e04284ff70aec7a5cd9c62ff7ea71363983b3e4 @dahlia dahlia committed
Showing with 43 additions and 0 deletions.
  1. +1 −0 Lisphp/Environment.php
  2. +1 −0 Lisphp/Runtime.php
  3. +24 −0 Lisphp/Runtime/From.php
  4. +1 −0 Lisphp/Test/EnvironmentTest.php
  5. +16 −0 Lisphp/Test/RuntimeTest.php
View
1 Lisphp/Environment.php
@@ -65,6 +65,7 @@ static function sandbox() {
static function full() {
$scope = new Lisphp_Scope(self::sandbox());
$scope->let('use', new Lisphp_Runtime_Use);
+ $scope->let('from', new Lisphp_Runtime_From);
$scope->let('*env*', $_ENV);
$scope->let('*server*', $_SERVER);
return $scope;
View
1 Lisphp/Runtime.php
@@ -19,4 +19,5 @@
require_once 'Runtime/PHPFunction.php';
require_once 'Runtime/PHPClass.php';
require_once 'Runtime/Use.php';
+require_once 'Runtime/From.php';
View
24 Lisphp/Runtime/From.php
@@ -0,0 +1,24 @@
+<?php
+require_once 'Lisphp/Applicable.php';
+require_once 'Lisphp/List.php';
+require_once 'Lisphp/Symbol.php';
+require_once 'Lisphp/Scope.php';
+require_once 'Lisphp/Runtime/Use.php';
+
+final class Lisphp_Runtime_From implements Lisphp_Applicable {
+ function apply(Lisphp_Scope $scope, Lisphp_List $arguments) {
+ $tmp = new Lisphp_Scope;
+ $use = new Lisphp_Runtime_Use;
+ $ns = (string) $arguments->car();
+ $simpleNames = iterator_to_array($arguments[1]);
+ foreach ($simpleNames as $name) {
+ $names[] = Lisphp_Symbol::get("$ns/$name");
+ }
+ $retval = $use->apply($tmp, new Lisphp_List($names));
+ foreach ($simpleNames as $i => $name) {
+ $scope[$name] = $retval[$i];
+ }
+ return $retval;
+ }
+}
+
View
1 Lisphp/Test/EnvironmentTest.php
@@ -94,6 +94,7 @@ function testFull() {
$scope = Lisphp_Environment::full();
$this->testSandbox($scope);
$this->assertType('Lisphp_Runtime_Use', $scope['use']);
+ $this->assertType('Lisphp_Runtime_From', $scope['from']);
$this->assertEquals($_ENV, $scope['*env*']);
$this->assertEquals($_SERVER, $scope['*server*']);
}
View
16 Lisphp/Test/RuntimeTest.php
@@ -814,5 +814,21 @@ function testUse() {
# pass
}
}
+
+ function testFrom() {
+ $from = new Lisphp_Runtime_From;
+ $env = Lisphp_Environment::sandbox();
+ $scope = new Lisphp_Scope($env);
+ $values = $from->apply($scope, Lisphp_Parser::parseForm('{
+ Lisphp
+ (<Symbol> <Program>)
+ }', $_));
+ $this->assertEquals('Lisphp_Symbol', $values[0]->class->getName());
+ $this->assertEquals('Lisphp_Symbol',
+ $scope['<Symbol>']->class->getName());
+ $this->assertEquals('Lisphp_Program', $values[1]->class->getName());
+ $this->assertEquals('Lisphp_Program',
+ $scope['<Program>']->class->getName());
+ }
}

0 comments on commit 2e04284

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