debugging support for eval

This commit is contained in:
Pekka Laiho 2020-06-06 21:02:02 +07:00
parent 01d429324e
commit aa71b4da36
2 changed files with 21 additions and 2 deletions

View File

@ -5,8 +5,8 @@ function ml_get_lisp(): array
{ {
$tokenizer = new MadLisp\Tokenizer(); $tokenizer = new MadLisp\Tokenizer();
$reader = new MadLisp\Reader(); $reader = new MadLisp\Reader();
$eval = new MadLisp\Evaller($tokenizer, $reader);
$printer = new MadLisp\Printer(); $printer = new MadLisp\Printer();
$eval = new MadLisp\Evaller($tokenizer, $reader, $printer);
$lisp = new MadLisp\Lisp($tokenizer, $reader, $eval, $printer); $lisp = new MadLisp\Lisp($tokenizer, $reader, $eval, $printer);

View File

@ -5,17 +5,36 @@ class Evaller
{ {
protected Tokenizer $tokenizer; protected Tokenizer $tokenizer;
protected Reader $reader; protected Reader $reader;
protected Printer $printer;
public function __construct(Tokenizer $tokenizer, Reader $reader) private const DEBUG = true;
public function __construct(Tokenizer $tokenizer, Reader $reader, Printer $printer)
{ {
$this->tokenizer = $tokenizer; $this->tokenizer = $tokenizer;
$this->reader = $reader; $this->reader = $reader;
$this->printer = $printer;
} }
public function eval($ast, Env $env) public function eval($ast, Env $env)
{ {
if (self::DEBUG) {
print("eval: ");
$this->printer->print($ast);
print("\n");
$loops = 0;
}
while (true) { while (true) {
if (self::DEBUG) {
if ($loops++ > 0) {
print("eval loop: ");
$this->printer->print($ast);
print("\n");
}
}
// Not list or empty list // Not list or empty list
if (!($ast instanceof MList)) { if (!($ast instanceof MList)) {
return $this->evalAst($ast, $env); return $this->evalAst($ast, $env);