2020-05-28 04:55:58 +00:00
|
|
|
<?php
|
|
|
|
namespace MadLisp;
|
|
|
|
|
|
|
|
class Lisp
|
|
|
|
{
|
2020-05-28 10:10:00 +00:00
|
|
|
protected Tokenizer $tokenizer;
|
|
|
|
protected Reader $reader;
|
|
|
|
protected Evaller $eval;
|
|
|
|
protected Printer $printer;
|
2020-06-17 10:24:15 +00:00
|
|
|
protected Env $env;
|
2020-05-28 09:49:32 +00:00
|
|
|
|
2020-06-17 10:24:15 +00:00
|
|
|
public function __construct(Tokenizer $tokenizer, Reader $reader, Evaller $eval, Printer $printer, Env $env)
|
2020-05-28 04:55:58 +00:00
|
|
|
{
|
2020-05-28 10:10:00 +00:00
|
|
|
$this->tokenizer = $tokenizer;
|
|
|
|
$this->reader = $reader;
|
|
|
|
$this->eval = $eval;
|
|
|
|
$this->printer = $printer;
|
2020-06-17 10:24:15 +00:00
|
|
|
$this->env = $env;
|
2020-05-28 07:40:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 01:09:12 +00:00
|
|
|
public function print($value, bool $printReadable): void
|
|
|
|
{
|
|
|
|
$this->printer->print($value, $printReadable);
|
|
|
|
}
|
|
|
|
|
2020-10-24 07:27:02 +00:00
|
|
|
public function readEval(string $input)
|
2020-05-28 07:40:54 +00:00
|
|
|
{
|
2020-05-28 10:10:00 +00:00
|
|
|
$tokens = $this->tokenizer->tokenize($input);
|
2020-05-28 07:40:54 +00:00
|
|
|
|
2020-06-04 02:10:48 +00:00
|
|
|
$expr = $this->reader->read($tokens);
|
2020-05-28 07:40:54 +00:00
|
|
|
|
2020-10-24 07:27:02 +00:00
|
|
|
return $this->eval->eval($expr, $this->env);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read, eval, print
|
|
|
|
public function rep(string $input, bool $printReadable): void
|
|
|
|
{
|
2020-12-09 01:09:12 +00:00
|
|
|
$this->print($this->readEval($input), $printReadable);
|
2020-05-28 04:55:58 +00:00
|
|
|
}
|
2020-12-06 02:32:45 +00:00
|
|
|
|
2020-12-13 07:33:33 +00:00
|
|
|
public function setDebug(bool $value): void
|
|
|
|
{
|
|
|
|
$this->eval->setDebug($value);
|
|
|
|
}
|
|
|
|
|
2020-12-06 02:32:45 +00:00
|
|
|
public function setEnv(Env $env): void
|
|
|
|
{
|
|
|
|
$this->env = $env;
|
|
|
|
}
|
2020-12-13 07:33:33 +00:00
|
|
|
|
|
|
|
public function setEnvValue(string $key, $value): void
|
|
|
|
{
|
|
|
|
$this->env->set($key, $value);
|
|
|
|
}
|
2020-05-28 04:55:58 +00:00
|
|
|
}
|