From 0c09cceffc3861a5f1d8bde0a075828d61bd6c78 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Tue, 26 Oct 2010 02:05:21 -0400 Subject: [PATCH] Made struct dereference a separate expression type, not a binary expression. --- mojoshader_compiler.c | 26 +++++++++++++++++++++++--- mojoshader_parser_hlsl.lemon | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/mojoshader_compiler.c b/mojoshader_compiler.c index 4e992f00..54de5bff 100644 --- a/mojoshader_compiler.c +++ b/mojoshader_compiler.c @@ -59,7 +59,6 @@ typedef enum ASTNodeType AST_OP_START_RANGE_BINARY, AST_OP_DEREF_ARRAY, - AST_OP_DEREF_STRUCT, AST_OP_COMMA, AST_OP_MULTIPLY, AST_OP_DIVIDE, @@ -105,6 +104,7 @@ typedef enum ASTNodeType AST_OP_END_RANGE_DATA, AST_OP_START_RANGE_MISC, + AST_OP_DEREF_STRUCT, AST_OP_CALLFUNC, AST_OP_CONSTRUCTOR, AST_OP_CAST, @@ -281,6 +281,13 @@ typedef struct ExpressionConstructor Arguments *args; } ExpressionConstructor; +typedef struct ExpressionDerefStruct +{ + ASTNode ast; // Always AST_OP_DEREF_STRUCT + Expression *identifier; + const char *member; +} ExpressionDerefStruct; + typedef struct ExpressionCallFunction { ASTNode ast; // Always AST_OP_CALLFUNC @@ -809,6 +816,15 @@ static Expression *new_ternary_expr(Context *ctx, const ASTNodeType op, return (Expression *) retval; } // new_ternary_expr +static Expression *new_deref_struct_expr(Context *ctx, Expression *identifier, + const char *member) +{ + NEW_AST_NODE(retval, ExpressionDerefStruct, AST_OP_DEREF_STRUCT); + retval->identifier = identifier; + retval->member = member; // cached; don't copy string. + return (Expression *) retval; +} // new_deref_struct_expr + static Expression *new_identifier_expr(Context *ctx, const char *string) { NEW_AST_NODE(retval, ExpressionIdentifier, AST_OP_IDENTIFIER); @@ -875,6 +891,10 @@ static void delete_expr(Context *ctx, Expression *expr) { delete_arguments(ctx, ((ExpressionConstructor *) expr)->args); } // else if + else if (expr->ast.type == AST_OP_DEREF_STRUCT) + { + delete_expr(ctx, ((ExpressionDerefStruct *) expr)->identifier); + } // else if else if (expr->ast.type == AST_OP_CALLFUNC) { delete_expr(ctx, ((ExpressionCallFunction *) expr)->identifier); @@ -1601,9 +1621,9 @@ static void print_ast(const int substmt, void *ast) break; case AST_OP_DEREF_STRUCT: - print_ast(0, ((ExpressionBinary *) ast)->left); + print_ast(0, ((ExpressionDerefStruct *) ast)->identifier); printf("."); - print_ast(0, ((ExpressionBinary *) ast)->right); + printf("%s", ((ExpressionDerefStruct *) ast)->member); break; case AST_OP_COMMA: diff --git a/mojoshader_parser_hlsl.lemon b/mojoshader_parser_hlsl.lemon index 93a7adb0..ff66e0b7 100644 --- a/mojoshader_parser_hlsl.lemon +++ b/mojoshader_parser_hlsl.lemon @@ -470,7 +470,7 @@ postfix_expr(A) ::= primary_expr(B). { A = B; } postfix_expr(A) ::= postfix_expr(B) LBRACKET expression(C) RBRACKET. { A = new_binary_expr(ctx, AST_OP_DEREF_ARRAY, B, C); } postfix_expr(A) ::= postfix_expr(B) arguments(C). { A = new_callfunc_expr(ctx, B, C); } postfix_expr(A) ::= datatype(B) arguments(C). { A = new_constructor_expr(ctx, B, C); } // HLSL constructor -postfix_expr(A) ::= postfix_expr(B) DOT IDENTIFIER(C). { A = new_binary_expr(ctx, AST_OP_DEREF_STRUCT, B, new_identifier_expr(ctx, C.string)); } +postfix_expr(A) ::= postfix_expr(B) DOT IDENTIFIER(C). { A = new_deref_struct_expr(ctx, B, C.string); } postfix_expr(A) ::= postfix_expr(B) PLUSPLUS. { A = new_unary_expr(ctx, AST_OP_POSTINCREMENT, B); } postfix_expr(A) ::= postfix_expr(B) MINUSMINUS. { A = new_unary_expr(ctx, AST_OP_POSTDECREMENT, B); }