From 63a00d0faf6f1da9ddab96a60904f5455817b003 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 16 Feb 2023 01:45:58 +0800 Subject: [PATCH] add inline block --- src/compiler.h | 7 ++++++- tests/_inline_blocks.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/_inline_blocks.py diff --git a/src/compiler.h b/src/compiler.h index dd842a6f..1d6e7ecf 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -690,6 +690,10 @@ __LISTCOMP: void compile_block_body(CompilerAction action=nullptr) { if(action == nullptr) action = &Compiler::compile_stmt; consume(TK(":")); + if(peek()!=TK("@eol") && peek()!=TK("@eof")){ + (this->*action)(); // inline block + return; + } if(!match_newlines(mode()==REPL_MODE)){ SyntaxError("expected a new line after ':'"); } @@ -1009,7 +1013,8 @@ __LISTCOMP: consume(TK("@id")); func->name = parser->prev.str(); - if (match(TK("(")) && !match(TK(")"))) { + consume(TK("(")); + if (!match(TK(")"))) { _compile_f_args(func, true); consume(TK(")")); } diff --git a/tests/_inline_blocks.py b/tests/_inline_blocks.py new file mode 100644 index 00000000..4407f129 --- /dev/null +++ b/tests/_inline_blocks.py @@ -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 \ No newline at end of file