madlisp/run.php

61 lines
1.5 KiB
PHP
Raw Normal View History

2020-05-26 08:43:59 +00:00
<?php
2020-05-28 04:55:58 +00:00
require('bootstrap.php');
2020-05-27 09:27:47 +00:00
2020-06-10 13:40:15 +00:00
if (php_sapi_name() != 'cli') {
exit('Currently only cli usage is supported.');
2020-06-06 13:12:45 +00:00
}
2020-06-10 13:40:15 +00:00
function ml_repl($lisp, $env)
{
// Read history
$historyFile = $_SERVER['HOME'] . '/.madlisp_history';
if (is_readable($historyFile)) {
readline_read_history($historyFile);
}
2020-05-26 08:43:59 +00:00
2020-06-10 13:40:15 +00:00
while (true) {
$input = readline('> ');
2020-05-26 08:43:59 +00:00
2020-06-10 13:40:15 +00:00
try {
$lisp->rep($input, $env, true);
2020-06-06 13:12:45 +00:00
2020-06-10 13:40:15 +00:00
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());
2020-06-06 13:12:45 +00:00
}
2020-06-10 13:40:15 +00:00
print(PHP_EOL);
2020-05-26 08:43:59 +00:00
}
2020-06-10 13:40:15 +00:00
}
function ml_run()
{
$args = getopt('de:f:r');
2020-05-26 08:43:59 +00:00
2020-06-10 13:40:15 +00:00
$debug = array_key_exists('d', $args);
list($lisp, $env) = ml_get_lisp($debug);
if (array_key_exists('e', $args)) {
$lisp->rep($args['e'], $env, false);
2020-06-10 13:40:15 +00:00
} elseif (array_key_exists('f', $args)) {
$input = "(load \"{$args['f']}\")";
$lisp->rep($input, $env, false);
2020-06-10 13:40:15 +00:00
} 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);
}
2020-05-26 08:43:59 +00:00
}
2020-06-10 13:40:15 +00:00
ml_run();