mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 21:35:03 +00:00
58 lines
1.5 KiB
PHP
58 lines
1.5 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);
|
|
|
|
$expr = $this->reader->read($tokens);
|
|
|
|
$result = $this->eval->eval($expr, $env);
|
|
|
|
$this->printer->print($result);
|
|
}
|
|
|
|
public function register(Env $env): void
|
|
{
|
|
$env->set('doc', new CoreFunc('doc', 'Get documentation for a function.', 1, 1,
|
|
function ($a) {
|
|
if ($a instanceof Func) {
|
|
return $a->getDoc();
|
|
}
|
|
|
|
throw new MadLispException('first argument to doc is not function');
|
|
}
|
|
));
|
|
|
|
$env->set('read', new CoreFunc('read', 'Read string as code.', 1, 1,
|
|
fn (string $a) => $this->reader->read($this->tokenizer->tokenize($a))
|
|
));
|
|
|
|
$env->set('eval', new CoreFunc('eval', 'Evaluate argument.', 1, 1,
|
|
fn ($a) => $this->eval->eval($a, $env)
|
|
));
|
|
|
|
$env->set('print', new CoreFunc('print', 'Print argument.', 1, 1,
|
|
function ($a) {
|
|
$this->printer->print($a);
|
|
return null;
|
|
}
|
|
));
|
|
}
|
|
}
|