From d139ee9f682b76b7b134360f1c94fcaba9433cc3 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 8 Oct 2016 15:27:20 +0200 Subject: [PATCH] - give the 'default' block its own node type in the AST. - group a class's body the same way as the global AST. Only partially implemented so far so that constants can be processed. --- src/dobjtype.h | 25 +++++++-------- src/zscript/ast.cpp | 12 +++++++- src/zscript/zcc-parse.lemon | 2 +- src/zscript/zcc_compile.cpp | 61 ++++++++++++++++++++++++++++++++----- src/zscript/zcc_compile.h | 57 ++++++++++++++++++++++++++++++++-- src/zscript/zcc_parser.h | 5 +++ 6 files changed, 138 insertions(+), 24 deletions(-) diff --git a/src/dobjtype.h b/src/dobjtype.h index 338c51f982..d2a8aabf5e 100644 --- a/src/dobjtype.h +++ b/src/dobjtype.h @@ -86,18 +86,6 @@ public: PSymbolType() : PSymbol(NAME_None) {} }; -// A symbol for a compiler tree node ---------------------------------------- - -class PSymbolTreeNode : public PSymbol -{ - DECLARE_CLASS(PSymbolTreeNode, PSymbol); -public: - struct ZCC_NamedNode *Node; - - PSymbolTreeNode(FName name, struct ZCC_NamedNode *node) : PSymbol(name), Node(node) {} - PSymbolTreeNode() : PSymbol(NAME_None) {} -}; - // A symbol table ----------------------------------------------------------- struct PSymbolTable @@ -141,6 +129,19 @@ private: friend class DObject; }; +// A symbol for a compiler tree node ---------------------------------------- + +class PSymbolTreeNode : public PSymbol +{ + DECLARE_CLASS(PSymbolTreeNode, PSymbol); +public: + struct ZCC_NamedNode *Node; + PSymbolTable Symbols; + + PSymbolTreeNode(FName name, struct ZCC_NamedNode *node) : PSymbol(name), Node(node) {} + PSymbolTreeNode() : PSymbol(NAME_None) {} +}; + extern PSymbolTable GlobalSymbols; // Basic information shared by all types ------------------------------------ diff --git a/src/zscript/ast.cpp b/src/zscript/ast.cpp index efb4a020b3..a0a823b130 100644 --- a/src/zscript/ast.cpp +++ b/src/zscript/ast.cpp @@ -629,6 +629,15 @@ static void PrintCompoundStmt(FLispString &out, ZCC_TreeNode *node) out.Close(); } +static void PrintDefault(FLispString &out, ZCC_TreeNode *node) +{ + ZCC_Default *snode = (ZCC_Default *)node; + out.Break(); + out.Open("default"); + PrintNodes(out, snode->Content, false, true); + out.Close(); +} + static void PrintContinueStmt(FLispString &out, ZCC_TreeNode *node) { out.Break(); @@ -853,7 +862,8 @@ void (* const TreeNodePrinter[NUM_AST_NODE_TYPES])(FLispString &, ZCC_TreeNode * PrintConstantDef, PrintDeclarator, PrintVarDeclarator, - PrintFuncDeclarator + PrintFuncDeclarator, + PrintDefault }; FString ZCC_PrintAST(ZCC_TreeNode *root) diff --git a/src/zscript/zcc-parse.lemon b/src/zscript/zcc-parse.lemon index 9fdc4603c4..09faf9272a 100644 --- a/src/zscript/zcc-parse.lemon +++ b/src/zscript/zcc-parse.lemon @@ -491,7 +491,7 @@ state_call_params(X) ::= LPAREN func_expr_list(A) RPAREN. { X = A; /*X-overwrite /* Definition of a default class instance. */ %type default_def {ZCC_CompoundStmt *} -default_def(X) ::= DEFAULT compound_statement(A). { X = A; /*X-overwrites-A*/ } +default_def(X) ::= DEFAULT compound_statement(A). { X = A; /*X-overwrites-A*/ X->NodeType = AST_Default; } /* Type names */ %type type_name {ZCC_BasicType *} diff --git a/src/zscript/zcc_compile.cpp b/src/zscript/zcc_compile.cpp index 9dfbab8470..d8af55394d 100644 --- a/src/zscript/zcc_compile.cpp +++ b/src/zscript/zcc_compile.cpp @@ -45,6 +45,50 @@ #define DEFINING_CONST ((PSymbolConst *)(void *)1) +//========================================================================== +// +// ZCCCompiler :: ProcessClass +// +//========================================================================== + +void ZCCCompiler::ProcessClass(ZCC_Class *cnode, PSymbolTreeNode *tnode) +{ + Classes.Push(ZCC_ClassWork(static_cast(cnode), tnode)); + ZCC_ClassWork &cls = Classes.Last(); + + auto node = cnode->Body; + + do + { + switch (node->NodeType) + { + case AST_Struct: + case AST_ConstantDef: + if ((tnode = AddNamedNode(static_cast(node)))) + { + switch (node->NodeType) + { + case AST_Struct: cls.Structs.Push(ZCC_StructWork(static_cast(node), tnode)); break; + case AST_ConstantDef: cls.Constants.Push(static_cast(node)); break; + default: assert(0 && "Default case is just here to make GCC happy. It should never be reached"); + } + } + break; + + case AST_Enum: break; + case AST_EnumTerminator:break; + + // todo + case AST_States: + case AST_VarDeclarator: + case AST_FuncDeclarator: + break; + } + node = node->SiblingNext; + } + while (node != cnode->Body); +} + //========================================================================== // // ZCCCompiler Constructor @@ -58,6 +102,7 @@ ZCCCompiler::ZCCCompiler(ZCC_AST &ast, DObject *_outer, PSymbolTable &_symbols, if (ast.TopNode != NULL) { ZCC_TreeNode *node = ast.TopNode; + PSymbolTreeNode *tnode; do { switch (node->NodeType) @@ -65,12 +110,12 @@ ZCCCompiler::ZCCCompiler(ZCC_AST &ast, DObject *_outer, PSymbolTable &_symbols, case AST_Class: case AST_Struct: case AST_ConstantDef: - if (AddNamedNode(static_cast(node))) + if ((tnode = AddNamedNode(static_cast(node)))) { switch (node->NodeType) { - case AST_Class: Classes.Push(static_cast(node)); break; - case AST_Struct: Structs.Push(static_cast(node)); break; + case AST_Class: ProcessClass(static_cast(node), tnode); break; + case AST_Struct: Structs.Push(ZCC_StructWork(static_cast(node), tnode)); break; case AST_ConstantDef: Constants.Push(static_cast(node)); break; default: assert(0 && "Default case is just here to make GCC happy. It should never be reached"); } @@ -99,7 +144,7 @@ ZCCCompiler::ZCCCompiler(ZCC_AST &ast, DObject *_outer, PSymbolTable &_symbols, // //========================================================================== -bool ZCCCompiler::AddNamedNode(ZCC_NamedNode *node) +PSymbolTreeNode *ZCCCompiler::AddNamedNode(ZCC_NamedNode *node, PSymbolTable *parentsym) { FName name = node->NodeName; PSymbol *check = Symbols->FindSymbol(name, false); @@ -108,12 +153,14 @@ bool ZCCCompiler::AddNamedNode(ZCC_NamedNode *node) assert(check->IsA(RUNTIME_CLASS(PSymbolTreeNode))); Error(node, "Attempt to redefine '%s'", name.GetChars()); Error(static_cast(check)->Node, " Original definition is here"); - return false; + return nullptr; } else { - Symbols->AddSymbol(new PSymbolTreeNode(name, node)); - return true; + auto sy = new PSymbolTreeNode(name, node); + sy->Symbols.SetParentTable(parentsym); + Symbols->AddSymbol(sy); + return sy; } } diff --git a/src/zscript/zcc_compile.h b/src/zscript/zcc_compile.h index 6ef4a34f28..8b21f2617e 100644 --- a/src/zscript/zcc_compile.h +++ b/src/zscript/zcc_compile.h @@ -1,6 +1,56 @@ #ifndef ZCC_COMPILE_H #define ZCC_COMPILE_H +struct ZCC_StructWork +{ + ZCC_Struct *strct; + PSymbolTreeNode *node; + TArray Constants; + + ZCC_StructWork(ZCC_Struct * s, PSymbolTreeNode *n) + { + strct = s; + node = n; + }; + + ZCC_Struct *operator->() + { + return strct; + } + + operator ZCC_Struct *() + { + return strct; + } + + +}; + +struct ZCC_ClassWork +{ + ZCC_Class *cls; + PSymbolTreeNode *node; + TArray Constants; + TArray Structs; + + ZCC_ClassWork(ZCC_Class * s, PSymbolTreeNode *n) + { + cls = s; + node = n; + }; + + ZCC_Class *operator->() + { + return cls; + } + + operator ZCC_Class *() + { + return cls; + } + +}; + class ZCCCompiler { public: @@ -8,16 +58,17 @@ public: int Compile(); private: + void ProcessClass(ZCC_Class *node, PSymbolTreeNode *tnode); void CreateStructTypes(); void CreateClassTypes(); void CompileConstants(const TArray &defs); PSymbolConst *CompileConstant(ZCC_ConstantDef *def); TArray Constants; - TArray Structs; - TArray Classes; + TArray Structs; + TArray Classes; - bool AddNamedNode(ZCC_NamedNode *node); + PSymbolTreeNode *AddNamedNode(ZCC_NamedNode *node, PSymbolTable *parentsym = nullptr); ZCC_Expression *Simplify(ZCC_Expression *root); ZCC_Expression *SimplifyUnary(ZCC_ExprUnary *unary); diff --git a/src/zscript/zcc_parser.h b/src/zscript/zcc_parser.h index 31ad22c5ee..8de0c39522 100644 --- a/src/zscript/zcc_parser.h +++ b/src/zscript/zcc_parser.h @@ -93,6 +93,7 @@ enum EZCCTreeNodeType AST_Declarator, AST_VarDeclarator, AST_FuncDeclarator, + AST_Default, NUM_AST_NODE_TYPES }; @@ -463,6 +464,10 @@ struct ZCC_FuncDeclarator : ZCC_Declarator ZCC_Statement *Body; }; +struct ZCC_Default : ZCC_CompoundStmt +{ +}; + typedef ZCC_ExprConstant *(*EvalConst1op)(ZCC_ExprConstant *); typedef ZCC_ExprConstant *(*EvalConst2op)(ZCC_ExprConstant *, ZCC_ExprConstant *, FSharedStringArena &);