static KMETHOD Expression_ExtendedTextLiteral(KonohaContext *kctx, KonohaStack *sfp) { VAR_Expression(expr, tokenList, beginIdx, opIdx, endIdx); kNameSpace *ns = kNode_ns(expr); kToken *tk = tokenList->TokenItems[opIdx]; INIT_GCSTACK(); kString *text = remove_escapes(kctx, tk); if(beginIdx != opIdx) { /* FIXME */ assert(0 && "FIXME"); KReturnUnboxValue(-1); } if(text == NULL) { /* text contain unsupported escape sequences */ RESET_GCSTACK(); KReturnUnboxValue(-1); } const char *str = kString_text(text); const char *end = NULL; const char *start = strstr(str, "${"); if(start == NULL) { /* text does not contain Interpolation expressions */ RESET_GCSTACK(); KReturnUnboxValue(beginIdx+1); } kSyntax *addSyntax = kSyntax_(ns, KSymbol_("+")); kTokenVar *opToken = tokenList->TokenVarItems[beginIdx]; opToken->symbol = KSymbol_("+"); opToken->text = KLIB new_kString(kctx, OnGcStack, "+", 1, 0); KFieldSet(opToken, opToken->resolvedSyntaxInfo, addSyntax); SUGAR kNode_Op(kctx, expr, opToken, 0); /* [before] "aaa${bbb}ccc" * [after] "" + "aaa" + bbb + "ccc" */ SUGAR kNode_AddNode(kctx, expr, new_ConstNode(kctx, ns, NULL, UPCAST(TS_EMPTY))); while(true) { start = strstr(str, "${"); if(start == NULL) break; if(start == strstr(str, "${}")) { str += 3; continue; } end = strchr(start, '}'); if(end == NULL) break; kNode *newexpr = ParseSource(kctx, ns, start+2, end-(start+2)); if(start - str > 0) { kNode *first = new_ConstNode(kctx, ns, NULL, UPCAST(KLIB new_kString(kctx, OnGcStack, str, (start - str), 0))); SUGAR kNode_AddNode(kctx, expr, first); } SUGAR kNode_AddNode(kctx, expr, newexpr); str = end + 1; } if((start == NULL) || (start != NULL && end == NULL)) { kNode *rest = new_ConstNode(kctx, ns, KClass_String, UPCAST(KLIB new_kString(kctx, OnGcStack, str, strlen(str), 0))); SUGAR kNode_AddNode(kctx, expr, rest); } /* (+ 1 2 3 4) => (+ (+ (+ 1 2) 3 ) 4) */ int i, size = kNode_GetNodeListSize(kctx, expr); assert(size > 2); kNode *leftNode = kNode_At(expr, 1), *rightNode; for(i = 2; i < size-1; i++) { kNode *node = KNewNode(ns); rightNode = kNode_At(expr, i); SUGAR kNode_Op(kctx, node, opToken, 2, leftNode, rightNode); leftNode = node; } rightNode = kNode_At(expr, i); KLIB kArray_Clear(kctx, expr->NodeList, 1); KLIB kArray_Add(kctx, expr->NodeList, leftNode); KLIB kArray_Add(kctx, expr->NodeList, rightNode); RESET_GCSTACK(); KReturnUnboxValue(beginIdx+1); }
static KMETHOD Statement_import(KonohaContext *kctx, KonohaStack *sfp) { int ret = false; VAR_Statement(stmt, gma); kTokenArray *tokenList = (kTokenArray *) kStmt_GetObjectNULL(kctx, stmt, KW_TokenPattern); if(tokenList == NULL) { KReturnUnboxValue(false); } kNameSpace *ns = Stmt_ns(stmt); SugarSyntaxVar *syn = (SugarSyntaxVar *) SYN_(ns, KW_ExprMethodCall); kExpr *expr; kTokenVar *tkImport = /*G*/new_(TokenVar, 0, OnGcStack); tkImport->resolvedSymbol = MN_("import"); if(IS_Token(tokenList)) { kTokenArray *list = ((kToken *) tokenList)->subTokenList; if(IS_String(list)) { /* case: import cstyle; */ kString *pkgname = (kString *) list; expr = CreateImportCall(kctx, syn, tkImport, ns, pkgname); } else if(kArray_size(list) == 1) { /* case : import("konoha.import"); */ kExpr *param0 = makeStringConstValue(kctx, list->TokenItems[0]->text); expr = SUGAR new_UntypedCallStyleExpr(kctx, syn, 3, tkImport, new_ConstValueExpr(kctx, O_typeId(ns), UPCAST(ns)), param0); } else if(kArray_size(list) == 2) { /* case : import("konoha.import", "import"); */ kExpr *param0 = makeStringConstValue(kctx, list->TokenItems[0]->text); kExpr *param1 = makeStringConstValue(kctx, list->TokenItems[1]->text); expr = SUGAR new_UntypedCallStyleExpr(kctx, syn, 4, tkImport, new_ConstValueExpr(kctx, O_typeId(ns), UPCAST(ns)), param0, param1); } else { KReturnUnboxValue(false); } } else { KGrowingBuffer wb; KLIB Kwb_Init(&(kctx->stack->cwb), &wb); /* case : import konoha.import */ ksymbol_t star = SYM_("*"); size_t i = 0; if(i + 2 < kArray_size(tokenList)) { for (; i < kArray_size(tokenList)-1; i+=2) { /* name . */ kToken *tk = tokenList->TokenItems[i+0]; if(i+2 < kArray_size(tokenList)) { kToken *startTk = tokenList->TokenItems[i+2]; if(startTk->resolvedSyntaxInfo->keyword == star) { break; } } KLIB Kwb_Write(kctx, &wb, S_text(tk->text), S_size(tk->text)); KLIB Kwb_Write(kctx, &wb, ".", 1); } } kString *name = tokenList->TokenItems[i]->text; KLIB Kwb_Write(kctx, &wb, S_text(name), S_size(name)); kString *pkgname = KLIB new_kString(kctx, OnGcStack, KLIB Kwb_top(kctx, &wb, 1), Kwb_bytesize(&wb), 0); expr = CreateImportCall(kctx, syn, tkImport, ns, pkgname); } KLIB kObject_setObject(kctx, stmt, KW_ExprPattern, TY_Expr, expr); ret = SUGAR kStmt_TypeCheckByName(kctx, stmt, KW_ExprPattern, gma, TY_void, TypeCheckPolicy_ALLOWVOID); if(ret) { kStmt_typed(stmt, EXPR); } KReturnUnboxValue(ret); }
//## boolean Console.inputUserApproval(String msg, String yes, String no, kbool_t defval) static KMETHOD Console_inputUserApproval(KonohaContext *kctx, KonohaStack *sfp) { KReturnUnboxValue(PLATAPI InputUserApproval(kctx, kString_text(sfp[1].asString), kString_text(sfp[2].asString), kString_text(sfp[3].asString), sfp[4].boolValue)); }
/* float > float */ static KMETHOD Float_opGT(KonohaContext *kctx, KonohaStack *sfp) { KReturnUnboxValue(sfp[0].floatValue > sfp[1].floatValue); }
static KMETHOD TokenFunc_ExtendedIntLiteral(KonohaContext *kctx, KonohaStack *sfp) { kTokenVar *tk = (kTokenVar *)sfp[1].asObject; const char *source = kString_text(sfp[2].asString); const char *start = source, *end; int c = *source++; /* * DIGIT = 0-9 * DIGITS = DIGIT | DIGIT DIGITS * HEX = 0-9a-fA-F * HEXS = HEX | HEX HEXS * BIN = 0 | 1 * BINS = BIN | BIN BINS * TAG = "0x" | "0b" * HEXINT = ("0x" | "0X") HEXS * INT = DIGITS | HEXS | BINS */ int base = 10; bool isFloat = false; char (*parseDigit)(char) = parseDecimalDigit; if(c == '0') { c = *source++; switch (c) { case 'b': base = 2; parseDigit = parseBinaryDigit; break; case 'x': base = 16; parseDigit = parseHexDigit; break; case '0':case '1':case '2':case '3': case '4':case '5':case '6':case '7': base = 8; parseDigit = parseOctalDigit; break; default: source--; break; } } for (; (c = *source) != 0; ++source) { if(c == '_') continue; if(parseDigit(c) == -1) break; } /* * DIGIT = 0-9 * DIGITS = DIGIT | DIGIT DIGITS * INT = DIGIT | DIGIT1-9 DIGITS * FLOAT = INT * | INT FRAC * | INT EXP * | INT FRAC EXP * FRAC = "." digits * EXP = E digits * E = 'e' | 'e+' | 'e-' | 'E' | 'E+' | 'E-' */ if(base != 10 && c != '.' && c != 'e' && c != 'E') { goto L_emit; } if(c == '.') { isFloat = true; source++; for (; (c = *source) != 0; ++source) { if(c == '_') continue; if(parseDecimalDigit(c) == -1) break; } } if(c == 'e' || c == 'E') { isFloat = true; c = *(++source); if(!('0' <= c && c <= '9') && !(c == '+' || c == '-')) { source--; goto L_emit; } if(c == '+' || c == '-') { c = *source++; } for (; (c = *source) != 0; ++source) { if(c == '_') continue; if(parseDecimalDigit(c) == -1) break; } } L_emit:; if(IS_NOTNULL(tk)) { /* skip unit */ for (; (c = *source) != 0; ++source) { if(c == '_') continue; if(!isalpha(c)) break; } end = source; KFieldSet(tk, tk->text, KLIB new_kString(kctx, OnField, start, end - start, StringPolicy_ASCII)); tk->tokenType = isFloat ? KSymbol_("$Float") : TokenType_Number; } KReturnUnboxValue(source - start); }
// Object.getTypeId() static KMETHOD Object_getTypeId(KonohaContext *kctx, KonohaStack *sfp) { KReturnUnboxValue(kObject_class(sfp[0].asObject)->typeId); }
//## @Native boolean XmlReader.isEmptyElement(); static KMETHOD XmlReader_isEmptyElement(KonohaContext *kctx, KonohaStack *sfp) { xmlTextReaderPtr reader = getRawXmlReader(sfp[0]); int ret = (reader != NULL) ? xmlTextReaderIsEmptyElement(reader) : 0; KReturnUnboxValue(ret); }
// int MecabNode.getLCAttr() static KMETHOD MecabNode_getLCAttr(KonohaContext *kctx, KonohaStack *sfp) { struct _kMecabNode *node = (struct _kMecabNode *)sfp[0].asObject; short ret = node->node->lcAttr; KReturnUnboxValue(ret); }
// int MecabNode.getCharType() static KMETHOD MecabNode_getCharType(KonohaContext *kctx, KonohaStack *sfp) { struct _kMecabNode *node = (struct _kMecabNode *)sfp[0].asObject; unsigned int ret = node->node->char_type; KReturnUnboxValue(ret); }
// Boolean Tagger.NBestInit(String input) static KMETHOD Tagger_NBestInit(KonohaContext *kctx, KonohaStack *sfp) { struct _kTagger *mecab = (struct _kTagger *)sfp[0].asObject; const char *input = S_text(sfp[1].asString); KReturnUnboxValue(mecab_nbest_Init(mecab->mecab, input)); }
// int MecabNode.getRLength() static KMETHOD MecabNode_getRLength(KonohaContext *kctx, KonohaStack *sfp) { struct _kMecabNode *node = (struct _kMecabNode *)sfp[0].asObject; unsigned int ret = node->node->rlength; KReturnUnboxValue(ret); }
//## int JSON.getSize(); static KMETHOD kJSONArray_getSize(KonohaContext *kctx, KonohaStack *sfp) { JSON obj = ((kJSON *)sfp[0].asObject)->json; KReturnUnboxValue(JSON_length(obj)); }
/* float to int */ static KMETHOD Float_toInt(KonohaContext *kctx, KonohaStack *sfp) { KReturnUnboxValue((kint_t)sfp[0].floatValue); }
/* float >= float */ static KMETHOD Int_opGTE(KonohaContext *kctx, KonohaStack *sfp) { KReturnUnboxValue(sfp[0].intValue >= sfp[1].floatValue); }
//## int System.sleep(int sec) static KMETHOD System_sleep(KonohaContext *kctx, KonohaStack *sfp) { unsigned int sec = sfp[1].intValue; unsigned int left = sleep(sec); KReturnUnboxValue(left); }
// Boolean MecabNode.isBest() static KMETHOD MecabNode_isBest(KonohaContext *kctx, KonohaStack *sfp) { struct _kMecabNode *node = (struct _kMecabNode *)sfp[0].asObject; unsigned int ret = node->node->isbest; KReturnUnboxValue(ret); }
static KMETHOD System_getegid(KonohaContext *kctx, KonohaStack *sfp) { KReturnUnboxValue(getegid()); }
// int MecabNode.cost() static KMETHOD MecabNode_cost(KonohaContext *kctx, KonohaStack *sfp) { struct _kMecabNode *node = (struct _kMecabNode *)sfp[0].asObject; long ret = node->node->cost; KReturnUnboxValue(ret); }
// boolean Object.instanceOf(Object o) static KMETHOD Object_instanceOf(KonohaContext *kctx, KonohaStack *sfp) { KClass *selfClass = kObject_class(sfp[0].asObject), *targetClass = kObject_class(sfp[1].asObject); KReturnUnboxValue(selfClass == targetClass || selfClass->isSubType(kctx, selfClass, targetClass)); }
//## boolean KonohaFactory.loadModule(String string); static KMETHOD KonohaFactory_loadModule(KonohaContext *kctx, KonohaStack *sfp) { kKonohaFactory *f = (kKonohaFactory *)sfp[0].asObject; KReturnUnboxValue(f->factory->LoadPlatformModule(f->factory, kString_text(sfp[1].asString), ReleaseModule)); }
//## @Native boolean XmlReader.hasAttributes(); static KMETHOD XmlReader_hasAttributes(KonohaContext *kctx, KonohaStack *sfp) { xmlTextReaderPtr reader = getRawXmlReader(sfp[0]); int ret = (reader != NULL) ? xmlTextReaderHasAttributes(reader) : 0; KReturnUnboxValue(ret); }
static KMETHOD KMethodFunc_UnboxFunctionGetter(KonohaContext *kctx, KonohaStack *sfp) { size_t delta = sfp[K_MTDIDX].calledMethod->delta; KReturnUnboxValue((sfp[0].asFunc)->env->fieldUnboxItems[delta]); }