Skip to content

Commit

Permalink
Made struct dereference a separate expression type, not a binary expr…
Browse files Browse the repository at this point in the history
…ession.
  • Loading branch information
icculus committed Oct 26, 2010
1 parent 6e40389 commit 0c09cce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 23 additions & 3 deletions mojoshader_compiler.c
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion mojoshader_parser_hlsl.lemon
Expand Up @@ -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); }

Expand Down

0 comments on commit 0c09cce

Please sign in to comment.