Example #1
0
 public function testFromFile()
 {
     $testFiles = glob(__DIR__ . '/Functional/*.lisphp');
     foreach ($testFiles as $file) {
         $code = file_get_contents($file);
         $scope = Lisphp_Environment::workflow();
         $program = new Lisphp_Program($code);
         $result = $program->execute($scope);
         $expected = file_get_contents(preg_replace('/\\.lisphp$/', '.out', $file));
         $this->assertSame(trim($expected), trim($result));
     }
 }
Example #2
0
function balrog_front_controller($baseDir)
{
    $environment = Lisphp_Environment::full();
    $scope = new Lisphp_Scope($environment);
    $scope['require'] = new Lisphp_Runtime_PHPFunction(function ($file) use($scope) {
        $program = Lisphp_Program::load(__DIR__ . '/' . $file . '.lisphp');
        return $program->execute($scope);
    });
    $scope['base-dir'] = $baseDir;
    $filename = __DIR__ . '/front-controller.lisphp';
    $program = Lisphp_Program::load($filename);
    $program->execute($scope);
}
Example #3
0
 public function testFromFile()
 {
     $testFiles = glob(__DIR__ . '/Functional/*.lisphp');
     foreach ($testFiles as $file) {
         $this->result = '';
         $program = Lisphp_Program::load($file);
         $scope = Lisphp_Environment::full();
         $scope['echo'] = new Lisphp_Runtime_PHPFunction(array($this, 'displayStrings'));
         $program->execute($scope);
         $expected = file_get_contents(preg_replace('/\\.lisphp$/', '.out', $file));
         $this->assertSame(trim($expected), trim($this->result));
     }
 }
Example #4
0
 protected static function antimagic($vars)
 {
     if (!get_magic_quotes_gpc()) {
         return $vars;
     }
     if (!($f = self::$antimagicFunction)) {
         self::$antimagicFunction = create_function('$vars', '
             return is_array($vars)
                  ? array_map(' . __CLASS__ . '::$antimagicFunction, $vars)
                  : stripslashes($vars);
         ');
     }
     return $f($vars);
 }
Example #5
0
 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*']);
 }
