From 33af148010fd3228659a7ca5aefd83e410a630cf Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 20 Feb 2010 00:27:28 -0500 Subject: [PATCH] Uncommented some grammar bits that got masked out in the calculator experiment. --- mojoshader_compiler.c | 48 +++++++++++++++++++++++++++++++++--- mojoshader_parser_hlsl.lemon | 4 +-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/mojoshader_compiler.c b/mojoshader_compiler.c index 4d77d679..115a9b2c 100644 --- a/mojoshader_compiler.c +++ b/mojoshader_compiler.c @@ -88,6 +88,9 @@ typedef enum Operator OP_FLOAT_LITERAL, OP_STRING_LITERAL, OP_END_RANGE_DATA, + + OP_CONSTRUCTOR, + OP_CAST } Operator; typedef enum VariableAttributes @@ -189,6 +192,20 @@ typedef struct ExpressionStringLiteral const char *string; } ExpressionStringLiteral; +typedef struct ExpressionConstructor +{ + Operator op; // Always OP_CONSTRUCTOR + const char *datatype; + Expression *args; +} ExpressionConstructor; + +typedef struct ExpressionCast +{ + Operator op; // Always OP_CAST + const char *datatype; + Expression *operand; +} ExpressionCast; + typedef enum CompilationUnitType { COMPUNITTYPE_FUNCTION, // function declaration or definition @@ -517,6 +534,26 @@ static inline void Free(Context *ctx, void *ptr) static void delete_compilation_unit(Context *ctx, CompilationUnit *unit); static void delete_statement(Context *ctx, Statement *stmt); +static Expression *new_constructor_expr(Context *ctx, const char *datatype, + Expression *args) +{ + NEW_AST_NODE(ExpressionConstructor); + retval->op = OP_CONSTRUCTOR; + retval->datatype = datatype; + retval->args = args; + return (Expression *) retval; +} // new_constructor_expr + +static Expression *new_cast_expr(Context *ctx, const char *datatype, + Expression *operand) +{ + NEW_AST_NODE(ExpressionCast); + retval->op = OP_CAST; + retval->datatype = datatype; + retval->operand = operand; + return (Expression *) retval; +} // new_cast_expr + static Expression *new_unary_expr(Context *ctx, const Operator op, Expression *operand) { @@ -604,9 +641,14 @@ static void delete_expr(Context *ctx, Expression *expr) delete_expr(ctx, ternary->center); delete_expr(ctx, ternary->right); } // else if - - // don't need to free extra fields in other types at the moment. - + else if (expr->op == OP_CAST) + { + delete_expr(ctx, ((ExpressionCast *) expr)->operand); + } // else if + else if (expr->op == OP_CONSTRUCTOR) + { + delete_expr(ctx, ((ExpressionConstructor *) expr)->args); + } // else if Free(ctx, expr); } // delete_expr diff --git a/mojoshader_parser_hlsl.lemon b/mojoshader_parser_hlsl.lemon index 6290be59..a70a7c78 100644 --- a/mojoshader_parser_hlsl.lemon +++ b/mojoshader_parser_hlsl.lemon @@ -591,7 +591,7 @@ postfix_expr(A) ::= primary_expr(B). { A = B; } postfix_expr(A) ::= postfix_expr(B) LBRACKET expression(C) RBRACKET. { A = new_binary_expr(ctx, OP_DEREF_ARRAY, B, C); } postfix_expr(A) ::= postfix_expr(B) LPAREN RPAREN. { A = new_binary_expr(ctx, OP_CALLFUNC, B, NULL); } postfix_expr(A) ::= postfix_expr(B) LPAREN argument_expr_list(C) RPAREN. { A = new_binary_expr(ctx, OP_CALLFUNC, B, C); } -//postfix_expr(A) ::= datatype(B) LPAREN argument_expr_list(C) RPAREN. { A = new_constructor_expr(ctx, B, C); } // HLSL constructor +postfix_expr(A) ::= datatype(B) LPAREN argument_expr_list(C) RPAREN. { A = NULL; new_constructor_expr(ctx, B, C); } // HLSL constructor postfix_expr(A) ::= postfix_expr(B) DOT IDENTIFIER(C). { A = new_binary_expr(ctx, OP_DEREF_STRUCT, B, new_identifier_expr(ctx, C.string)); } postfix_expr(A) ::= postfix_expr(B) PLUSPLUS. { A = new_unary_expr(ctx, OP_POSTINCREMENT, B); } postfix_expr(A) ::= postfix_expr(B) MINUSMINUS. { A = new_unary_expr(ctx, OP_POSTDECREMENT, B); } @@ -614,7 +614,7 @@ unary_expr(A) ::= EXCLAMATION cast_expr(B). { A = new_unary_expr(ctx, OP_NOT, B) %type cast_expr { Expression * } %destructor cast_expr { delete_expr(ctx, $$); } cast_expr(A) ::= unary_expr(B). { A = B; } -//cast_expr(A) ::= LPAREN datatype(B) RPAREN cast_expr(C). { A = new_cast_expr(ctx, B, C); } +cast_expr(A) ::= LPAREN datatype(B) RPAREN cast_expr(C). { A = new_cast_expr(ctx, B, C); } %type multiplicative_expr { Expression * } %destructor multiplicative_expr { delete_expr(ctx, $$); }