print null as empty string, enable functions eval and print

This commit is contained in:
Pekka Laiho 2020-05-31 21:16:20 +07:00
parent b0f22a19fe
commit 69918f3300
3 changed files with 26 additions and 15 deletions

View File

@ -10,25 +10,16 @@ function ml_get_lisp(): array
$lisp = new MadLisp\Lisp($tokenizer, $reader, $eval, $printer);
// environment
// Environment
$env = new MadLisp\Env();
// Register core functions
$lisp->register($env);
// Register libraries
(new MadLisp\Lib\Math())->register($env);
(new MadLisp\Lib\Compare())->register($env);
(new MadLisp\Lib\Types())->register($env);
/*
$env->set('eval', function (...$args) use ($eval, $env) {
$results = $eval->eval($args, $env);
return $results[count($results) - 1];
});
$env->set('print', function (...$args) use ($printer) {
$printer->print($args);
return null;
});
*/
return [$lisp, $env];
}

View File

@ -26,4 +26,24 @@ class Lisp
$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;
}
));
}
}

View File

@ -28,7 +28,7 @@ class Printer
} elseif ($a === false) {
return 'false';
} elseif ($a === null) {
return 'null';
return '';
} elseif (is_string($a)) {
return '"' . $a . '"';
} else {