mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-26 07:04:27 +00:00
add shortcuts for quasiquote and unquote
This commit is contained in:
parent
a2f5cdbcc0
commit
f1c49009ab
@ -16,12 +16,11 @@ class Reader
|
||||
private function readForm(array $tokens, int &$index)
|
||||
{
|
||||
if ($tokens[$index] == "'") {
|
||||
$index++;
|
||||
$contents = [new Symbol('quote')];
|
||||
if ($index < count($tokens) && !in_array($tokens[$index], [')', ']', '}'])) {
|
||||
$contents[] = $this->readForm($tokens, $index);
|
||||
}
|
||||
return new MList($contents);
|
||||
return $this->readSpecialForm($tokens, $index, 'quote');
|
||||
} elseif ($tokens[$index] == "`") {
|
||||
return $this->readSpecialForm($tokens, $index, 'quasiquote');
|
||||
} elseif ($tokens[$index] == "~") {
|
||||
return $this->readSpecialForm($tokens, $index, 'unquote');
|
||||
} elseif ($tokens[$index] == '(') {
|
||||
return $this->readList($tokens, $index);
|
||||
} elseif ($tokens[$index] == '[') {
|
||||
@ -33,6 +32,16 @@ class Reader
|
||||
}
|
||||
}
|
||||
|
||||
private function readSpecialForm(array $tokens, int &$index, string $symbol)
|
||||
{
|
||||
$index++;
|
||||
$contents = [new Symbol($symbol)];
|
||||
if ($index < count($tokens) && !in_array($tokens[$index], [')', ']', '}'])) {
|
||||
$contents[] = $this->readForm($tokens, $index);
|
||||
}
|
||||
return new MList($contents);
|
||||
}
|
||||
|
||||
private function readList(array $tokens, int &$index): MList
|
||||
{
|
||||
return new MList($this->readCollection($tokens, $index, ')'));
|
||||
|
@ -69,7 +69,7 @@ class Tokenizer
|
||||
$addCurrent();
|
||||
$tokens[] = $c;
|
||||
$parens[$parenIndexes[$c]]--;
|
||||
} elseif ($c == "'") {
|
||||
} elseif ($c == "'" || $c == "`" || $c == "~") {
|
||||
// Other special characters
|
||||
$addCurrent();
|
||||
$tokens[] = $c;
|
||||
|
Loading…
Reference in New Issue
Block a user