support for command line arguments

This commit is contained in:
Pekka Laiho 2020-06-10 20:40:15 +07:00
parent fcb43515dd
commit 37ad2ec0bf
3 changed files with 66 additions and 29 deletions

View File

@ -1,16 +1,17 @@
<?php <?php
require('vendor/autoload.php'); require('vendor/autoload.php');
function ml_get_lisp(): array function ml_get_lisp(bool $debug): array
{ {
$tokenizer = new MadLisp\Tokenizer(); $tokenizer = new MadLisp\Tokenizer();
$reader = new MadLisp\Reader(); $reader = new MadLisp\Reader();
$printer = new MadLisp\Printer(); $printer = new MadLisp\Printer();
$eval = new MadLisp\Evaller($tokenizer, $reader, $printer); $eval = new MadLisp\Evaller($tokenizer, $reader, $printer);
$eval->setDebug($debug);
$lisp = new MadLisp\Lisp($tokenizer, $reader, $eval, $printer); $lisp = new MadLisp\Lisp($tokenizer, $reader, $eval, $printer);
// Environment // Root environment
$env = new MadLisp\Env('root'); $env = new MadLisp\Env('root');
// Register core functions // Register core functions
@ -25,5 +26,8 @@ function ml_get_lisp(): array
(new MadLisp\Lib\Time())->register($env); (new MadLisp\Lib\Time())->register($env);
(new MadLisp\Lib\Types())->register($env); (new MadLisp\Lib\Types())->register($env);
// User environment
$env = new MadLisp\Env('user', $env);
return [$lisp, $env]; return [$lisp, $env];
} }

38
run.php
View File

@ -1,22 +1,23 @@
<?php <?php
require('bootstrap.php'); require('bootstrap.php');
list($lisp, $rootEnv) = ml_get_lisp(); if (php_sapi_name() != 'cli') {
exit('Currently only cli usage is supported.');
}
function ml_repl($lisp, $env)
{
// Read history // Read history
$historyFile = $_SERVER['HOME'] . '/.madlisp_history'; $historyFile = $_SERVER['HOME'] . '/.madlisp_history';
if (is_readable($historyFile)) { if (is_readable($historyFile)) {
readline_read_history($historyFile); readline_read_history($historyFile);
} }
// Create new env for user definitions
$userEnv = new MadLisp\Env('repl', $rootEnv);
while (true) { while (true) {
$input = readline('> '); $input = readline('> ');
try { try {
$lisp->rep($input, $userEnv); $lisp->rep($input, $env);
if ($input) { if ($input) {
readline_add_history($input); readline_add_history($input);
@ -30,3 +31,30 @@ while (true) {
print(PHP_EOL); print(PHP_EOL);
} }
}
function ml_run()
{
$args = getopt('de:f:r');
$debug = array_key_exists('d', $args);
list($lisp, $env) = ml_get_lisp($debug);
if (array_key_exists('e', $args)) {
$lisp->rep($args['e'], $env);
} elseif (array_key_exists('f', $args)) {
$input = "(load \"{$args['f']}\")";
$lisp->rep($input, $env);
} elseif (array_key_exists('r', $args)) {
ml_repl($lisp, $env);
} else {
print("Usage:" . PHP_EOL);
print("-d :: Debug mode" . PHP_EOL);
print("-e <code> :: Evaluate code" . PHP_EOL);
print("-f <file> :: Evaluate file" . PHP_EOL);
print("-r :: Run the interactive repl" . PHP_EOL);
}
}
ml_run();

View File

@ -7,7 +7,7 @@ class Evaller
protected Reader $reader; protected Reader $reader;
protected Printer $printer; protected Printer $printer;
private const DEBUG = true; protected bool $debug = false;
public function __construct(Tokenizer $tokenizer, Reader $reader, Printer $printer) public function __construct(Tokenizer $tokenizer, Reader $reader, Printer $printer)
{ {
@ -18,7 +18,7 @@ class Evaller
public function eval($ast, Env $env) public function eval($ast, Env $env)
{ {
if (self::DEBUG) { if ($this->debug) {
print("eval: "); print("eval: ");
$this->printer->print($ast); $this->printer->print($ast);
print("\n"); print("\n");
@ -27,7 +27,7 @@ class Evaller
while (true) { while (true) {
if (self::DEBUG) { if ($this->debug) {
if ($loops++ > 0) { if ($loops++ > 0) {
print("eval loop: "); print("eval loop: ");
$this->printer->print($ast); $this->printer->print($ast);
@ -257,6 +257,11 @@ class Evaller
} }
} }
public function setDebug(bool $val): void
{
$this->debug = $val;
}
private function evalAst($ast, Env $env) private function evalAst($ast, Env $env)
{ {
if ($ast instanceof Symbol) { if ($ast instanceof Symbol) {