Consolidate constant expression nodes into a single type

- Instead of having ZCC_ExprString, ZCC_ExprInt, and ZCC_ExprFloat,
  just use a single ZCC_ExprConstant. It should simplify type
  promotion and constant folding in the future.
This commit is contained in:
Randy Heit 2013-09-10 21:10:48 -05:00
parent f9f8d1e79b
commit 61666e1515
3 changed files with 41 additions and 63 deletions

View file

@ -442,10 +442,7 @@ static void OpenExprType(FLispString &out, EZCCExprType type)
"id",
"super",
"self",
"string-const",
"int-const",
"uint-const",
"float-const",
"const",
"func-call",
"array-access",
"member-access",
@ -518,30 +515,23 @@ static void PrintExprID(FLispString &out, ZCC_TreeNode *node)
out.Close();
}
static void PrintExprString(FLispString &out, ZCC_TreeNode *node)
static void PrintExprConstant(FLispString &out, ZCC_TreeNode *node)
{
ZCC_ExprString *enode = (ZCC_ExprString *)node;
assert(enode->Operation == PEX_StringConst);
out.Open("expr-string-const");
PrintStringConst(out, *enode->Value);
out.Close();
}
static void PrintExprInt(FLispString &out, ZCC_TreeNode *node)
{
ZCC_ExprInt *enode = (ZCC_ExprInt *)node;
assert(enode->Operation == PEX_IntConst || enode->Operation == PEX_UIntConst);
OpenExprType(out, enode->Operation);
out.AddInt(enode->Value);
out.Close();
}
static void PrintExprFloat(FLispString &out, ZCC_TreeNode *node)
{
ZCC_ExprFloat *enode = (ZCC_ExprFloat *)node;
assert(enode->Operation == PEX_FloatConst);
out.Open("expr-float-const");
out.AddFloat(enode->Value);
ZCC_ExprConstant *enode = (ZCC_ExprConstant *)node;
assert(enode->Operation == PEX_ConstValue);
out.Open("expr-const");
if (enode->Type == TypeString)
{
PrintStringConst(out, *enode->StringVal);
}
else if (enode->Type == TypeFloat64)
{
out.AddFloat(enode->DoubleVal);
}
else
{
out.AddInt(enode->IntVal);
}
out.Close();
}
@ -817,9 +807,7 @@ void (* const TreeNodePrinter[NUM_AST_NODE_TYPES])(FLispString &, ZCC_TreeNode *
PrintClassType,
PrintExpression,
PrintExprID,
PrintExprString,
PrintExprInt,
PrintExprFloat,
PrintExprConstant,
PrintExprFuncCall,
PrintExprMemberAccess,
PrintExprUnary,

View file

@ -726,7 +726,7 @@ const_def(X) ::= CONST(T) IDENTIFIER(A) EQ expr(B) SEMICOLON.
%type expr{ZCC_Expression *}
%type primary{ZCC_Expression *}
%type unary_expr{ZCC_Expression *}
%type constant{ZCC_Expression *}
%type constant{ZCC_ExprConstant *}
%include {
#define UNARY_EXPR(X,T) NEW_AST_NODE(ExprUnary, expr, X); expr->Operation = T; expr->Operand = X; expr->Type = NULL
@ -1091,22 +1091,22 @@ named_expr(X) ::= expr(B).
/************ Constants ************/
/* Allow C-like concatenation of adjacent string constants. */
%type string_constant{ZCC_ExprString *}
%type string_constant{ZCC_ExprConstant *}
string_constant(X) ::= STRCONST(A).
{
NEW_AST_NODE(ExprString, strconst, A);
strconst->Operation = PEX_StringConst;
NEW_AST_NODE(ExprConstant, strconst, A);
strconst->Operation = PEX_ConstValue;
strconst->Type = TypeString;
strconst->Value = A.String;
strconst->StringVal = A.String;
X = strconst;
}
string_constant(X) ::= string_constant(A) STRCONST(B).
{
NEW_AST_NODE(ExprString, strconst, A);
strconst->Operation = PEX_StringConst;
NEW_AST_NODE(ExprConstant, strconst, A);
strconst->Operation = PEX_ConstValue;
strconst->Type = TypeString;
strconst->Value = stat->Strings.Alloc(*(A->Value) + *(B.String));
strconst->StringVal = stat->Strings.Alloc(*(A->StringVal) + *(B.String));
X = strconst;
}
@ -1116,18 +1116,18 @@ constant(X) ::= string_constant(A).
}
constant(X) ::= INTCONST(A).
{
NEW_AST_NODE(ExprInt, intconst, A);
intconst->Operation = PEX_IntConst;
NEW_AST_NODE(ExprConstant, intconst, A);
intconst->Operation = PEX_ConstValue;
intconst->Type = TypeSInt32;
intconst->Value = A.Int;
intconst->IntVal = A.Int;
X = intconst;
}
constant(X) ::= FLOATCONST(A).
{
NEW_AST_NODE(ExprFloat, floatconst, A);
floatconst->Operation = PEX_FloatConst;
NEW_AST_NODE(ExprConstant, floatconst, A);
floatconst->Operation = PEX_ConstValue;
floatconst->Type = TypeFloat64;
floatconst->Value = A.Float;
floatconst->DoubleVal = A.Float;
X = floatconst;
}

View file

@ -75,9 +75,7 @@ enum EZCCTreeNodeType
AST_ClassType,
AST_Expression,
AST_ExprID,
AST_ExprString,
AST_ExprInt,
AST_ExprFloat,
AST_ExprConstant,
AST_ExprFuncCall,
AST_ExprMemberAccess,
AST_ExprUnary,
@ -136,10 +134,7 @@ enum EZCCExprType
PEX_ID,
PEX_Super,
PEX_Self,
PEX_StringConst,
PEX_IntConst,
PEX_UIntConst,
PEX_FloatConst,
PEX_ConstValue,
PEX_FuncCall,
PEX_ArrayAccess,
PEX_MemberAccess,
@ -352,19 +347,14 @@ struct ZCC_ExprID : ZCC_Expression
ENamedName Identifier;
};
struct ZCC_ExprString : ZCC_Expression
struct ZCC_ExprConstant : ZCC_Expression
{
FString *Value;
};
struct ZCC_ExprInt : ZCC_Expression
{
int Value;
};
struct ZCC_ExprFloat : ZCC_Expression
{
double Value;
union
{
FString *StringVal;
int IntVal;
double DoubleVal;
};
};
struct ZCC_FuncParm : ZCC_TreeNode