blueloveTH há 1 ano atrás
pai
commit
0806b1aee1
2 ficheiros alterados com 15 adições e 30 exclusões
  1. 12 7
      src/compiler/compiler.c
  2. 3 23
      src/public/exec.c

+ 12 - 7
src/compiler/compiler.c

@@ -1470,7 +1470,7 @@ static void Compiler__dtor(Compiler* self) {
     if(!match(expected))                                                                           \
         return SyntaxError(self,                                                                   \
                            "expected '%s', got '%s'",                                              \
-                           TokenSymbols[expected],                                              \
+                           TokenSymbols[expected],                                                 \
                            TokenSymbols[curr()->type]);
 #define consume_end_stmt()                                                                         \
     if(!match_end_stmt(self)) return SyntaxError(self, "expected statement end")
@@ -1641,13 +1641,18 @@ static Error* pop_context(Compiler* self) {
     FuncDecl* func = ctx()->func;
     if(func) {
         // check generator
-        c11__foreach(Bytecode, &func->code.codes, bc) {
-            if(bc->op == OP_YIELD_VALUE) {
+        Bytecode* codes = func->code.codes.data;
+        int codes_length = func->code.codes.count;
+
+        for(int i = 0; i < codes_length; i++) {
+            if(codes[i].op == OP_YIELD_VALUE) {
                 func->type = FuncType_GENERATOR;
-                c11__foreach(Bytecode, &func->code.codes, bc) {
-                    if(bc->op == OP_RETURN_VALUE && bc->arg == BC_NOARG) {
-                        return SyntaxError(self,
-                                           "'return' with argument inside generator function");
+                for(int j = 0; j < codes_length; j++) {
+                    if(codes[j].op == OP_RETURN_VALUE && codes[j].arg == BC_NOARG) {
+                        Error* err =
+                            SyntaxError(self, "'return' with argument inside generator function");
+                        err->lineno = c11__at(BytecodeEx, &func->code.codes_ex, j)->lineno;
+                        return err;
                     }
                 }
                 break;

+ 3 - 23
src/public/exec.c

@@ -8,41 +8,21 @@
 #include "pocketpy/interpreter/vm.h"
 #include "pocketpy/compiler/compiler.h"
 
-typedef struct {
-    const char* source;
-    const char* filename;
-    int mode;
-    int is_dynamic;
-} py_ExecKey;
-
-static int py_ExecKey__cmp(const py_ExecKey* a, const py_ExecKey* b) {
-    return memcmp(a, b, sizeof(py_ExecKey));
-}
-
-static void py_ExecKey__ctor(py_ExecKey* key, const char* source, const char* filename,
-                             enum py_CompileMode mode, bool is_dynamic) {
-    key->source = source;
-    key->filename = filename;
-    key->mode = mode;
-    key->is_dynamic = is_dynamic;
-}
-
 static bool _py_exec(const char* source,
                      const char* filename,
                      enum py_CompileMode mode,
                      py_Ref module,
                      bool is_dynamic) {
     VM* vm = pk_current_vm;
-    // py_ExecKey cache_key;
-    // py_ExecKey__ctor(&cache_key, source, filename, mode, is_dynamic);
     CodeObject co;
     SourceData_ src = SourceData__rcnew(source, filename, mode, is_dynamic);
     Error* err = pk_compile(src, &co);
     if(err) {
         py_exception(tp_SyntaxError, err->msg);
-        py_BaseException__stpush(&vm->curr_exception, src, err->lineno, NULL);
-
+        py_BaseException__stpush(&vm->curr_exception, err->src, err->lineno, NULL);
         PK_DECREF(src);
+        
+        PK_DECREF(err->src);
         free(err);
         return false;
     }