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

View file

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

View file

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