Example #6
0
 public function testWorkflow($scope = null)
 {
     if (is_null($scope)) {
         $scope = Lisphp_Environment::workflow();
     }
     $this->assertType('Lisphp_Scope', $scope);
     $this->assertNull($scope['nil']);
     $this->assertTrue($scope['true']);
     $this->assertTrue($scope['else']);
     $this->assertFalse($scope['false']);
     $this->assertTrue($scope['#t']);
     $this->assertFalse($scope['#f']);
     $this->assertNoType('Lisphp_Runtime_Eval', $scope['eval']);
     $this->assertType('Lisphp_Runtime_Quote', $scope['quote']);
     $this->assertType('Lisphp_Runtime_PHPFunction', $scope['symbol']);
     $this->assertEquals(['Lisphp_Symbol', 'get'], $scope['symbol']->callback);
     $this->assertType('Lisphp_Runtime_Define', $scope['define']);
     $this->assertNoType('Lisphp_Runtime_Let', $scope['let']);
     $this->assertNoType('Lisphp_Runtime_Macro', $scope['macro']);
     $this->assertNoType('Lisphp_Runtime_Lambda', $scope['lambda']);
     $this->assertNoType('Lisphp_Runtime_Apply', $scope['apply']);
     $this->assertNoType('Lisphp_Runtime_Do', $scope['do']);
     $this->assertNoType('Lisphp_Runtime_Dict', $scope['dict']);
     $this->assertNoType('Lisphp_Runtime_Array', $scope['array']);
     $this->assertNoType('Lisphp_Runtime_List', $scope['list']);
     $this->assertNoType('Lisphp_Runtime_List_Car', $scope['car']);
     $this->assertNoType('Lisphp_Runtime_List_Cdr', $scope['cdr']);
     $this->assertNoType('Lisphp_Runtime_List_At', $scope['at']);
     $this->assertNoType('Lisphp_Runtime_List_SetAt', $scope['set-at!']);
     $this->assertNoType('Lisphp_Runtime_List_UnsetAt', $scope['unset-at!']);
     $this->assertNoType('Lisphp_Runtime_List_ExistsAt', $scope['exists-at?']);
     $this->assertNoType('Lisphp_Runtime_List_Count', $scope['count']);
     $this->assertNoType('Lisphp_Runtime_List_Map', $scope['map']);
     $this->assertNoType('Lisphp_Runtime_List_Filter', $scope['filter']);
     $this->assertNoType('Lisphp_Runtime_List_Fold', $scope['fold']);
     $this->assertNoType('Lisphp_Runtime_List_Cond', $scope['cond']);
     $this->assertType('Lisphp_Runtime_Predicate_Eq', $scope['==']);
     $this->assertType('Lisphp_Runtime_Predicate_Eq', $scope['eq']);
     $this->assertType('Lisphp_Runtime_Predicate_Eq', $scope['eq?']);
     $this->assertType('Lisphp_Runtime_Predicate_Equal', $scope['=']);
     $this->assertType('Lisphp_Runtime_Predicate_Equal', $scope['equal']);
     $this->assertType('Lisphp_Runtime_Predicate_Equal', $scope['equal?']);
     $this->assertType('Lisphp_Runtime_Predicate_NotEq', $scope['/==']);
     $this->assertType('Lisphp_Runtime_Predicate_NotEq', $scope['!==']);
     $this->assertType('Lisphp_Runtime_Predicate_NotEq', $scope['not-eq']);
     $this->assertType('Lisphp_Runtime_Predicate_NotEq', $scope['not-eq?']);
     $this->assertType('Lisphp_Runtime_Predicate_NotEqual', $scope['!=']);
     $this->assertType('Lisphp_Runtime_Predicate_NotEqual', $scope['/=']);
     $this->assertType('Lisphp_Runtime_Predicate_NotEqual', $scope['not-equal']);
     $this->assertType('Lisphp_Runtime_Predicate_NotEqual', $scope['not-equal?']);
     foreach (Lisphp_Runtime_Predicate_Type::$types as $type) {
         $this->assertNoType('Lisphp_Runtime_Predicate_Type', $scope["{$type}?"]);
         // $this->assertEquals($type, $scope["$type?"]->type);
     }
     $this->assertNoType('Lisphp_Runtime_Predicate_Type', $scope['nil?']);
     // $this->assertEquals('null', $scope['nil?']->type);
     $this->assertNoType('Lisphp_Runtime_Predicate_IsA', $scope['is-a?']);
     $this->assertNoType('Lisphp_Runtime_Predicate_IsA', $scope['isa?']);
     $this->assertType('Lisphp_Runtime_Arithmetic_Addition', $scope['+']);
     $this->assertType('Lisphp_Runtime_Arithmetic_Subtraction', $scope['-']);
     $this->assertType('Lisphp_Runtime_Arithmetic_Multiplication', $scope['*']);
     $this->assertType('Lisphp_Runtime_Arithmetic_Division', $scope['/']);
     $this->assertType('Lisphp_Runtime_Arithmetic_Modulus', $scope['%']);
     $this->assertType('Lisphp_Runtime_Arithmetic_Modulus', $scope['mod']);
     $this->assertNoType('Lisphp_Runtime_PHPFunction', $scope['string']);
     // $this->assertEquals('strval', $scope['string']->callback);
     $this->assertNoType('Lisphp_Runtime_String_Concat', $scope['.']);
     $this->assertNoType('Lisphp_Runtime_String_Concat', $scope['concat']);
     $this->assertNoType('Lisphp_Runtime_String_StringJoin', $scope['string-join']);
     $this->assertNoType('Lisphp_Runtime_PHPFunction', $scope['substring']);
     // $this->assertEquals('substr', $scope['substring']->callback);
     $this->assertNoType('Lisphp_Runtime_PHPFunction', $scope['string-upcase']);
     // $this->assertEquals('strtoupper', $scope['string-upcase']->callback);
     $this->assertNoType('Lisphp_Runtime_PHPFunction', $scope['string-downcase']);
     // $this->assertEquals('strtolower', $scope['string-downcase']->callback);
     $this->assertType('Lisphp_Runtime_Logical_Not', $scope['not']);
     $this->assertType('Lisphp_Runtime_Logical_And', $scope['and']);
     $this->assertType('Lisphp_Runtime_Logical_Or', $scope['or']);
     $this->assertType('Lisphp_Runtime_Logical_If', $scope['if']);
     $this->assertNoType('Lisphp_Runtime_Object_GetAttribute', $scope['->']);
     // Expand
     $this->assertType('Workflow\\LispExpand\\String\\FetchUrl', $scope['fetch-url']);
     $this->assertType('Workflow\\LispExpand\\String\\ToJson', $scope['to-json']);
     $this->assertType('Workflow\\LispExpand\\String\\ShortUrl', $scope['short-url']);
 }
