2010-06-06 04:15:28 +00:00
|
|
|
%include
|
|
|
|
{
|
2010-06-06 21:04:55 +00:00
|
|
|
// Allocates a new AST node off the parse state's arena.
|
2013-08-29 03:59:03 +00:00
|
|
|
#define NEW_AST_NODE(type,name,tok) \
|
|
|
|
ZCC_##type *name = static_cast<ZCC_##type *>(InitNode(sizeof(ZCC_##type), AST_##type, stat)); \
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetNodeLine(ZCC_TreeNode *name, ZCC_TreeNode *node)
|
|
|
|
{
|
|
|
|
name->SourceLoc = node->SourceLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetNodeLine(ZCC_TreeNode *name, int line)
|
|
|
|
{
|
|
|
|
name->SourceLoc = line;
|
|
|
|
}
|
2010-06-06 21:04:55 +00:00
|
|
|
|
|
|
|
// If a is non-null, appends b to a. Otherwise, sets a to b.
|
|
|
|
#define SAFE_APPEND(a,b) \
|
|
|
|
if (a == NULL) a = b; else a->AppendSibling(b);
|
2010-06-06 04:15:28 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 04:04:13 +00:00
|
|
|
%token_prefix ZCC_
|
|
|
|
%token_type { ZCCToken }
|
|
|
|
%token_destructor {} // just to avoid a compiler warning
|
|
|
|
%name ZCCParse
|
2010-06-06 04:15:28 +00:00
|
|
|
%extra_argument { ZCCParseState *stat }
|
2010-03-19 04:04:13 +00:00
|
|
|
%syntax_error
|
|
|
|
{
|
|
|
|
FString unexpected, expecting;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int stateno = yypParser->yystack[yypParser->yyidx].stateno;
|
|
|
|
|
|
|
|
unexpected << "Unexpected " << ZCCTokenName(yymajor);
|
|
|
|
|
|
|
|
// Determine all the terminals that the parser would have accepted at this point
|
|
|
|
// (see yy_find_shift_action). This list can get quite long. Is it worthwhile to
|
|
|
|
// print it when not debugging the grammar, or would that be too confusing to
|
|
|
|
// the average user?
|
|
|
|
if (stateno < YY_SHIFT_MAX && (i = yy_shift_ofst[stateno])!=YY_SHIFT_USE_DFLT)
|
|
|
|
{
|
|
|
|
for (int j = 1; j < YYERRORSYMBOL; ++j)
|
|
|
|
{
|
|
|
|
int k = i + j;
|
|
|
|
if (k >= 0 && k < YY_SZ_ACTTAB && yy_lookahead[k] == j)
|
|
|
|
{
|
|
|
|
expecting << (expecting.IsEmpty() ? "Expecting " : " or ") << ZCCTokenName(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-06 04:15:28 +00:00
|
|
|
stat->sc.ScriptMessage("%s\n%s\n", unexpected.GetChars(), expecting.GetChars());
|
2010-03-19 04:04:13 +00:00
|
|
|
}
|
2010-06-06 04:15:28 +00:00
|
|
|
%parse_accept { stat->sc.ScriptMessage("input accepted\n"); }
|
2010-03-19 04:04:13 +00:00
|
|
|
%parse_failure { /**failed = true;*/ }
|
|
|
|
|
|
|
|
%nonassoc EQ MULEQ DIVEQ MODEQ ADDEQ SUBEQ LSHEQ RSHEQ ANDEQ OREQ XOREQ.
|
|
|
|
%right QUESTION COLON.
|
|
|
|
%left OROR.
|
|
|
|
%left ANDAND.
|
|
|
|
%left EQEQ NEQ APPROXEQ.
|
|
|
|
%left LT GT LTEQ GTEQ LTGTEQ IS.
|
|
|
|
%left DOTDOT.
|
|
|
|
%left OR. /* Note that this is like the Ruby precedence for these */
|
|
|
|
%left XOR. /* three operators and not the C precedence, since */
|
|
|
|
%left AND. /* they are higher priority than the comparisons. */
|
|
|
|
%left LSH RSH.
|
|
|
|
%left SUB ADD.
|
|
|
|
%left MUL DIV MOD CROSSPROD DOTPROD.
|
|
|
|
%left POW.
|
|
|
|
%right UNARY ADDADD SUBSUB.
|
|
|
|
%left DOT LPAREN LBRACKET.
|
|
|
|
%left SCOPE.
|
|
|
|
|
2010-09-07 04:11:32 +00:00
|
|
|
%type declarator {ZCC_Declarator *}
|
|
|
|
%type declarator_no_fun {ZCC_Declarator *}
|
|
|
|
%type opt_func_body {ZCC_CompoundStmt *}
|
|
|
|
%type function_body {ZCC_CompoundStmt *}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2012-07-20 02:54:51 +00:00
|
|
|
main ::= translation_unit(A). { stat->TopNode = A; stat->sc.ScriptMessage("Parse complete\n"); }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2012-07-20 02:54:51 +00:00
|
|
|
%type translation_unit {ZCC_TreeNode *}
|
|
|
|
translation_unit(X) ::= . { X = NULL; }
|
|
|
|
translation_unit(X) ::= translation_unit(A) external_declaration(B). { SAFE_APPEND(A,B); X = A; }
|
|
|
|
translation_unit(X) ::= translation_unit(A) EOF. { X = A; }
|
|
|
|
translation_unit(X) ::= error. { X = NULL; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2012-07-20 02:54:51 +00:00
|
|
|
%type external_declaration {ZCC_TreeNode *}
|
|
|
|
external_declaration(X) ::= class_definition(A). { X = A; }
|
2013-08-25 01:28:26 +00:00
|
|
|
external_declaration(X) ::= struct_def(A). { X = A; }
|
|
|
|
external_declaration(X) ::= enum_def(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* Optional bits. */
|
|
|
|
opt_semicolon ::= .
|
|
|
|
opt_semicolon ::= SEMICOLON.
|
|
|
|
|
|
|
|
opt_comma ::= .
|
|
|
|
opt_comma ::= COMMA.
|
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
%type opt_expr{ZCC_Expression *}
|
|
|
|
opt_expr(X) ::= .
|
|
|
|
{
|
|
|
|
X = NULL;
|
|
|
|
}
|
|
|
|
opt_expr(X) ::= expr(A).
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
/************ Class Definition ************/
|
|
|
|
/* Can only occur at global scope. */
|
|
|
|
|
|
|
|
%type class_definition{ZCC_Class *}
|
|
|
|
%type class_head{ZCC_Class *}
|
2010-09-08 04:32:31 +00:00
|
|
|
%type class_innards{ZCC_TreeNode *}
|
|
|
|
%type class_member{ZCC_TreeNode *}
|
2010-06-06 21:04:55 +00:00
|
|
|
%type class_body{ZCC_TreeNode *}
|
|
|
|
|
|
|
|
class_definition(X) ::= class_head(A) class_body(B).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2010-06-06 21:04:55 +00:00
|
|
|
A->Body = B;
|
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
class_head(X) ::= CLASS(T) dottable_id(A) class_ancestry(B) class_flags(C).
|
2010-06-06 21:04:55 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(Class,head,T);
|
2010-06-06 21:04:55 +00:00
|
|
|
head->ClassName = A;
|
|
|
|
head->ParentName = B;
|
|
|
|
head->Flags = C.Flags;
|
|
|
|
head->Replaces = C.Replaces;
|
|
|
|
X = head;
|
|
|
|
}
|
|
|
|
|
|
|
|
%type class_ancestry{ZCC_Identifier *}
|
|
|
|
class_ancestry(X) ::= . { X = NULL; }
|
|
|
|
class_ancestry(X) ::= COLON dottable_id(A). { X = A; }
|
|
|
|
|
|
|
|
%type class_flags{ClassFlagsBlock}
|
2010-09-08 04:32:31 +00:00
|
|
|
%include {
|
2010-06-06 21:04:55 +00:00
|
|
|
struct ClassFlagsBlock {
|
|
|
|
VM_UWORD Flags;
|
|
|
|
ZCC_Identifier *Replaces;
|
|
|
|
};
|
2010-06-06 04:15:28 +00:00
|
|
|
}
|
2010-06-06 21:04:55 +00:00
|
|
|
class_flags(X) ::= . { X.Flags = 0; X.Replaces = NULL; }
|
|
|
|
class_flags(X) ::= class_flags(A) ABSTRACT. { X.Flags = A.Flags | 0/*FIXME*/; X.Replaces = A.Replaces; }
|
|
|
|
class_flags(X) ::= class_flags(A) NATIVE. { X.Flags = A.Flags | 0/*FIXME*/; X.Replaces = A.Replaces; }
|
|
|
|
class_flags(X) ::= class_flags(A) REPLACES dottable_id(B). { X.Flags = A.Flags; X.Replaces = B; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
/*----- Dottable Identifier -----*/
|
|
|
|
// This can be either a single identifier or two identifiers connected by a .
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
%type dottable_id{ZCC_Identifier *}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
dottable_id(X) ::= IDENTIFIER(A).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(Identifier,id,A);
|
2010-06-06 21:04:55 +00:00
|
|
|
id->Id = A.Name();
|
|
|
|
X = id;
|
|
|
|
}
|
2010-07-11 18:53:41 +00:00
|
|
|
dottable_id(X) ::= dottable_id(A) DOT IDENTIFIER(B).
|
2010-06-06 21:04:55 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(Identifier,id2,A);
|
2010-06-06 21:04:55 +00:00
|
|
|
id2->Id = B.Name();
|
2010-07-11 18:53:41 +00:00
|
|
|
A->AppendSibling(id2);
|
|
|
|
X = A;
|
2010-06-06 21:04:55 +00:00
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
/*------ Class Body ------*/
|
|
|
|
// Body is a list of:
|
|
|
|
// * variable definitions
|
|
|
|
// * function definitions
|
|
|
|
// * enum definitions
|
|
|
|
// * struct definitions
|
|
|
|
// * state definitions
|
|
|
|
// * constants
|
|
|
|
// * defaults
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-09-08 04:32:31 +00:00
|
|
|
class_body(X) ::= SEMICOLON class_innards(A) EOF. { X = A; }
|
|
|
|
class_body(X) ::= LBRACE class_innards(A) RBRACE. { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-09-08 04:32:31 +00:00
|
|
|
class_innards(X) ::= . { X = NULL; }
|
|
|
|
class_innards(X) ::= class_innards(A) class_member(B). { SAFE_APPEND(A,B); X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
%type struct_def{ZCC_Struct *}
|
|
|
|
%type enum_def {ZCC_Enum *}
|
|
|
|
%type states_def {ZCC_States *}
|
2010-08-10 04:25:52 +00:00
|
|
|
%type const_def {ZCC_ConstantDef *}
|
2010-06-06 21:04:55 +00:00
|
|
|
|
2010-09-08 04:32:31 +00:00
|
|
|
class_member(X) ::= declarator(A). { X = A; }
|
|
|
|
class_member(X) ::= enum_def(A). { X = A; }
|
|
|
|
class_member(X) ::= struct_def(A). { X = A; }
|
|
|
|
class_member(X) ::= states_def(A). { X = A; }
|
|
|
|
class_member(X) ::= default_def(A). { X = A; }
|
|
|
|
class_member(X) ::= const_def(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
/*----- Struct Definition -----*/
|
2010-09-07 04:11:32 +00:00
|
|
|
/* Structs can define variables and enums. */
|
2010-06-06 21:04:55 +00:00
|
|
|
|
2013-08-25 01:32:59 +00:00
|
|
|
%type opt_struct_body{ZCC_TreeNode *}
|
2010-06-06 21:04:55 +00:00
|
|
|
%type struct_body{ZCC_TreeNode *}
|
|
|
|
%type struct_member{ZCC_TreeNode *}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
struct_def(X) ::= STRUCT(T) IDENTIFIER(A) LBRACE opt_struct_body(B) RBRACE opt_semicolon.
|
2010-06-06 21:04:55 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(Struct,def,T);
|
2010-06-06 21:04:55 +00:00
|
|
|
def->StructName = A.Name();
|
|
|
|
def->Body = B;
|
|
|
|
X = def;
|
|
|
|
}
|
|
|
|
|
2013-08-25 01:32:59 +00:00
|
|
|
opt_struct_body(X) ::= . { X = NULL; }
|
|
|
|
opt_struct_body(X) ::= struct_body(A). { X = A; }
|
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
struct_body(X) ::= error. { X = NULL; }
|
|
|
|
struct_body(X) ::= struct_member(A). { X = A; }
|
|
|
|
struct_body(X) ::= struct_member(A) struct_body(B). { X = A; A->AppendSibling(B); }
|
|
|
|
|
|
|
|
struct_member(X) ::= declarator_no_fun(A). { X = A; }
|
|
|
|
struct_member(X) ::= enum_def(A). { X = A; }
|
|
|
|
|
|
|
|
|
|
|
|
/*----- Enum Definition -----*/
|
2010-03-19 04:04:13 +00:00
|
|
|
/* Enumerators are lists of named integers. */
|
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
%type enum_list {ZCC_EnumNode *}
|
2013-08-25 01:32:59 +00:00
|
|
|
%type opt_enum_list {ZCC_EnumNode *}
|
2010-06-06 21:04:55 +00:00
|
|
|
%type enumerator {ZCC_EnumNode *}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
enum_def(X) ::= ENUM(T) IDENTIFIER(A) enum_type(B) LBRACE opt_enum_list(C) RBRACE opt_semicolon.
|
2010-06-06 21:04:55 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(Enum,def,T);
|
2010-06-06 21:04:55 +00:00
|
|
|
def->EnumName = A.Name();
|
2013-08-29 03:59:03 +00:00
|
|
|
def->EnumType = (EZCCBuiltinType)B.Int;
|
2010-06-06 21:04:55 +00:00
|
|
|
def->Elements = C;
|
|
|
|
X = def;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
enum_type(X) ::= . { X.Int = ZCC_IntAuto; X.SourceLoc = stat->sc.GetMessageLine(); }
|
2010-06-06 21:04:55 +00:00
|
|
|
enum_type(X) ::= COLON int_type(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
enum_list(X) ::= error. { X = NULL; }
|
|
|
|
enum_list(X) ::= enumerator(A). { X = A; }
|
|
|
|
enum_list(X) ::= enum_list(A) COMMA enumerator(B). { X = A; A->AppendSibling(B); }
|
|
|
|
|
2013-08-25 01:32:59 +00:00
|
|
|
opt_enum_list(X) ::= . { X = NULL; }
|
|
|
|
opt_enum_list(X) ::= enum_list(A) opt_comma. { X = A; }
|
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
enumerator(X) ::= IDENTIFIER(A).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(EnumNode,node,A);
|
2010-06-06 21:04:55 +00:00
|
|
|
node->ElemName = A.Name();
|
|
|
|
node->ElemValue = NULL;
|
|
|
|
X = node;
|
|
|
|
}
|
|
|
|
enumerator(X) ::= IDENTIFIER(A) EQ expr(B). /* Expression must be constant. */
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(EnumNode,node,A);
|
2010-06-06 21:04:55 +00:00
|
|
|
node->ElemName = A.Name();
|
|
|
|
node->ElemValue = B;
|
|
|
|
X = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************ States ************/
|
|
|
|
|
|
|
|
%type states_body {ZCC_StatePart *}
|
|
|
|
%type state_line {ZCC_StatePart *}
|
|
|
|
%type state_label {ZCC_StatePart *}
|
|
|
|
%type state_flow {ZCC_StatePart *}
|
2010-07-11 18:53:41 +00:00
|
|
|
%type state_flow_type {ZCC_StatePart *}
|
|
|
|
%type state_goto_offset {ZCC_Expression *}
|
|
|
|
%type state_action {ZCC_TreeNode *}
|
|
|
|
%type state_call {ZCC_ExprFuncCall *}
|
2013-07-24 00:34:21 +00:00
|
|
|
%type state_call_params {ZCC_FuncParm *}
|
2010-07-11 18:53:41 +00:00
|
|
|
|
2010-09-08 04:32:31 +00:00
|
|
|
%include {
|
|
|
|
struct StateOpts {
|
2010-07-11 18:53:41 +00:00
|
|
|
ZCC_Expression *Offset;
|
|
|
|
bool Bright;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
%type state_opts {StateOpts}
|
2010-06-06 21:04:55 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
states_def(X) ::= STATES(T) scanner_mode LBRACE states_body(A) RBRACE.
|
2010-06-06 21:04:55 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(States,def,T);
|
2010-06-06 21:04:55 +00:00
|
|
|
def->Body = A;
|
|
|
|
X = def;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* We use a special scanner mode to allow for sprite names and frame characters
|
|
|
|
* to not be quoted even if they contain special characters. The scanner_mode
|
|
|
|
* nonterminal is used to enter this mode. The scanner automatically leaves it
|
|
|
|
* upon pre-defined conditions. See the comments by FScanner::SetStateMode().
|
|
|
|
*
|
|
|
|
* Note that rules are reduced *after* one token of lookahead has been
|
|
|
|
* consumed, so this nonterminal must be placed one token before we want it to
|
|
|
|
* take effect. For example, in states_def above, the scanner mode will be
|
|
|
|
* set immediately after LBRACE is consumed, rather than immediately after
|
|
|
|
* STATES is consumed.
|
|
|
|
*/
|
2010-06-06 04:15:28 +00:00
|
|
|
scanner_mode ::= . { stat->sc.SetStateMode(true); }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 21:04:55 +00:00
|
|
|
states_body(X) ::= . { X = NULL; }
|
|
|
|
states_body(X) ::= error. { X = NULL; }
|
|
|
|
states_body(X) ::= states_body(A) state_line(B). { SAFE_APPEND(A,B); X = A; }
|
|
|
|
states_body(X) ::= states_body(A) state_label(B). { SAFE_APPEND(A,B); X = A; }
|
|
|
|
states_body(X) ::= states_body(A) state_flow(B). { SAFE_APPEND(A,B); X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-07-11 18:53:41 +00:00
|
|
|
state_label(X) ::= NWS(A) COLON.
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(StateLabel, label, A);
|
2010-07-11 18:53:41 +00:00
|
|
|
label->Label = A.Name();
|
|
|
|
X = label;
|
|
|
|
}
|
|
|
|
|
|
|
|
state_flow(X) ::= state_flow_type(A) scanner_mode SEMICOLON. { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
state_flow_type(X) ::= STOP(A). { NEW_AST_NODE(StateStop, flow, A); X = flow; }
|
|
|
|
state_flow_type(X) ::= WAIT(A). { NEW_AST_NODE(StateWait, flow, A); X = flow; }
|
|
|
|
state_flow_type(X) ::= FAIL(A). { NEW_AST_NODE(StateFail, flow, A); X = flow; }
|
|
|
|
state_flow_type(X) ::= LOOP(A). { NEW_AST_NODE(StateLoop, flow, A); X = flow; }
|
|
|
|
state_flow_type(X) ::= GOTO(T) dottable_id(A) state_goto_offset(B).
|
2010-07-11 18:53:41 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(StateGoto, flow, T);
|
2010-07-11 18:53:41 +00:00
|
|
|
flow->Label = A;
|
|
|
|
flow->Offset = B;
|
|
|
|
X = flow;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-07-11 18:53:41 +00:00
|
|
|
state_goto_offset(X) ::= . { X = NULL; }
|
|
|
|
state_goto_offset(X) ::= PLUS expr(A). { X = A; } /* Must evaluate to a non-negative integer constant. */
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-07-11 18:53:41 +00:00
|
|
|
state_line(X) ::= NWS(A) NWS(B) expr state_opts(C) state_action(D).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(StateLine, line, A);
|
2010-07-11 18:53:41 +00:00
|
|
|
const char *sprite = FName(A.Name()).GetChars();
|
|
|
|
if (strlen(sprite) != 4)
|
|
|
|
{
|
|
|
|
Printf("Sprite name '%s' must be four characters", sprite);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(line->Sprite, sprite, 4);
|
|
|
|
}
|
|
|
|
line->Frames = stat->Strings.Alloc(FName(B.Name()).GetChars());
|
|
|
|
line->bBright = C.Bright;
|
|
|
|
line->Offset = C.Offset;
|
|
|
|
line->Action = D;
|
|
|
|
X = line;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-07-11 18:53:41 +00:00
|
|
|
state_opts(X) ::= . { StateOpts opts; opts.Offset = NULL; opts.Bright = false; X = opts; }
|
|
|
|
state_opts(X) ::= state_opts(A) BRIGHT. { A.Bright = true; X = A; }
|
|
|
|
state_opts(X) ::= state_opts(A) OFFSET LPAREN expr(B) COMMA expr(C) RPAREN. { A.Offset = B; B->AppendSibling(C); X = A; }
|
|
|
|
state_opts(X) ::= state_opts(A) LIGHT LPAREN light_list RPAREN. { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
light_list ::= STRCONST.
|
|
|
|
light_list ::= light_list COMMA STRCONST.
|
|
|
|
|
|
|
|
/* A state action can be either a compound statement or a single action function call. */
|
2010-07-11 18:53:41 +00:00
|
|
|
state_action(X) ::= LBRACE statement_list(A) scanner_mode RBRACE. { X = A; }
|
|
|
|
state_action(X) ::= LBRACE error scanner_mode RBRACE. { X = NULL; }
|
|
|
|
state_action(X) ::= state_call(A) scanner_mode SEMICOLON. { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-07-11 18:53:41 +00:00
|
|
|
state_call(X) ::= . { X = NULL; }
|
2013-07-24 00:34:21 +00:00
|
|
|
state_call(X) ::= IDENTIFIER(A) state_call_params(B).
|
2010-07-11 18:53:41 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ExprFuncCall, expr, A);
|
|
|
|
NEW_AST_NODE(ExprID, func, A);
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-07-11 18:53:41 +00:00
|
|
|
func->Operation = PEX_ID;
|
|
|
|
func->Identifier = A.Name();
|
|
|
|
expr->Operation = PEX_FuncCall;
|
|
|
|
expr->Function = func;
|
|
|
|
expr->Parameters = B;
|
|
|
|
X = expr;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-07-24 00:34:21 +00:00
|
|
|
state_call_params(X) ::= . { X = NULL; }
|
|
|
|
state_call_params(X) ::= LPAREN func_expr_list(A) RPAREN. { X = A; }
|
|
|
|
|
2010-03-19 04:04:13 +00:00
|
|
|
/* Definition of a default class instance. */
|
2010-09-08 04:32:31 +00:00
|
|
|
%type default_def {ZCC_CompoundStmt *}
|
|
|
|
default_def(X) ::= DEFAULT compound_statement(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* Type names */
|
2010-09-07 04:11:32 +00:00
|
|
|
%type type_name {ZCC_BasicType *}
|
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
int_type(X) ::= SBYTE(T). { X.Int = ZCC_SInt8; X.SourceLoc = T.SourceLoc; }
|
|
|
|
int_type(X) ::= BYTE(T). { X.Int = ZCC_UInt8; X.SourceLoc = T.SourceLoc; }
|
|
|
|
int_type(X) ::= SHORT(T). { X.Int = ZCC_SInt16; X.SourceLoc = T.SourceLoc; }
|
|
|
|
int_type(X) ::= USHORT(T). { X.Int = ZCC_UInt16; X.SourceLoc = T.SourceLoc; }
|
|
|
|
int_type(X) ::= INT(T). { X.Int = ZCC_SInt32; X.SourceLoc = T.SourceLoc; }
|
|
|
|
int_type(X) ::= UINT(T). { X.Int = ZCC_UInt32; X.SourceLoc = T.SourceLoc; }
|
2010-09-07 04:11:32 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
type_name1(X) ::= BOOL(T). { X.Int = ZCC_Bool; X.SourceLoc = T.SourceLoc; }
|
2010-09-07 04:11:32 +00:00
|
|
|
type_name1(X) ::= int_type(A). { X = A; }
|
2013-08-29 03:59:03 +00:00
|
|
|
type_name1(X) ::= FLOAT(T). { X.Int = ZCC_FloatAuto; X.SourceLoc = T.SourceLoc; }
|
|
|
|
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; }
|
2010-09-07 04:11:32 +00:00
|
|
|
|
|
|
|
type_name(X) ::= type_name1(A).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(BasicType, type, A);
|
|
|
|
type->Type = (EZCCBuiltinType)A.Int;
|
2010-09-07 04:11:32 +00:00
|
|
|
type->UserType = NULL;
|
|
|
|
X = type;
|
|
|
|
}
|
|
|
|
type_name(X) ::= IDENTIFIER(A). /* User-defined type (struct, enum, or class) */
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(BasicType, type, A);
|
|
|
|
NEW_AST_NODE(Identifier, id, A);
|
2010-09-07 04:11:32 +00:00
|
|
|
type->Type = ZCC_UserType;
|
|
|
|
type->UserType = id;
|
2010-09-08 04:32:31 +00:00
|
|
|
id->Id = A.Name();
|
2010-09-07 04:11:32 +00:00
|
|
|
X = type;
|
|
|
|
}
|
2013-07-20 04:25:01 +00:00
|
|
|
type_name(X) ::= DOT dottable_id(A).
|
2010-09-07 04:11:32 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(BasicType, type, A);
|
2010-09-07 04:11:32 +00:00
|
|
|
type->Type = ZCC_UserType;
|
|
|
|
type->UserType = A;
|
|
|
|
X = type;
|
2010-06-06 04:15:28 +00:00
|
|
|
}
|
|
|
|
|
2010-09-07 04:11:32 +00:00
|
|
|
/* Vectors can be 2, 3, or 4 entries long. Default is a 3D vector.
|
|
|
|
* (Well, actually, I'm not sure if 4D ones are going to happen
|
|
|
|
* straight away.)
|
|
|
|
*/
|
2013-08-29 03:59:03 +00:00
|
|
|
vector_size(X) ::= . { X.Int = ZCC_Vector3; X.SourceLoc = stat->sc.GetMessageLine(); }
|
2010-09-07 04:11:32 +00:00
|
|
|
vector_size(X) ::= LT INTCONST(A) GT.
|
|
|
|
{
|
|
|
|
if (A.Int >= 2 && A.Int <= 4)
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
X.Int = ZCC_Vector2 + A.Int - 2;
|
2010-09-07 04:11:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
X.Int = ZCC_Vector3;
|
2010-09-07 04:11:32 +00:00
|
|
|
stat->sc.ScriptMessage("Invalid vector size %d\n", A.Int);
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
X.SourceLoc = A.SourceLoc;
|
2010-09-07 04:11:32 +00:00
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* Type names can also be used as identifiers in contexts where type names
|
|
|
|
* are not normally allowed. */
|
|
|
|
%fallback IDENTIFIER
|
2013-08-03 02:54:50 +00:00
|
|
|
SBYTE BYTE SHORT USHORT INT UINT BOOL FLOAT DOUBLE STRING VECTOR NAME MAP ARRAY VOID.
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* Aggregate types */
|
2010-08-10 04:25:52 +00:00
|
|
|
%type aggregate_type {ZCC_Type *}
|
|
|
|
%type type {ZCC_Type *}
|
|
|
|
%type type_list {ZCC_Type *}
|
|
|
|
%type type_list_or_void {ZCC_Type *}
|
|
|
|
%type type_or_array {ZCC_Type *}
|
2010-09-07 04:11:32 +00:00
|
|
|
%type class_restrictor {ZCC_Identifier *}
|
2010-08-10 04:25:52 +00:00
|
|
|
%type array_size{ZCC_Expression *}
|
2013-07-20 04:25:01 +00:00
|
|
|
%type array_size_expr{ZCC_Expression *}
|
2010-06-06 04:15:28 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
aggregate_type(X) ::= MAP(T) LT type_or_array(A) COMMA type_or_array(B) GT. /* Hash table */
|
2010-09-07 04:11:32 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(MapType,map,T);
|
2010-09-07 04:11:32 +00:00
|
|
|
map->KeyType = A;
|
|
|
|
map->ValueType = B;
|
|
|
|
X = map;
|
|
|
|
}
|
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
aggregate_type(X) ::= ARRAY(T) LT type_or_array(A) GT. /* TArray<type> */
|
2010-09-07 04:11:32 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(DynArrayType,arr,T);
|
2010-09-08 04:32:31 +00:00
|
|
|
arr->ElementType = A;
|
|
|
|
X = arr;
|
2010-09-07 04:11:32 +00:00
|
|
|
}
|
2010-06-06 04:15:28 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
aggregate_type(X) ::= CLASS(T) class_restrictor(A). /* class<type> */
|
2010-09-07 04:11:32 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ClassType,cls,T);
|
2010-09-07 04:11:32 +00:00
|
|
|
cls->Restriction = A;
|
|
|
|
X = cls;
|
|
|
|
}
|
|
|
|
class_restrictor(X) ::= . { X = NULL; }
|
|
|
|
class_restrictor(X) ::= LT dottable_id(A) GT. { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-08-10 04:25:52 +00:00
|
|
|
type(X) ::= type_name(A). { X = A; A->ArraySize = NULL; }
|
|
|
|
type(X) ::= aggregate_type(A). { X = A; A->ArraySize = NULL; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-08-10 04:25:52 +00:00
|
|
|
type_or_array(X) ::= type(A). { X = A; }
|
|
|
|
type_or_array(X) ::= type(A) array_size(B). { X = A; A->ArraySize = B; }
|
2010-06-06 04:15:28 +00:00
|
|
|
|
2010-08-10 04:25:52 +00:00
|
|
|
type_list(X) ::= type_or_array(A). { X = A; }/* A comma-separated list of types */
|
|
|
|
type_list(X) ::= type_list(A) COMMA type_or_array(B). { X = A; A->AppendSibling(B); }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-08-10 04:25:52 +00:00
|
|
|
type_list_or_void(X) ::= VOID. { X = NULL; }
|
|
|
|
type_list_or_void(X) ::= type_list(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-07-20 04:25:01 +00:00
|
|
|
array_size_expr(X) ::= LBRACKET opt_expr(A) RBRACKET.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
|
|
|
if (A == NULL)
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(Expression,nil,A);
|
2010-08-10 04:25:52 +00:00
|
|
|
nil->Operation = PEX_Nil;
|
2013-09-11 01:50:21 +00:00
|
|
|
nil->Type = NULL;
|
2010-06-06 04:15:28 +00:00
|
|
|
X = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
}
|
2013-07-20 04:25:01 +00:00
|
|
|
array_size(X) ::= array_size_expr(A).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-07-20 04:25:01 +00:00
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
array_size(X) ::= array_size(A) array_size_expr(B).
|
|
|
|
{
|
|
|
|
A->AppendSibling(B);
|
2010-06-06 04:15:28 +00:00
|
|
|
X = A;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-09-07 04:11:32 +00:00
|
|
|
%include
|
|
|
|
{
|
|
|
|
struct VarOrFun
|
|
|
|
{
|
|
|
|
ZCC_VarName *VarNames;
|
|
|
|
ZCC_FuncParamDecl *FuncParams;
|
2010-09-08 04:32:31 +00:00
|
|
|
ZCC_CompoundStmt *FuncBody;
|
2010-09-07 04:11:32 +00:00
|
|
|
ENamedName FuncName;
|
|
|
|
int FuncFlags;
|
2013-08-29 03:59:03 +00:00
|
|
|
int SourceLoc;
|
2010-09-07 04:11:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
%type variables_or_function {VarOrFun}
|
|
|
|
|
|
|
|
/* Multiple type names are only valid for functions. */
|
|
|
|
declarator(X) ::= decl_flags(A) type_list_or_void(B) variables_or_function(C).
|
|
|
|
{
|
2013-07-24 01:43:15 +00:00
|
|
|
if (C.FuncName == NAME_None && C.VarNames == NULL)
|
2010-09-07 04:11:32 +00:00
|
|
|
{ // An error. A message was already printed.
|
|
|
|
X = NULL;
|
|
|
|
}
|
2013-07-24 01:43:15 +00:00
|
|
|
else if (C.FuncName != NAME_None)
|
2010-09-07 04:11:32 +00:00
|
|
|
{ // A function
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(FuncDeclarator, decl, A.SourceLoc);
|
2010-09-07 04:11:32 +00:00
|
|
|
decl->Type = B;
|
|
|
|
decl->Params = C.FuncParams;
|
|
|
|
decl->Name = C.FuncName;
|
2013-08-29 03:59:03 +00:00
|
|
|
decl->Flags = A.Int | C.FuncFlags;
|
2013-07-11 04:26:56 +00:00
|
|
|
decl->Body = C.FuncBody;
|
2010-09-07 04:11:32 +00:00
|
|
|
X = decl;
|
|
|
|
}
|
2010-09-08 04:32:31 +00:00
|
|
|
else if (B != NULL && B->SiblingNext == B)
|
2010-09-07 04:11:32 +00:00
|
|
|
{ // A variable
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(VarDeclarator, decl, A.SourceLoc);
|
2010-09-07 04:11:32 +00:00
|
|
|
decl->Type = B;
|
|
|
|
decl->Names = C.VarNames;
|
2013-08-29 03:59:03 +00:00
|
|
|
decl->Flags = A.Int;
|
2010-09-07 04:11:32 +00:00
|
|
|
X = decl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // An invalid
|
|
|
|
if (B == NULL)
|
|
|
|
{
|
2010-09-08 04:32:31 +00:00
|
|
|
stat->sc.ScriptMessage("Variables may not be of type void.\n");
|
2010-09-07 04:11:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-08 04:32:31 +00:00
|
|
|
stat->sc.ScriptMessage("Variables may be of only one type.\n");
|
2010-09-07 04:11:32 +00:00
|
|
|
}
|
|
|
|
X = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
declarator_no_fun(X) ::= decl_flags(A) type(B) variable_list(C).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(VarDeclarator, decl, A.SourceLoc ? A.SourceLoc : B->SourceLoc);
|
2010-09-07 04:11:32 +00:00
|
|
|
decl->Type = B;
|
|
|
|
decl->Names = C;
|
2013-08-29 03:59:03 +00:00
|
|
|
decl->Flags = A.Int;
|
2010-09-07 04:11:32 +00:00
|
|
|
X = decl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to split it up like this to avoid parsing conflicts.
|
|
|
|
variables_or_function(X) ::= IDENTIFIER(A) LPAREN func_params(B) RPAREN func_const(C) opt_func_body(D). /* Function */
|
|
|
|
{
|
|
|
|
VarOrFun fun;
|
|
|
|
|
|
|
|
fun.VarNames = NULL;
|
|
|
|
fun.FuncParams = B;
|
2013-08-29 03:59:03 +00:00
|
|
|
fun.FuncFlags = C.Int;
|
2010-09-07 04:11:32 +00:00
|
|
|
fun.FuncName = A.Name();
|
|
|
|
fun.FuncBody = D;
|
2013-08-29 03:59:03 +00:00
|
|
|
fun.SourceLoc = A.SourceLoc;
|
2010-09-07 04:11:32 +00:00
|
|
|
X = fun;
|
|
|
|
}
|
|
|
|
variables_or_function(X) ::= variable_list(A) SEMICOLON.
|
|
|
|
{
|
|
|
|
VarOrFun var;
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-09-07 04:11:32 +00:00
|
|
|
var.VarNames = A;
|
|
|
|
var.FuncParams = NULL;
|
|
|
|
var.FuncFlags = 0;
|
|
|
|
var.FuncName = NAME_None;
|
|
|
|
var.FuncBody = NULL;
|
2013-08-29 03:59:03 +00:00
|
|
|
var.SourceLoc = A->SourceLoc;
|
2010-09-07 04:11:32 +00:00
|
|
|
X = var;
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
variables_or_function(X) ::= error SEMICOLON(T).
|
2010-09-07 04:11:32 +00:00
|
|
|
{
|
|
|
|
VarOrFun bad;
|
|
|
|
bad.VarNames = NULL;
|
|
|
|
bad.FuncParams = NULL;
|
|
|
|
bad.FuncFlags = 0;
|
|
|
|
bad.FuncName = NAME_None;
|
2010-09-08 04:32:31 +00:00
|
|
|
bad.FuncBody = NULL;
|
2013-08-29 03:59:03 +00:00
|
|
|
bad.SourceLoc = T.SourceLoc;
|
2010-09-07 04:11:32 +00:00
|
|
|
X = bad;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
/*----- Variable Names -----*/
|
|
|
|
|
|
|
|
%type variable_name{ZCC_VarName *}
|
|
|
|
%type variable_list{ZCC_VarName *}
|
|
|
|
|
|
|
|
variable_name(X) ::= IDENTIFIER(A).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(VarName,var,A);
|
2010-06-06 04:15:28 +00:00
|
|
|
var->Name = ENamedName(A.Int);
|
2013-07-24 00:59:47 +00:00
|
|
|
var->ArraySize = NULL;
|
2010-06-06 04:15:28 +00:00
|
|
|
X = var;
|
|
|
|
}
|
2013-07-24 00:59:47 +00:00
|
|
|
variable_name(X) ::= IDENTIFIER(A) array_size(B).
|
2013-07-20 04:25:01 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(VarName,var,A);
|
2013-07-20 04:25:01 +00:00
|
|
|
var->Name = ENamedName(A.Int);
|
2013-07-24 00:59:47 +00:00
|
|
|
var->ArraySize = B;
|
2013-07-20 04:25:01 +00:00
|
|
|
X = var;
|
|
|
|
}
|
2010-08-10 04:25:52 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
variable_list(X) ::= variable_name(A).
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
variable_list(X) ::= variable_list(A) COMMA variable_name(B).
|
|
|
|
{
|
|
|
|
A->AppendSibling(B);
|
|
|
|
X = A;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
decl_flags(X) ::= . { X.Int = 0; X.SourceLoc = 0; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) NATIVE(T). { X.Int = A.Int | ZCC_Native; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) STATIC(T). { X.Int = A.Int | ZCC_Static; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) PRIVATE(T). { X.Int = A.Int | ZCC_Private; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) PROTECTED(T). { X.Int = A.Int | ZCC_Protected; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) LATENT(T). { X.Int = A.Int | ZCC_Latent; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) FINAL(T). { X.Int = A.Int | ZCC_Final; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) META(T). { X.Int = A.Int | ZCC_Meta; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) ACTION(T). { X.Int = A.Int | ZCC_Action; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) READONLY(T). { X.Int = A.Int | ZCC_ReadOnly; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
decl_flags(X) ::= decl_flags(A) DEPRECATED(T). { X.Int = A.Int | ZCC_Deprecated; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
func_const(X) ::= . { X.Int = 0; X.SourceLoc = stat->sc.GetMessageLine(); }
|
|
|
|
func_const(X) ::= CONST(T). { X.Int = ZCC_FuncConst; X.SourceLoc = T.SourceLoc; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-09-07 04:11:32 +00:00
|
|
|
opt_func_body(X) ::= SEMICOLON. { X = NULL; }
|
|
|
|
opt_func_body(X) ::= function_body(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-08-10 04:25:52 +00:00
|
|
|
%type func_params {ZCC_FuncParamDecl *}
|
|
|
|
%type func_param_list {ZCC_FuncParamDecl *}
|
|
|
|
%type func_param {ZCC_FuncParamDecl *}
|
|
|
|
|
|
|
|
func_params(X) ::= . /* empty */ { X = NULL; }
|
|
|
|
func_params(X) ::= VOID. { X = NULL; }
|
|
|
|
func_params(X) ::= func_param_list(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-08-10 04:25:52 +00:00
|
|
|
func_param_list(X) ::= func_param(A). { X = A; }
|
2012-07-20 02:54:51 +00:00
|
|
|
func_param_list(X) ::= func_param_list(A) COMMA func_param(B). { X = A; A->AppendSibling(B); }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-08-10 04:25:52 +00:00
|
|
|
func_param(X) ::= func_param_flags(A) type(B) IDENTIFIER(C).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(FuncParamDecl,parm,A.SourceLoc ? A.SourceLoc : B->SourceLoc);
|
2010-08-10 04:25:52 +00:00
|
|
|
parm->Type = B;
|
|
|
|
parm->Name = C.Name();
|
2013-08-29 03:59:03 +00:00
|
|
|
parm->Flags = A.Int;
|
2010-08-10 04:25:52 +00:00
|
|
|
X = parm;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
func_param_flags(X) ::= . { X.Int = 0; X.SourceLoc = 0; }
|
|
|
|
func_param_flags(X) ::= func_param_flags(A) IN(T). { X.Int = A.Int | ZCC_In; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
func_param_flags(X) ::= func_param_flags(A) OUT(T). { X.Int = A.Int | ZCC_Out; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
|
|
|
func_param_flags(X) ::= func_param_flags(A) OPTIONAL(T). { X.Int = A.Int | ZCC_Optional; X.SourceLoc = A.SourceLoc ? A.SourceLoc : T.SourceLoc; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* Like UnrealScript, a constant's type is implied by its value's type. */
|
2013-08-29 03:59:03 +00:00
|
|
|
const_def(X) ::= CONST(T) IDENTIFIER(A) EQ expr(B) SEMICOLON.
|
2010-08-10 04:25:52 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ConstantDef,def,T);
|
2010-08-10 04:25:52 +00:00
|
|
|
def->Name = A.Name();
|
|
|
|
def->Value = B;
|
|
|
|
X = def;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
/************ Expressions ************/
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* We use default to access a class's default instance. */
|
|
|
|
%fallback IDENTIFIER
|
|
|
|
DEFAULT.
|
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
%type expr{ZCC_Expression *}
|
|
|
|
%type primary{ZCC_Expression *}
|
|
|
|
%type unary_expr{ZCC_Expression *}
|
2013-09-11 02:10:48 +00:00
|
|
|
%type constant{ZCC_ExprConstant *}
|
2010-06-06 04:15:28 +00:00
|
|
|
|
|
|
|
%include {
|
2013-09-11 01:50:21 +00:00
|
|
|
#define UNARY_EXPR(X,T) NEW_AST_NODE(ExprUnary, expr, X); expr->Operation = T; expr->Operand = X; expr->Type = NULL
|
|
|
|
#define BINARY_EXPR(X,Y,T) NEW_AST_NODE(ExprBinary, expr, X); expr->Operation = T; expr->Type = NULL; expr->Left = X; expr->Right = Y
|
2010-06-06 04:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*----- Primary Expressions -----*/
|
|
|
|
|
|
|
|
primary(X) ::= IDENTIFIER(A).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ExprID, expr, A);
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Operation = PEX_ID;
|
2010-07-11 18:53:41 +00:00
|
|
|
expr->Identifier = A.Name();
|
2013-09-11 01:50:21 +00:00
|
|
|
expr->Type = NULL;
|
2010-06-06 04:15:28 +00:00
|
|
|
X = expr;
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
primary(X) ::= SUPER(T).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(Expression, expr, T);
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Operation = PEX_Super;
|
2013-09-11 01:50:21 +00:00
|
|
|
expr->Type = NULL;
|
2010-06-06 04:15:28 +00:00
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
primary(X) ::= constant(A).
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
primary(X) ::= SELF(T).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(Expression, expr, T);
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Operation = PEX_Self;
|
2013-09-11 01:50:21 +00:00
|
|
|
expr->Type = NULL;
|
2010-06-06 04:15:28 +00:00
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
primary(X) ::= LPAREN expr(A) RPAREN.
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
primary ::= LPAREN error RPAREN.
|
2010-06-06 04:15:28 +00:00
|
|
|
primary(X) ::= primary(A) LPAREN func_expr_list(B) RPAREN. [DOT] // Function call
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ExprFuncCall, expr, A);
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Operation = PEX_FuncCall;
|
2013-09-11 01:50:21 +00:00
|
|
|
expr->Type = NULL;
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Function = A;
|
|
|
|
expr->Parameters = B;
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
primary(X) ::= primary(A) LBRACKET expr(B) RBRACKET. [DOT] // Array access
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ExprBinary, expr, B);
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Operation = PEX_ArrayAccess;
|
2013-09-11 01:50:21 +00:00
|
|
|
expr->Type = NULL;
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Left = A;
|
|
|
|
expr->Right = B;
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
primary(X) ::= primary(A) DOT IDENTIFIER(B). // Member access
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ExprMemberAccess, expr, B);
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Operation = PEX_MemberAccess;
|
2013-09-11 01:50:21 +00:00
|
|
|
expr->Type = NULL;
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Left = A;
|
|
|
|
expr->Right = ENamedName(B.Int);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
primary(X) ::= primary(A) ADDADD. /* postfix++ */
|
|
|
|
{
|
|
|
|
UNARY_EXPR(A,PEX_PostInc);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
primary(X) ::= primary(A) SUBSUB. /* postfix-- */
|
|
|
|
{
|
|
|
|
UNARY_EXPR(A,PEX_PostDec);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
primary(X) ::= SCOPE primary(B).
|
|
|
|
{
|
|
|
|
BINARY_EXPR(NULL,B,PEX_Scope);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----- Unary Expressions -----*/
|
|
|
|
|
|
|
|
unary_expr(X) ::= primary(A).
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
unary_expr(X) ::= SUB unary_expr(A). [UNARY]
|
|
|
|
{
|
|
|
|
UNARY_EXPR(A,PEX_Negate);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
unary_expr(X) ::= ADD unary_expr(A). [UNARY]
|
|
|
|
{
|
|
|
|
// Even though this is really a no-op, we still need to make a node for
|
|
|
|
// it so we can type check that it is being applied to something numeric.
|
|
|
|
UNARY_EXPR(A,PEX_AntiNegate);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
unary_expr(X) ::= SUBSUB unary_expr(A). [UNARY]
|
|
|
|
{
|
|
|
|
UNARY_EXPR(A,PEX_PreDec);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
unary_expr(X) ::= ADDADD unary_expr(A). [UNARY]
|
|
|
|
{
|
|
|
|
UNARY_EXPR(A,PEX_PreInc);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
unary_expr(X) ::= TILDE unary_expr(A). [UNARY]
|
|
|
|
{
|
|
|
|
UNARY_EXPR(A,PEX_BitNot);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
unary_expr(X) ::= BANG unary_expr(A). [UNARY]
|
|
|
|
{
|
|
|
|
UNARY_EXPR(A,PEX_BoolNot);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
unary_expr(X) ::= SIZEOF unary_expr(A). [UNARY]
|
|
|
|
{
|
|
|
|
UNARY_EXPR(A,PEX_SizeOf);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
unary_expr(X) ::= ALIGNOF unary_expr(A). [UNARY]
|
|
|
|
{
|
|
|
|
UNARY_EXPR(A,PEX_AlignOf);
|
|
|
|
X = expr;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* Due to parsing conflicts, C-style casting is not supported. You
|
|
|
|
* must use C++ function call-style casting instead.
|
|
|
|
*/
|
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
/*----- Binary Expressions -----*/
|
|
|
|
|
|
|
|
expr(X) ::= unary_expr(A).
|
|
|
|
{
|
2013-07-14 04:25:53 +00:00
|
|
|
X = A;
|
2010-06-06 04:15:28 +00:00
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) ADD expr(B). /* a + b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_Add);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) SUB expr(B). /* a - b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_Sub);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) MUL expr(B). /* a * b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_Mul);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) DIV expr(B). /* a / b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_Div);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) MOD expr(B). /* a % b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_Mod);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) POW expr(B). /* a ** b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_Pow);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) CROSSPROD expr(B). /* a cross b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_CrossProduct);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) DOTPROD expr(B). /* a dot b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_DotProduct);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) LSH expr(B). /* a << b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_LeftShift);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) RSH expr(B). /* a >> b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_RightShift);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) DOTDOT expr(B). /* a .. b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_Concat);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr(X) ::= expr(A) LT expr(B). /* a < b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_LT);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) GT expr(B). /* a > b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_GT);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) LTEQ expr(B). /* a <= b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_LTEQ);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) GTEQ expr(B). /* a >= b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_GTEQ);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) LTGTEQ expr(B). /* a <>= b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_LTGTEQ);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) IS expr(B). /* a is b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_Is);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr(X) ::= expr(A) EQEQ expr(B). /* a == b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_EQEQ);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) NEQ expr(B). /* a != b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_NEQ);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) APPROXEQ expr(B). /* a ~== b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_APREQ);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr(X) ::= expr(A) AND expr(B). /* a & b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_BitAnd);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) XOR expr(B). /* a ^ b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_BitXor);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) OR expr(B). /* a | b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_BitOr);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) ANDAND expr(B). /* a && b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_BoolAnd);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
expr(X) ::= expr(A) OROR expr(B). /* a || b */
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_BoolOr);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr(X) ::= expr(A) SCOPE expr(B).
|
|
|
|
{
|
|
|
|
BINARY_EXPR(A,B,PEX_Scope);
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----- Trinary Expression -----*/
|
|
|
|
|
|
|
|
expr(X) ::= expr(A) QUESTION expr(B) COLON expr(C).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ExprTrinary, expr, A);
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Operation = PEX_Trinary;
|
2013-09-11 01:50:21 +00:00
|
|
|
expr->Type = NULL;
|
2010-06-06 04:15:28 +00:00
|
|
|
expr->Test = A;
|
|
|
|
expr->Left = B;
|
|
|
|
expr->Right = C;
|
|
|
|
X = expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************ Expression Lists ***********/
|
|
|
|
|
|
|
|
%type expr_list{ZCC_Expression *}
|
|
|
|
|
|
|
|
expr_list(X) ::= expr(A).
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
expr_list(X) ::= expr_list(A) COMMA expr(B).
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
A->AppendSibling(B);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----- Function argument lists -----*/
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* A function expression list can also specify a parameter's name,
|
2010-06-06 04:15:28 +00:00
|
|
|
* but once you do that, all remaining parameters must also be named.
|
|
|
|
* We let higher-level code handle this to keep this file simpler. */
|
|
|
|
%type func_expr_list{ZCC_FuncParm *}
|
2013-07-25 01:59:29 +00:00
|
|
|
%type func_expr_item{ZCC_FuncParm *}
|
2010-06-06 04:15:28 +00:00
|
|
|
%type named_expr{ZCC_FuncParm *}
|
|
|
|
|
2013-07-25 01:59:29 +00:00
|
|
|
func_expr_list(X) ::= func_expr_item(A).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-07-25 01:59:29 +00:00
|
|
|
X = A;
|
2010-06-06 04:15:28 +00:00
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
func_expr_list(X) ::= func_expr_list(A) COMMA(T) func_expr_item(B).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-07-25 01:59:29 +00:00
|
|
|
// Omitted parameters still need to appear as nodes in the list.
|
|
|
|
if (A == NULL)
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(FuncParm,nil_a,T);
|
2013-07-25 01:59:29 +00:00
|
|
|
nil_a->Value = NULL;
|
|
|
|
nil_a->Label = NAME_None;
|
|
|
|
A = nil_a;
|
|
|
|
}
|
|
|
|
if (B == NULL)
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(FuncParm,nil_b,T);
|
2013-07-25 01:59:29 +00:00
|
|
|
nil_b->Value = NULL;
|
|
|
|
nil_b->Label = NAME_None;
|
|
|
|
B = nil_b;
|
|
|
|
}
|
2010-06-06 04:15:28 +00:00
|
|
|
X = A;
|
2013-07-25 01:59:29 +00:00
|
|
|
A->AppendSibling(B);
|
2010-06-06 04:15:28 +00:00
|
|
|
}
|
2013-07-25 01:59:29 +00:00
|
|
|
|
|
|
|
func_expr_item(X) ::= .
|
2013-07-24 00:28:58 +00:00
|
|
|
{
|
2013-07-25 01:59:29 +00:00
|
|
|
X = NULL;
|
2013-07-24 00:28:58 +00:00
|
|
|
}
|
2013-07-25 01:59:29 +00:00
|
|
|
func_expr_item(X) ::= named_expr(A).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
named_expr(X) ::= IDENTIFIER(A) COLON expr(B).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(FuncParm, parm, A);
|
2010-06-06 04:15:28 +00:00
|
|
|
parm->Value = B;
|
|
|
|
parm->Label = ENamedName(A.Int);
|
|
|
|
X = parm;
|
|
|
|
}
|
|
|
|
named_expr(X) ::= expr(B).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(FuncParm, parm, B);
|
2010-06-06 04:15:28 +00:00
|
|
|
parm->Value = B;
|
|
|
|
parm->Label = NAME_None;
|
|
|
|
X = parm;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
/************ Constants ************/
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* Allow C-like concatenation of adjacent string constants. */
|
2013-09-11 02:10:48 +00:00
|
|
|
%type string_constant{ZCC_ExprConstant *}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
string_constant(X) ::= STRCONST(A).
|
|
|
|
{
|
2013-09-11 02:10:48 +00:00
|
|
|
NEW_AST_NODE(ExprConstant, strconst, A);
|
|
|
|
strconst->Operation = PEX_ConstValue;
|
2013-09-11 01:50:21 +00:00
|
|
|
strconst->Type = TypeString;
|
2013-09-11 02:10:48 +00:00
|
|
|
strconst->StringVal = A.String;
|
2010-06-06 04:15:28 +00:00
|
|
|
X = strconst;
|
|
|
|
}
|
|
|
|
string_constant(X) ::= string_constant(A) STRCONST(B).
|
|
|
|
{
|
2013-09-11 02:10:48 +00:00
|
|
|
NEW_AST_NODE(ExprConstant, strconst, A);
|
|
|
|
strconst->Operation = PEX_ConstValue;
|
2013-09-11 01:50:21 +00:00
|
|
|
strconst->Type = TypeString;
|
2013-09-11 02:10:48 +00:00
|
|
|
strconst->StringVal = stat->Strings.Alloc(*(A->StringVal) + *(B.String));
|
2010-06-06 04:15:28 +00:00
|
|
|
X = strconst;
|
|
|
|
}
|
|
|
|
|
|
|
|
constant(X) ::= string_constant(A).
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
constant(X) ::= INTCONST(A).
|
|
|
|
{
|
2013-09-11 02:10:48 +00:00
|
|
|
NEW_AST_NODE(ExprConstant, intconst, A);
|
|
|
|
intconst->Operation = PEX_ConstValue;
|
2013-09-11 01:50:21 +00:00
|
|
|
intconst->Type = TypeSInt32;
|
2013-09-11 02:10:48 +00:00
|
|
|
intconst->IntVal = A.Int;
|
2010-06-06 04:15:28 +00:00
|
|
|
X = intconst;
|
|
|
|
}
|
|
|
|
constant(X) ::= FLOATCONST(A).
|
|
|
|
{
|
2013-09-11 02:10:48 +00:00
|
|
|
NEW_AST_NODE(ExprConstant, floatconst, A);
|
|
|
|
floatconst->Operation = PEX_ConstValue;
|
2013-09-11 01:50:21 +00:00
|
|
|
floatconst->Type = TypeFloat64;
|
2013-09-11 02:10:48 +00:00
|
|
|
floatconst->DoubleVal = A.Float;
|
2010-06-06 04:15:28 +00:00
|
|
|
X = floatconst;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************ Statements ************/
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-07-14 03:26:29 +00:00
|
|
|
function_body(X) ::= compound_statement(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
%type statement{ZCC_Statement *}
|
2013-07-14 03:26:29 +00:00
|
|
|
statement(X) ::= SEMICOLON. { X = NULL; }
|
|
|
|
statement(X) ::= labeled_statement(A). { X = A; }
|
|
|
|
statement(X) ::= compound_statement(A). { X = A; }
|
|
|
|
statement(X) ::= expression_statement(A) SEMICOLON. { X = A; }
|
|
|
|
statement(X) ::= selection_statement(A). { X = A; }
|
|
|
|
statement(X) ::= iteration_statement(A). { X = A; }
|
|
|
|
statement(X) ::= jump_statement(A). { X = A; }
|
|
|
|
statement(X) ::= assign_statement(A) SEMICOLON. { X = A; }
|
|
|
|
statement(X) ::= local_var(A) SEMICOLON. { X = A; }
|
|
|
|
statement(X) ::= error SEMICOLON. { X = NULL; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
/*----- Jump Statements -----*/
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
%type jump_statement{ZCC_Statement *}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
jump_statement(A) ::= CONTINUE(T) SEMICOLON.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ContinueStmt, stmt, T);
|
2010-06-06 04:15:28 +00:00
|
|
|
A = stmt;
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
jump_statement(A) ::= BREAK(T) SEMICOLON.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(BreakStmt, stmt, T);
|
2010-06-06 04:15:28 +00:00
|
|
|
A = stmt;
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
jump_statement(A) ::= RETURN(T) SEMICOLON.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ReturnStmt, stmt, T);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Values = NULL;
|
|
|
|
A = stmt;
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
jump_statement(A) ::= RETURN(T) expr_list(X) SEMICOLON.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ReturnStmt, stmt, T);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Values = X;
|
|
|
|
A = stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----- Compound Statements -----*/
|
|
|
|
|
|
|
|
%type compound_statement{ZCC_CompoundStmt *}
|
|
|
|
%type statement_list{ZCC_Statement *}
|
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
compound_statement(X) ::= LBRACE(T) RBRACE.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(CompoundStmt,stmt,T);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Content = NULL;
|
|
|
|
X = stmt;
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
compound_statement(X) ::= LBRACE(T) statement_list(A) RBRACE.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(CompoundStmt,stmt,T);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Content = A;
|
|
|
|
X = stmt;
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
compound_statement(X) ::= LBRACE(T) error RBRACE.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(CompoundStmt,stmt,T);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Content = NULL;
|
|
|
|
X = stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
statement_list(X) ::= statement(A).
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
statement_list(X) ::= statement_list(A) statement(B).
|
|
|
|
{
|
2013-07-14 03:26:29 +00:00
|
|
|
SAFE_APPEND(A,B);
|
2010-06-06 04:15:28 +00:00
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----- Expression Statements -----*/
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
%type expression_statement{ZCC_ExpressionStmt *}
|
|
|
|
|
|
|
|
expression_statement(X) ::= expr(A).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(ExpressionStmt, stmt, A);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Expression = A;
|
|
|
|
X = stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----- Iteration Statements -----*/
|
|
|
|
|
|
|
|
%type iteration_statement{ZCC_Statement *}
|
|
|
|
|
|
|
|
// while/until (expr) statement
|
|
|
|
iteration_statement(X) ::= while_or_until(TY) LPAREN expr(EX) RPAREN statement(ST).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(IterationStmt, iter, TY);
|
2010-06-06 04:15:28 +00:00
|
|
|
if (TY.Int == ZCC_UNTIL)
|
|
|
|
{ // Negate the loop condition
|
|
|
|
UNARY_EXPR(EX,PEX_BoolNot);
|
|
|
|
iter->LoopCondition = expr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iter->LoopCondition = EX;
|
|
|
|
}
|
|
|
|
iter->LoopStatement = ST;
|
|
|
|
iter->LoopBumper = NULL;
|
|
|
|
iter->CheckAt = ZCC_IterationStmt::Start;
|
|
|
|
X = iter;
|
|
|
|
}
|
|
|
|
// do statement while/until (expr)
|
2013-08-29 03:59:03 +00:00
|
|
|
iteration_statement(X) ::= DO(T) statement(ST) while_or_until(TY) LPAREN expr(EX) RPAREN.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(IterationStmt, iter, T);
|
2010-06-06 04:15:28 +00:00
|
|
|
if (TY.Int == ZCC_UNTIL)
|
|
|
|
{ // Negate the loop condition
|
|
|
|
UNARY_EXPR(EX,PEX_BoolNot);
|
|
|
|
iter->LoopCondition = expr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iter->LoopCondition = EX;
|
|
|
|
}
|
|
|
|
iter->LoopStatement = ST;
|
|
|
|
iter->LoopBumper = NULL;
|
|
|
|
iter->CheckAt = ZCC_IterationStmt::End;
|
|
|
|
X = iter;
|
|
|
|
}
|
|
|
|
// for (init; cond; bump) statement
|
2013-08-29 03:59:03 +00:00
|
|
|
iteration_statement(X) ::= FOR(T) LPAREN for_init(IN) SEMICOLON opt_expr(EX) SEMICOLON for_bump(DO) RPAREN statement(ST).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(IterationStmt, iter, T);
|
2010-06-06 04:15:28 +00:00
|
|
|
iter->LoopCondition = EX;
|
|
|
|
iter->LoopStatement = ST;
|
|
|
|
iter->LoopBumper = DO;
|
|
|
|
iter->CheckAt = ZCC_IterationStmt::Start;
|
|
|
|
// The initialization expression appears outside the loop
|
2012-07-20 02:54:51 +00:00
|
|
|
// for_init may be NULL if there is no initialization.
|
|
|
|
SAFE_APPEND(IN, iter);
|
2010-06-06 04:15:28 +00:00
|
|
|
// And the whole thing gets wrapped inside a compound statement in case the loop
|
|
|
|
// initializer defined any variables.
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(CompoundStmt, wrap, T);
|
2010-06-06 04:15:28 +00:00
|
|
|
wrap->Content = IN;
|
|
|
|
X = wrap;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
while_or_until(X) ::= WHILE(T).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
|
|
|
X.Int = ZCC_WHILE;
|
2013-08-29 03:59:03 +00:00
|
|
|
X.SourceLoc = T.SourceLoc;
|
2010-06-06 04:15:28 +00:00
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
while_or_until(X) ::= UNTIL(T).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
|
|
|
X.Int = ZCC_UNTIL;
|
2013-08-29 03:59:03 +00:00
|
|
|
X.SourceLoc = T.SourceLoc;
|
2010-06-06 04:15:28 +00:00
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
%type for_init{ZCC_Statement *}
|
|
|
|
for_init(X) ::= local_var(A). { X = A; }
|
|
|
|
for_init(X) ::= for_bump(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
%type for_bump{ZCC_Statement *}
|
|
|
|
for_bump(X) ::= . { X = NULL; }
|
|
|
|
for_bump(X) ::= expression_statement(A). { X = A; }
|
|
|
|
for_bump(X) ::= assign_statement(A). { X = A; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
/*----- If Statements -----*/
|
2010-03-19 04:04:13 +00:00
|
|
|
|
|
|
|
/* Resolve the shift-reduce conflict here in favor of the shift.
|
|
|
|
* This is the default behavior, but using precedence symbols
|
|
|
|
* lets us do it without warnings.
|
|
|
|
*/
|
|
|
|
%left IF.
|
|
|
|
%left ELSE.
|
2010-06-06 04:15:28 +00:00
|
|
|
%type selection_statement{ZCC_Statement *}
|
|
|
|
%type if_front{ZCC_IfStmt *}
|
|
|
|
|
|
|
|
selection_statement(X) ::= if_front(A). [IF]
|
|
|
|
{
|
|
|
|
X = A;
|
|
|
|
}
|
|
|
|
selection_statement(X) ::= if_front(A) ELSE statement(B). [ELSE]
|
|
|
|
{
|
|
|
|
A->FalsePath = B;
|
|
|
|
X = A;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
if_front(X) ::= IF(T) LPAREN expr(A) RPAREN statement(B).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(IfStmt,stmt,T);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Condition = A;
|
|
|
|
stmt->TruePath = B;
|
|
|
|
stmt->FalsePath = NULL;
|
|
|
|
X = stmt;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
/*----- Switch Statements -----*/
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
selection_statement(X) ::= SWITCH(T) LPAREN expr(A) RPAREN statement(B).
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(SwitchStmt,stmt,T);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Condition = A;
|
|
|
|
stmt->Content = B;
|
|
|
|
X = stmt;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
/*----- Case Label "Statements" -----*/
|
|
|
|
|
|
|
|
%type labeled_statement{ZCC_CaseStmt *}
|
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
labeled_statement(X) ::= CASE(T) expr(A) COLON.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(CaseStmt,stmt,T);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Condition = A;
|
|
|
|
X = stmt;
|
|
|
|
}
|
2013-08-29 03:59:03 +00:00
|
|
|
labeled_statement(X) ::= DEFAULT(T) COLON.
|
2010-06-06 04:15:28 +00:00
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(CaseStmt,stmt,T);
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Condition = NULL;
|
|
|
|
X = stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----- Assignment Statements -----*/
|
|
|
|
|
|
|
|
%type assign_statement{ZCC_AssignStmt *}
|
|
|
|
|
|
|
|
assign_statement(X) ::= expr_list(A) assign_op(OP) expr_list(B). [EQ]
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(AssignStmt,stmt,OP);
|
|
|
|
stmt->AssignOp = OP.Int;
|
2010-06-06 04:15:28 +00:00
|
|
|
stmt->Dests = A;
|
|
|
|
stmt->Sources = B;
|
|
|
|
X = stmt;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2013-08-29 03:59:03 +00:00
|
|
|
assign_op(X) ::= EQ(T). { X.Int = ZCC_EQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= MULEQ(T). { X.Int = ZCC_MULEQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= DIVEQ(T). { X.Int = ZCC_DIVEQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= MODEQ(T). { X.Int = ZCC_MODEQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= ADDEQ(T). { X.Int = ZCC_ADDEQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= SUBEQ(T). { X.Int = ZCC_SUBEQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= LSHEQ(T). { X.Int = ZCC_LSHEQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= RSHEQ(T). { X.Int = ZCC_RSHEQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= ANDEQ(T). { X.Int = ZCC_ANDEQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= OREQ(T). { X.Int = ZCC_OREQ; X.SourceLoc = T.SourceLoc; }
|
|
|
|
assign_op(X) ::= XOREQ(T). { X.Int = ZCC_XOREQ; X.SourceLoc = T.SourceLoc; }
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
/*----- Local Variable Definition "Statements" -----*/
|
|
|
|
|
|
|
|
%type local_var{ZCC_LocalVarStmt *}
|
|
|
|
|
|
|
|
local_var(X) ::= type(A) variable_list(B) var_init(C).
|
|
|
|
{
|
2013-08-29 03:59:03 +00:00
|
|
|
NEW_AST_NODE(LocalVarStmt,vardef,A);
|
2010-06-06 04:15:28 +00:00
|
|
|
vardef->Type = A;
|
|
|
|
vardef->Vars = B;
|
|
|
|
vardef->Inits = C;
|
|
|
|
X = vardef;
|
|
|
|
}
|
2010-03-19 04:04:13 +00:00
|
|
|
|
2010-06-06 04:15:28 +00:00
|
|
|
%type var_init{ZCC_Expression *}
|
|
|
|
var_init(X) ::= . { X = NULL; }
|
|
|
|
var_init(X) ::= EQ expr_list(A). { X = A; }
|