Attach __doc__ to modules, default to None (but still set) if missing
This commit is contained in:
parent
11e6b79e49
commit
ecb5f1e4ec
14
compiler.c
14
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)) {
|
||||
|
1
kuroko.c
1
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("<module>");
|
||||
krk_attachNamedValue(&vm.module->fields,"__doc__", NONE_VAL());
|
||||
|
||||
/* Set ^D to send EOF */
|
||||
rline_exit_string="";
|
||||
|
4
test/testModuleDocstring.krk
Normal file
4
test/testModuleDocstring.krk
Normal file
@ -0,0 +1,4 @@
|
||||
#!/usr/bin/kuroko
|
||||
"""This is a docstring."""
|
||||
|
||||
print("hello, here is my docstring:",__doc__)
|
1
test/testModuleDocstring.krk.expect
Normal file
1
test/testModuleDocstring.krk.expect
Normal file
@ -0,0 +1 @@
|
||||
hello, here is my docstring: This is a docstring.
|
4
vm.c
4
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);
|
||||
|
Loading…
Reference in New Issue
Block a user