2 Copyright (C) 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
29 static char expression_syntax[] = "Syntax error in expression at %C";
32 /* Match a user-defined operator name. This is a normal name with a
33 few restrictions. The error_flag controls whether an error is
34 raised if 'true' or 'false' are used or not. */
37 gfc_match_defined_op_name (char *result, int error_flag)
39 static const char * const badops[] = {
40 "and", "or", "not", "eqv", "neqv", "eq", "ne", "ge", "le", "lt", "gt",
44 char name[GFC_MAX_SYMBOL_LEN + 1];
49 old_loc = *gfc_current_locus ();
51 m = gfc_match (" . %n .", name);
55 /* .true. and .false. have interpretations as constants. Trying to
56 use these as operators will fail at a later time. */
58 if (strcmp (name, "true") == 0 || strcmp (name, "false") == 0)
62 gfc_set_locus (&old_loc);
66 for (i = 0; badops[i]; i++)
67 if (strcmp (badops[i], name) == 0)
70 for (i = 0; name[i]; i++)
71 if (!ISALPHA (name[i]))
73 gfc_error ("Bad character '%c' in OPERATOR name at %C", name[i]);
77 strcpy (result, name);
81 gfc_error ("The name '%s' cannot be used as a defined operator at %C",
84 gfc_set_locus (&old_loc);
89 /* Match a user defined operator. The symbol found must be an
93 match_defined_operator (gfc_user_op ** result)
95 char name[GFC_MAX_SYMBOL_LEN + 1];
98 m = gfc_match_defined_op_name (name, 0);
102 *result = gfc_get_uop (name);
107 /* Check to see if the given operator is next on the input. If this
108 is not the case, the parse pointer remains where it was. */
111 next_operator (gfc_intrinsic_op t)
116 old_loc = *gfc_current_locus ();
117 if (gfc_match_intrinsic_op (&u) == MATCH_YES && t == u)
120 gfc_set_locus (&old_loc);
125 /* Match a primary expression. */
128 match_primary (gfc_expr ** result)
132 m = gfc_match_literal_constant (result, 0);
136 m = gfc_match_array_constructor (result);
140 m = gfc_match_rvalue (result);
144 /* Match an expression in parenthesis. */
145 if (gfc_match_char ('(') != MATCH_YES)
148 m = gfc_match_expr (result);
151 if (m == MATCH_ERROR)
154 m = gfc_match_char (')');
156 gfc_error ("Expected a right parenthesis in expression at %C");
160 gfc_free_expr (*result);
167 gfc_error (expression_syntax);
172 /* Build an operator expression node. */
175 build_node (gfc_intrinsic_op operator, locus * where,
176 gfc_expr * op1, gfc_expr * op2)
180 new = gfc_get_expr ();
181 new->expr_type = EXPR_OP;
182 new->operator = operator;
192 /* Match a level 1 expression. */
195 match_level_1 (gfc_expr ** result)
202 where = *gfc_current_locus ();
204 m = match_defined_operator (&uop);
205 if (m == MATCH_ERROR)
208 m = match_primary (&e);
216 f = build_node (INTRINSIC_USER, &where, e, NULL);
226 match_mult_operand (gfc_expr ** result)
228 gfc_expr *e, *exp, *r;
232 m = match_level_1 (&e);
236 if (!next_operator (INTRINSIC_POWER))
242 where = *gfc_current_locus ();
244 m = match_mult_operand (&exp);
246 gfc_error ("Expected exponent in expression at %C");
253 r = gfc_power (e, exp);
269 match_add_operand (gfc_expr ** result)
271 gfc_expr *all, *e, *total;
272 locus where, old_loc;
276 m = match_mult_operand (&all);
282 /* Build up a string of products or quotients. */
284 old_loc = *gfc_current_locus ();
286 if (next_operator (INTRINSIC_TIMES))
290 if (next_operator (INTRINSIC_DIVIDE))
291 i = INTRINSIC_DIVIDE;
296 where = *gfc_current_locus ();
298 m = match_mult_operand (&e);
301 gfc_set_locus (&old_loc);
305 if (m == MATCH_ERROR)
311 if (i == INTRINSIC_TIMES)
312 total = gfc_multiply (all, e);
314 total = gfc_divide (all, e);
336 if (next_operator (INTRINSIC_MINUS))
338 if (next_operator (INTRINSIC_PLUS))
344 /* Match a level 2 expression. */
347 match_level_2 (gfc_expr ** result)
349 gfc_expr *all, *e, *total;
354 where = *gfc_current_locus ();
357 m = match_add_operand (&e);
358 if (i != 0 && m == MATCH_NO)
360 gfc_error (expression_syntax);
372 all = gfc_uminus (e);
385 /* Append add-operands to the sum */
389 where = *gfc_current_locus ();
394 m = match_add_operand (&e);
396 gfc_error (expression_syntax);
404 total = gfc_subtract (all, e);
406 total = gfc_add (all, e);
424 /* Match a level three expression. */
427 match_level_3 (gfc_expr ** result)
429 gfc_expr *all, *e, *total;
433 m = match_level_2 (&all);
439 if (!next_operator (INTRINSIC_CONCAT))
442 where = *gfc_current_locus ();
444 m = match_level_2 (&e);
447 gfc_error (expression_syntax);
453 total = gfc_concat (all, e);
470 /* Match a level 4 expression. */
473 match_level_4 (gfc_expr ** result)
475 gfc_expr *left, *right, *r;
481 m = match_level_3 (&left);
485 old_loc = *gfc_current_locus ();
487 if (gfc_match_intrinsic_op (&i) != MATCH_YES)
493 if (i != INTRINSIC_EQ && i != INTRINSIC_NE && i != INTRINSIC_GE
494 && i != INTRINSIC_LE && i != INTRINSIC_LT && i != INTRINSIC_GT)
496 gfc_set_locus (&old_loc);
501 where = *gfc_current_locus ();
503 m = match_level_3 (&right);
505 gfc_error (expression_syntax);
508 gfc_free_expr (left);
515 r = gfc_eq (left, right);
519 r = gfc_ne (left, right);
523 r = gfc_lt (left, right);
527 r = gfc_le (left, right);
531 r = gfc_gt (left, right);
535 r = gfc_ge (left, right);
539 gfc_internal_error ("match_level_4(): Bad operator");
544 gfc_free_expr (left);
545 gfc_free_expr (right);
557 match_and_operand (gfc_expr ** result)
564 i = next_operator (INTRINSIC_NOT);
565 where = *gfc_current_locus ();
567 m = match_level_4 (&e);
590 match_or_operand (gfc_expr ** result)
592 gfc_expr *all, *e, *total;
596 m = match_and_operand (&all);
602 if (!next_operator (INTRINSIC_AND))
604 where = *gfc_current_locus ();
606 m = match_and_operand (&e);
608 gfc_error (expression_syntax);
615 total = gfc_and (all, e);
633 match_equiv_operand (gfc_expr ** result)
635 gfc_expr *all, *e, *total;
639 m = match_or_operand (&all);
645 if (!next_operator (INTRINSIC_OR))
647 where = *gfc_current_locus ();
649 m = match_or_operand (&e);
651 gfc_error (expression_syntax);
658 total = gfc_or (all, e);
675 /* Match a level 5 expression. */
678 match_level_5 (gfc_expr ** result)
680 gfc_expr *all, *e, *total;
685 m = match_equiv_operand (&all);
691 if (next_operator (INTRINSIC_EQV))
695 if (next_operator (INTRINSIC_NEQV))
701 where = *gfc_current_locus ();
703 m = match_equiv_operand (&e);
705 gfc_error (expression_syntax);
712 if (i == INTRINSIC_EQV)
713 total = gfc_eqv (all, e);
715 total = gfc_neqv (all, e);
733 /* Match an expression. At this level, we are stringing together
734 level 5 expressions separated by binary operators. */
737 gfc_match_expr (gfc_expr ** result)
744 m = match_level_5 (&all);
750 m = match_defined_operator (&uop);
753 if (m == MATCH_ERROR)
759 where = *gfc_current_locus ();
761 m = match_level_5 (&e);
763 gfc_error (expression_syntax);
770 all = build_node (INTRINSIC_USER, &where, all, e);