static ekU32 fileIterate(struct ekContext *E, ekU32 argCount) { ekValue *thisValue; ekValue *ret = ekValueNullPtr; ekValue *chompValue = NULL; ekFile *file; ekValue *closure; if(!ekContextGetArgs(E, argCount, "*F|?", &thisValue, &chompValue)) { return ekContextArgsFailure(E, argCount, "file.iterate([optional bool] chompNewline)"); } file = (ekFile *)thisValue->ptrVal; if(chompValue) { chompValue = ekValueToBool(E, chompValue); } else { chompValue = ekValueCreateInt(E, 0); } closure = ekValueCreateCFunction(E, fileIterator); closure->closureVars = ekMapCreate(E, EMKT_STRING); ekMapGetS2P(E, closure->closureVars, "file") = thisValue; ekMapGetS2P(E, closure->closureVars, "chomp") = chompValue; ekContextReturn(E, closure); }
static ekU32 fileCreateIterator(struct ekContext *E, ekU32 argCount) { ekValue *thisValue = NULL; ekValue *closure; if(!ekContextGetArgs(E, argCount, "F", &thisValue)) { return ekContextArgsFailure(E, argCount, "file iterator missing argument"); } closure = ekValueCreateCFunction(E, fileIterator); closure->closureVars = ekMapCreate(E, EMKT_STRING); ekMapGetS2P(E, closure->closureVars, "file") = thisValue; ekMapGetS2P(E, closure->closureVars, "chomp") = ekValueCreateInt(E, 0); ekContextReturn(E, closure); }
// feeds one readline() at a time static ekU32 fileIterator(struct ekContext *E, ekU32 argCount) { ekFrame *frame = ekArrayTop(E, &E->frames); ekValue *fileVal; ekValue *chompVal; ekFile *file; ekAssert(frame->closure && frame->closure->closureVars); fileVal = ekMapGetS2P(E, frame->closure->closureVars, "file"); chompVal = ekMapGetS2P(E, frame->closure->closureVars, "chomp"); ekAssert(argCount == 0); ekContextPopValues(E, argCount); file = (ekFile *)fileVal->ptrVal; switchState(E, file, EFS_READ); ekContextReturn(E, readLineInternal(E, file, chompVal->intVal)); }
void ekValueTypeAddIntrinsic(struct ekContext *E, ekValueType *type, const char *name, ekCFunction func) { if(!type->intrinsics) { type->intrinsics = ekMapCreate(E, EMKT_STRING); } ekMapGetS2P(E, type->intrinsics, name) = func; }
static ekU32 regexGMatch(struct ekContext * E, ekU32 argCount) { ekRegex * regex = regexCreateMatchFromArgs(E, argCount, "re.gmatch([string] subject, [string] pattern, [optional string] options)"); if (regex) { ekValue * closure = ekValueCreateCFunction(E, regexGMatchIterator); ekValue * regexValue = ekValueCreate(E, ekValueTypeId(E, 'R')); regexValue->ptrVal = regex; closure->closureVars = ekMapCreate(E, EMKT_STRING); ekMapGetS2P(E, closure->closureVars, "regex") = regexValue; ekContextReturn(E, closure); } ekContextReturn(E, ekValueNullPtr); }
static ekU32 regexGMatchIterator(struct ekContext * E, ekU32 argCount) { ekFrame * frame = ekArrayTop(E, &E->frames); ekValue * regexValue; ekRegex * regex; ekValue * results; ekAssert(frame->closure && frame->closure->closureVars); regexValue = ekMapGetS2P(E, frame->closure->closureVars, "regex"); regex = (ekRegex *)regexValue->ptrVal; ekAssert(argCount == 0); ekContextPopValues(E, argCount); results = ekValueCreateArray(E); if (!ekRegexMatchNext(E, regex, results)) { results = ekValueNullPtr; } ekContextReturn(E, results); }