mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 21:35:03 +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];
|
||||
}
|
||||
|
38
run.php
38
run.php
@ -1,22 +1,23 @@
|
||||
<?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
|
||||
$historyFile = $_SERVER['HOME'] . '/.madlisp_history';
|
||||
if (is_readable($historyFile)) {
|
||||
readline_read_history($historyFile);
|
||||
}
|
||||
|
||||
// Create new env for user definitions
|
||||
$userEnv = new MadLisp\Env('repl', $rootEnv);
|
||||
|
||||
while (true) {
|
||||
$input = readline('> ');
|
||||
|
||||
try {
|
||||
$lisp->rep($input, $userEnv);
|
||||
$lisp->rep($input, $env);
|
||||
|
||||
if ($input) {
|
||||
readline_add_history($input);
|
||||
@ -30,3 +31,30 @@ while (true) {
|
||||
|
||||
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