From c0217c915254d69b9554d11c3e3112813de7fcfc Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 8 Apr 2020 18:02:53 +0200 Subject: [PATCH] - removed the remaining Doom-specific parts of the script compiler. --- source/common/scripting/core/imports.cpp | 23 -------- .../common/scripting/frontend/zcc_compile.cpp | 52 +++++++++++++------ .../common/scripting/frontend/zcc_compile.h | 9 ++-- .../common/scripting/frontend/zcc_parser.cpp | 41 +++------------ source/common/scripting/frontend/zcc_parser.h | 3 ++ tools/CMakeLists.txt | 1 + 6 files changed, 54 insertions(+), 75 deletions(-) diff --git a/source/common/scripting/core/imports.cpp b/source/common/scripting/core/imports.cpp index bbb0146d0..2b4d56756 100644 --- a/source/common/scripting/core/imports.cpp +++ b/source/common/scripting/core/imports.cpp @@ -228,26 +228,3 @@ void InitImports() } } -//========================================================================== -// -// SetImplicitArgs -// -// Adds the parameters implied by the function flags. -// -//========================================================================== - -void SetImplicitArgs(TArray *args, TArray *argflags, TArray *argnames, PContainerType *cls, uint32_t funcflags, int useflags) -{ - // Must be called before adding any other arguments. - assert(args == nullptr || args->Size() == 0); - assert(argflags == nullptr || argflags->Size() == 0); - - if (funcflags & VARF_Method) - { - // implied self pointer - if (args != nullptr) args->Push(NewPointer(cls, !!(funcflags & VARF_ReadOnly))); - if (argflags != nullptr) argflags->Push(VARF_Implicit | VARF_ReadOnly); - if (argnames != nullptr) argnames->Push(NAME_self); - } -} - diff --git a/source/common/scripting/frontend/zcc_compile.cpp b/source/common/scripting/frontend/zcc_compile.cpp index 344566391..59870ee9b 100644 --- a/source/common/scripting/frontend/zcc_compile.cpp +++ b/source/common/scripting/frontend/zcc_compile.cpp @@ -39,19 +39,18 @@ #include "zcc_compile.h" #include "printf.h" #include "symbols.h" -//#include "v_video.h" FSharedStringArena VMStringConstants; -static int GetIntConst(FxExpression *ex, FCompileContext &ctx) +int GetIntConst(FxExpression *ex, FCompileContext &ctx) { ex = new FxIntCast(ex, false); ex = ex->Resolve(ctx); return ex ? static_cast(ex)->GetValue().GetInt() : 0; } -static double GetFloatConst(FxExpression *ex, FCompileContext &ctx) +double GetFloatConst(FxExpression *ex, FCompileContext &ctx) { ex = new FxFloatCast(ex); ex = ex->Resolve(ctx); @@ -641,6 +640,7 @@ void ZCCCompiler::MessageV(ZCC_TreeNode *node, const char *txtcolor, const char // ZCCCompiler :: Compile // // Compile everything defined at this level. +// This can be overridden to add custom content. // //========================================================================== @@ -1347,15 +1347,13 @@ void ZCCCompiler::CompileAllFields() type->Size = Classes[i]->ClassType()->ParentClass->Size; } } - /*if (type->TypeName == NAME_Actor) + if (!PrepareMetaData(type)) { - assert(type->MetaSize == 0); - AddActorInfo(type); // AActor needs the actor info manually added to its meta data before adding any scripted fields. + if (Classes[i]->ClassType()->ParentClass) + type->MetaSize = Classes[i]->ClassType()->ParentClass->MetaSize; + else + type->MetaSize = 0; } - else*/ if (Classes[i]->ClassType()->ParentClass) - type->MetaSize = Classes[i]->ClassType()->ParentClass->MetaSize; - else - type->MetaSize = 0; if (CompileFields(type->VMType, Classes[i]->Fields, nullptr, &Classes[i]->TreeNodes, false, !!HasNativeChildren.CheckKey(type->TypeName))) { @@ -1922,11 +1920,33 @@ PType *ZCCCompiler::ResolveArraySize(PType *baseType, ZCC_Expression *arraysize, //========================================================================== // +// SetImplicitArgs // +// Adds the parameters implied by the function flags. // //========================================================================== +void ZCCCompiler::SetImplicitArgs(TArray* args, TArray* argflags, TArray* argnames, PContainerType* cls, uint32_t funcflags, int useflags) +{ + // Must be called before adding any other arguments. + assert(args == nullptr || args->Size() == 0); + assert(argflags == nullptr || argflags->Size() == 0); + if (funcflags & VARF_Method) + { + // implied self pointer + if (args != nullptr) args->Push(NewPointer(cls, !!(funcflags & VARF_ReadOnly))); + if (argflags != nullptr) argflags->Push(VARF_Implicit | VARF_ReadOnly); + if (argnames != nullptr) argnames->Push(NAME_self); + } +} + + +//========================================================================== +// +// +// +//========================================================================== void ZCCCompiler::CompileFunction(ZCC_StructWork *c, ZCC_FuncDeclarator *f, bool forclass) { @@ -2041,14 +2061,14 @@ void ZCCCompiler::CompileFunction(ZCC_StructWork *c, ZCC_FuncDeclarator *f, bool } if (f->Flags & ZCC_Action) { - /* - if (compileEnvironment.processActionFunc) + implicitargs = CheckActionKeyword(f,varflags, useflags, c); + if (implicitargs < 0) { - + Error(f, "'Action' not allowed as a function qualifier"); + // Set state to allow continued compilation to find more errors. + varflags &= ~VARF_ReadOnly; + implicitargs = 1; } - else - */ - Error(f, "'Action' not allowed as a function qualifier"); } if (f->Flags & ZCC_Static) varflags = (varflags & ~VARF_Method) | VARF_Final, implicitargs = 0; // Static implies Final. diff --git a/source/common/scripting/frontend/zcc_compile.h b/source/common/scripting/frontend/zcc_compile.h index 01836d3ee..5c90f77dd 100644 --- a/source/common/scripting/frontend/zcc_compile.h +++ b/source/common/scripting/frontend/zcc_compile.h @@ -106,11 +106,14 @@ class ZCCCompiler { public: ZCCCompiler(ZCC_AST &tree, DObject *outer, PSymbolTable &symbols, PNamespace *outnamespace, int lumpnum, const VersionInfo & ver); - ~ZCCCompiler(); - int Compile(); + virtual ~ZCCCompiler(); + virtual int Compile(); -private: +protected: const char * GetStringConst(FxExpression *ex, FCompileContext &ctx); + virtual int CheckActionKeyword(ZCC_FuncDeclarator* f, uint32_t &varflags, int useflags, ZCC_StructWork *c) { return -1; } // stock implementation does not support this. + virtual bool PrepareMetaData(PClass *type) { return false; } + virtual void SetImplicitArgs(TArray* args, TArray* argflags, TArray* argnames, PContainerType* cls, uint32_t funcflags, int useflags); int IntConstFromNode(ZCC_TreeNode *node, PContainerType *cls); FString StringConstFromNode(ZCC_TreeNode *node, PContainerType *cls); diff --git a/source/common/scripting/frontend/zcc_parser.cpp b/source/common/scripting/frontend/zcc_parser.cpp index 91facfedc..e176c646e 100644 --- a/source/common/scripting/frontend/zcc_parser.cpp +++ b/source/common/scripting/frontend/zcc_parser.cpp @@ -350,16 +350,20 @@ parse_end: //**-------------------------------------------------------------------------- -static void DoParse(int lumpnum) +PNamespace *ParseOneScript(const int baselump, ZCCParseState &state) { FScanner sc; void *parser; ZCCToken value; - auto baselump = lumpnum; + int lumpnum = baselump; auto fileno = fileSystem.GetFileContainer(lumpnum); + if (TokenMap.CountUsed() == 0) + { + InitTokenMap(); + } + parser = ZCCParseAlloc(malloc); - ZCCParseState state; #ifndef NDEBUG FILE *f = nullptr; @@ -471,37 +475,8 @@ static void DoParse(int lumpnum) } } - PSymbolTable symtable; auto newns = fileSystem.GetFileContainer(baselump) == 0 ? Namespaces.GlobalNamespace : Namespaces.NewNamespace(fileSystem.GetFileContainer(baselump)); - ZCCCompiler cc(state, NULL, symtable, newns, baselump, state.ParseVersion); - cc.Compile(); - - if (FScriptPosition::ErrorCounter > 0) - { - // Abort if the compiler produced any errors. Also do not compile further lumps, because they very likely miss some stuff. - I_Error("%d errors, %d warnings while compiling %s", FScriptPosition::ErrorCounter, FScriptPosition::WarnCounter, fileSystem.GetFileFullPath(baselump).GetChars()); - } - else if (FScriptPosition::WarnCounter > 0) - { - // If we got warnings, but no errors, print the information but continue. - Printf(TEXTCOLOR_ORANGE "%d warnings while compiling %s\n", FScriptPosition::WarnCounter, fileSystem.GetFileFullPath(baselump).GetChars()); - } - -} - -void ParseScripts() -{ - if (TokenMap.CountUsed() == 0) - { - InitTokenMap(); - } - int lump, lastlump = 0; - FScriptPosition::ResetErrorCounter(); - - while ((lump = fileSystem.FindLump("ZSCRIPT", &lastlump)) != -1) - { - DoParse(lump); - } + return newns; } static FString ZCCTokenName(int terminal) diff --git a/source/common/scripting/frontend/zcc_parser.h b/source/common/scripting/frontend/zcc_parser.h index 3b0e9eb19..2d89d0d94 100644 --- a/source/common/scripting/frontend/zcc_parser.h +++ b/source/common/scripting/frontend/zcc_parser.h @@ -618,4 +618,7 @@ const char *GetMixinTypeString(EZCCMixinType type); ZCC_TreeNode *TreeNodeDeepCopy(ZCC_AST *ast, ZCC_TreeNode *orig, bool copySiblings); +// Main entry point for the parser. Returns some data needed by the compiler. +PNamespace* ParseOneScript(const int baselump, ZCCParseState& state); + #endif diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index c143f08ab..b3fed70ff 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required( VERSION 2.8.7 ) add_subdirectory( re2c ) +add_subdirectory( lemon ) add_subdirectory( zipdir ) set( CROSS_EXPORTS ${CROSS_EXPORTS} PARENT_SCOPE )