madlisp/run.php

60 lines
1.5 KiB
PHP
Raw Normal View History

2020-05-26 08:43:59 +00:00
<?php
2020-06-17 10:24:15 +00:00
require(__DIR__ . '/vendor/autoload.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-17 10:24:15 +00:00
function ml_repl($lisp)
2020-06-10 13:40:15 +00:00
{
// 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 {
2020-06-17 10:24:15 +00:00
$lisp->rep($input, 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-17 10:24:15 +00:00
$factory = new MadLisp\LispFactory();
$lisp = $factory->make();
2020-06-10 13:40:15 +00:00
if (array_key_exists('e', $args)) {
2020-06-17 10:24:15 +00:00
$lisp->rep($args['e'], false);
2020-06-10 13:40:15 +00:00
} elseif (array_key_exists('f', $args)) {
$input = "(load \"{$args['f']}\")";
2020-06-17 10:24:15 +00:00
$lisp->rep($input, false);
2020-06-10 13:40:15 +00:00
} elseif (array_key_exists('r', $args)) {
2020-06-17 10:24:15 +00:00
ml_repl($lisp);
2020-06-10 13:40:15 +00:00
} 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();