static void DumpVisitor_visitConstExpr(KonohaContext *kctx, IRBuilder *self, kExpr *expr) { KGrowingBuffer wb; KLIB Kwb_init(&(kctx->stack->cwb), &wb); KonohaStack sfp[1]; kObject *obj = expr->objectConstValue; sfp[0].asObject = obj; O_ct(obj)->p(kctx, sfp, 0, &wb, 0); char *str = (char *) KLIB Kwb_top(kctx, &wb, 0); char buf[128]; snprintf(buf, 128, "CONST:%s:'%s'", CT_t(O_ct(obj)), str); emit_string(buf, "", "", DUMPER(self)->indent); KLIB Kwb_free(&wb); }
static void DumpVisitor_visitNConstExpr(KonohaContext *kctx, IRBuilder *self, kExpr *expr) { KGrowingBuffer wb; KLIB Kwb_init(&(kctx->stack->cwb), &wb); KonohaStack sfp[1]; unsigned long unboxVal = expr->unboxConstValue; KonohaClass *ct = CT_(expr->ty); sfp[0].unboxValue = unboxVal; ct->p(kctx, sfp, 0, &wb, 0); char *str = (char *) KLIB Kwb_top(kctx, &wb, 0); char buf[128]; snprintf(buf, 128, "NCONST:'%s'", str); emit_string(buf, "", "", DUMPER(self)->indent); KLIB Kwb_free(&wb); }
static void DumpVisitor_visitCallExpr(KonohaContext *kctx, IRBuilder *self, kExpr *expr) { KGrowingBuffer wb; KLIB Kwb_init(&(kctx->stack->cwb), &wb); kMethod *mtd = CallExpr_getMethod(expr); KLIB Kwb_printf(kctx, &wb, "CALL: '%s%s'", T_mn(mtd->mn)); DUMPER(self)->indent++; emit_string(KLIB Kwb_top(kctx, &wb, 1), "(", "", DUMPER(self)->indent); DUMPER(self)->indent++; unsigned i; for (i = 1; i < kArray_size(expr->cons); ++i) { handleExpr(kctx, self, kExpr_at(expr, i)); } DUMPER(self)->indent--; emit_string(")", "", "", DUMPER(self)->indent); DUMPER(self)->indent--; KLIB Kwb_free(&wb); }
//## @Const method String String.replace(RegExp searchvalue, String newvalue); static KMETHOD String_replace(KonohaContext *kctx, KonohaStack *sfp) { kString *s0 = sfp[0].asString; kRegExp *re = sfp[1].asRegExp; const char* fmttext = S_text(sfp[2].asString); size_t fmtlen = S_size(sfp[2].asString); kString *s = s0; if(IS_NOTNULL(re) && S_size(re->pattern) > 0) { KGrowingBuffer wb; KLIB Kwb_init(&(kctx->stack->cwb), &wb); const char *str = S_text(s0); // necessary const char *base = str; const char *eos = str + S_size(s0); // end of str kregmatch_t pmatch[KREGEXP_MATCHSIZE+1]; int isGlobalOption = RegExp_isGlobal(re); do { if(str >= eos) break; int res = pcre_regexec(kctx, re->reg, str, KREGEXP_MATCHSIZE, pmatch, re->eflags); if(res != 0) { // TODO //LOG_regex(kctx, sfp, res, re, str); break; } size_t len = pmatch[0].rm_eo; if(pmatch[0].rm_so > 0) { KLIB Kwb_write(kctx, &wb, str, pmatch[0].rm_so); } size_t matched = knh_regexp_matched(pmatch, KREGEXP_MATCHSIZE); if(len > 0) { Kwb_writeRegexFormat(kctx, &wb, fmttext, fmtlen, base, pmatch, matched); str += len; } else { if(str == base) { // 0-length match at head of string Kwb_writeRegexFormat(kctx, &wb, fmttext, fmtlen, base, pmatch, matched); } break; } } while(isGlobalOption); KLIB Kwb_write(kctx, &wb, str, strlen(str)); // write out remaining string s = Kwb_newString(kctx, OnStack, &wb); // close cwb KLIB Kwb_free(&wb); } KReturn(s); }
static KMETHOD PyObject_toString(KonohaContext *kctx, KonohaStack *sfp) { kPyObject *po = (kPyObject*)sfp[0].asObject; KUtilsWriteBuffer wb; // assert DBG_ASSERT(po->self != NULL); KLIB Kwb_init(&(kctx->stack->cwb), &wb); O_ct(sfp[0].asObject)->p(kctx, sfp, 0, &wb, 0); kString* s = KLIB new_kString(kctx, KLIB Kwb_top(kctx, &wb, 1), Kwb_bytesize(&wb), 0); KLIB Kwb_free(&wb); RETURN_(s); //if (PyString_Check(po->self)) { // //dec // t = PyString_AsString(po->self); // RETURN_(KLIB new_kString(kctx, t, strlen(t), 0)); //} //else if (PyUnicode_Check(po->self)) { // //dec // PyObject* s = PyUnicode_AsUTF8String(po->self); // // [TODO] there is no t's NULL check. Is it OK? // t = PyString_AsString(s); // RETURN_(KLIB new_kString(kctx, t, strlen(t), 0)); //} //else if (PyByteArray_Check(po->self)) { // //dec // t = PyByteArray_AsString(po->self); // RETURN_(KLIB new_kString(kctx, t, strlen(t), 0)); //} //else { // KUtilsWriteBuffer wb; // KLIB Kwb_init(&(kctx->stack->cwb), &wb); // O_ct(sfp[0].asObject)->p(kctx, sfp, 0, &wb, 0); // kString* s = KLIB new_kString(kctx, KLIB Kwb_top(kctx, &wb, 1), Kwb_bytesize(&wb), 0); // KLIB Kwb_free(&wb); // RETURN_(s); //} }