From 1204b81a78ec6658e38dc58456a714264fa38da3 Mon Sep 17 00:00:00 2001 From: Daniel Buckmaster Date: Mon, 29 Dec 2014 21:49:52 +1100 Subject: [PATCH] Added anomymous functions as in Konrad Kiss's resource. --- Engine/source/console/CMDgram.y | 13 +++++++++++++ Engine/source/console/ast.h | 4 +++- Engine/source/console/codeBlock.cpp | 17 +++++++++++++++++ Engine/source/console/console.cpp | 2 ++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Engine/source/console/CMDgram.y b/Engine/source/console/CMDgram.y index 06830b7dd..af0122e9b 100644 --- a/Engine/source/console/CMDgram.y +++ b/Engine/source/console/CMDgram.y @@ -456,6 +456,19 @@ expr { $$ = (ExprNode*)VarNode::alloc( $1.lineNumber, $1.value, NULL); } | VAR '[' aidx_expr ']' { $$ = (ExprNode*)VarNode::alloc( $1.lineNumber, $1.value, $3 ); } + | rwDEFINE '(' var_list_decl ')' '{' statement_list '}' + { + String fnname = String("__anonymous_function_" + String::ToString(gAnonFunctionID++)); + StringTableEntry afnName = StringTable->insert(fnname.c_str()); + StmtNode *fndef = FunctionDeclStmtNode::alloc($1.lineNumber, afnName, NULL, $3, $6); + + if(!gAnonFunctionList) + gAnonFunctionList = fndef; + else + gAnonFunctionList->append(fndef); + + $$ = StrConstNode::alloc( $1.lineNumber, (UTF8*)fnname.utf8(), false ); + } ; slot_acc diff --git a/Engine/source/console/ast.h b/Engine/source/console/ast.h index 5177cbaf0..f1327a75a 100644 --- a/Engine/source/console/ast.h +++ b/Engine/source/console/ast.h @@ -575,6 +575,8 @@ struct FunctionDeclStmtNode : StmtNode }; extern StmtNode *gStatementList; -extern ExprEvalState gEvalState;; +extern StmtNode *gAnonFunctionList; +extern U32 gAnonFunctionID; +extern ExprEvalState gEvalState; #endif diff --git a/Engine/source/console/codeBlock.cpp b/Engine/source/console/codeBlock.cpp index 3599ae15b..c8cd57223 100644 --- a/Engine/source/console/codeBlock.cpp +++ b/Engine/source/console/codeBlock.cpp @@ -468,6 +468,7 @@ bool CodeBlock::compile(const char *codeFileName, StringTableEntry fileName, con STEtoCode = compileSTEtoCode; gStatementList = NULL; + gAnonFunctionList = NULL; // Set up the parser. smCurrentParser = getParserForFile(fileName); @@ -477,6 +478,14 @@ bool CodeBlock::compile(const char *codeFileName, StringTableEntry fileName, con smCurrentParser->setScanBuffer(script, fileName); smCurrentParser->restart(NULL); smCurrentParser->parse(); + if (gStatementList) + { + if (gAnonFunctionList) + { + gStatementList->append(gAnonFunctionList); + } + } + if(gSyntaxError) { @@ -599,6 +608,7 @@ const char *CodeBlock::compileExec(StringTableEntry fileName, const char *inStri addToCodeList(); gStatementList = NULL; + gAnonFunctionList = NULL; // Set up the parser. smCurrentParser = getParserForFile(fileName); @@ -608,6 +618,13 @@ const char *CodeBlock::compileExec(StringTableEntry fileName, const char *inStri smCurrentParser->setScanBuffer(string, fileName); smCurrentParser->restart(NULL); smCurrentParser->parse(); + if (gStatementList) + { + if (gAnonFunctionList) + { + gStatementList->append(gAnonFunctionList); + } + } if(!gStatementList) { diff --git a/Engine/source/console/console.cpp b/Engine/source/console/console.cpp index 18d32bed4..fb0232051 100644 --- a/Engine/source/console/console.cpp +++ b/Engine/source/console/console.cpp @@ -47,6 +47,8 @@ extern ConsoleValueStack CSTK; ConsoleDocFragment* ConsoleDocFragment::smFirst; ExprEvalState gEvalState; StmtNode *gStatementList; +StmtNode *gAnonFunctionList; +U32 gAnonFunctionID = 0; ConsoleConstructor *ConsoleConstructor::first = NULL; bool gWarnUndefinedScriptVariables;