Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Uncommented some grammar bits that got masked out in the calculator e…
…xperiment.
  • Loading branch information
icculus committed Feb 20, 2010
1 parent 7282018 commit 33af148
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
48 changes: 45 additions & 3 deletions mojoshader_compiler.c
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions mojoshader_parser_hlsl.lemon
Expand Up @@ -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); }
Expand All @@ -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, $$); }
Expand Down

0 comments on commit 33af148

Please sign in to comment.