blueloveTH 3 лет назад
Родитель
Сommit
ff4776f756
3 измененных файлов с 17 добавлено и 12 удалено
  1. 4 2
      src/compiler.h
  2. 7 8
      src/pocketpy.h
  3. 6 2
      src/vm.h

+ 4 - 2
src/compiler.h

@@ -321,8 +321,10 @@ public:
     void exprLambda() {
         _Func func;
         func.name = "<lambda>";
-        __compileFunctionArgs(func);
-        consume(TK(":"));
+        if(!match(TK(":"))){
+            __compileFunctionArgs(func);
+            consume(TK(":"));
+        }
         func.code = std::make_shared<CodeObject>(parser->src, func.name);
         this->codes.push(func.code);
         EXPR_TUPLE();

+ 7 - 8
src/pocketpy.h

@@ -497,11 +497,6 @@ void __initializeBuiltinFunctions(VM* _vm) {
     });
 }
 
-void __runCodeBuiltins(VM* vm, const char* src){
-    _Code code = compile(vm, src, "builtins.py");
-    if(code != nullptr) vm->exec(code, vm->builtins);
-}
-
 #include "builtins.h"
 
 #ifdef _WIN32
@@ -535,11 +530,15 @@ extern "C" {
     VM* createVM(PrintFn _stdout, PrintFn _stderr){
         VM* vm = new VM();
         __initializeBuiltinFunctions(vm);
-        __runCodeBuiltins(vm, __BUILTINS_CODE);
-        __addModuleSys(vm);
-        __addModuleTime(vm);
         vm->_stdout = _stdout;
         vm->_stderr = _stderr;
+
+        _Code code = compile(vm, __BUILTINS_CODE, "builtins.py");
+        if(code == nullptr) exit(1);
+        vm->_exec(code, vm->builtins);
+
+        __addModuleSys(vm);
+        __addModuleTime(vm);
         return vm;
     }
 

+ 6 - 2
src/vm.h

@@ -411,8 +411,7 @@ public:
     PyVar exec(const _Code& code, PyVar _module=nullptr){
         if(_module == nullptr) _module = _main;
         try {
-            PyVarDict locals;
-            return _exec(code, _module, locals);
+            return _exec(code, _module);
         } catch (const std::exception& e) {
             if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
                 _stderr(e.what());
@@ -425,6 +424,11 @@ public:
         }
     }
 
+    PyVar _exec(const _Code& code, PyVar _module){
+        PyVarDict locals;
+        return _exec(code, _module, locals);
+    }
+
     PyVar _exec(const _Code& code, PyVar _module, PyVarDict& locals){
         if(code == nullptr) UNREACHABLE();
         Frame* frame = new Frame(