add inline block

This commit is contained in:
blueloveTH 2023-02-16 01:45:58 +08:00
parent 051560279d
commit 63a00d0faf
2 changed files with 26 additions and 1 deletions

View File

@ -690,6 +690,10 @@ __LISTCOMP:
void compile_block_body(CompilerAction action=nullptr) { void compile_block_body(CompilerAction action=nullptr) {
if(action == nullptr) action = &Compiler::compile_stmt; if(action == nullptr) action = &Compiler::compile_stmt;
consume(TK(":")); consume(TK(":"));
if(peek()!=TK("@eol") && peek()!=TK("@eof")){
(this->*action)(); // inline block
return;
}
if(!match_newlines(mode()==REPL_MODE)){ if(!match_newlines(mode()==REPL_MODE)){
SyntaxError("expected a new line after ':'"); SyntaxError("expected a new line after ':'");
} }
@ -1009,7 +1013,8 @@ __LISTCOMP:
consume(TK("@id")); consume(TK("@id"));
func->name = parser->prev.str(); func->name = parser->prev.str();
if (match(TK("(")) && !match(TK(")"))) { consume(TK("("));
if (!match(TK(")"))) {
_compile_f_args(func, true); _compile_f_args(func, true);
consume(TK(")")); consume(TK(")"));
} }

20
tests/_inline_blocks.py Normal file
View File

@ -0,0 +1,20 @@
class A: pass
class B: pass
a = A()
assert type(a) is A
x = 0
if x==0: x=1
assert x==1
def f(x, y): return x+y
assert f(1,2)==3
c = 1
if c==0: x=1
elif c==1: x=2
else: x=3
assert x==2
def f1(x): return x+1
assert f1(1)==2