From 61666e15153e5e5f1640bf8455033a353ece87e9 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Tue, 10 Sep 2013 21:10:48 -0500 Subject: [PATCH] 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. --- src/zscript/ast.cpp | 48 ++++++++++++++----------------------- src/zscript/zcc-parse.lemon | 28 +++++++++++----------- src/zscript/zcc_parser.h | 28 +++++++--------------- 3 files changed, 41 insertions(+), 63 deletions(-) diff --git a/src/zscript/ast.cpp b/src/zscript/ast.cpp index ae687eefc..dc9422cf4 100644 --- a/src/zscript/ast.cpp +++ b/src/zscript/ast.cpp @@ -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, diff --git a/src/zscript/zcc-parse.lemon b/src/zscript/zcc-parse.lemon index 3384bab03..da940dc6f 100644 --- a/src/zscript/zcc-parse.lemon +++ b/src/zscript/zcc-parse.lemon @@ -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; } diff --git a/src/zscript/zcc_parser.h b/src/zscript/zcc_parser.h index 7bd12ede1..3ad675abe 100644 --- a/src/zscript/zcc_parser.h +++ b/src/zscript/zcc_parser.h @@ -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