2020-06-17 10:24:15 +00:00
|
|
|
<?php
|
2020-12-14 01:49:07 +00:00
|
|
|
/**
|
|
|
|
* MadLisp language
|
|
|
|
* @link http://madlisp.com/
|
|
|
|
* @copyright Copyright (c) 2020 Pekka Laiho
|
|
|
|
*/
|
|
|
|
|
2020-06-17 10:24:15 +00:00
|
|
|
namespace MadLisp;
|
|
|
|
|
|
|
|
class LispFactory
|
|
|
|
{
|
2020-12-05 04:24:31 +00:00
|
|
|
public function make(bool $safemode = false): Lisp
|
2020-06-17 10:24:15 +00:00
|
|
|
{
|
|
|
|
$tokenizer = new Tokenizer();
|
|
|
|
$reader = new Reader();
|
|
|
|
$printer = new Printer();
|
2020-12-05 04:24:31 +00:00
|
|
|
$eval = new Evaller($tokenizer, $reader, $printer, $safemode);
|
2020-06-17 10:24:15 +00:00
|
|
|
|
|
|
|
// Root environment
|
|
|
|
$env = new Env('root');
|
|
|
|
|
|
|
|
// Register core functions
|
2020-12-05 04:24:31 +00:00
|
|
|
(new Lib\Core($tokenizer, $reader, $printer, $eval, $safemode))->register($env);
|
2020-10-17 07:47:30 +00:00
|
|
|
|
2020-06-17 10:24:15 +00:00
|
|
|
// Register core libraries
|
|
|
|
(new Lib\Collections())->register($env);
|
|
|
|
(new Lib\Compare())->register($env);
|
2020-12-10 12:51:49 +00:00
|
|
|
if (extension_loaded('json')) {
|
|
|
|
(new Lib\Json())->register($env);
|
|
|
|
}
|
2020-06-17 10:24:15 +00:00
|
|
|
(new Lib\Math())->register($env);
|
2020-12-10 12:51:49 +00:00
|
|
|
if (extension_loaded('pcre')) {
|
|
|
|
(new Lib\Regex())->register($env);
|
|
|
|
}
|
2020-06-17 10:24:15 +00:00
|
|
|
(new Lib\Strings())->register($env);
|
|
|
|
(new Lib\Time())->register($env);
|
|
|
|
(new Lib\Types())->register($env);
|
|
|
|
|
2020-12-06 03:18:52 +00:00
|
|
|
// Register unsafe libraries if not in safe-mode
|
2020-12-05 04:24:31 +00:00
|
|
|
if (!$safemode) {
|
2020-12-10 12:51:49 +00:00
|
|
|
if (extension_loaded('PDO')) {
|
|
|
|
(new Lib\Database())->register($env);
|
|
|
|
}
|
|
|
|
if (extension_loaded('curl')) {
|
|
|
|
(new Lib\Http())->register($env);
|
|
|
|
}
|
2020-12-05 04:24:31 +00:00
|
|
|
(new Lib\IO())->register($env);
|
2020-06-17 10:24:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 02:32:45 +00:00
|
|
|
$lisp = new Lisp($tokenizer, $reader, $eval, $printer, $env);
|
2020-06-17 10:24:15 +00:00
|
|
|
|
2020-12-06 02:32:45 +00:00
|
|
|
// Add some built-in macros
|
|
|
|
$lisp->readEval('(def defn (macro (name args body) (quasiquote (def (unquote name) (fn (unquote args) (unquote body))))))');
|
|
|
|
$lisp->readEval('(def defmacro (macro (name args body) (quasiquote (def (unquote name) (macro (unquote args) (unquote body))))))');
|
|
|
|
|
|
|
|
// Separate environment for user-defined stuff
|
|
|
|
$lisp->setEnv(new Env('user', $env));
|
|
|
|
|
|
|
|
return $lisp;
|
2020-06-17 10:24:15 +00:00
|
|
|
}
|
|
|
|
}
|