static kBlock* kStmt_ParseClassBlockNULL(KonohaContext *kctx, kStmt *stmt, kToken *tokenClassName) { kBlock *bk = NULL; kToken *blockToken = (kToken *)kStmt_GetObject(kctx, stmt, KW_BlockPattern, NULL); if(blockToken != NULL && blockToken->resolvedSyntaxInfo->keyword == KW_BlockPattern) { const char *cname = S_text(tokenClassName->text); TokenSeq range = {Stmt_ns(stmt), GetSugarContext(kctx)->preparedTokenList}; TokenSeq_Push(kctx, range); SUGAR TokenSeq_Tokenize(kctx, &range, S_text(blockToken->text), blockToken->uline); { TokenSeq sourceRange = {range.ns, range.tokenList, range.endIdx}; kToken *prevToken = blockToken; int i; for(i = range.beginIdx; i < range.endIdx; i++) { kToken *tk = range.tokenList->TokenItems[i]; if(tk->hintChar == '(' && prevToken->unresolvedTokenType == TokenType_SYMBOL && strcmp(cname, S_text(prevToken->text)) == 0) { kTokenVar *newToken = new_(TokenVar, TokenType_SYMBOL, sourceRange.tokenList); KFieldSet(newToken, newToken->text, SYM_s(MN_new)); } KLIB kArray_Add(kctx, sourceRange.tokenList, tk); prevToken = tk; } TokenSeq_end(kctx, (&sourceRange)); bk = SUGAR new_kBlock(kctx, stmt/*parent*/, NULL, &sourceRange); KLIB kObject_setObject(kctx, stmt, KW_BlockPattern, TY_Block, bk); } TokenSeq_Pop(kctx, range); } return bk; }
//## Block Stmt.newBlock(String macro); static KMETHOD Stmt_newBlock(KonohaContext *kctx, KonohaStack *sfp) { kStmt *stmt = sfp[0].asStmt; kString *macro = sfp[1].asString; TokenSeq source = {Stmt_ns(stmt), GetSugarContext(kctx)->preparedTokenList/*TODO: set appropriate tokenList to TokenSeq*/}; TokenSeq_Push(kctx, source); SUGAR TokenSeq_Tokenize(kctx, &source, S_text(macro), 0); kBlock *bk = SUGAR new_kBlock(kctx, stmt, NULL, &source); TokenSeq_Pop(kctx, source); KReturn(bk); }
static kstatus_t kNameSpace_Eval(KonohaContext *kctx, kNameSpace *ns, const char *script, kfileline_t uline, KTraceInfo *trace) { kstatus_t result; kmodsugar->h.setupModuleContext(kctx, (KonohaModule *)kmodsugar, 0/*lazy*/); INIT_GCSTACK(); { TokenSeq tokens = {ns, GetSugarContext(kctx)->preparedTokenList}; TokenSeq_Push(kctx, tokens); TokenSeq_Tokenize(kctx, &tokens, script, uline); result = TokenSeq_Eval(kctx, &tokens, trace); TokenSeq_Pop(kctx, tokens); } RESET_GCSTACK(); return result; }
static kBlock *new_MacroBlock(KonohaContext *kctx, kStmt *stmt, kToken *IteratorTypeToken, kToken *IteratorExprToken, kToken *TypeToken, kToken *VariableToken) { kNameSpace *ns = Stmt_ns(stmt); TokenSeq source = {ns, GetSugarContext(kctx)->preparedTokenList}; TokenSeq_Push(kctx, source); /* FIXME(imasahiro) * we need to implement template as Block * "T _ = E; if(_.hasNext()) { N = _.next(); }" * ^^^^^^^^^^^^^^^^^ */ SUGAR TokenSeq_Tokenize(kctx, &source, "T _ = E; if(_.hasNext()) N = _.next();", 0); MacroSet macroSet[4] = {{0, NULL, 0, 0}}; MacroSet_setTokenAt(kctx, macroSet, 0, source.tokenList, "T", IteratorTypeToken, NULL); MacroSet_setTokenAt(kctx, macroSet, 1, source.tokenList, "E", IteratorExprToken, NULL); if(TypeToken == NULL) { MacroSet_setTokenAt(kctx, macroSet, 2, source.tokenList, "N", VariableToken, NULL); } else { MacroSet_setTokenAt(kctx, macroSet, 2, source.tokenList, "N", TypeToken, VariableToken, NULL); } kBlock *bk = SUGAR new_kBlock(kctx, stmt, macroSet, &source); TokenSeq_Pop(kctx, source); return bk; }