Beispiel #1
0
//## void File.print(String line);
static KMETHOD File_print(KonohaContext *kctx, KonohaStack *sfp)
{
	kFile   *file = sfp[0].asFile;
	kString *line = sfp[1].asString;
	KMakeTrace(trace, sfp);
	if(file->writerIconv == ICONV_NULL || kString_Is(ASCII, line)) {
		TRACE_fwrite(kctx, file, kString_text(line), kString_size(line), trace);
	}
	else {
		KBuffer wb;
		KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
		KLIB KBuffer_iconv(kctx, &wb, file->writerIconv, kString_text(line), kString_size(line), trace);
		TRACE_fwrite(kctx, file, KLIB KBuffer_text(kctx, &wb, NonZero), KBuffer_bytesize(&wb), trace);
		KLIB KBuffer_Free(&wb);
	}
}
Beispiel #2
0
//## Bytes String.toBytes();
static KMETHOD String_toBytes(KonohaContext *kctx, KonohaStack *sfp)
{
	kString* thisString = sfp[0].asString;
	size_t size = kString_size(thisString);
	if(PLATAPI I18NModule.isSystemCharsetUTF8(kctx)) {
		KReturn(new_kBytes(kctx, OnStack, KGetReturnType(sfp), kString_text(thisString), size));
	}
	else {
		KMakeTrace(trace, sfp);
		KBuffer wb;
		KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
		KBuffer_convertCharset(kctx, &wb, I18NAPI systemCharset, "UTF-8", kString_text(thisString), size, trace);
		KReturnWith(
			new_kBytes(kctx, OnStack, KGetReturnType(sfp), KLIB KBuffer_text(kctx, &wb, NonZero), KBuffer_bytesize(&wb)),
			KLIB KBuffer_Free(&wb)
		);
	}
}
static kNode *ParseSource(KonohaContext *kctx, kNameSpace *ns, const char *script, size_t len)
{
	KBuffer wb;
	KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
	KLIB KBuffer_Write(kctx, &wb, "(", 1);
	KLIB KBuffer_Write(kctx, &wb, script, len);
	KLIB KBuffer_Write(kctx, &wb, ")", 1);

	KTokenSeq tokens = {ns, KGetParserContext(kctx)->preparedTokenList};
	KTokenSeq_Push(kctx, tokens);
	const char *buf = KLIB KBuffer_text(kctx, &wb, EnsureZero);
	SUGAR Tokenize(kctx, ns, buf, 0, 0, tokens.tokenList);
	KTokenSeq_End(kctx, tokens);

	KTokenSeq step2 = {ns, tokens.tokenList, kArray_size(tokens.tokenList)};
	SUGAR Preprocess(kctx, ns, RangeTokenSeq(tokens), NULL, step2.tokenList);
	KTokenSeq_End(kctx, step2);
	kNode *newexpr = SUGAR ParseNewNode(kctx, ns, step2.tokenList, &step2.beginIdx, step2.endIdx, 0, NULL);
	KTokenSeq_Pop(kctx, tokens);
	KLIB KBuffer_Free(&wb);
	return newexpr;
}
Beispiel #4
0
static KMETHOD PyObject_toString(KonohaContext *kctx, KonohaStack *sfp)
{
	kPyObject *po = (kPyObject *)sfp[0].asObject;
	KBuffer wb;
	// assert
	DBG_ASSERT(po->self != NULL);
	KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
	kObject_class(sfp[0].asObject)->p(kctx, sfp, 0, &wb);
	kString *s = KLIB new_kString(kctx, OnStack, KLIB KBuffer_text(kctx, &wb, 1), KBuffer_bytesize(&wb), 0);
	KLIB KBuffer_Free(&wb);
	KReturn(s);
	//if(PyString_Check(po->self)) {
	//	//dec
	//	t = PyString_AsString(po->self);
	//	KReturn(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);
	//	KReturn(KLIB new_kString(kctx, t, strlen(t), 0));
	//}
	//else if(PyByteArray_Check(po->self)) {
	//	//dec
	//	t = PyByteArray_AsString(po->self);
	//	KReturn(KLIB new_kString(kctx, t, strlen(t), 0));
	//}
	//else {
	//	KBuffer wb;
	//	KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
	//	kObject_class(sfp[0].asObject)->p(kctx, sfp, 0, &wb, 0);
	//	kString *s = KLIB new_kString(kctx, KLIB KBuffer_text(kctx, &wb, 1), KBuffer_bytesize(&wb), 0);
	//	KLIB KBuffer_Free(&wb);
	//	KReturn(s);
	//}
}