From e310239b052237267709b40227eafd2f02731b13 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 16 Nov 2022 01:12:50 +0800 Subject: [PATCH] fix a bug --- src/compiler.h | 7 ++++--- src/parser.h | 3 +++ src/pocketpy.h | 7 ++++--- tests/6.py | 12 ++++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 tests/6.py diff --git a/src/compiler.h b/src/compiler.h index 782ffa1a..9ef79461 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -176,6 +176,8 @@ public: parser->current = parser->nextToken(); //_Str _info = parser->current.info(); printf("%s\n", (const char*)_info); + if(parser->current.type == TK("(")) parser->ignoreIndent += 1; + if(parser->current.type == TK(")")) parser->ignoreIndent -= 1; while (parser->peekChar() != '\0') { parser->token_start = parser->current_char; @@ -520,13 +522,12 @@ __LISTCOMP: void exprCall() { int ARGC = 0; do { - matchNewLines(); + matchNewLines(mode()==SINGLE_MODE); if (peek() == TK(")")) break; EXPR(); ARGC++; - matchNewLines(); + matchNewLines(mode()==SINGLE_MODE); } while (match(TK(","))); - matchNewLines(); consume(TK(")")); emitCode(OP_CALL, ARGC); } diff --git a/src/parser.h b/src/parser.h index 43e3df2e..7f1b2bc9 100644 --- a/src/parser.h +++ b/src/parser.h @@ -103,6 +103,8 @@ struct Parser { std::queue nexts; std::stack indents; + int ignoreIndent = 0; + Token nextToken(){ if(nexts.empty()) return makeErrToken(); Token t = nexts.front(); @@ -137,6 +139,7 @@ struct Parser { } bool eatIndentation(){ + if(ignoreIndent > 0) return true; int spaces = eatSpaces(); // https://docs.python.org/3/reference/lexical_analysis.html#indentation if(spaces > indents.top()){ diff --git a/src/pocketpy.h b/src/pocketpy.h index 2f0fd74c..db79ab16 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -723,10 +723,11 @@ extern "C" { } __EXPORT - void pkpy_tvm_start_exec(ThreadedVM* vm, const char* source){ + bool pkpy_tvm_start_exec(ThreadedVM* vm, const char* source){ _Code code = compile(vm, source, "main.py"); - if(code == nullptr) return; - return vm->startExec(code); + if(code == nullptr) return false; + vm->startExec(code); + return true; } __EXPORT diff --git a/tests/6.py b/tests/6.py new file mode 100644 index 00000000..c0491fa4 --- /dev/null +++ b/tests/6.py @@ -0,0 +1,12 @@ +import ink + +print('Once upon a time...') + +index, val = ink.choice( + 'There were two choices.', + 'There were four lines of content.' +) + +print(f'You selected {index}') + +print('They lived happily ever after.') \ No newline at end of file