From ecb5f1e4ec584caa90eeb4499325a865bac7f8f6 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Thu, 7 Jan 2021 11:16:12 +0900 Subject: [PATCH] Attach __doc__ to modules, default to None (but still set) if missing --- compiler.c | 14 ++++++++++++++ kuroko.c | 1 + test/testModuleDocstring.krk | 4 ++++ test/testModuleDocstring.krk.expect | 1 + vm.c | 4 ++-- vm.h | 2 +- 6 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 test/testModuleDocstring.krk create mode 100644 test/testModuleDocstring.krk.expect diff --git a/compiler.c b/compiler.c index a0884bd..a2e0b3f 100644 --- a/compiler.c +++ b/compiler.c @@ -2012,6 +2012,20 @@ KrkFunction * krk_compile(const char * src, int newScope, char * fileName) { advance(); + if (vm.module) { + KrkValue doc; + if (!krk_tableGet(&vm.module->fields, OBJECT_VAL(krk_copyString("__doc__", 7)), &doc)) { + if (match(TOKEN_STRING) || match(TOKEN_BIG_STRING)) { + string(parser.previous.type == TOKEN_BIG_STRING); + krk_attachNamedObject(&vm.module->fields, "__doc__", + (KrkObj*)AS_STRING(currentChunk()->constants.values[currentChunk()->constants.count-1])); + consume(TOKEN_EOL,"Garbage after docstring"); + } else { + krk_attachNamedValue(&vm.module->fields, "__doc__", NONE_VAL()); + } + } + } + while (!match(TOKEN_EOF)) { declaration(); if (check(TOKEN_EOL) || check(TOKEN_INDENTATION) || check(TOKEN_EOF)) { diff --git a/kuroko.c b/kuroko.c index 802cd1e..f1ae0a6 100644 --- a/kuroko.c +++ b/kuroko.c @@ -68,6 +68,7 @@ int main(int argc, char * argv[]) { krk_defineNative(&vm.builtins->fields, "exit", exitFunc); krk_defineNative(&vm.builtins->fields, "paste", paste); krk_startModule(""); + krk_attachNamedValue(&vm.module->fields,"__doc__", NONE_VAL()); /* Set ^D to send EOF */ rline_exit_string=""; diff --git a/test/testModuleDocstring.krk b/test/testModuleDocstring.krk new file mode 100644 index 0000000..4ecf103 --- /dev/null +++ b/test/testModuleDocstring.krk @@ -0,0 +1,4 @@ +#!/usr/bin/kuroko +"""This is a docstring.""" + +print("hello, here is my docstring:",__doc__) diff --git a/test/testModuleDocstring.krk.expect b/test/testModuleDocstring.krk.expect new file mode 100644 index 0000000..9735e70 --- /dev/null +++ b/test/testModuleDocstring.krk.expect @@ -0,0 +1 @@ +hello, here is my docstring: This is a docstring. diff --git a/vm.c b/vm.c index 57cb1f2..5b51aff 100644 --- a/vm.c +++ b/vm.c @@ -3449,14 +3449,14 @@ _finishException: KrkInstance * krk_startModule(const char * name) { KrkInstance * module = krk_newInstance(vm.objectClass); - vm.module = (KrkObj*)module; + vm.module = module; krk_attachNamedObject(&module->fields, "__builtins__", (KrkObj*)vm.builtins); krk_attachNamedObject(&module->fields, "__name__", (KrkObj*)krk_copyString(name,strlen(name))); return module; } KrkValue krk_interpret(const char * src, int newScope, char * fromName, char * fromFile) { - KrkObj * enclosing = vm.module; + KrkInstance * enclosing = vm.module; if (newScope) krk_startModule(fromName); KrkFunction * function = krk_compile(src, 0, fromFile); diff --git a/vm.h b/vm.h index 0586701..86af0f2 100644 --- a/vm.h +++ b/vm.h @@ -49,7 +49,7 @@ typedef struct { size_t stackSize; KrkValue * stack; KrkValue * stackTop; - KrkObj * module; + KrkInstance * module; KrkTable strings; KrkTable modules; KrkUpvalue * openUpvalues;