CRB_Value crb_nv_print_proc(CRB_Interpreter *interpreter, CRB_LocalEnvironment *env, int arg_count, CRB_Value *args) { CRB_Value value; CRB_Char *str; value.type = CRB_NULL_VALUE; check_argument_count(arg_count, 1); str = CRB_value_to_string(&args[0]); CRB_print_wcs(stdout, str); MEM_free(str); return value; }
CRB_Char * CRB_value_to_string(CRB_Value *value) { VString vstr; char buf[LINE_BUF_SIZE]; CRB_Char wc_buf[LINE_BUF_SIZE]; int i; crb_vstr_clear(&vstr); switch (value->type) { case CRB_BOOLEAN_VALUE: if (value->u.boolean_value) { CRB_mbstowcs("true", wc_buf); } else { CRB_mbstowcs("false", wc_buf); } crb_vstr_append_string(&vstr, wc_buf); break; case CRB_INT_VALUE: sprintf(buf, "%d", value->u.int_value); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_DOUBLE_VALUE: sprintf(buf, "%f", value->u.double_value); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_STRING_VALUE: crb_vstr_append_string(&vstr, value->u.object->u.string.string); break; case CRB_NATIVE_POINTER_VALUE: sprintf(buf, "(%s:%p)", value->u.native_pointer.info->name, value->u.native_pointer.pointer); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_NULL_VALUE: CRB_mbstowcs("null", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_ARRAY_VALUE: CRB_mbstowcs("(", wc_buf); crb_vstr_append_string(&vstr, wc_buf); for (i = 0; i < value->u.object->u.array.size; i++) { CRB_Char *new_str; if (i > 0) { CRB_mbstowcs(", ", wc_buf); crb_vstr_append_string(&vstr, wc_buf); } new_str = CRB_value_to_string(&value->u.object->u.array.array[i]); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); } CRB_mbstowcs(")", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; default: DBG_panic(("value->type..%d\n", value->type)); } return vstr.string; }
CRB_Char * CRB_value_to_string(CRB_Interpreter *inter, CRB_LocalEnvironment *env, int line_number, CRB_Value *value) { VString vstr; char buf[LINE_BUF_SIZE]; CRB_Char wc_buf[LINE_BUF_SIZE]; int i; crb_vstr_clear(&vstr); switch (value->type) { case CRB_BOOLEAN_VALUE: if (value->u.boolean_value) { CRB_mbstowcs("true", wc_buf); } else { CRB_mbstowcs("false", wc_buf); } crb_vstr_append_string(&vstr, wc_buf); break; case CRB_INT_VALUE: sprintf(buf, "%d", value->u.int_value); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_DOUBLE_VALUE: sprintf(buf, "%f", value->u.double_value); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_STRING_VALUE: crb_vstr_append_string(&vstr, value->u.object->u.string.string); break; case CRB_NATIVE_POINTER_VALUE: sprintf(buf, "%s(%p)", value->u.object->u.native_pointer.info->name, value->u.object->u.native_pointer.pointer); CRB_mbstowcs(buf, wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_NULL_VALUE: CRB_mbstowcs("null", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_ARRAY_VALUE: CRB_mbstowcs("(", wc_buf); crb_vstr_append_string(&vstr, wc_buf); for (i = 0; i < value->u.object->u.array.size; i++) { CRB_Char *new_str; if (i > 0) { CRB_mbstowcs(", ", wc_buf); crb_vstr_append_string(&vstr, wc_buf); } new_str = CRB_value_to_string(inter, env, line_number, &value->u.object->u.array.array[i]); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); } CRB_mbstowcs(")", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_ASSOC_VALUE: CRB_mbstowcs("(", wc_buf); crb_vstr_append_string(&vstr, wc_buf); for (i = 0; i < value->u.object->u.assoc.member_count; i++) { CRB_Char *new_str; if (i > 0) { CRB_mbstowcs(", ", wc_buf); crb_vstr_append_string(&vstr, wc_buf); } new_str = CRB_mbstowcs_alloc(inter, env, line_number, value->u.object->u.assoc.member[i].name); DBG_assert(new_str != NULL, ("new_str is null.\n")); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); CRB_mbstowcs("=>", wc_buf); crb_vstr_append_string(&vstr, wc_buf); new_str = CRB_value_to_string(inter, env, line_number, &value->u.object ->u.assoc.member[i].value); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); } CRB_mbstowcs(")", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_CLOSURE_VALUE: CRB_mbstowcs("closure(", wc_buf); crb_vstr_append_string(&vstr, wc_buf); if (value->u.closure.function->name == NULL) { CRB_mbstowcs("null", wc_buf); crb_vstr_append_string(&vstr, wc_buf); } else { CRB_Char *new_str; new_str = CRB_mbstowcs_alloc(inter, env, line_number, value->u.closure.function->name); DBG_assert(new_str != NULL, ("new_str is null.\n")); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); } CRB_mbstowcs(")", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_FAKE_METHOD_VALUE: CRB_mbstowcs("fake_method(", wc_buf); crb_vstr_append_string(&vstr, wc_buf); { CRB_Char *new_str; new_str = CRB_mbstowcs_alloc(inter, env, line_number, value->u.fake_method.method_name); DBG_assert(new_str != NULL, ("new_str is null.\n")); crb_vstr_append_string(&vstr, new_str); MEM_free(new_str); } CRB_mbstowcs(")", wc_buf); crb_vstr_append_string(&vstr, wc_buf); break; case CRB_SCOPE_CHAIN_VALUE: /* FALLTHRU*/ default: DBG_panic(("value->type..%d\n", value->type)); } return vstr.string; }