Ejemplo n.º 1
0
void
YogVM_boot(YogEnv* env, YogVM* vm)
{
    YogHandleScope scope;
    YogHandleScope_OPEN(env, &scope);

    setup_encodings1(env, vm);
    setup_symbol_tables(env, vm);
    setup_basic_classes(env, vm);
    YogHandle* builtins = YogHandle_REGISTER(env, alloc_skelton_pkg(env, vm));
    setup_classes(env, vm, HDL2VAL(builtins));
    YogPackage_init(env, HDL2VAL(builtins), TYPE_PACKAGE);
    set_main_thread_class(env, vm);
    YogException_define_classes(env, HDL2VAL(builtins));
    YogObject_boot(env, vm->cObject, HDL2VAL(builtins));
    YogClass_boot(env, vm->cClass, HDL2VAL(builtins));

    vm->pkgs = YogTable_create_symbol_table(env);

    vm->encodings = YogDict_new(env);
    setup_encodings2(env, vm);
    setup_default_encoding(env, vm);

    vm->finish_code = YogCompiler_compile_finish_code(env);

    vm->id_star = YogVM_intern(env, vm, "*");
    vm->id_star2 = YogVM_intern(env, vm, "**");
    vm->id_amp = YogVM_intern(env, vm, "&");

    setup_builtins(env, vm, builtins);
    YogArray_eval_builtin_script(env, vm->cArray);
    YogBinary_eval_builtin_script(env, vm->cBinary);
    YogDatetime_eval_builtin_script(env, vm->cDatetime);
    YogDict_eval_builtin_script(env, vm->cDict);
    YogFixnum_eval_builtin_script(env, vm->cFixnum);
    YogObject_eval_builtin_script(env, vm->cObject);
    YogSet_eval_builtin_script(env, vm->cSet);
    YogString_eval_builtin_script(env, vm->cString);
    YogPath_eval_builtin_script(env, vm->cPath);
    YogSymbol_eval_builtin_script(env, vm->cSymbol);
    YogCallable_eval_builtin_script(env, vm->mCallable);
    YogEnumerable_eval_builtin_script(env, vm->mEnumerable);

    YogHandleScope_close(env);
}
Ejemplo n.º 2
0
char *compilestyle(const char *source, int len, int *len_out) {
#if defined(YYDEBUG) && YYDEBUG
	extern int yydebug;
	yydebug = 1;
#endif

	CompilerState _state={0,}, *state = &_state;
	StrReader reader = {source, 0, len};
	YYLTYPE yylloc = {0,};

	yyscan_t scanner;

	if (!errorprint) {
		set_errorprint((void*) stderr, (errorprint_t)fprintf);
	}

	
	state->ma_r = memarena_new(4096);
	yylex_init(&scanner);
	yyset_in((FILE*)&reader, scanner);

	printf("compiling AST tree. . .\n");

	if (yyparse(state, &yylloc, scanner)==0) {
		astnode_print(state->result, 0, stdout, fprintf);
	} else { //error
		errorprint(errorarg, "Parse error!\n");
		return NULL;
	}

	state->index_globals =  memarena_malloc(state->ma_r, sizeof(*state->globals));
	state->global_indices = memarena_malloc(state->ma_r, sizeof(*state->globals));
	state->globals = memarena_malloc(state->ma_r, sizeof(*state->globals));
	state->scope = memarena_malloc(state->ma_r, sizeof(*state->scope));
	state->builtin_funcs = memarena_malloc(state->ma_r, sizeof(*state->builtin_funcs));
	state->funcs = memarena_malloc(state->ma_r, sizeof(*state->funcs));
	
	strhash_init(state->index_globals);
	strhash_init(state->global_indices);
	strhash_init(state->globals);
	strhash_init(state->scope);
	strhash_init(state->builtin_funcs);
	strhash_init(state->funcs);

	state->error = cerror;

	//set longjump
	state->jmpbuf = memarena_malloc(state->ma_r, sizeof(jmp_buf));
	setjmp(*state->jmpbuf);

	char *buf = NULL;
	int len2 = 0;

	//see if an exception happened
	if (state->haderror) {
		errorprint(errorarg, "compilation failed; aborting\n");
	} else {
		setup_builtins(state);
		transform_ast(state);
		buf = emit_bytecode(state, &len2);
	}

	strhash_release(state->index_globals);
	strhash_release(state->global_indices);
	strhash_release(state->globals);
	strhash_release(state->scope);
	strhash_release(state->builtin_funcs);
	strhash_release(state->funcs);

	yylex_destroy(scanner);
	memarena_free(state->ma_r);

	*len_out = len2;
	return buf;
}