예제 #1
0
 function testParseForm_list()
 {
     $expected = new Lisphp_List(array(Lisphp_Symbol::get('define'), Lisphp_Symbol::get('add'), new Lisphp_List(array(Lisphp_Symbol::get('lambda'), new Lisphp_List(array(Lisphp_Symbol::get('a'), Lisphp_Symbol::get('b'))), new Lisphp_List(array(Lisphp_Symbol::get('+'), Lisphp_Symbol::get('a'), Lisphp_Symbol::get('b')))))));
     $this->assertForm($expected, 35, '(define add {lambda [a b] (+ a b)})');
     try {
         Lisphp_Parser::parseForm('(abc d ])', $offset);
         $this->fails();
     } catch (Lisphp_ParsingException $e) {
         $this->assertEquals('(abc d ])', $e->code);
         $this->assertEquals(7, $e->offset);
     }
 }
예제 #2
0
 function testEval()
 {
     $eval = new Lisphp_Runtime_Eval();
     $form = Lisphp_Parser::parseForm(':(+ 1 2 [- 4 3])', $_);
     $scope = Lisphp_Environment::sandbox();
     $args = new Lisphp_List(array($form));
     $this->assertEquals(4, $eval->apply($scope, $args));
     $args = new Lisphp_List(array($form, Lisphp_Symbol::get('scope')));
     $names = new Lisphp_Scope();
     $names['scope'] = $scope;
     $this->assertEquals(4, $eval->apply($names, $args));
 }
예제 #3
0
 function testInvalidApplication2()
 {
     $this->setExpectedException('InvalidApplicationException');
     $l = Lisphp_Parser::parseForm('("trim" "  hello  ")', $_);
     $l->evaluate(new Lisphp_Scope());
 }
예제 #4
0
 /**
  * @param $program
  * @throws Lisphp_ParsingException
  */
 public function __construct($program)
 {
     $this->forms = Lisphp_Parser::parse($program, true);
 }
예제 #5
0
파일: lis.php 프로젝트: huwenlong92/lisphp
        $exit = false;
    } else {
        $readline = create_function('$prompt', '
            echo $prompt;
            return fread(STDIN, 8192);
        ');
        $add_history = create_function('', '');
        $exit = '';
    }
    while (true) {
        $code = $readline(LISPHP_REPL_PROMPT);
        if ($code === $exit) {
            die("\n");
        } else {
            if (trim($code) == '') {
                continue;
            }
        }
        try {
            $form = Lisphp_Parser::parseForm($code, $_);
            echo LISPHP_REPL_VALUE_PROMPT;
            var_export($form->evaluate($scope));
            echo "\n";
        } catch (Lisphp_ParsingException $e) {
            Lisphp_printParsingError($e);
        } catch (Exception $e) {
            echo LISPHP_REPL_EXCEPTION_PROMPT, $e->getMessage(), "\n", preg_replace('/^|\\n/', '\\0    ', $e->getTraceAsString()), "\n";
        }
        $add_history($code);
    }
}