Explorar el Código

add inline block

blueloveTH hace 3 años
padre
commit
63a00d0faf
Se han modificado 2 ficheros con 26 adiciones y 1 borrados
  1. 6 1
      src/compiler.h
  2. 20 0
      tests/_inline_blocks.py

+ 6 - 1
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(")"));
         }

+ 20 - 0
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