Example #7
0
 public function testFrom()
 {
     $from = new Lisphp_Runtime_From();
     $env = Lisphp_Environment::sandbox();
     $scope = new Lisphp_Scope($env);
     $values = $from->apply($scope, self::lst('Foo (<Bar> <Baz>)'));
     $this->assertEquals('Foo\\Bar', $values[0]->class->getName());
     $this->assertEquals('Foo\\Bar', $scope['<Bar>']->class->getName());
     $this->assertEquals('Foo\\Baz', $values[1]->class->getName());
     $this->assertEquals('Foo\\Baz', $scope['<Baz>']->class->getName());
 }
Example #8
0
<?php

require dirname(__FILE__) . '/Lisphp.php';
$options = getopt('v', array('verbose'));
function displayStrings()
{
    global $result;
    $args = func_get_args();
    $result .= join('', array_map('strval', $args));
}
$testFiles = glob(dirname(__FILE__) . '/tests/*.lisphp');
$fails = array();
foreach ($testFiles as $file) {
    $program = Lisphp_Program::load($file);
    $result = '';
    $scope = Lisphp_Environment::full();
    $scope['echo'] = new Lisphp_Runtime_PHPFunction('displayStrings');
    $program->execute($scope);
    $expected = file_get_contents(preg_replace('/\\.lisphp$/', '.out', $file));
    if (trim($result) == trim($expected)) {
        echo '.';
    } else {
        echo 'F';
        $fails[$file] = $result;
    }
}
if ($fails) {
    echo "\nFailed: ";
} else {
    echo "\nOK ";
}
Example #9
0
 /**
  * @return \Phalcon\Http\ResponseInterface|string|void
  */
 public function resultAction()
 {
     $hash_id = $this->request->getQuery('id', 'string', '');
     $workflow = $this->workflow->findFirst($hash_id, $this->current_user->id);
     if ($workflow) {
         // delete old result
         $result = WorkflowResult::findByWf_id($workflow->id);
         $result->delete();
         // set globals var
         $GLOBALS['workflow_id'] = $workflow->id;
         $GLOBALS['user_id'] = $this->current_user->id;
         $var_code = '';
         $var = WorkflowVar::findByWf_id($workflow->id);
         foreach ($var as $item) {
             if (is_string($item->value)) {
                 $var_code .= sprintf('(define %s "%s")', $item->name, str_replace('"', '\\"', $item->value));
             } else {
                 $var_code .= sprintf('(define %s %s)', $item->name, $item->value);
             }
         }
         // run code
         try {
             $env = Lisphp_Environment::workflow();
             $program = new Lisphp_Program($var_code . $workflow->code_snippets);
             $code_result = $program->execute($env);
         } catch (Exception $e) {
             $code_result = $e->getMessage();
         }
         // active
         UserActive::record('workflow-run', $this->current_user->id);
         // unset globals var
         unset($GLOBALS['workflow_id']);
         // new result
         $workflow_result = WorkflowResult::findByWf_id($workflow->id);
         $this->view->setVar('title', $workflow->title);
         $this->view->setVar('code_result', $code_result);
         $this->view->setVar('workflow_result', $workflow_result);
         return $this->view->partial('workflow/result');
     } else {
         return $this->response->redirect('workflow/lists');
     }
 }
Example #10
0
 function testFrom()
 {
     $from = new Lisphp_Runtime_From();
     $env = Lisphp_Environment::sandbox();
     $scope = new Lisphp_Scope($env);
     $values = $from->apply($scope, self::lst('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());
 }
Example #11
0
    echo $lines[$e->getLisphpLine() - 1], "\n";
    echo str_repeat(' ', $e->getLisphpColumn() - 1), "^\n";
}
$options = getopt('hvsc:');
if (isset($options['h']) || isset($options['v'])) {
    echo 'Lisphp ' . LISPHP_VERSION . "\n";
    if (isset($options['v'])) {
        echo 'PHP-', PHP_VERSION, "\n", php_uname(), "\n";
    }
    if (isset($options['h'])) {
        echo "Usage: {$_SERVER['argv'][0]} [options] <file>\n\n";
        echo Lisphp_usage(), "\n";
    }
    exit;
}
$environment = isset($options['s']) ? Lisphp_Environment::sandbox() : Lisphp_Environment::full();
$scope = new Lisphp_Scope($environment);
$scope['echo'] = new Lisphp_Runtime_PHPFunction(create_function('', '
    $args = func_get_args();
    foreach ($args as $arg) echo $arg;
'));
class Lisphp_EnterREPL extends Exception
{
}
try {
    $file = end($_SERVER['argv']);
    if (isset($options['c'])) {
        $program = new Lisphp_Program($options['c']);
    } else {
        if (count($_SERVER['argv']) > 1 && $file != '-s') {
            $program = Lisphp_Program::load($file);