Add kuroko.argv

This commit is contained in:
K Lange 2021-01-08 19:05:11 +09:00
parent 2f7503cd0d
commit 2bd0d93596
5 changed files with 16 additions and 1 deletions

View File

@ -305,6 +305,15 @@ int main(int argc, char * argv[]) {
krk_initVM(flags);
/* Attach kuroko.argv - argv[0] will be set to an empty string for the repl */
if (argc == optind) krk_push(OBJECT_VAL(krk_copyString("",0)));
for (int arg = optind; arg < argc; ++arg) {
krk_push(OBJECT_VAL(krk_copyString(argv[arg],strlen(argv[arg]))));
}
KrkValue argList = krk_list_of(argc - optind + (optind == argc), &vm.stackTop[-(argc - optind + (optind == argc))]);
krk_attachNamedValue(&vm.system->fields, "argv", argList);
for (int arg = optind; arg < argc + (optind == argc); ++arg) krk_pop();
/* Bind interrupt signal */
signal(SIGINT, handleSigint);

4
test/testArgv.krk Normal file
View File

@ -0,0 +1,4 @@
import kuroko
print(kuroko.argv)
# ['test/testArgv.krk']

1
test/testArgv.krk.expect Normal file
View File

@ -0,0 +1 @@
['test/testArgv.krk']

2
vm.c
View File

@ -509,7 +509,7 @@ KrkValue krk_runNext(void) {
* Exposed method called to produce lists from [expr,...] sequences in managed code.
* Presented in the global namespace as listOf(...)
*/
static KrkValue krk_list_of(int argc, KrkValue argv[]) {
KrkValue krk_list_of(int argc, KrkValue argv[]) {
KrkValue Class;
krk_tableGet(&vm.builtins->fields,OBJECT_VAL(S("list")), &Class);
KrkInstance * outList = krk_newInstance(AS_CLASS(Class));

1
vm.h
View File

@ -139,6 +139,7 @@ extern KrkValue krk_typeOf(int argc, KrkValue argv[]);
extern int krk_bindMethod(KrkClass * _class, KrkString * name);
extern int krk_callValue(KrkValue callee, int argCount, int extra);
extern KrkValue krk_list_of(int argc, KrkValue argv[]);
extern KrkValue krk_dict_of(int argc, KrkValue argv[]);
extern KrkValue krk_callSimple(KrkValue value, int argCount, int isMethod);
extern void krk_finalizeClass(KrkClass * _class);