From d0968af9a6c02ffd4f18c3c14ba2bcdb988b876a Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Wed, 23 Oct 2013 22:49:02 -0500 Subject: [PATCH] Separate the AST from the parser state. --- src/zscript/zcc-parse.lemon | 12 +----------- src/zscript/zcc_parser.cpp | 21 +++++++++++++++++++++ src/zscript/zcc_parser.h | 34 +++++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/zscript/zcc-parse.lemon b/src/zscript/zcc-parse.lemon index a3c9d1141..ae31a1e09 100644 --- a/src/zscript/zcc-parse.lemon +++ b/src/zscript/zcc-parse.lemon @@ -2,19 +2,9 @@ { // Allocates a new AST node off the parse state's arena. #define NEW_AST_NODE(type,name,tok) \ - ZCC_##type *name = static_cast(InitNode(sizeof(ZCC_##type), AST_##type, stat)); \ + ZCC_##type *name = static_cast(stat->InitNode(sizeof(ZCC_##type), AST_##type)); \ SetNodeLine(name, tok) -static ZCC_TreeNode *InitNode(size_t size, EZCCTreeNodeType type, ZCCParseState *stat) -{ - ZCC_TreeNode *node = (ZCC_TreeNode *)stat->SyntaxArena.Alloc(size); - node->SiblingNext = node; - node->SiblingPrev = node; - node->NodeType = type; - node->SourceName = stat->Strings.Alloc(stat->sc.ScriptName); - return node; -} - static void SetNodeLine(ZCC_TreeNode *name, ZCCToken &tok) { name->SourceLoc = tok.SourceLoc; diff --git a/src/zscript/zcc_parser.cpp b/src/zscript/zcc_parser.cpp index 766f28c2a..32127ed46 100644 --- a/src/zscript/zcc_parser.cpp +++ b/src/zscript/zcc_parser.cpp @@ -297,3 +297,24 @@ static FString ZCCTokenName(int terminal) } return FScanner::TokenName(sc_token); } + +ZCC_TreeNode *ZCC_AST::InitNode(size_t size, EZCCTreeNodeType type, ZCC_TreeNode *basis) +{ + ZCC_TreeNode *node = (ZCC_TreeNode *)SyntaxArena.Alloc(size); + node->SiblingNext = node; + node->SiblingPrev = node; + node->NodeType = type; + if (basis != NULL) + { + node->SourceName = basis->SourceName; + node->SourceLoc = basis->SourceLoc; + } + return node; +} + +ZCC_TreeNode *ZCCParseState::InitNode(size_t size, EZCCTreeNodeType type) +{ + ZCC_TreeNode *node = ZCC_AST::InitNode(size, type, NULL); + node->SourceName = Strings.Alloc(sc.ScriptName); + return node; +} diff --git a/src/zscript/zcc_parser.h b/src/zscript/zcc_parser.h index f5131e839..50e75fd94 100644 --- a/src/zscript/zcc_parser.h +++ b/src/zscript/zcc_parser.h @@ -1,15 +1,7 @@ -struct ZCCParseState -{ - ZCCParseState(FScanner &scanner) : sc(scanner) - { - TopNode = NULL; - } +#ifndef ZCC_PARSER_H +#define ZCC_PARSER_H - FScanner ≻ - FSharedStringArena Strings; - FMemArena SyntaxArena; - struct ZCC_TreeNode *TopNode; -}; +#include "memarena.h" struct ZCCToken { @@ -500,3 +492,23 @@ struct ZCC_FuncDeclarator : ZCC_Declarator }; FString ZCC_PrintAST(ZCC_TreeNode *root); + +struct ZCC_AST +{ + ZCC_AST() : TopNode(NULL) {} + ZCC_TreeNode *InitNode(size_t size, EZCCTreeNodeType type, ZCC_TreeNode *basis); + + FSharedStringArena Strings; + FMemArena SyntaxArena; + struct ZCC_TreeNode *TopNode; +}; + +struct ZCCParseState : public ZCC_AST +{ + ZCCParseState(FScanner &scanner) : sc(scanner) {} + ZCC_TreeNode *InitNode(size_t size, EZCCTreeNodeType type); + + FScanner ≻ +}; + +#endif