mirror of
https://github.com/peklaiho/madlisp.git
synced 2025-01-07 06:36:25 +00:00
50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
namespace MadLisp;
|
|
|
|
class Lisp
|
|
{
|
|
protected Tokenizer $tokenizer;
|
|
protected Reader $reader;
|
|
protected Evaller $eval;
|
|
protected Printer $printer;
|
|
|
|
public function __construct(Tokenizer $tokenizer, Reader $reader, Evaller $eval, Printer $printer)
|
|
{
|
|
$this->tokenizer = $tokenizer;
|
|
$this->reader = $reader;
|
|
$this->eval = $eval;
|
|
$this->printer = $printer;
|
|
}
|
|
|
|
public function rep(string $input, Env $env): void
|
|
{
|
|
$tokens = $this->tokenizer->tokenize($input);
|
|
|
|
$expressions = $this->reader->read($tokens);
|
|
|
|
$results = $this->eval->eval($expressions, $env);
|
|
|
|
$this->printer->print($results);
|
|
}
|
|
|
|
public function register(Env $env): void
|
|
{
|
|
$env->set('eval', new CoreFunc('eval', 'Evaluate arguments.', 1, -1,
|
|
function (...$args) use ($env) {
|
|
$results = $this->eval->eval($args, $env);
|
|
|
|
// Return last evaluated value
|
|
return $results[count($results) - 1];
|
|
}
|
|
));
|
|
|
|
$env->set('print', new CoreFunc('print', 'Print arguments.', 1, -1,
|
|
function (...$args) {
|
|
$this->printer->print($args);
|
|
|
|
return null;
|
|
}
|
|
));
|
|
}
|
|
}
|