Add ast_expression_bin subclass of ast_expression
authorIan Romanick <ian.d.romanick@intel.com>
Tue, 23 Feb 2010 03:10:25 +0000 (19:10 -0800)
committerIan Romanick <ian.d.romanick@intel.com>
Tue, 23 Feb 2010 03:10:25 +0000 (19:10 -0800)
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
ast.h
ast_expr.cpp [new file with mode: 0644]
glsl_parser.y
glsl_parser_extras.cpp

index 0d9017c3069cd0652b9beef55b24bc0a57caf7f4..7b1f3f1f93f629bbebe6786cd954f0bc54de098d 100644 (file)
--- 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 591655d06ccd41a147475ad2dc245a891add1c7c..1f659933e989141e93d5c67f3cd25212076fcc8c 100644 (file)
--- 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 (file)
index 0000000..ad29cdc
--- /dev/null
@@ -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 <cstdio>
+#include <cassert>
+#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();
+}
index f9bfb0bc8127fe0c42c6ed4652a9850cd0635655..f4105652b8e998a97a3bd6fd0745f204b067a8f6 100644 (file)
@@ -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);
        }
        ;
 
index 36a6ca8264b4e38e774512dc5181da99855628d5..a0fad52d62ac3fd7a4818ad334a2f2cceef6e478 100644 (file)
@@ -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;
    }
 }