コード例 #1
0
ファイル: parser.c プロジェクト: kkuramitsu/minikonoha
void MODSUGAR_Init(KonohaContext *kctx, KonohaContextVar *ctx)
{
	KModuleSugar *mod = (KModuleSugar *)KCalloc_UNTRACE(sizeof(KModuleSugar), 1);
	mod->h.name     = "sugar";
	mod->h.allocSize = sizeof(KModuleSugar);
	mod->h.setupModuleContext    = SugarModule_Setup;
	KLIB KonohaRuntime_setModule(kctx, MOD_sugar, (KonohaModule *)mod, 0);

	KonohaLibVar* l = (KonohaLibVar *)ctx->klib;
	l->kNameSpace_GetClass       = kNameSpace_GetClass;
	l->kNameSpace_DefineClass    = kNameSpace_DefineClass;
	l->kNameSpace_LoadMethodData = kNameSpace_LoadMethodData;
	l->kNameSpace_SetConstData   = kNameSpace_SetConstData;
	l->kNameSpace_LoadConstData  = kNameSpace_LoadConstData;
	l->kNameSpace_GetGetterMethodNULL  = kNameSpace_GetGetterMethodNULL;
	l->kNameSpace_GetSetterMethodNULL  = kNameSpace_GetSetterMethodNULL;
	l->kNameSpace_GetCoercionMethodNULL = kNameSpace_GetCoercionMethodNULL;
	l->kNameSpace_GetMethodByParamSizeNULL  = kNameSpace_GetMethodByParamSizeNULL;
	l->kNameSpace_GetMethodBySignatureNULL  = kNameSpace_GetMethodBySignatureNULL;
	l->kMethod_DoLazyCompilation = kMethod_DoLazyCompilation;
//	l->kNameSpace_compileAllDefinedMethods  = kNameSpace_compileAllDefinedMethods;
	l->kNameSpace_FreeSugarExtension =  kNameSpace_FreeSugarExtension;
	l->Konoha_LoadScript = Konoha_LoadScript;
	l->Konoha_Eval       = Konoha_Eval;
	l->kMethod_GenCode   = kMethod_GenCode;
	l->kMethod_setFunc   = kMethod_setFunc;

	KDEFINE_CLASS defToken = {0};
	SETSTRUCTNAME(defToken, Token);
	defToken.init = kToken_Init;
	defToken.reftrace = kToken_Reftrace;
	defToken.p = kToken_p;
	
	KDEFINE_CLASS defExpr = {0};
	SETSTRUCTNAME(defExpr, Expr);
	defExpr.init = kExpr_Init;
	defExpr.reftrace = kExpr_Reftrace;
	defExpr.p        = kExpr_p;
	
	KDEFINE_CLASS defStmt = {0};
	SETSTRUCTNAME(defStmt, Stmt);
	defStmt.init = kStmt_Init;
	defStmt.reftrace = kStmt_Reftrace;
	defStmt.p        = kStmt_p;
	
	KDEFINE_CLASS defBlock = {0};
	SETSTRUCTNAME(defBlock, Block);
	defBlock.init = kBlock_Init;
	defBlock.reftrace = kBlock_Reftrace;
	
	KDEFINE_CLASS defGamma = {0};
	SETSTRUCTNAME(defGamma, Gamma);
	defGamma.init = Gamma_Init;

	mod->cToken =     KLIB KonohaClass_define(kctx, PackageId_sugar, NULL, &defToken, 0);
	mod->cExpr  =     KLIB KonohaClass_define(kctx, PackageId_sugar, NULL, &defExpr, 0);
	mod->cStmt  =     KLIB KonohaClass_define(kctx, PackageId_sugar, NULL, &defStmt, 0);
	mod->cBlock =     KLIB KonohaClass_define(kctx, PackageId_sugar, NULL, &defBlock, 0);
	mod->cGamma =     KLIB KonohaClass_define(kctx, PackageId_sugar, NULL, &defGamma, 0);
	mod->cTokenArray = CT_p0(kctx, CT_Array, mod->cToken->typeId);

	KLIB Knull(kctx, mod->cToken);
	KLIB Knull(kctx, mod->cExpr);
	kStmtVar *NullStmt = (kStmtVar *)KLIB Knull(kctx, mod->cStmt);
	KFieldSet(NullStmt, NullStmt->parentBlockNULL, (kBlock *)KLIB Knull(kctx, mod->cBlock));

	SugarModule_Setup(kctx, &mod->h, 0);

	KDEFINE_INT_CONST ClassData[] = {   // minikonoha defined class
		{"void", VirtualType_KonohaClass, (uintptr_t)CT_void},
		{"boolean", VirtualType_KonohaClass, (uintptr_t)CT_Boolean},
		{"int",    VirtualType_KonohaClass, (uintptr_t)CT_Int},
		{"String", VirtualType_KonohaClass, (uintptr_t)CT_String},
		{"Func",   VirtualType_KonohaClass, (uintptr_t)CT_Func},
		{"System", VirtualType_KonohaClass, (uintptr_t)CT_System},
		{NULL},
	};
	kNameSpace_LoadConstData(kctx, KNULL(NameSpace), KonohaConst_(ClassData), 0);

	mod->kNameSpace_SetTokenFunc       = kNameSpace_SetTokenFunc;
	mod->TokenSeq_Tokenize        = TokenSeq_Tokenize;
	mod->TokenSeq_ApplyMacro      = TokenSeq_ApplyMacro;
	mod->kNameSpace_SetMacroData       = kNameSpace_SetMacroData;
	mod->TokenSeq_Resolve        = TokenSeq_Resolve;
	mod->TokenSeq_Eval            = TokenSeq_Eval;
	mod->TokenUtils_ParseTypePattern     = TokenUtils_ParseTypePattern;
	mod->kToken_ToBraceGroup = kToken_ToBraceGroup;
	mod->kStmt_AddParsedObject      = kStmt_AddParsedObject;
	mod->kNameSpace_FindEndOfStatement = kNameSpace_FindEndOfStatement;
	mod->kStmt_ParseFlag            = kStmt_ParseFlag;
	mod->kStmt_GetToken             = kStmt_GetToken;
	mod->kStmt_GetBlock             = kStmt_GetBlock;
	mod->kStmt_GetExpr              = kStmt_GetExpr;
	mod->kStmt_GetText              = kStmt_GetText;
	mod->kExpr_SetConstValue        = kExpr_SetConstValue;
	mod->kExpr_SetUnboxConstValue   = kExpr_SetUnboxConstValue;
	mod->kExpr_SetVariable          = kExpr_SetVariable;
	mod->kStmt_TypeCheckExprAt        = kStmt_TypeCheckExprAt;
	mod->kStmt_TypeCheckByName        = kStmt_TypeCheckByName;
	mod->kBlock_TypeCheckAll          = kBlock_TypeCheckAll;
	mod->kStmtkExpr_TypeCheckCallParam = kStmtkExpr_TypeCheckCallParam;
	mod->new_TypedCallExpr          = new_TypedCallExpr;
	mod->kGamma_AddLocalVariable = kGamma_AddLocalVariable;
	mod->kStmt_DeclType             = kStmt_DeclType;
	mod->kStmt_TypeCheckVariableNULL  = kStmt_TypeCheckVariableNULL;

	mod->kNameSpace_DefineSyntax    = kNameSpace_DefineSyntax;
	mod->kNameSpace_GetSyntax       = kNameSpace_GetSyntax;
	mod->kArray_AddSyntaxRule       = kArray_AddSyntaxPattern;
//	mod->kNameSpace_SetSugarFunc    = kNameSpace_SetSugarFunc;
	mod->kNameSpace_AddSugarFunc    = kNameSpace_AddSugarFunc;
	mod->new_kBlock                 = new_kBlock;
	mod->new_kStmt                  = new_kStmt;
	mod->kBlock_InsertAfter         = kBlock_InsertAfter;
	mod->new_UntypedTermExpr        = new_UntypedTermExpr;
	mod->new_UntypedCallStyleExpr   = new_UntypedCallStyleExpr;
	mod->kStmt_ParseOperatorExpr    = kStmt_ParseOperatorExpr;
	mod->kStmt_ParseExpr            = kStmt_ParseExpr;
	mod->kStmt_AddExprParam         = kStmt_AddExprParam;
	mod->kStmt_RightJoinExpr        = kStmt_RightJoinExpr;
	mod->kToken_ToError        = kToken_ToError;
	mod->kStmt_Message2        = kStmt_Message2;

	mod->VisitStmt                  = VisitStmt;
	mod->VisitExpr                  = VisitExpr;
	mod->VisitBlock                 = VisitBlock;

#ifndef USE_SMALLBUILD
	mod->dumpToken      = dumpToken;
	mod->dumpTokenArray = dumpTokenArray;
	mod->dumpExpr       = dumpExpr;
#endif

	DefineDefaultSyntax(kctx, KNULL(NameSpace));
}
コード例 #2
0
ファイル: regexp_glue.c プロジェクト: shinpei/minikonoha
static kbool_t regexp_defineMethod(KonohaContext *kctx, kNameSpace *ns, KTraceInfo *trace)
{
	kregexpshare_t *base = (kregexpshare_t *)KCalloc_UNTRACE(sizeof(kregexpshare_t), 1);
	base->h.name     = "regexp";
	base->h.setup    = kregexpshare_setup;
	base->h.reftrace = kregexpshare_reftrace;
	base->h.free     = kregexpshare_free;
	KLIB KonohaRuntime_setModule(kctx, MOD_REGEXP, &base->h, trace);

	KDEFINE_CLASS RegExpDef = {
		STRUCTNAME(RegExp),
		.cflag = 0,
		.init = RegExp_init,
		.free = RegExp_free,
		.p    = RegExp_p,
	};
	base->cRegExp = KLIB kNameSpace_defineClass(kctx, ns, NULL, &RegExpDef, trace);

	ktype_t TY_StringArray0 = CT_StringArray0->typeId;
	KDEFINE_METHOD MethodData[] = {
		/*JS*/_Public|_Const|_Im, _F(RegExp_getglobal), TY_boolean, TY_RegExp, MN_("getglobal"), 0,
		/*JS*/_Public|_Const|_Im, _F(RegExp_getignoreCase), TY_boolean, TY_RegExp, MN_("getignoreCase"), 0,
		/*JS*/_Public|_Const|_Im, _F(RegExp_getmultiline), TY_boolean, TY_RegExp, MN_("getmultiline"), 0,
		/*JS*/_Public|_Const|_Im, _F(RegExp_getsource), TY_String, TY_RegExp, MN_("getsource"), 0,
		/*JS*/_Public|_Const|_Im, _F(RegExp_getlastIndex), TY_int, TY_RegExp, MN_("getlastIndex"), 0,
		/*JS*/_Public|_Im, _F(String_match), TY_StringArray0, TY_String, MN_("match"), 1, TY_RegExp, FN_("regexp"),
		/*JS*/_Public|_Const|_Im, _F(String_replace), TY_String, TY_String, MN_("replace"), 2, TY_RegExp, FN_("searchvalue"), TY_String, FN_("newvalue"),
		/*JS*/_Public|_Const|_Im, _F(String_search), TY_int, TY_String, MN_("search"), 1, TY_RegExp, FN_("searchvalue"),
		/*JS*/_Public|_Im, _F(String_split), TY_StringArray0, TY_String, MN_("split"), 1, TY_RegExp, FN_("separator"),
		/*JS*/_Public|_Im, _F(String_splitWithLimit), TY_StringArray0, TY_String, MN_("split"), 2, TY_RegExp, FN_("separator"), TY_int, FN_("limit"),
		/*JS*/_Public|_Const, _F(RegExp_new),     TY_RegExp,  TY_RegExp,  MN_("new"), 1, TY_String, FN_("pattern"),
		/*JS*/_Public|_Const, _F(RegExp_new2),     TY_RegExp,  TY_RegExp,  MN_("new"), 2, TY_String, FN_("pattern"), TY_String, FN_("option"),
		/*JS*/_Public|_Const, _F(RegExp_exec),    TY_StringArray0, TY_RegExp,  MN_("exec"), 1, TY_String, FN_("str"),
		/*JS*/_Public|_Const|_Im, _F(RegExp_test),    TY_boolean, TY_RegExp,  MN_("test"), 1, TY_String, FN_("str"),
		DEND,
	};
	KLIB kNameSpace_loadMethodData(kctx, ns, MethodData);
	return true;
}

