From 88349b22caa0ab0b44188dbb9e002549aadb0590 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Mon, 22 Feb 2010 19:10:25 -0800 Subject: [PATCH] Add ast_expression_bin subclass of ast_expression The ast_expression_bin subclass is used for all binary expressions such as addition, subtraction, and comparisons. Several other subclasses are soon to follow. --- Makefile | 3 +- ast.h | 10 ++++++ ast_expr.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ glsl_parser.y | 38 ++++++++++----------- glsl_parser_extras.cpp | 74 ++++------------------------------------ 5 files changed, 130 insertions(+), 87 deletions(-) create mode 100644 ast_expr.cpp diff --git a/Makefile b/Makefile index 0d9017c..7b1f3f1 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ CSRCS = symbol_table.c hash_table.c glsl_types.c -CCSRCS = glsl_parser.tab.cpp glsl_lexer.cpp glsl_parser_extras.cpp +CCSRCS = glsl_parser.tab.cpp glsl_lexer.cpp glsl_parser_extras.cpp \ + ast_expr.cpp # ast_to_hir.cpp ir.cpp hir_field_selection.cpp OBJS = $(CSRCS:.c=.o) $(CCSRCS:.cpp=.o) diff --git a/ast.h b/ast.h index 591655d..1f65993 100644 --- a/ast.h +++ b/ast.h @@ -159,6 +159,8 @@ public: ast_expression(int oper, ast_expression *, ast_expression *, ast_expression *); + static const char *operator_string(enum ast_operators op); + virtual void print(void) const; enum ast_operators oper; @@ -180,6 +182,14 @@ public: struct simple_node expressions; }; +class ast_expression_bin : public ast_expression { +public: + ast_expression_bin(int oper, ast_expression *, ast_expression *); + + virtual void print(void) const; +}; + + /** * Number of possible operators for an ast_expression * diff --git a/ast_expr.cpp b/ast_expr.cpp new file mode 100644 index 0000000..ad29cdc --- /dev/null +++ b/ast_expr.cpp @@ -0,0 +1,92 @@ +/* + * Copyright © 2010 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ +#include +#include +#include "ast.h" + +const char * +ast_expression::operator_string(enum ast_operators op) +{ + static const char *const operators[] = { + "=", + "+", + "-", + "+", + "-", + "*", + "/", + "%", + "<<", + ">>", + "<", + ">", + "<=", + ">=", + "==", + "!=", + "&", + "^", + "|", + "~", + "&&", + "^^", + "!", + + "*=", + "/=", + "%=", + "+=", + "-=", + "<<=", + ">>=", + "&=", + "^=", + "|=", + + "?:", + "++", + "--", + "++", + "--", + ".", + }; + + return operators[op]; +} + + +ast_expression_bin::ast_expression_bin(int oper, ast_expression *ex0, + ast_expression *ex1) : + ast_expression(oper, ex0, ex1, NULL) +{ + assert((oper >= ast_plus) && (oper <= ast_logic_not)); +} + + +void +ast_expression_bin::print(void) const +{ + subexpressions[0]->print(); + printf("%s ", operator_string(oper)); + subexpressions[1]->print(); +} diff --git a/glsl_parser.y b/glsl_parser.y index f9bfb0b..f410565 100644 --- a/glsl_parser.y +++ b/glsl_parser.y @@ -395,15 +395,15 @@ multiplicative_expression: unary_expression | multiplicative_expression '*' unary_expression { - $$ = new ast_expression(ast_mul, $1, $3, NULL); + $$ = new ast_expression_bin(ast_mul, $1, $3); } | multiplicative_expression '/' unary_expression { - $$ = new ast_expression(ast_div, $1, $3, NULL); + $$ = new ast_expression_bin(ast_div, $1, $3); } | multiplicative_expression '%' unary_expression { - $$ = new ast_expression(ast_mod, $1, $3, NULL); + $$ = new ast_expression_bin(ast_mod, $1, $3); } ; @@ -411,11 +411,11 @@ additive_expression: multiplicative_expression | additive_expression '+' multiplicative_expression { - $$ = new ast_expression(ast_add, $1, $3, NULL); + $$ = new ast_expression_bin(ast_add, $1, $3); } | additive_expression '-' multiplicative_expression { - $$ = new ast_expression(ast_sub, $1, $3, NULL); + $$ = new ast_expression_bin(ast_sub, $1, $3); } ; @@ -423,11 +423,11 @@ shift_expression: additive_expression | shift_expression LEFT_OP additive_expression { - $$ = new ast_expression(ast_lshift, $1, $3, NULL); + $$ = new ast_expression_bin(ast_lshift, $1, $3); } | shift_expression RIGHT_OP additive_expression { - $$ = new ast_expression(ast_rshift, $1, $3, NULL); + $$ = new ast_expression_bin(ast_rshift, $1, $3); } ; @@ -435,19 +435,19 @@ relational_expression: shift_expression | relational_expression '<' shift_expression { - $$ = new ast_expression(ast_less, $1, $3, NULL); + $$ = new ast_expression_bin(ast_less, $1, $3); } | relational_expression '>' shift_expression { - $$ = new ast_expression(ast_greater, $1, $3, NULL); + $$ = new ast_expression_bin(ast_greater, $1, $3); } | relational_expression LE_OP shift_expression { - $$ = new ast_expression(ast_lequal, $1, $3, NULL); + $$ = new ast_expression_bin(ast_lequal, $1, $3); } | relational_expression GE_OP shift_expression { - $$ = new ast_expression(ast_gequal, $1, $3, NULL); + $$ = new ast_expression_bin(ast_gequal, $1, $3); } ; @@ -455,11 +455,11 @@ equality_expression: relational_expression | equality_expression EQ_OP relational_expression { - $$ = new ast_expression(ast_equal, $1, $3, NULL); + $$ = new ast_expression_bin(ast_equal, $1, $3); } | equality_expression NE_OP relational_expression { - $$ = new ast_expression(ast_nequal, $1, $3, NULL); + $$ = new ast_expression_bin(ast_nequal, $1, $3); } ; @@ -467,7 +467,7 @@ and_expression: equality_expression | and_expression '&' equality_expression { - $$ = new ast_expression(ast_bit_or, $1, $3, NULL); + $$ = new ast_expression_bin(ast_bit_or, $1, $3); } ; @@ -475,7 +475,7 @@ exclusive_or_expression: and_expression | exclusive_or_expression '^' and_expression { - $$ = new ast_expression(ast_bit_xor, $1, $3, NULL); + $$ = new ast_expression_bin(ast_bit_xor, $1, $3); } ; @@ -483,7 +483,7 @@ inclusive_or_expression: exclusive_or_expression | inclusive_or_expression '|' exclusive_or_expression { - $$ = new ast_expression(ast_bit_or, $1, $3, NULL); + $$ = new ast_expression_bin(ast_bit_or, $1, $3); } ; @@ -491,7 +491,7 @@ logical_and_expression: inclusive_or_expression | logical_and_expression AND_OP inclusive_or_expression { - $$ = new ast_expression(ast_logic_and, $1, $3, NULL); + $$ = new ast_expression_bin(ast_logic_and, $1, $3); } ; @@ -499,7 +499,7 @@ logical_xor_expression: logical_and_expression | logical_xor_expression XOR_OP logical_and_expression { - $$ = new ast_expression(ast_logic_xor, $1, $3, NULL); + $$ = new ast_expression_bin(ast_logic_xor, $1, $3); } ; @@ -507,7 +507,7 @@ logical_or_expression: logical_xor_expression | logical_or_expression OR_OP logical_xor_expression { - $$ = new ast_expression(ast_logic_or, $1, $3, NULL); + $$ = new ast_expression_bin(ast_logic_or, $1, $3); } ; diff --git a/glsl_parser_extras.cpp b/glsl_parser_extras.cpp index 36a6ca8..a0fad52 100644 --- a/glsl_parser_extras.cpp +++ b/glsl_parser_extras.cpp @@ -239,72 +239,8 @@ ast_compound_statement::ast_compound_statement(int new_scope, void ast_expression::print(void) const { - static const char *const operators[] = { - "=", - "+", - "-", - "+", - "-", - "*", - "/", - "%", - "<<", - ">>", - "<", - ">", - "<=", - ">=", - "==", - "!=", - "&", - "^", - "|", - "~", - "&&", - "^^", - "!", - - "*=", - "/=", - "%=", - "+=", - "-=", - "<<=", - ">>=", - "&=", - "^=", - "|=", - - "?:", - "++", - "--", - "++", - "--", - ".", - }; - - switch (oper) { case ast_assign: - case ast_add: - case ast_sub: - case ast_mul: - case ast_div: - case ast_mod: - case ast_lshift: - case ast_rshift: - case ast_less: - case ast_greater: - case ast_lequal: - case ast_gequal: - case ast_equal: - case ast_nequal: - case ast_bit_and: - case ast_bit_xor: - case ast_bit_or: - case ast_logic_and: - case ast_logic_xor: - case ast_logic_or: case ast_mul_assign: case ast_div_assign: case ast_mod_assign: @@ -316,7 +252,7 @@ ast_expression::print(void) const case ast_xor_assign: case ast_or_assign: subexpressions[0]->print(); - printf("%s ", operators[oper]); + printf("%s ", operator_string(oper)); subexpressions[1]->print(); break; @@ -331,14 +267,14 @@ ast_expression::print(void) const case ast_logic_not: case ast_pre_inc: case ast_pre_dec: - printf("%s ", operators[oper]); + printf("%s ", operator_string(oper)); subexpressions[0]->print(); break; case ast_post_inc: case ast_post_dec: subexpressions[0]->print(); - printf("%s ", operators[oper]); + printf("%s ", operator_string(oper)); break; case ast_conditional: @@ -412,6 +348,10 @@ ast_expression::print(void) const printf(") "); break; } + + default: + assert(0); + break; } } -- 2.7.4