From 132542c6ab88af8b412ca270e86ebd7908879691 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Mon, 4 Jan 2021 09:13:39 +0900 Subject: [PATCH] allow 'elif' for Python compatibility --- compiler.c | 4 ++-- rline.c | 2 +- scanner.c | 5 ++++- scanner.h | 1 + 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/compiler.c b/compiler.c index 1721378..5978e71 100644 --- a/compiler.c +++ b/compiler.c @@ -1015,8 +1015,8 @@ static void ifStatement() { previous = parser.previous; advance(); } - if (match(TOKEN_ELSE)) { - if (check(TOKEN_IF)) { + if (match(TOKEN_ELSE) || check(TOKEN_ELIF)) { + if (parser.current.type == TOKEN_ELIF || check(TOKEN_IF)) { parser.previous = myPrevious; ifStatement(); /* Keep nesting */ } else { diff --git a/rline.c b/rline.c index dbe7feb..defa718 100644 --- a/rline.c +++ b/rline.c @@ -521,7 +521,7 @@ void paint_krk_string(struct syntax_state * state, int type) { char * syn_krk_keywords[] = { "and","class","def","else","export","for","if","in","import", "let","not","or","print","return","while","try","except","raise", - "continue","break","as","from", + "continue","break","as","from","elif", NULL }; diff --git a/scanner.c b/scanner.c index 6334e27..fa45a1a 100644 --- a/scanner.c +++ b/scanner.c @@ -202,7 +202,10 @@ static KrkTokenType identifierType() { } break; case 'd': return checkKeyword(1, "ef", TOKEN_DEF); case 'e': if (MORE(1)) switch(scanner.start[1]) { - case 'l': return checkKeyword(2, "se", TOKEN_ELSE); + case 'l': if (MORE(2)) switch(scanner.start[2]) { + case 's': return checkKeyword(3,"e", TOKEN_ELSE); + case 'i': return checkKeyword(3,"f", TOKEN_ELIF); + } break; case 'x': if MORE(2) switch(scanner.start[2]) { case 'p': return checkKeyword(3, "ort", TOKEN_EXPORT); case 'c': return checkKeyword(3, "ept", TOKEN_EXCEPT); diff --git a/scanner.h b/scanner.h index c287dda..5b7ca94 100644 --- a/scanner.h +++ b/scanner.h @@ -66,6 +66,7 @@ typedef enum { TOKEN_AS, TOKEN_FROM, + TOKEN_ELIF, /* Provided for compatibility, recommend `else if` instead. */ TOKEN_ERROR, TOKEN_EOF,