Exemple #1
0
int main(int argc, const char *argv[])
{
    int i, argc_ = argc;
    const char *argv_[argc_];
    const char *fname = parse_option(&argc_, argv, argv_);
    if (fname == NULL) {
        fprintf(stderr, "%s [--emit-llvm/--emit-js] file\n", argv[0]);
        return 1;
    }
    konoha_t konoha = konoha_open();
    CTX ctx = konoha;
    kString *s = new_T(fname);
    knh_DictMap_set(ctx, ctx->share->props, new_T("script.name"), s);

    kArray *a = new_Array(ctx, CLASS_String, argc_);
    for(i = 2; i < argc_; i++) {
        knh_Array_add(ctx, a, new_String2(ctx, CLASS_String, argv_[i],
                    knh_strlen(argv_[i]), SPOL_TEXT|SPOL_POOLALWAYS));
    }
    knh_DictMap_set(ctx, ctx->share->props, new_T("script.argv"), a);

    kbytes_t t = knh_bytes_nsname(S_tobytes(s));
    knh_Script_setNSName(ctx, ctx->script, new_S(t.text, t.len));
    kbytes_t pkgname = STEXT("konoha.compiler");
    knh_loadPackage(ctx, pkgname);

    load_codegenerator(ctx);
    if (!compiler_run_main) {
        knh_setCompileMode(ctx, 1);
    }
    knh_startScript(ctx, (const char*)fname);

    /* CompilerAPI->dump() */
    kMethod *mtd = load_method(ctx,
            O_cid(ctx->share->konoha_compiler), STEXT("dump"));
    BEGIN_LOCAL(ctx, lsfp, K_CALLDELTA+1); {
        KNH_SETv(ctx, lsfp[K_CALLDELTA].o, ctx->share->konoha_compiler);
        KNH_SCALL(ctx, lsfp, 0, mtd, 0);
    } END_LOCAL(ctx, lsfp);
    if (compiler_run_main) {
        knh_stack_clear(ctx, ctx->stack);
        knh_runMain(ctx, argc_, argv_);
    }
    konoha_close(konoha);
    return 0;
}
Exemple #2
0
int konoha_main(konoha_t konoha, int argc, const char **argv)
{
	CTX ctx = (CTX)konoha;
	int i, ret = 0, n = knh_parseopt(ctx, argc, argv);
	knh_linkDynamicReadline(ctx);
	knh_linkDynamicRegex(ctx);
	knh_linkDynamicIconv(ctx);
	for (i = 0; konoha_modules[i].init != NULL; ++i) {
		konoha_modules[i].init(argc, n, argv);
	}
	argc = argc - n;
	argv = argv + n;
	if(isActorMode) {
		char portstr[6] = {0};
		knh_snprintf(portstr, sizeof(portstr), "%d", port);
		const char *argv_actor[3] = {"actsrv", portstr, NULL};
		argc = 2;
		argv = argv_actor;
	}
	knh_parsearg(ctx, argc, argv);
	if(argc == 0) {
		ret = konoha_shell(ctx, NULL);
	}
	else if(isMPIMode) {
		kMPI_argv0 = argv[0];
		knh_loadPackage(ctx, STEXT("konoha.mpi"));
		knh_eval(ctx, "using konoha.mpi.*; int main(String[] args) { new TaskScript().exec(MPI.vload()); MPI.vmainloop(); return 0 }", 1, NULL);
		ret = knh_runMain(ctx, argc, argv);
	}
	else {
		if(knh_startScript(ctx, argv[0]) == K_CONTINUE && !knh_isCompileOnly(ctx)) {
			ret = knh_runMain(ctx, argc, argv);
			if(isInteractiveMode) {
				konoha_shell(ctx, NULL);
			}
		}
	}
	for (i = 0; konoha_modules[i].exit != NULL; ++i) {
		konoha_modules[i].exit();
	}
	return ret;
}
Exemple #3
0
static void load_codegenerator(CTX ctx)
{
    KONOHA_BEGIN(ctx);
    CompilerAPI_disable(ctx);
    if (codegenerator_file[compile_mode]) {
        kbytes_t t = new_bytes((char*)codegenerator_file[compile_mode]);
        knh_loadPackage(ctx, t);
        CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf);
        kString *s = (kString *) knh_DictMap_getNULL(ctx, ctx->share->props, STEXT("konoha.package.path"));
        CWB_clear(cwb, 0);
        knh_buff_addpath(ctx, cwb->ba, cwb->pos, 0, S_tobytes(s));
        knh_buff_addpath(ctx, cwb->ba, cwb->pos, 1, STEXT("konoha.compiler"));
        knh_buff_addpath(ctx, cwb->ba, cwb->pos, 1, STEXT("compiler"));
        knh_buff_addpath(ctx, cwb->ba, cwb->pos, 0, STEXT(K_OSDLLEXT));
        void *p = knh_dlopen(ctx, CWB_totext(ctx, cwb));
        typedef void (*knh_Fpkgcomplete)(CTX);
        knh_Fpkgcomplete pkgcomplete = (knh_Fpkgcomplete) knh_dlsym(ctx, p, "reset_compiler_api", NULL, 1);
        if (pkgcomplete) pkgcomplete(ctx);
        CWB_close(ctx, cwb);
    }
    CompilerAPI_enable(ctx);
    KONOHA_END(ctx);
}
Exemple #4
0
void knh_loadScriptPackageList(CTX ctx, const char *pkglist)
{
	if(pkglist != NULL) {
		kbytes_t t = {{pkglist}, knh_strlen(pkglist)};
		char buf[256];
		size_t i = 0;
		int isExists = 0;
		L_NEXT:;
		isExists = 0;
		while(i < t.len + 1) {
			char *c = buf;
			while(i < t.len + 1) {
				int ch = t.ubuf[i];
				i++;
				if(ch ==':' || ch == ';' || ch == ',' || ch == 0) {
					*c = 0;
					DBG_P("loading '%s'", buf);
					if(!knh_loadPackage(ctx, B(buf)) && isExists == 0) {
						KNH_LOG("package not found: package=%s", buf+8);
					}
					goto L_NEXT;
				}
				else {
					if(ch == '?') {
						isExists = 1; continue;
					}
					*c = ch;
				}
				c++;
				if(!(c - buf < 256)) {
					KNH_LOG("too long name %s", pkglist);
					return ;
				}
			}
		}
	}
}