mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 13:24:46 +00:00
support for command line arguments
This commit is contained in:
parent
fcb43515dd
commit
37ad2ec0bf
@ -1,16 +1,17 @@
|
||||
<?php
|
||||
require('vendor/autoload.php');
|
||||
|
||||
function ml_get_lisp(): array
|
||||
function ml_get_lisp(bool $debug): array
|
||||
{
|
||||
$tokenizer = new MadLisp\Tokenizer();
|
||||
$reader = new MadLisp\Reader();
|
||||
$printer = new MadLisp\Printer();
|
||||
$eval = new MadLisp\Evaller($tokenizer, $reader, $printer);
|
||||
$eval->setDebug($debug);
|
||||
|
||||
$lisp = new MadLisp\Lisp($tokenizer, $reader, $eval, $printer);
|
||||
|
||||
// Environment
|
||||
// Root environment
|
||||
$env = new MadLisp\Env('root');
|
||||
|
||||
// Register core functions
|
||||
@ -25,5 +26,8 @@ function ml_get_lisp(): array
|
||||
(new MadLisp\Lib\Time())->register($env);
|
||||
(new MadLisp\Lib\Types())->register($env);
|
||||
|
||||
// User environment
|
||||
$env = new MadLisp\Env('user', $env);
|
||||
|
||||
return [$lisp, $env];
|
||||
}
|
||||
|
76
run.php
76
run.php
@ -1,32 +1,60 @@
|
||||
<?php
|
||||
require('bootstrap.php');
|
||||
|
||||
list($lisp, $rootEnv) = ml_get_lisp();
|
||||
|
||||
// Read history
|
||||
$historyFile = $_SERVER['HOME'] . '/.madlisp_history';
|
||||
if (is_readable($historyFile)) {
|
||||
readline_read_history($historyFile);
|
||||
if (php_sapi_name() != 'cli') {
|
||||
exit('Currently only cli usage is supported.');
|
||||
}
|
||||
|
||||
// Create new env for user definitions
|
||||
$userEnv = new MadLisp\Env('repl', $rootEnv);
|
||||
|
||||
while (true) {
|
||||
$input = readline('> ');
|
||||
|
||||
try {
|
||||
$lisp->rep($input, $userEnv);
|
||||
|
||||
if ($input) {
|
||||
readline_add_history($input);
|
||||
readline_write_history($historyFile);
|
||||
}
|
||||
} catch (MadLisp\MadLispException $ex) {
|
||||
print('error: ' . $ex->getMessage());
|
||||
} catch (TypeError $ex) {
|
||||
print('error: invalid argument type: ' . $ex->getMessage());
|
||||
function ml_repl($lisp, $env)
|
||||
{
|
||||
// Read history
|
||||
$historyFile = $_SERVER['HOME'] . '/.madlisp_history';
|
||||
if (is_readable($historyFile)) {
|
||||
readline_read_history($historyFile);
|
||||
}
|
||||
|
||||
print(PHP_EOL);
|
||||
while (true) {
|
||||
$input = readline('> ');
|
||||
|
||||
try {
|
||||
$lisp->rep($input, $env);
|
||||
|
||||
if ($input) {
|
||||
readline_add_history($input);
|
||||
readline_write_history($historyFile);
|
||||
}
|
||||
} catch (MadLisp\MadLispException $ex) {
|
||||
print('error: ' . $ex->getMessage());
|
||||
} catch (TypeError $ex) {
|
||||
print('error: invalid argument type: ' . $ex->getMessage());
|
||||
}
|
||||
|
||||
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();
|
||||
|
@ -7,7 +7,7 @@ class Evaller
|
||||
protected Reader $reader;
|
||||
protected Printer $printer;
|
||||
|
||||
private const DEBUG = true;
|
||||
protected bool $debug = false;
|
||||
|
||||
public function __construct(Tokenizer $tokenizer, Reader $reader, Printer $printer)
|
||||
{
|
||||
@ -18,7 +18,7 @@ class Evaller
|
||||
|
||||
public function eval($ast, Env $env)
|
||||
{
|
||||
if (self::DEBUG) {
|
||||
if ($this->debug) {
|
||||
print("eval: ");
|
||||
$this->printer->print($ast);
|
||||
print("\n");
|
||||
@ -27,7 +27,7 @@ class Evaller
|
||||
|
||||
while (true) {
|
||||
|
||||
if (self::DEBUG) {
|
||||
if ($this->debug) {
|
||||
if ($loops++ > 0) {
|
||||
print("eval loop: ");
|
||||
$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)
|
||||
{
|
||||
if ($ast instanceof Symbol) {
|
||||
|
Loading…
Reference in New Issue
Block a user