Skip to content

Commit

Permalink
XS #623
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Soquet committed Apr 7, 2021
1 parent a4b667f commit 593cfd6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
2 changes: 2 additions & 0 deletions xs/sources/xsCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ enum {
mxSuperFlag = 1 << 5,
mxTargetFlag = 1 << 6,
mxFieldFlag = 1 << 15,
mxFunctionFlag = 1 << 16,
mxGeneratorFlag = 1 << 21,
};

extern void fxDeleteScript(txScript* script);
Expand Down
4 changes: 2 additions & 2 deletions xs/sources/xsFunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ void fx_Function(txMachine* the)
stream.slot = the->stack;
stream.offset = 0;
stream.size = c_strlen(the->stack->value.string);
fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxFunctionFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
mxPullSlot(mxResult);
if (!mxIsUndefined(mxTarget) && !fxIsSameSlot(the, mxTarget, mxFunction)) {
mxPushSlot(mxTarget);
Expand Down Expand Up @@ -740,7 +740,7 @@ void fx_AsyncFunction(txMachine* the)
stream.slot = the->stack;
stream.offset = 0;
stream.size = c_strlen(the->stack->value.string);
fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxFunctionFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
mxPullSlot(mxResult);
if (!mxIsUndefined(mxTarget) && !fxIsSameSlot(the, mxTarget, mxFunction)) {
mxPushSlot(mxTarget);
Expand Down
4 changes: 2 additions & 2 deletions xs/sources/xsGenerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void fx_GeneratorFunction(txMachine* the)
stream.slot = the->stack;
stream.offset = 0;
stream.size = c_strlen(the->stack->value.string);
fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxGeneratorFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
mxPullSlot(mxResult);
if (!mxIsUndefined(mxTarget) && !fxIsSameSlot(the, mxTarget, mxFunction)) {
mxPushSlot(mxTarget);
Expand Down Expand Up @@ -676,7 +676,7 @@ void fx_AsyncGeneratorFunction(txMachine* the)
stream.slot = the->stack;
stream.offset = 0;
stream.size = c_strlen(the->stack->value.string);
fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxGeneratorFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
mxPullSlot(mxResult);
if (!mxIsUndefined(mxTarget) && !fxIsSameSlot(the, mxTarget, mxFunction)) {
mxPushSlot(mxTarget);
Expand Down
3 changes: 2 additions & 1 deletion xs/sources/xsScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ enum {
/* mxSuperFlag = 1 << 5, */
/* mxTargetFlag = 1 << 6, */
/* mxFieldFlag = 1 << 15, */
/* mxFunctionFlag = 1 << 16, */
/* mxGeneratorFlag = 1 << 21, */
mxParserFlags = mxCFlag | mxDebugFlag | mxProgramFlag,


Expand All @@ -917,7 +919,6 @@ enum {
mxElisionFlag = 1 << 18,
mxExpressionNoValue = 1 << 19,
mxForFlag = 1 << 20,
mxGeneratorFlag = 1 << 21,
mxGetterFlag = 1 << 22,
mxMethodFlag = 1 << 23,
mxNotSimpleParametersFlag = 1 << 24,
Expand Down
49 changes: 48 additions & 1 deletion xs/sources/xsTree.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ typedef struct {

static void fxNodeDistribute(void* it, txNodeCall call, void* param);

static void fxCheckFunction(txParser* parser);
static void fxCheckGenerator(txParser* parser);

static void fxArrayNodeDistribute(void* it, txNodeCall call, void* param);
static void fxArrayBindingNodeDistribute(void* it, txNodeCall call, void* param);
static void fxAssignNodeDistribute(void* it, txNodeCall call, void* param);
Expand Down Expand Up @@ -133,6 +136,46 @@ void fxIncludeTree(txParser* parser, void* stream, txGetter getter, txUnsigned f
parser->path = symbol;
}

void fxCheckFunction(txParser* parser)
{
txNode* node = parser->root;
if (node->description->token == XS_TOKEN_PROGRAM) {
node = ((txProgramNode*)node)->body;
if (node->description->token == XS_TOKEN_STATEMENT) {
node = ((txStatementNode*)node)->expression;
if (node->description->token == XS_TOKEN_EXPRESSIONS) {
txNodeList* list = ((txExpressionsNode*)node)->items;
if (list->length == 1) {
node = list->first;
if (node->description->token == XS_TOKEN_FUNCTION)
return;
}
}
}
}
fxReportParserError(parser, parser->line, "no function");
}

void fxCheckGenerator(txParser* parser)
{
txNode* node = parser->root;
if (node->description->token == XS_TOKEN_PROGRAM) {
node = ((txProgramNode*)node)->body;
if (node->description->token == XS_TOKEN_STATEMENT) {
node = ((txStatementNode*)node)->expression;
if (node->description->token == XS_TOKEN_EXPRESSIONS) {
txNodeList* list = ((txExpressionsNode*)node)->items;
if (list->length == 1) {
node = list->first;
if (node->description->token == XS_TOKEN_GENERATOR)
return;
}
}
}
}
fxReportParserError(parser, parser->line, "no generator function");
}

void fxParserTree(txParser* parser, void* theStream, txGetter theGetter, txUnsigned flags, txString* name)
{
mxTryParser(parser) {
Expand All @@ -148,7 +191,7 @@ void fxParserTree(txParser* parser, void* theStream, txGetter theGetter, txUnsig

parser->root = NULL;

parser->flags &= ~mxEvalFlag;
parser->flags &= ~(mxEvalFlag | mxFunctionFlag | mxGeneratorFlag);
if (!(parser->flags & mxProgramFlag))
parser->flags |= mxStrictFlag | mxAsyncFlag;
fxGetNextCharacter(parser);
Expand All @@ -167,6 +210,10 @@ void fxParserTree(txParser* parser, void* theStream, txGetter theGetter, txUnsig
fxGetNextToken(parser);
if (parser->flags & mxProgramFlag) {
fxProgram(parser);
if (flags & mxFunctionFlag)
fxCheckFunction(parser);
else if (flags & mxGeneratorFlag)
fxCheckGenerator(parser);
}
else {
fxModule(parser);
Expand Down

0 comments on commit 593cfd6

Please sign in to comment.