add shortcut ~@ for unquote-splice

This commit is contained in:
Pekka Laiho 2020-12-16 22:01:50 +07:00
parent 5d6fbcc9cc
commit d363700a39
4 changed files with 14 additions and 0 deletions

View File

@ -27,6 +27,8 @@ class Reader
return $this->readSpecialForm($tokens, $index, 'quasiquote');
} elseif ($tokens[$index] == "~") {
return $this->readSpecialForm($tokens, $index, 'unquote');
} elseif ($tokens[$index] == "~@") {
return $this->readSpecialForm($tokens, $index, 'unquote-splice');
} elseif ($tokens[$index] == '(') {
return $this->readList($tokens, $index);
} elseif ($tokens[$index] == '[') {

View File

@ -101,6 +101,14 @@ class Tokenizer
// Other special characters
$addCurrent();
$tokens[] = $c;
} elseif ($c == '@') {
// If the last token was ~ then add @ to it
if (count($tokens) > 0 && $tokens[count($tokens) - 1] == '~') {
$tokens[count($tokens) - 1] .= $c;
} else {
// Otherwise treat it like normal character
$current .= $c;
}
} else {
// All other characters
$current .= $c;

View File

@ -28,6 +28,7 @@ class ReaderTest extends TestCase
["'", 'quote'],
['`', 'quasiquote'],
['~', 'unquote'],
['~@', 'unquote-splice'],
];
}

View File

@ -76,6 +76,9 @@ class TokenizerTest extends TestCase
// Other non-alphabet characters are symbols
["(aa!@#$%^&*-_=+bb<>,./?\\|cc)", ['(', "aa!@#$%^&*-_=+bb<>,./?\\|cc", ')']],
// @ after ~ is single token, @ anywhere else is normal character
['aa@~@@bb', ['aa@', '~@', '@bb']],
// Strings
['"abc"', ['"abc"']],
['aa"bb"cc', ['aa', '"bb"', 'cc']],