$_POST['command'] = '(print ' . $_POST['command'] . ')'; } $lisp = new Lisphp(stripslashes($_POST['command'])); $globalMacros = ''; if (isset($_POST['showSource'])) { echo implode("\n\n", LisphpSpecialForms::$lambdas) . "\n\n"; } eval(implode(LisphpSpecialForms::$lambdas)); foreach (LisphpSpecialForms::$macros as $macro => $mac) { if (isset($_POST['showSource'])) { echo "macro {$macro} :: {$mac}\n\n"; } eval('LisphpSpecialForms::$macros["' . $macro . '"] = ' . $mac . ';'); $globalMacros .= '$' . $macro . ' =& LisphpSpecialForms::$macros["' . $macro . '"];' . "\n"; } LisphpSpecialForms::$lambdas = array(); // $this->ast = $this->expandMacros($this->ast); $ast = $lisp->getAst(); array_unshift($ast, 'begin'); $php = evalLisp($ast); $transformed = implode("\n", LisphpSpecialForms::$lambdas) . $globalMacros . implode(";\n", $php) . ";"; if (isset($_POST['showAst'])) { echo json_encode($lisp->getAst()) . "\n\n"; } if (isset($_POST['showSource'])) { foreach (explode("\n", $transformed) as $num => $line) { echo (isset($_POST['showLineNumbers']) ? '#' . ($num + 1) . ' ' : '') . "{$line}\n"; } } eval($transformed); }
function evalLisp($ast, &$env = false) { if (!is_array($ast)) { if (is_numeric($ast) || $ast[0] == '(') { return $ast; } else { if ($env) { $env->uses($ast); } return '$' . $ast; } } if (!$env) { $env = new StackFrame(); } $form = array_shift($ast); $formMethod = "{$form}Form"; if (is_callable(array('LisphpSpecialForms', $formMethod))) { return LisphpSpecialForms::$formMethod($ast, $env); } else { return LisphpSpecialForms::applyForm(array($form, $ast), $env); } }