Attach __doc__ to modules, default to None (but still set) if missing

This commit is contained in:
K. Lange 2021-01-07 11:16:12 +09:00
parent 11e6b79e49
commit ecb5f1e4ec
6 changed files with 23 additions and 3 deletions

View File

@ -2012,6 +2012,20 @@ KrkFunction * krk_compile(const char * src, int newScope, char * fileName) {
advance(); 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)) { while (!match(TOKEN_EOF)) {
declaration(); declaration();
if (check(TOKEN_EOL) || check(TOKEN_INDENTATION) || check(TOKEN_EOF)) { if (check(TOKEN_EOL) || check(TOKEN_INDENTATION) || check(TOKEN_EOF)) {

View File

@ -68,6 +68,7 @@ int main(int argc, char * argv[]) {
krk_defineNative(&vm.builtins->fields, "exit", exitFunc); krk_defineNative(&vm.builtins->fields, "exit", exitFunc);
krk_defineNative(&vm.builtins->fields, "paste", paste); krk_defineNative(&vm.builtins->fields, "paste", paste);
krk_startModule("<module>"); krk_startModule("<module>");
krk_attachNamedValue(&vm.module->fields,"__doc__", NONE_VAL());
/* Set ^D to send EOF */ /* Set ^D to send EOF */
rline_exit_string=""; rline_exit_string="";

View File

@ -0,0 +1,4 @@
#!/usr/bin/kuroko
"""This is a docstring."""
print("hello, here is my docstring:",__doc__)

View File

@ -0,0 +1 @@
hello, here is my docstring: This is a docstring.

4
vm.c
View File

@ -3449,14 +3449,14 @@ _finishException:
KrkInstance * krk_startModule(const char * name) { KrkInstance * krk_startModule(const char * name) {
KrkInstance * module = krk_newInstance(vm.objectClass); 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, "__builtins__", (KrkObj*)vm.builtins);
krk_attachNamedObject(&module->fields, "__name__", (KrkObj*)krk_copyString(name,strlen(name))); krk_attachNamedObject(&module->fields, "__name__", (KrkObj*)krk_copyString(name,strlen(name)));
return module; return module;
} }
KrkValue krk_interpret(const char * src, int newScope, char * fromName, char * fromFile) { 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); if (newScope) krk_startModule(fromName);
KrkFunction * function = krk_compile(src, 0, fromFile); KrkFunction * function = krk_compile(src, 0, fromFile);

2
vm.h
View File

@ -49,7 +49,7 @@ typedef struct {
size_t stackSize; size_t stackSize;
KrkValue * stack; KrkValue * stack;
KrkValue * stackTop; KrkValue * stackTop;
KrkObj * module; KrkInstance * module;
KrkTable strings; KrkTable strings;
KrkTable modules; KrkTable modules;
KrkUpvalue * openUpvalues; KrkUpvalue * openUpvalues;