Ejemplo n.º 1
0
static KMETHOD Expression_Bracket(KonohaContext *kctx, KonohaStack *sfp)
{
	VAR_Expression(stmt, tokenList, beginIdx, operatorIdx, endIdx);
	KonohaClass *genericsClass = NULL;
	kNameSpace *ns = Stmt_ns(stmt);
	int nextIdx = SUGAR TokenUtils_ParseTypePattern(kctx, ns, tokenList, beginIdx, endIdx, &genericsClass);
	if(nextIdx != -1) {  // to avoid Func[T]
		KReturn(SUGAR kStmt_ParseOperatorExpr(kctx, stmt, tokenList->TokenItems[beginIdx]->resolvedSyntaxInfo, tokenList, beginIdx, beginIdx, endIdx));
	}
	kToken *currentToken = tokenList->TokenItems[operatorIdx];
	if(beginIdx == operatorIdx) {
		/* transform '[ Value1, Value2, ... ]' to '(Call Untyped new (Value1, Value2, ...))' */
		DBG_ASSERT(currentToken->resolvedSyntaxInfo->keyword == KW_BracketGroup);
		kExpr *arrayExpr = SUGAR new_UntypedCallStyleExpr(kctx, currentToken->resolvedSyntaxInfo, 2, currentToken, K_NULL);
		KReturn(SUGAR kStmt_AddExprParam(kctx, stmt, arrayExpr, currentToken->subTokenList, 0, kArray_size(currentToken->subTokenList), NULL));
	}
	else {
		kExpr *leftExpr = SUGAR kStmt_ParseExpr(kctx, stmt, tokenList, beginIdx, operatorIdx, NULL);
		if(leftExpr == K_NULLEXPR) {
			KReturn(leftExpr);
		}
		if(leftExpr->syn->keyword == SYM_("new")) {  // new int[100], new int[]();
			DBG_P("cur:%d, beg:%d, endIdx:%d", operatorIdx, beginIdx, endIdx);
			size_t subTokenSize = kArray_size(currentToken->subTokenList);
			if(subTokenSize == 0) {
				/* transform 'new Type0 [ ]' => (Call Type0 new) */
				kExpr_Setsyn(leftExpr, SYN_(ns, KW_ExprMethodCall));
			} else {
				/* transform 'new Type0 [ Type1 ] (...) => new 'Type0<Type1>' (...) */
				KonohaClass *classT0 = NULL;
				kArray *subTokenList = currentToken->subTokenList;
				int beginIdx = -1;
				if(kArray_size(subTokenList) > 0) {
					beginIdx = SUGAR TokenUtils_ParseTypePattern(kctx, ns, subTokenList, 0, kArray_size(subTokenList), &classT0);
				}
				beginIdx = (beginIdx == -1) ? 0 : beginIdx;
				kExpr_Setsyn(leftExpr, SYN_(ns, KW_ExprMethodCall));
				DBG_P("currentToken->subtoken:%d", kArray_size(subTokenList));
				leftExpr = SUGAR kStmt_AddExprParam(kctx, stmt, leftExpr, subTokenList, beginIdx, kArray_size(subTokenList), "[");
			}
		}
		else {
			/* transform 'Value0 [ Value1 ]=> (Call Value0 get (Value1)) */
			kTokenVar *tkN = /*G*/new_(TokenVar, 0, OnGcStack);
			tkN->resolvedSymbol= MN_toGETTER(0);
			tkN->uline = currentToken->uline;
			SugarSyntax *syn = SYN_(Stmt_ns(stmt), KW_ExprMethodCall);
			leftExpr  = SUGAR new_UntypedCallStyleExpr(kctx, syn, 2, tkN, leftExpr);
			leftExpr = SUGAR kStmt_AddExprParam(kctx, stmt, leftExpr, currentToken->subTokenList, 0, kArray_size(currentToken->subTokenList), "[");
		}
		KReturn(SUGAR kStmt_RightJoinExpr(kctx, stmt, leftExpr, tokenList, operatorIdx + 1, endIdx));
	}
}
Ejemplo n.º 2
0
static kToken* new_ParsedExprToken(KonohaContext *kctx, kNameSpace *ns, kExpr *expr)
{
	kTokenVar *ParsedExprToken = new_(TokenVar, 0, OnGcStack);
	ParsedExprToken->resolvedSyntaxInfo = SYN_(ns, KW_ExprPattern);
	KFieldSet(ParsedExprToken, ParsedExprToken->parsedExpr, expr);
	return (kToken *)ParsedExprToken;
}
Ejemplo n.º 3
0
//## Expr Stmt.newUntypedCallStyleExpr(Token token, Expr expr1);
static KMETHOD Stmt_newUntypedCallStyleExpr2(KonohaContext *kctx, KonohaStack *sfp)
{
	kStmt *stmt   = sfp[0].asStmt;
	kToken *token = sfp[1].asToken;
	kExpr *expr1  = sfp[2].asExpr;
	SugarSyntax *syn = SYN_(Stmt_ns(stmt), KW_ExprMethodCall);
	KReturn(SUGAR new_UntypedCallStyleExpr(kctx, syn, 2, token, expr1));
}
Ejemplo n.º 4
0
static kStmt* TypeDeclAndMakeSetter(KonohaContext *kctx, kStmt *stmt, kGamma *gma, ktype_t ty, kExpr *termExpr, kExpr *valueExpr, kObject *scr)
{
    kNameSpace *ns = Stmt_ns(stmt);
    kMethod *mtd = Object_newProtoSetterNULL(kctx, stmt, scr, ty, termExpr->termToken->resolvedSymbol);
    if(mtd != NULL) {
        kExpr *recvExpr =  new_ConstValueExpr(kctx, O_typeId(scr), scr);
        kExpr *setterExpr = SUGAR new_TypedCallExpr(kctx, stmt, gma, TY_void, mtd,  2, recvExpr, valueExpr);
        kStmt *newstmt = new_(Stmt, stmt->uline, OnGcStack);
        kStmt_setsyn(newstmt, SYN_(ns, KW_ExprPattern));
        KLIB kObject_setObject(kctx, newstmt, KW_ExprPattern, TY_Expr, setterExpr);
        return newstmt;
    }
    return NULL;
}
Ejemplo n.º 5
0
static void filterArrayList(KonohaContext *kctx, kNameSpace *ns, kArray *tokenList, int beginIdx, int endIdx)
{
	int i;
	for(i = beginIdx; i < endIdx; i++) {
		if(i + 1 == endIdx || tokenList->TokenItems[i+1]->resolvedSyntaxInfo->keyword == KW_COMMA) {
			kTokenVar *tk = tokenList->TokenVarItems[i];
			if(tk->resolvedSyntaxInfo->keyword != KW_SymbolPattern) {  // defined
				tk->resolvedSyntaxInfo = SYN_(ns, KW_TextPattern);  // switch to text pattern
			}
			i++;
		}
		while(i < endIdx) {
			kTokenVar *tk = tokenList->TokenVarItems[i];
			i++;
			if(tk->resolvedSyntaxInfo->keyword == KW_COMMA) break;
		}
	}
}
Ejemplo n.º 6
0
//## boolean NameSpace.definedBinaryOperator(symbol keyword);
static KMETHOD NameSpace_DefinedBinaryOperator(KonohaContext *kctx, KonohaStack *sfp)
{
	ksymbol_t keyword = (ksymbol_t)sfp[1].intValue;
	SugarSyntax* syn = SYN_(sfp[0].asNameSpace, keyword);
	KReturnUnboxValue(syn != NULL && (syn->precedence_op2 > 0));
}
Ejemplo n.º 7
0
//## boolean NameSpace.definedStatement(symbol keyword);
static KMETHOD NameSpace_DefinedStatement(KonohaContext *kctx, KonohaStack *sfp)
{
	ksymbol_t keyword = (ksymbol_t)sfp[1].intValue;
	SugarSyntax* syn = SYN_(sfp[0].asNameSpace, keyword);
	KReturnUnboxValue(SugarSyntax_hasSyntaxPatternList(syn) && SugarSyntax_hasSugarFunc(syn, SugarFunc_Statement));
}
Ejemplo n.º 8
0
//## boolean NameSpace.definedLiteral(symbol keyword);
static KMETHOD NameSpace_DefinedLiteral(KonohaContext *kctx, KonohaStack *sfp)
{
	ksymbol_t keyword = (ksymbol_t)sfp[1].intValue;
	SugarSyntax* syn = SYN_(sfp[0].asNameSpace, keyword);
	KReturnUnboxValue(SugarSyntax_hasSugarFunc(syn, SugarFunc_TokenFunc));
}
Ejemplo n.º 9
0
//## boolean NameSpace.definedSyntax(symbol keyword);
static KMETHOD NameSpace_DefinedSyntax(KonohaContext *kctx, KonohaStack *sfp)
{
	ksymbol_t keyword = (ksymbol_t)sfp[1].intValue;
	SugarSyntax* syn = SYN_(sfp[0].asNameSpace, keyword);
	KReturnUnboxValue(syn != NULL);
}
Ejemplo n.º 10
0
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_ct(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_ct(ns), UPCAST(ns)),
					param0, param1);
		} else {
			KReturnUnboxValue(false);
		}
	} else {
		KGrowingBuffer wb;
		KLIB KBuffer_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 KBuffer_Write(kctx, &wb, S_text(tk->text), S_size(tk->text));
				KLIB KBuffer_Write(kctx, &wb, ".", 1);
			}
		}
		kString *name = tokenList->TokenItems[i]->text;
		KLIB KBuffer_Write(kctx, &wb, S_text(name), S_size(name));

		kString *pkgname = KLIB new_kString(kctx, OnGcStack, KLIB KBuffer_Stringfy(kctx, &wb, 1), KBuffer_bytesize(&wb), 0);
		expr = CreateImportCall(kctx, syn, tkImport, ns, pkgname);
	}
	KLIB kObjectProto_SetObject(kctx, stmt, KW_ExprPattern, TY_Expr, expr);
	ret = SUGAR kStmt_TypeCheckByName(kctx, stmt, KW_ExprPattern, gma, CT_void, TypeCheckPolicy_ALLOWVOID);
	if(ret) {
		kStmt_typed(stmt, EXPR);
	}
	KReturnUnboxValue(ret);
}