DVM_ExecutableList * DKC_compile(DKC_Compiler *compiler, FILE *fp, char *path) { extern FILE *yyin; DVM_ExecutableList *list; DVM_Executable *exe; DBG_assert(st_compiler_list == NULL, ("st_compiler_list != NULL(%p)", st_compiler_list)); set_path_to_compiler(compiler, path); compiler->input_mode = FILE_INPUT_MODE; yyin = fp; list = MEM_malloc(sizeof(DVM_ExecutableList)); list->list = NULL; exe = do_compile(compiler, list, NULL, DVM_FALSE); exe->path = MEM_strdup(path); list->top_level = exe; /* dvm_disassemble(exe);*/ dispose_compiler_list(); dkc_reset_string_literal_buffer(); return list; }
void MVM_add_native_function(MVM_VirtualMachine *mvm, char *func_name, MVM_NativeFunctionProc *proc, int arg_count) { mvm->function = MEM_realloc(mvm->function, sizeof(Function) * (mvm->function_count + 1)); mvm->function[mvm->function_count].name = MEM_strdup(func_name); mvm->function[mvm->function_count].kind = NATIVE_FUNCTION; mvm->function[mvm->function_count].u.native_f.proc = proc; mvm->function[mvm->function_count].u.native_f.arg_count = arg_count; mvm->function_count++; }
/* 文字列srcをヒープ領域へ割り当てる. 文字列はヒープ領域へディープコピーされる. */ LL1LL_Object* alloc_string(char *src, LL1LL_Boolean is_literal) { /* 領域確保 */ LL1LL_Object *new_entry = (LL1LL_Object *)MEM_malloc(sizeof(LL1LL_Object)); /* オブジェクトの種類と, マークの初期化 */ new_entry->type = STRING_OBJECT; new_entry->marked = LL1LL_FALSE; /* 内容を埋める */ new_entry->u.str.string_value = MEM_strdup(src); new_entry->u.str.is_literal = is_literal; /* ヒープ管理リストへ追加 */ addHeapEntry(new_entry); return new_entry; }
static void add_global_variable(DKC_Compiler *compiler, DVM_Executable *exe) { DeclarationList *dl; int i; int var_count = 0; for (dl = compiler->declaration_list; dl; dl = dl->next) { var_count++; } exe->global_variable_count = var_count; exe->global_variable = MEM_malloc(sizeof(DVM_Variable) * var_count); for (dl = compiler->declaration_list, i = 0; dl; dl = dl->next, i++) { exe->global_variable[i].name = MEM_strdup(dl->declaration->name); exe->global_variable[i].type = copy_type_specifier(dl->declaration->type); } }
static DVM_LocalVariable * copy_local_variables(FunctionDefinition *fd, int param_count) { int i; int local_variable_count; DVM_LocalVariable *dest; local_variable_count = fd->local_variable_count - param_count; dest = MEM_malloc(sizeof(DVM_LocalVariable) * local_variable_count); for (i = 0; i < local_variable_count; i++) { dest[i].name = MEM_strdup(fd->local_variable[i+param_count]->name); dest[i].type = copy_type_specifier(fd->local_variable[i+param_count]->type); } return dest; }
static DVM_LocalVariable * copy_parameter_list(ParameterList *src, int *param_count_p) { int param_count = 0; ParameterList *param; DVM_LocalVariable *dest; int i; for (param = src; param; param = param->next) { param_count++; } *param_count_p = param_count; dest = MEM_malloc(sizeof(DVM_LocalVariable) * param_count); for (param = src, i = 0; param; param = param->next, i++) { dest[i].name = MEM_strdup(param->name); dest[i].type = copy_type_specifier(param->type); } return dest; }
Statement * Asm_create_statement(char *label, Bytecode *code, Constant *const_opt) { NameSpace *name_space; Statement *ret; name_space = &Asm_get_current_compiler()->name_space[Asm_get_current_compiler()->current_name_space_index]; ret = ASM_malloc(sizeof(Statement)); ret->label = label; ret->bytecode = code; ret->constant = const_opt; ret->line_number = get_current_line_number(); if (code && code->name == NULL && code->next && !strcmp(code->next->name, Loopr_CR_Info[LCR_FUNCTION].assembly_name)) { if (name_space->function_definition) { name_space->function_definition = MEM_realloc(name_space->function_definition, sizeof(FunctionDefinition) * (name_space->function_count + 1)); } else { name_space->function_definition = MEM_malloc(sizeof(FunctionDefinition) * (name_space->function_count + 1)); } name_space->function_definition[name_space->function_count].is_void = LPR_False; while (const_opt && const_opt->type == CONST_KEYWORD) { switch (const_opt->u.keyword_value) { case ASM_VOID: name_space->function_definition[name_space->function_count].is_void = LPR_True; const_opt = const_opt->next; break; default: DBG_panic(("line %d: Unknown keyword %d\n", const_opt->line_number, const_opt->u.keyword_value)); break; } } name_space->function_definition[name_space->function_count].name = MEM_strdup(const_opt->u.string_value); name_space->function_count++; } return ret; }
static void add_functions(MVM_VirtualMachine *mvm, MVM_Executable *executable) { int src_idx; int dest_idx; int func_count = 0; for (src_idx = 0; src_idx < executable->function_count; src_idx++) { if (executable->function[src_idx].is_implemented) { func_count++; for (dest_idx = 0; dest_idx < mvm->function_count; dest_idx++) { if (!strcmp(mvm->function[dest_idx].name, executable->function[src_idx].name)) { /* mvm_error(NULL, NULL, NO_LINE_NUMBER_PC, FUNCTION_MULTIPLE_DEFINE_ERR, STRING_MESSAGE_ARGUMENT, "name", mvm->function[dest_idx].name, MESSAGE_ARGUMENT_END); */ } } } } mvm->function = MEM_realloc(mvm->function, sizeof(Function) * (mvm->function_count + func_count)); for (src_idx = 0, dest_idx = mvm->function_count; src_idx < executable->function_count; src_idx++) { if (!executable->function[src_idx].is_implemented) continue; mvm->function[dest_idx].name = MEM_strdup(executable->function[src_idx].name); mvm->function[dest_idx].u.minic_f.executable = executable; mvm->function[dest_idx].u.minic_f.index = src_idx; dest_idx++; } mvm->function_count += func_count; }
static DVM_Executable * do_compile(DKC_Compiler *compiler, DVM_ExecutableList *list, char *path, DVM_Boolean is_required) { extern FILE *yyin; extern int yyparse(void); RequireList *req_pos; DKC_Compiler *req_comp; DVM_Executable *exe; DVM_Executable *req_exe; char found_path[FILENAME_MAX]; DKC_Compiler *compiler_backup; SourceInput source_input; compiler_backup = dkc_get_current_compiler(); dkc_set_current_compiler(compiler); if (yyparse()) { fprintf(stderr, "Error ! Error ! Error !\n"); exit(1); } for (req_pos = compiler->require_list; req_pos; req_pos = req_pos->next) { req_comp = search_compiler(st_compiler_list, req_pos->package_name); if (req_comp) { compiler->required_list = add_compiler_to_list(compiler->required_list, req_comp); continue; } req_comp = DKC_create_compiler(); /* BUGBUG req_comp references parent compiler's MEM_storage */ req_comp->package_name = req_pos->package_name; req_comp->source_suffix = DKH_SOURCE; compiler->required_list = add_compiler_to_list(compiler->required_list, req_comp); st_compiler_list = add_compiler_to_list(st_compiler_list, req_comp); get_require_input(req_pos, found_path, &source_input); set_path_to_compiler(req_comp, found_path); req_comp->input_mode = source_input.input_mode; if (source_input.input_mode == FILE_INPUT_MODE) { yyin = source_input.u.file.fp; } else { dkc_set_source_string(source_input.u.string.lines); } req_exe = do_compile(req_comp, list, found_path, DVM_TRUE); } dkc_fix_tree(compiler); exe = dkc_generate(compiler); if (path) { exe->path = MEM_strdup(path); } else { exe->path = NULL; } /* dvm_disassemble(exe);*/ exe->is_required = is_required; if (!add_exe_to_list(exe, list)) { dvm_dispose_executable(exe); } dkc_set_current_compiler(compiler_backup); return exe; }
CRB_Value crb_eval_binary_expression(CRB_Interpret *inter, LocalEnvironment *env, ExpressionType operator, Expression *left, Expression *right) { CRB_Value left_val; CRB_Value right_val; CRB_Value result; left_val = eval_expression(inter, env, left); right_val = eval_expression(inter, env, right); if(left_val.type == CRB_INT_VALUE && right_val.type == CRB_INT_VALUE){ eval_binary_int(inter, operator, left_val.u.int_value, right_val.u.int_value, &result, letf->line_number); } else if(left_val.type == CRB_DOUBLE_VALUE && right_val.type == CRB_DOUBLE_VALUE){ eval_binary_double(inter, operator, left_val.u.double_value, right_val.u.double_value, &result, left->line_number); } else if(left_val.type == CRB_INT_VALUE && right_val.type == CRB_DOUBLE_VALUE){ right_val.u.double_value = right_val.u.int_value; eval_binary_double(inter, operator, left_val.u.double_value, right_val.u.double_value, &result, left->line_number); } else if(left_val.type == CRB_DOUBLE_VALUE && right_val.type == CRB_INT_VALUE){ right_val.u.double_value = right_val.u.int_value; eval_binary_double(inter, operator, left_val.u.double_value, right_val.u.double_value, &result, left->line_number); } else if(left_val.type == CRB_BOOLEAN_VALUE && right_val.type == CRB_BOOLEAN_VALUE){ result.type = CRB_BOOLEAN_VALUE; result.u.boolean_value = eval_binary_boolean(inter, operator, left_val.u.doublean_value, right_val.u.double_value, left->line_number); }else if(left_val.type == CRB_STRING_VALUE && operator == ADD_EXPRESSION){ char buf[LINE_BUF_SIZE]; CRB_String *right_str; if(right_val.type == CRB_INT_VALUE){ sprintf(buf, "%d", right_val.u.int_value); right_str = crb_create_crowbar_string(inter, MEM_strdup(buf)); } else if(right_val.tpye == CRB_DOUBLE_VALUE){ sprintf(buf, "%f", right_val.u.double_value); right_str = crb_create_crowbar_string(inter, MEM_strdup(buf)); } else if(right_val.type == CRB_BOOLEAN_VALUE){ if(right_val.u.boolean_value){ right_str = crb_create_crowbar_str(inter, MEM_strdup("true")); } else { right_str = crb_create_crowbar_string(inter, MEM_strdup("false")); } }else if(right_val.type == CRB_STRING_VALUE){ right_str = right_val.u.tring_value; } else if(right_val.type == CRB_NATIVE_POINTER_VALUE){ sprintf(buf, "%s:%p)", right_val.u.native_pointer.info->name, right_val.u.native_pointer.pointer); right_str = crb_create_crowbar_string(inter, MEM_strdup(buf)); } else if(right_val.type == CRB_NULL_VALUE){ right_str = crb_create_crowbar_string(inter, MEM_strdup("null")); } result.type = CRB_STRING_VALUE; result.u.string_value = chain_string(inter, left_val.u.string_value, right_str); } else if(left_val.type == CRB_STRING_VALUE && right_val.type == CRB_STRING_VALUE){ resultr.type = CRB_BOOLEAN_VALUE; result.u.boolean_value = eval_compare_string(operator, &left_val, &right_val, left->line_number); } else if(left_val.type == CRB_NULL_VALUE || right_val.type == CRB_NULL_VALUE){ result.type = CRB_BOOLEAN_VALUE; result.u.boolean_value = eval_binary_null(inter, operator,&left_val, &right_val, left->line_number); } else { char *op_str = crb_get_operator_string(operator); crb_runtime_error(left->line_number, BAD_OPERAND_TYPE_ERR, STRING_MESSAGE_ARGUMENT, "operator", op_str, MESSAGE_ARGUMENT_END); } return result; }