Example #1
0
 /** @test */
 public function evaluateShouldTokenizeAndParseCode()
 {
     $lexer = $this->getMock('Igorw\\Ilias\\Lexer');
     $lexer->expects($this->once())->method('tokenize')->with('2')->will($this->returnValue(['2']));
     $reader = $this->getMock('Igorw\\Ilias\\Reader');
     $reader->expects($this->once())->method('parse')->with(['2'])->will($this->returnValue([2]));
     $builder = $this->getMock('Igorw\\Ilias\\FormTreeBuilder');
     $builder->expects($this->once())->method('parseAst')->with([2])->will($this->returnValue([new Form\LiteralForm(2)]));
     $walker = $this->getMock('Igorw\\Ilias\\Walker');
     $walker->expects($this->once())->method('expand')->with($this->isInstanceOf('Igorw\\Ilias\\Environment'), $this->isInstanceOf('Igorw\\Ilias\\Form\\Form'))->will($this->returnArgument(1));
     $env = $this->getMock('Igorw\\Ilias\\Environment');
     $program = new Program($lexer, $reader, $builder, $walker);
     $this->assertSame(2, $program->evaluate($env, '2'));
 }
Example #2
0
// example call:
// php examples/macro-expand.php
//     "(defmacro when (condition a b c) (list 'if condition (list 'begin a b c)))"
//     "(when (> x 10) 1 2 3)"
namespace Igorw\Ilias;

require __DIR__ . '/../vendor/autoload.php';
if ($argc < 3) {
    echo "Usage: php examples/macro-expand.php MACROS CODE\n";
    exit(1);
}
list($_, $macros, $code) = $argv;
$program = new Program(new Lexer(), new Reader(), new FormTreeBuilder(), new Walker());
$env = Environment::standard();
$program->evaluate($env, $macros);
$expandedForms = expand($env, $code);
foreach ($expandedForms as $expanded) {
    echo encode($expanded->getAst()) . "\n";
}
function buildForms($code)
{
    $lexer = new Lexer();
    $reader = new Reader();
    $builder = new FormTreeBuilder();
    $tokens = $lexer->tokenize($code);
    $ast = $reader->parse($tokens);
    return $builder->parseAst($ast);
}
function expand(Environment $env, $code)
{