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'); return $this->readSpecialForm($tokens, $index, 'quasiquote');
} elseif ($tokens[$index] == "~") { } elseif ($tokens[$index] == "~") {
return $this->readSpecialForm($tokens, $index, 'unquote'); return $this->readSpecialForm($tokens, $index, 'unquote');
} elseif ($tokens[$index] == "~@") {
return $this->readSpecialForm($tokens, $index, 'unquote-splice');
} elseif ($tokens[$index] == '(') { } elseif ($tokens[$index] == '(') {
return $this->readList($tokens, $index); return $this->readList($tokens, $index);
} elseif ($tokens[$index] == '[') { } elseif ($tokens[$index] == '[') {

View File

@ -101,6 +101,14 @@ class Tokenizer
// Other special characters // Other special characters
$addCurrent(); $addCurrent();
$tokens[] = $c; $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 { } else {
// All other characters // All other characters
$current .= $c; $current .= $c;

View File

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

View File

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