Forráskód Böngészése

fix https://github.com/blueloveTH/pocketpy/issues/39

blueloveTH 2 éve
szülő
commit
897456c2f6
5 módosított fájl, 28 hozzáadás és 1 törlés
  1. 9 1
      python/builtins.py
  2. 4 0
      src/ceval.h
  3. 13 0
      src/compiler.h
  4. 1 0
      src/opcodes.h
  5. 1 0
      src/str.h

+ 9 - 1
python/builtins.py

@@ -203,4 +203,12 @@ class staticmethod:
         return self.f(*args)
     
 def type@__repr__(self):
-    return "<class '" + self.__name__ + "'>"
+    return "<class '" + self.__name__ + "'>"
+
+def help(obj):
+    if hasattr(obj, '__func__'):
+        obj = obj.__func__
+    if hasattr(obj, '__doc__'):
+        print(obj.__doc__)
+    else:
+        print("No docstring found")

+ 4 - 0
src/ceval.h

@@ -543,6 +543,10 @@ __NEXT_STEP:;
         _error(StrName(byte.arg), msg);
     } DISPATCH();
     TARGET(RE_RAISE) _raise(); DISPATCH();
+    /*****************************************/
+    TARGET(SETUP_DOCSTRING)
+        TOP()->attr().set(__doc__, co_consts[byte.arg]);
+        DISPATCH();
 #if !PK_ENABLE_COMPUTED_GOTO
 #if DEBUG_EXTRA_CHECK
     default: throw std::runtime_error(fmt(OP_NAMES[byte.op], " is not implemented"));

+ 13 - 0
src/compiler.h

@@ -922,7 +922,20 @@ __SUBSCR_END:
         }
         compile_block_body();
         pop_context();
+
+        PyObject* docstring = nullptr;
+        if(decl->code->codes.size()>=2 && decl->code->codes[0].op == OP_LOAD_CONST && decl->code->codes[1].op == OP_POP_TOP){
+            PyObject* c = decl->code->consts[decl->code->codes[0].arg];
+            if(is_type(c, vm->tp_str)){
+                decl->code->codes[0].op = OP_NO_OP;
+                decl->code->codes[1].op = OP_NO_OP;
+                docstring = c;
+            }
+        }
         ctx()->emit(OP_LOAD_FUNCTION, ctx()->add_func_decl(decl), prev().line);
+        if(docstring != nullptr){
+            ctx()->emit(OP_SETUP_DOCSTRING, ctx()->add_const(docstring), prev().line);
+        }
         // add decorators
         for(auto it=decorators.rbegin(); it!=decorators.rend(); ++it){
             (*it)->emit(ctx());

+ 1 - 0
src/opcodes.h

@@ -110,4 +110,5 @@ OPCODE(EXCEPTION_MATCH)
 OPCODE(RAISE)
 OPCODE(RE_RAISE)
 /**************************/
+OPCODE(SETUP_DOCSTRING)
 #endif

+ 1 - 0
src/str.h

@@ -394,6 +394,7 @@ const StrName __set__ = StrName::get("__set__");
 const StrName __getattr__ = StrName::get("__getattr__");
 const StrName __setattr__ = StrName::get("__setattr__");
 const StrName __call__ = StrName::get("__call__");
+const StrName __doc__ = StrName::get("__doc__");
 
 const StrName m_eval = StrName::get("eval");
 const StrName m_self = StrName::get("self");