Accept tabs as being equivalent to eight spaces, but no mixing on a single line

This commit is contained in:
K. Lange 2021-01-08 07:05:33 +09:00
parent ee518f9643
commit cdd3272d0e
5 changed files with 16 additions and 14 deletions

View File

@ -141,13 +141,7 @@ Blocks, including function `def` blocks and control flow structures like `if` an
You may indent blocks to whatever level you desire, so long as ordering remains consistent, though the recommendation indentation size is 4 spaces.
Tabs are not valid as indentation and will be ignored. It is recommended that you use an editor which provides a clear visual distinction between tabs and spaces, such as [Bim](https://github.com/klange/bim).
```py
if False:
print("Oh no, that was a tab.")
# → Oh no, that was a tab.
```
It is recommended that you use an editor which provides a clear visual distinction between tabs and spaces, such as [Bim](https://github.com/klange/bim).
Blocks can also accept a single inline statement:

View File

@ -86,18 +86,19 @@ static void skipWhitespace() {
}
static KrkToken makeIndentation() {
char reject = (peek() == ' ') ? '\t' : ' ';
while (!isAtEnd() && (peek() == ' ' || peek() == '\t')) advance();
if (isAtEnd()) return makeToken(TOKEN_EOF);
if (peek() == '\n') {
/* Pretend we didn't see this line */
return makeToken(TOKEN_INDENTATION);
for (const char * start = scanner.start; start < scanner.cur; start++) {
if (*start == reject) return errorToken("Invalid mix of indentation.");
}
KrkToken out = makeToken(TOKEN_INDENTATION);
if (reject == ' ') out.length *= 8;
if (peek() == '#') {
KrkToken out = makeToken(TOKEN_INDENTATION);
/* Skip the entirety of the comment but not the line feed */
while (!isAtEnd() && peek() != '\n') advance();
return out;
}
return makeToken(TOKEN_INDENTATION);
return out;
}
static KrkToken string(char quoteMark) {

View File

@ -1,2 +1,2 @@
960 lines
954 lines
['', 'Kuroko is a bytecode-interpreted, dynamic, strongly-typed language with syntax similar to Python.', '', 'The bytecode VM / compiler is substantially based on Robert Nystrom\'s [_Crafting Interpreters_](https://craftinginterpreters.com/).']

5
test/testTabWidth.krk Normal file
View File

@ -0,0 +1,5 @@
def foo():
print("Four spaces")
print("Four tabs")
foo()

View File

@ -0,0 +1,2 @@
Four spaces
Four tabs