- 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.
This commit is contained in:
Christoph Oelckers 2016-10-08 15:27:20 +02:00
parent 82ac2081b6
commit d139ee9f68
6 changed files with 138 additions and 24 deletions

View file

@ -86,18 +86,6 @@ public:
PSymbolType() : PSymbol(NAME_None) {} 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 ----------------------------------------------------------- // A symbol table -----------------------------------------------------------
struct PSymbolTable struct PSymbolTable
@ -141,6 +129,19 @@ private:
friend class DObject; 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; extern PSymbolTable GlobalSymbols;
// Basic information shared by all types ------------------------------------ // Basic information shared by all types ------------------------------------

View file

@ -629,6 +629,15 @@ static void PrintCompoundStmt(FLispString &out, ZCC_TreeNode *node)
out.Close(); 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) static void PrintContinueStmt(FLispString &out, ZCC_TreeNode *node)
{ {
out.Break(); out.Break();
@ -853,7 +862,8 @@ void (* const TreeNodePrinter[NUM_AST_NODE_TYPES])(FLispString &, ZCC_TreeNode *
PrintConstantDef, PrintConstantDef,
PrintDeclarator, PrintDeclarator,
PrintVarDeclarator, PrintVarDeclarator,
PrintFuncDeclarator PrintFuncDeclarator,
PrintDefault
}; };
FString ZCC_PrintAST(ZCC_TreeNode *root) FString ZCC_PrintAST(ZCC_TreeNode *root)

View file

@ -491,7 +491,7 @@ state_call_params(X) ::= LPAREN func_expr_list(A) RPAREN. { X = A; /*X-overwrite
/* Definition of a default class instance. */ /* Definition of a default class instance. */
%type default_def {ZCC_CompoundStmt *} %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 names */
%type type_name {ZCC_BasicType *} %type type_name {ZCC_BasicType *}

View file

@ -45,6 +45,50 @@
#define DEFINING_CONST ((PSymbolConst *)(void *)1) #define DEFINING_CONST ((PSymbolConst *)(void *)1)
//==========================================================================
//
// ZCCCompiler :: ProcessClass
//
//==========================================================================
void ZCCCompiler::ProcessClass(ZCC_Class *cnode, PSymbolTreeNode *tnode)
{
Classes.Push(ZCC_ClassWork(static_cast<ZCC_Class *>(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<ZCC_NamedNode *>(node))))
{
switch (node->NodeType)
{
case AST_Struct: cls.Structs.Push(ZCC_StructWork(static_cast<ZCC_Struct *>(node), tnode)); break;
case AST_ConstantDef: cls.Constants.Push(static_cast<ZCC_ConstantDef *>(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 // ZCCCompiler Constructor
@ -58,6 +102,7 @@ ZCCCompiler::ZCCCompiler(ZCC_AST &ast, DObject *_outer, PSymbolTable &_symbols,
if (ast.TopNode != NULL) if (ast.TopNode != NULL)
{ {
ZCC_TreeNode *node = ast.TopNode; ZCC_TreeNode *node = ast.TopNode;
PSymbolTreeNode *tnode;
do do
{ {
switch (node->NodeType) switch (node->NodeType)
@ -65,12 +110,12 @@ ZCCCompiler::ZCCCompiler(ZCC_AST &ast, DObject *_outer, PSymbolTable &_symbols,
case AST_Class: case AST_Class:
case AST_Struct: case AST_Struct:
case AST_ConstantDef: case AST_ConstantDef:
if (AddNamedNode(static_cast<ZCC_NamedNode *>(node))) if ((tnode = AddNamedNode(static_cast<ZCC_NamedNode *>(node))))
{ {
switch (node->NodeType) switch (node->NodeType)
{ {
case AST_Class: Classes.Push(static_cast<ZCC_Class *>(node)); break; case AST_Class: ProcessClass(static_cast<ZCC_Class *>(node), tnode); break;
case AST_Struct: Structs.Push(static_cast<ZCC_Struct *>(node)); break; case AST_Struct: Structs.Push(ZCC_StructWork(static_cast<ZCC_Struct *>(node), tnode)); break;
case AST_ConstantDef: Constants.Push(static_cast<ZCC_ConstantDef *>(node)); break; case AST_ConstantDef: Constants.Push(static_cast<ZCC_ConstantDef *>(node)); break;
default: assert(0 && "Default case is just here to make GCC happy. It should never be reached"); 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; FName name = node->NodeName;
PSymbol *check = Symbols->FindSymbol(name, false); PSymbol *check = Symbols->FindSymbol(name, false);
@ -108,12 +153,14 @@ bool ZCCCompiler::AddNamedNode(ZCC_NamedNode *node)
assert(check->IsA(RUNTIME_CLASS(PSymbolTreeNode))); assert(check->IsA(RUNTIME_CLASS(PSymbolTreeNode)));
Error(node, "Attempt to redefine '%s'", name.GetChars()); Error(node, "Attempt to redefine '%s'", name.GetChars());
Error(static_cast<PSymbolTreeNode *>(check)->Node, " Original definition is here"); Error(static_cast<PSymbolTreeNode *>(check)->Node, " Original definition is here");
return false; return nullptr;
} }
else else
{ {
Symbols->AddSymbol(new PSymbolTreeNode(name, node)); auto sy = new PSymbolTreeNode(name, node);
return true; sy->Symbols.SetParentTable(parentsym);
Symbols->AddSymbol(sy);
return sy;
} }
} }

View file

@ -1,6 +1,56 @@
#ifndef ZCC_COMPILE_H #ifndef ZCC_COMPILE_H
#define ZCC_COMPILE_H #define ZCC_COMPILE_H
struct ZCC_StructWork
{
ZCC_Struct *strct;
PSymbolTreeNode *node;
TArray<ZCC_ConstantDef *> 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<ZCC_ConstantDef *> Constants;
TArray<ZCC_StructWork> Structs;
ZCC_ClassWork(ZCC_Class * s, PSymbolTreeNode *n)
{
cls = s;
node = n;
};
ZCC_Class *operator->()
{
return cls;
}
operator ZCC_Class *()
{
return cls;
}
};
class ZCCCompiler class ZCCCompiler
{ {
public: public:
@ -8,16 +58,17 @@ public:
int Compile(); int Compile();
private: private:
void ProcessClass(ZCC_Class *node, PSymbolTreeNode *tnode);
void CreateStructTypes(); void CreateStructTypes();
void CreateClassTypes(); void CreateClassTypes();
void CompileConstants(const TArray<ZCC_ConstantDef *> &defs); void CompileConstants(const TArray<ZCC_ConstantDef *> &defs);
PSymbolConst *CompileConstant(ZCC_ConstantDef *def); PSymbolConst *CompileConstant(ZCC_ConstantDef *def);
TArray<ZCC_ConstantDef *> Constants; TArray<ZCC_ConstantDef *> Constants;
TArray<ZCC_Struct *> Structs; TArray<ZCC_StructWork> Structs;
TArray<ZCC_Class *> Classes; TArray<ZCC_ClassWork> Classes;
bool AddNamedNode(ZCC_NamedNode *node); PSymbolTreeNode *AddNamedNode(ZCC_NamedNode *node, PSymbolTable *parentsym = nullptr);
ZCC_Expression *Simplify(ZCC_Expression *root); ZCC_Expression *Simplify(ZCC_Expression *root);
ZCC_Expression *SimplifyUnary(ZCC_ExprUnary *unary); ZCC_Expression *SimplifyUnary(ZCC_ExprUnary *unary);

View file

@ -93,6 +93,7 @@ enum EZCCTreeNodeType
AST_Declarator, AST_Declarator,
AST_VarDeclarator, AST_VarDeclarator,
AST_FuncDeclarator, AST_FuncDeclarator,
AST_Default,
NUM_AST_NODE_TYPES NUM_AST_NODE_TYPES
}; };
@ -463,6 +464,10 @@ struct ZCC_FuncDeclarator : ZCC_Declarator
ZCC_Statement *Body; ZCC_Statement *Body;
}; };
struct ZCC_Default : ZCC_CompoundStmt
{
};
typedef ZCC_ExprConstant *(*EvalConst1op)(ZCC_ExprConstant *); typedef ZCC_ExprConstant *(*EvalConst1op)(ZCC_ExprConstant *);
typedef ZCC_ExprConstant *(*EvalConst2op)(ZCC_ExprConstant *, ZCC_ExprConstant *, FSharedStringArena &); typedef ZCC_ExprConstant *(*EvalConst2op)(ZCC_ExprConstant *, ZCC_ExprConstant *, FSharedStringArena &);