From 6989b8168859a67dfb449ea51767cda727292940 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 11 Oct 2016 13:11:40 +0200 Subject: [PATCH] - extended the grammar so that the DECORATE function declaration list can almost be used as-is, with the sole exception of requiring any action function to declare a return type, even if it is void. This adds: * builtin types color, state and sound. * ending a parameter list with an ellipsis to declare a varargs list. (A_Jump uses this.) * allowing to declare optional arguments by giving them a default value. * adding an 'action' qualifier for function declarations. --- src/zscript/ast.cpp | 8 +++++++- src/zscript/zcc-parse.lemon | 26 ++++++++++++++++++++++++++ src/zscript/zcc_parser.cpp | 5 +++++ src/zscript/zcc_parser.h | 6 ++++++ wadsrc/static/zscript/constants.txt | 15 +++++++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/zscript/ast.cpp b/src/zscript/ast.cpp index f80498738e..1f416f7ade 100644 --- a/src/zscript/ast.cpp +++ b/src/zscript/ast.cpp @@ -54,7 +54,12 @@ static const char *BuiltInTypeNames[] = "vector3", "vector4", "name", - "usertype" + "color", + "state", + "sound", + + "usertype", + }; class FLispString @@ -767,6 +772,7 @@ static void PrintFuncParamDecl(FLispString &out, ZCC_TreeNode *node) PrintNodes(out, dnode->Type); out.AddName(dnode->Name); out.AddHex(dnode->Flags); + PrintNodes(out, dnode->Default); out.Close(); } diff --git a/src/zscript/zcc-parse.lemon b/src/zscript/zcc-parse.lemon index 09fadedb49..1861463f90 100644 --- a/src/zscript/zcc-parse.lemon +++ b/src/zscript/zcc-parse.lemon @@ -188,6 +188,7 @@ class_ancestry(X) ::= COLON dottable_id(A). { X = A; /*X-overwrites-A*/ } class_flags(X) ::= . { X.Flags = 0; X.Replaces = NULL; } class_flags(X) ::= class_flags(A) ABSTRACT. { X.Flags = A.Flags | ZCC_Abstract; X.Replaces = A.Replaces; } class_flags(X) ::= class_flags(A) NATIVE. { X.Flags = A.Flags | ZCC_Native; X.Replaces = A.Replaces; } +class_flags(X) ::= class_flags(A) ACTION. { X.Flags = A.Flags | ZCC_Action; X.Replaces = A.Replaces; } class_flags(X) ::= class_flags(A) REPLACES dottable_id(B). { X.Flags = A.Flags; X.Replaces = B; } /*----- Dottable Identifier -----*/ @@ -585,6 +586,9 @@ type_name1(X) ::= DOUBLE(T). { X.Int = ZCC_Float64; X.SourceLoc = T.SourceLoc type_name1(X) ::= STRING(T). { X.Int = ZCC_String; X.SourceLoc = T.SourceLoc; } type_name1(X) ::= VECTOR(T) vector_size(A). { X.Int = A.Int; X.SourceLoc = T.SourceLoc; } type_name1(X) ::= NAME(T). { X.Int = ZCC_Name; X.SourceLoc = T.SourceLoc; } +type_name1(X) ::= SOUND(T). { X.Int = ZCC_Sound; X.SourceLoc = T.SourceLoc; } +type_name1(X) ::= STATE(T). { X.Int = ZCC_State; X.SourceLoc = T.SourceLoc; } +type_name1(X) ::= COLOR(T). { X.Int = ZCC_Color; X.SourceLoc = T.SourceLoc; } type_name(X) ::= type_name1(A). { @@ -841,6 +845,17 @@ func_params(X) ::= . /* empty */ { X = NULL; } func_params(X) ::= VOID. { X = NULL; } func_params(X) ::= func_param_list(X). +func_params(X) ::= func_param_list(A) COMMA ELLIPSIS. +{ + NEW_AST_NODE(FuncParamDecl,parm,stat->sc->GetMessageLine()); + parm->Type = nullptr; + parm->Name = NAME_None; + parm->Flags = 0; + parm->Default = nullptr; + X = A; /*X-overwrites-A*/ + X->AppendSibling(parm); +} + func_param_list(X) ::= func_param(X). func_param_list(X) ::= func_param_list(A) COMMA func_param(B). { X = A; /*X-overwrites-A*/ X->AppendSibling(B); } @@ -850,6 +865,17 @@ func_param(X) ::= func_param_flags(A) type(B) IDENTIFIER(C). parm->Type = B; parm->Name = C.Name(); parm->Flags = A.Int; + parm->Default = nullptr; + X = parm; +} + +func_param(X) ::= func_param_flags(A) type(B) IDENTIFIER(C) EQ expr(D). +{ + NEW_AST_NODE(FuncParamDecl,parm,A.SourceLoc ? A.SourceLoc : B->SourceLoc); + parm->Type = B; + parm->Name = C.Name(); + parm->Flags = A.Int; + parm->Default = D; X = parm; } diff --git a/src/zscript/zcc_parser.cpp b/src/zscript/zcc_parser.cpp index ba08f91eff..ce2dc98e74 100644 --- a/src/zscript/zcc_parser.cpp +++ b/src/zscript/zcc_parser.cpp @@ -88,6 +88,7 @@ static void InitTokenMap() TOKENDEF (TK_LtGtEq, ZCC_LTGTEQ); TOKENDEF (TK_Is, ZCC_IS); TOKENDEF (TK_DotDot, ZCC_DOTDOT); + TOKENDEF (TK_Ellipsis, ZCC_ELLIPSIS); TOKENDEF ('|', ZCC_OR); TOKENDEF ('^', ZCC_XOR); TOKENDEF ('&', ZCC_AND); @@ -112,6 +113,7 @@ static void InitTokenMap() TOKENDEF (TK_Class, ZCC_CLASS); TOKENDEF (TK_Abstract, ZCC_ABSTRACT); TOKENDEF (TK_Native, ZCC_NATIVE); + TOKENDEF (TK_Action, ZCC_ACTION); TOKENDEF (TK_Replaces, ZCC_REPLACES); TOKENDEF (TK_Static, ZCC_STATIC); TOKENDEF (TK_Private, ZCC_PRIVATE); @@ -172,6 +174,9 @@ static void InitTokenMap() TOKENDEF (TK_Loop, ZCC_LOOP); TOKENDEF (TK_Goto, ZCC_GOTO); TOKENDEF (TK_States, ZCC_STATES); + TOKENDEF (TK_State, ZCC_STATE); + TOKENDEF (TK_Color, ZCC_COLOR); + TOKENDEF (TK_Sound, ZCC_SOUND); TOKENDEF (TK_Identifier, ZCC_IDENTIFIER); TOKENDEF (TK_StringConst, ZCC_STRCONST); diff --git a/src/zscript/zcc_parser.h b/src/zscript/zcc_parser.h index 7500185f80..5d5c85827a 100644 --- a/src/zscript/zcc_parser.h +++ b/src/zscript/zcc_parser.h @@ -119,6 +119,11 @@ enum EZCCBuiltinType ZCC_Vector3, ZCC_Vector4, ZCC_Name, + + ZCC_Color, // special types for ZDoom. + ZCC_State, + ZCC_Sound, + ZCC_UserType, ZCC_NUM_BUILT_IN_TYPES @@ -437,6 +442,7 @@ struct ZCC_LocalVarStmt : ZCC_Statement struct ZCC_FuncParamDecl : ZCC_TreeNode { ZCC_Type *Type; + ZCC_Expression *Default; ENamedName Name; int Flags; }; diff --git a/wadsrc/static/zscript/constants.txt b/wadsrc/static/zscript/constants.txt index 12604234dd..aa72ee8ec1 100644 --- a/wadsrc/static/zscript/constants.txt +++ b/wadsrc/static/zscript/constants.txt @@ -804,3 +804,18 @@ enum ERenderStyle STYLE_AddStencil, STYLE_AddShaded, }; + +// Type definition for the implicit 'callingstate' parameter that gets passed to action functions. +enum EStateType +{ + STATE_Actor, + STATE_Psprite, + STATE_StateChain, +} + +struct FStateParamInfo +{ + state mCallingState; + EStateType mStateType; + int mPSPIndex; +}