rewrite run.php, support reading from stdin

This commit is contained in:
Pekka Laiho 2020-06-17 18:04:43 +07:00
parent d32ca9a4b6
commit 7f1bf405eb

47
run.php
View File

@ -33,27 +33,36 @@ function ml_repl($lisp)
} }
} }
function ml_run() // Create the Lisp interpreter
{ $factory = new MadLisp\LispFactory();
$args = getopt('de:f:r'); $lisp = $factory->make();
$factory = new MadLisp\LispFactory(); if ($argc < 2) {
$lisp = $factory->make(); // Read input from stdin
$input = file_get_contents('php://stdin');
if (array_key_exists('e', $args)) { $lisp->rep($input, false);
$lisp->rep($args['e'], false); } elseif ($argv[1] == '-r') {
} elseif (array_key_exists('f', $args)) { // Run the repl
$input = "(load \"{$args['f']}\")"; ml_repl($lisp);
} elseif ($argv[1] == '-e') {
// Evaluate next argument
$lisp->rep($argv[2] ?? '', false);
} elseif ($argv[1] == '-h') {
// Show help
print("Usage:" . PHP_EOL);
print("-e <code> :: Evaluate code" . PHP_EOL);
print("-h :: Show this help" . PHP_EOL);
print("-r :: Run the interactive Repl" . PHP_EOL);
print("<filename> :: Evaluate file" . PHP_EOL);
print("<no arguments> :: Read from stdin" . PHP_EOL);
} else {
// Read file
$file = $argv[1];
if (is_readable($file)) {
$input = "(load \"$file\")";
$lisp->rep($input, false); $lisp->rep($input, false);
} elseif (array_key_exists('r', $args)) {
ml_repl($lisp);
} else { } else {
print("Usage:" . PHP_EOL); print("Unable to read file: $file\n");
print("-d :: Debug mode" . PHP_EOL); exit(1); // exit with error code
print("-e <code> :: Evaluate code" . PHP_EOL);
print("-f <file> :: Evaluate file" . PHP_EOL);
print("-r :: Run the interactive repl" . PHP_EOL);
} }
} }
ml_run();