--- /dev/null
+/*
+ * 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();
+}
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);
}
;
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);
}
;
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);
}
;
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);
}
;
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);
}
;
equality_expression
| and_expression '&' equality_expression
{
- $$ = new ast_expression(ast_bit_or, $1, $3, NULL);
+ $$ = new ast_expression_bin(ast_bit_or, $1, $3);
}
;
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);
}
;
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);
}
;
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);
}
;
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);
}
;
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);
}
;