static KMETHOD TokenFunc_JavaScriptRegExp(KonohaContext *kctx, KonohaStack *sfp)
{
	kTokenVar *tk = (kTokenVar *)sfp[1].asObject;
	int ch, prev = '/', pos = 1;
	const char *source = S_text(sfp[2].asString);
	if(source[pos] == '*' || source[pos] == '/') {
		KReturnUnboxValue(0);
	}
	/*FIXME: we need to care about context sensitive case*/
	//int tokenListize = kArray_size(tenv->tokenList);
	//if(tokenListize > 0) {
	//	kToken *tkPrev = tenv->tokenList->TokenItems[tokenListize - 1];
	//	if(tkPrev->unresolvedTokenType == TokenType_INT ||
	//		(tkPrev->topCharHint != '(' && tkPrev->unresolvedTokenType == TokenType_SYMBOL)) {
	//		KReturnUnboxValue(0);
	//	}
	//}
	while((ch = source[pos++]) != 0) {
		if(ch == '\n') {
			break;
		}
		if(ch == '/' && prev != '\\') {
			int pos0 = pos;
			while(isalpha(source[pos])) pos++;
			if(IS_NOTNULL(tk)) {
				kArray *a = (kArray *)KLIB new_kObject(kctx, OnField, CT_StringArray0, 2); // FIXME
				KFieldSet(tk, tk->subTokenList, a);
				KLIB new_kString(kctx, a, source + 1, (pos0-2), 0);
				KLIB new_kString(kctx, a, source + pos0, pos-pos0, 0);
				tk->unresolvedTokenType = SYM_("$RegExp");
			}
			KReturnUnboxValue(pos);
		}
		prev = ch;
	}
	if(IS_NOTNULL(tk)) {
		SUGAR kToken_printMessage(kctx, tk, ErrTag, "must close with %s", "/");
	}
	KReturnUnboxValue(pos-1);
}