Shuffle ChangeLog entries into new files ChangeLog-1998,
[official-gcc.git] / gcc / fortran / matchexp.c
blob04fd31f3609ad6a180400e5288606dda9c20062d
1 /* Expression parser.
2 Copyright (C) 2000, 2001, 2002, 2004, 2005 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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "gfortran.h"
26 #include "arith.h"
27 #include "match.h"
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. */
36 match
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",
41 NULL
44 char name[GFC_MAX_SYMBOL_LEN + 1];
45 locus old_loc;
46 match m;
47 int i;
49 old_loc = gfc_current_locus;
51 m = gfc_match (" . %n .", name);
52 if (m != MATCH_YES)
53 return m;
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)
60 if (error_flag)
61 goto error;
62 gfc_current_locus = old_loc;
63 return MATCH_NO;
66 for (i = 0; badops[i]; i++)
67 if (strcmp (badops[i], name) == 0)
68 goto error;
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]);
74 return MATCH_ERROR;
77 strcpy (result, name);
78 return MATCH_YES;
80 error:
81 gfc_error ("The name '%s' cannot be used as a defined operator at %C",
82 name);
84 gfc_current_locus = old_loc;
85 return MATCH_ERROR;
89 /* Match a user defined operator. The symbol found must be an
90 operator already. */
92 static match
93 match_defined_operator (gfc_user_op ** result)
95 char name[GFC_MAX_SYMBOL_LEN + 1];
96 match m;
98 m = gfc_match_defined_op_name (name, 0);
99 if (m != MATCH_YES)
100 return m;
102 *result = gfc_get_uop (name);
103 return MATCH_YES;
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. */
110 static int
111 next_operator (gfc_intrinsic_op t)
113 gfc_intrinsic_op u;
114 locus old_loc;
116 old_loc = gfc_current_locus;
117 if (gfc_match_intrinsic_op (&u) == MATCH_YES && t == u)
118 return 1;
120 gfc_current_locus = old_loc;
121 return 0;
125 /* Match a primary expression. */
127 static match
128 match_primary (gfc_expr ** result)
130 match m;
132 m = gfc_match_literal_constant (result, 0);
133 if (m != MATCH_NO)
134 return m;
136 m = gfc_match_array_constructor (result);
137 if (m != MATCH_NO)
138 return m;
140 m = gfc_match_rvalue (result);
141 if (m != MATCH_NO)
142 return m;
144 /* Match an expression in parenthesis. */
145 if (gfc_match_char ('(') != MATCH_YES)
146 return MATCH_NO;
148 m = gfc_match_expr (result);
149 if (m == MATCH_NO)
150 goto syntax;
151 if (m == MATCH_ERROR)
152 return m;
154 m = gfc_match_char (')');
155 if (m == MATCH_NO)
156 gfc_error ("Expected a right parenthesis in expression at %C");
158 if (m != MATCH_YES)
160 gfc_free_expr (*result);
161 return MATCH_ERROR;
164 return MATCH_YES;
166 syntax:
167 gfc_error (expression_syntax);
168 return MATCH_ERROR;
172 /* Build an operator expression node. */
174 static gfc_expr *
175 build_node (gfc_intrinsic_op operator, locus * where,
176 gfc_expr * op1, gfc_expr * op2)
178 gfc_expr *new;
180 new = gfc_get_expr ();
181 new->expr_type = EXPR_OP;
182 new->value.op.operator = operator;
183 new->where = *where;
185 new->value.op.op1 = op1;
186 new->value.op.op2 = op2;
188 return new;
192 /* Match a level 1 expression. */
194 static match
195 match_level_1 (gfc_expr ** result)
197 gfc_user_op *uop;
198 gfc_expr *e, *f;
199 locus where;
200 match m;
202 where = gfc_current_locus;
203 uop = NULL;
204 m = match_defined_operator (&uop);
205 if (m == MATCH_ERROR)
206 return m;
208 m = match_primary (&e);
209 if (m != MATCH_YES)
210 return m;
212 if (uop == NULL)
213 *result = e;
214 else
216 f = build_node (INTRINSIC_USER, &where, e, NULL);
217 f->value.op.uop = uop;
218 *result = f;
221 return MATCH_YES;
225 /* As a GNU extension we support an expanded level-2 expression syntax.
226 Via this extension we support (arbitrary) nesting of unary plus and
227 minus operations following unary and binary operators, such as **.
228 The grammar of section 7.1.1.3 is effectively rewitten as:
230 R704 mult-operand is level-1-expr [ power-op ext-mult-operand ]
231 R704' ext-mult-operand is add-op ext-mult-operand
232 or mult-operand
233 R705 add-operand is add-operand mult-op ext-mult-operand
234 or mult-operand
235 R705' ext-add-operand is add-op ext-add-operand
236 or add-operand
237 R706 level-2-expr is [ level-2-expr ] add-op ext-add-operand
238 or add-operand
241 static match match_ext_mult_operand (gfc_expr ** result);
242 static match match_ext_add_operand (gfc_expr ** result);
245 static int
246 match_add_op (void)
249 if (next_operator (INTRINSIC_MINUS))
250 return -1;
251 if (next_operator (INTRINSIC_PLUS))
252 return 1;
253 return 0;
257 static match
258 match_mult_operand (gfc_expr ** result)
260 gfc_expr *e, *exp, *r;
261 locus where;
262 match m;
264 m = match_level_1 (&e);
265 if (m != MATCH_YES)
266 return m;
268 if (!next_operator (INTRINSIC_POWER))
270 *result = e;
271 return MATCH_YES;
274 where = gfc_current_locus;
276 m = match_ext_mult_operand (&exp);
277 if (m == MATCH_NO)
278 gfc_error ("Expected exponent in expression at %C");
279 if (m != MATCH_YES)
281 gfc_free_expr (e);
282 return MATCH_ERROR;
285 r = gfc_power (e, exp);
286 if (r == NULL)
288 gfc_free_expr (e);
289 gfc_free_expr (exp);
290 return MATCH_ERROR;
293 r->where = where;
294 *result = r;
296 return MATCH_YES;
300 static match
301 match_ext_mult_operand (gfc_expr ** result)
303 gfc_expr *all, *e;
304 locus where;
305 match m;
306 int i;
308 where = gfc_current_locus;
309 i = match_add_op ();
311 if (i == 0)
312 return match_mult_operand (result);
314 if (gfc_notify_std (GFC_STD_GNU, "Extension: Unary operator following"
315 " arithmetic operator (use parentheses) at %C")
316 == FAILURE)
317 return MATCH_ERROR;
319 m = match_ext_mult_operand (&e);
320 if (m != MATCH_YES)
321 return m;
323 if (i == -1)
324 all = gfc_uminus (e);
325 else
326 all = gfc_uplus (e);
328 if (all == NULL)
330 gfc_free_expr (e);
331 return MATCH_ERROR;
334 all->where = where;
335 *result = all;
336 return MATCH_YES;
340 static match
341 match_add_operand (gfc_expr ** result)
343 gfc_expr *all, *e, *total;
344 locus where, old_loc;
345 match m;
346 gfc_intrinsic_op i;
348 m = match_mult_operand (&all);
349 if (m != MATCH_YES)
350 return m;
352 for (;;)
354 /* Build up a string of products or quotients. */
356 old_loc = gfc_current_locus;
358 if (next_operator (INTRINSIC_TIMES))
359 i = INTRINSIC_TIMES;
360 else
362 if (next_operator (INTRINSIC_DIVIDE))
363 i = INTRINSIC_DIVIDE;
364 else
365 break;
368 where = gfc_current_locus;
370 m = match_ext_mult_operand (&e);
371 if (m == MATCH_NO)
373 gfc_current_locus = old_loc;
374 break;
377 if (m == MATCH_ERROR)
379 gfc_free_expr (all);
380 return MATCH_ERROR;
383 if (i == INTRINSIC_TIMES)
384 total = gfc_multiply (all, e);
385 else
386 total = gfc_divide (all, e);
388 if (total == NULL)
390 gfc_free_expr (all);
391 gfc_free_expr (e);
392 return MATCH_ERROR;
395 all = total;
396 all->where = where;
399 *result = all;
400 return MATCH_YES;
404 static match
405 match_ext_add_operand (gfc_expr ** result)
407 gfc_expr *all, *e;
408 locus where;
409 match m;
410 int i;
412 where = gfc_current_locus;
413 i = match_add_op ();
415 if (i == 0)
416 return match_add_operand (result);
418 if (gfc_notify_std (GFC_STD_GNU, "Extension: Unary operator following"
419 " arithmetic operator (use parentheses) at %C")
420 == FAILURE)
421 return MATCH_ERROR;
423 m = match_ext_add_operand (&e);
424 if (m != MATCH_YES)
425 return m;
427 if (i == -1)
428 all = gfc_uminus (e);
429 else
430 all = gfc_uplus (e);
432 if (all == NULL)
434 gfc_free_expr (e);
435 return MATCH_ERROR;
438 all->where = where;
439 *result = all;
440 return MATCH_YES;
444 /* Match a level 2 expression. */
446 static match
447 match_level_2 (gfc_expr ** result)
449 gfc_expr *all, *e, *total;
450 locus where;
451 match m;
452 int i;
454 where = gfc_current_locus;
455 i = match_add_op ();
457 if (i != 0)
459 m = match_ext_add_operand (&e);
460 if (m == MATCH_NO)
462 gfc_error (expression_syntax);
463 m = MATCH_ERROR;
466 else
467 m = match_add_operand (&e);
469 if (m != MATCH_YES)
470 return m;
472 if (i == 0)
473 all = e;
474 else
476 if (i == -1)
477 all = gfc_uminus (e);
478 else
479 all = gfc_uplus (e);
481 if (all == NULL)
483 gfc_free_expr (e);
484 return MATCH_ERROR;
488 all->where = where;
490 /* Append add-operands to the sum */
492 for (;;)
494 where = gfc_current_locus;
495 i = match_add_op ();
496 if (i == 0)
497 break;
499 m = match_ext_add_operand (&e);
500 if (m == MATCH_NO)
501 gfc_error (expression_syntax);
502 if (m != MATCH_YES)
504 gfc_free_expr (all);
505 return MATCH_ERROR;
508 if (i == -1)
509 total = gfc_subtract (all, e);
510 else
511 total = gfc_add (all, e);
513 if (total == NULL)
515 gfc_free_expr (all);
516 gfc_free_expr (e);
517 return MATCH_ERROR;
520 all = total;
521 all->where = where;
524 *result = all;
525 return MATCH_YES;
529 /* Match a level three expression. */
531 static match
532 match_level_3 (gfc_expr ** result)
534 gfc_expr *all, *e, *total;
535 locus where;
536 match m;
538 m = match_level_2 (&all);
539 if (m != MATCH_YES)
540 return m;
542 for (;;)
544 if (!next_operator (INTRINSIC_CONCAT))
545 break;
547 where = gfc_current_locus;
549 m = match_level_2 (&e);
550 if (m == MATCH_NO)
552 gfc_error (expression_syntax);
553 gfc_free_expr (all);
555 if (m != MATCH_YES)
556 return MATCH_ERROR;
558 total = gfc_concat (all, e);
559 if (total == NULL)
561 gfc_free_expr (all);
562 gfc_free_expr (e);
563 return MATCH_ERROR;
566 all = total;
567 all->where = where;
570 *result = all;
571 return MATCH_YES;
575 /* Match a level 4 expression. */
577 static match
578 match_level_4 (gfc_expr ** result)
580 gfc_expr *left, *right, *r;
581 gfc_intrinsic_op i;
582 locus old_loc;
583 locus where;
584 match m;
586 m = match_level_3 (&left);
587 if (m != MATCH_YES)
588 return m;
590 old_loc = gfc_current_locus;
592 if (gfc_match_intrinsic_op (&i) != MATCH_YES)
594 *result = left;
595 return MATCH_YES;
598 if (i != INTRINSIC_EQ && i != INTRINSIC_NE && i != INTRINSIC_GE
599 && i != INTRINSIC_LE && i != INTRINSIC_LT && i != INTRINSIC_GT)
601 gfc_current_locus = old_loc;
602 *result = left;
603 return MATCH_YES;
606 where = gfc_current_locus;
608 m = match_level_3 (&right);
609 if (m == MATCH_NO)
610 gfc_error (expression_syntax);
611 if (m != MATCH_YES)
613 gfc_free_expr (left);
614 return MATCH_ERROR;
617 switch (i)
619 case INTRINSIC_EQ:
620 r = gfc_eq (left, right);
621 break;
623 case INTRINSIC_NE:
624 r = gfc_ne (left, right);
625 break;
627 case INTRINSIC_LT:
628 r = gfc_lt (left, right);
629 break;
631 case INTRINSIC_LE:
632 r = gfc_le (left, right);
633 break;
635 case INTRINSIC_GT:
636 r = gfc_gt (left, right);
637 break;
639 case INTRINSIC_GE:
640 r = gfc_ge (left, right);
641 break;
643 default:
644 gfc_internal_error ("match_level_4(): Bad operator");
647 if (r == NULL)
649 gfc_free_expr (left);
650 gfc_free_expr (right);
651 return MATCH_ERROR;
654 r->where = where;
655 *result = r;
657 return MATCH_YES;
661 static match
662 match_and_operand (gfc_expr ** result)
664 gfc_expr *e, *r;
665 locus where;
666 match m;
667 int i;
669 i = next_operator (INTRINSIC_NOT);
670 where = gfc_current_locus;
672 m = match_level_4 (&e);
673 if (m != MATCH_YES)
674 return m;
676 r = e;
677 if (i)
679 r = gfc_not (e);
680 if (r == NULL)
682 gfc_free_expr (e);
683 return MATCH_ERROR;
687 r->where = where;
688 *result = r;
690 return MATCH_YES;
694 static match
695 match_or_operand (gfc_expr ** result)
697 gfc_expr *all, *e, *total;
698 locus where;
699 match m;
701 m = match_and_operand (&all);
702 if (m != MATCH_YES)
703 return m;
705 for (;;)
707 if (!next_operator (INTRINSIC_AND))
708 break;
709 where = gfc_current_locus;
711 m = match_and_operand (&e);
712 if (m == MATCH_NO)
713 gfc_error (expression_syntax);
714 if (m != MATCH_YES)
716 gfc_free_expr (all);
717 return MATCH_ERROR;
720 total = gfc_and (all, e);
721 if (total == NULL)
723 gfc_free_expr (all);
724 gfc_free_expr (e);
725 return MATCH_ERROR;
728 all = total;
729 all->where = where;
732 *result = all;
733 return MATCH_YES;
737 static match
738 match_equiv_operand (gfc_expr ** result)
740 gfc_expr *all, *e, *total;
741 locus where;
742 match m;
744 m = match_or_operand (&all);
745 if (m != MATCH_YES)
746 return m;
748 for (;;)
750 if (!next_operator (INTRINSIC_OR))
751 break;
752 where = gfc_current_locus;
754 m = match_or_operand (&e);
755 if (m == MATCH_NO)
756 gfc_error (expression_syntax);
757 if (m != MATCH_YES)
759 gfc_free_expr (all);
760 return MATCH_ERROR;
763 total = gfc_or (all, e);
764 if (total == NULL)
766 gfc_free_expr (all);
767 gfc_free_expr (e);
768 return MATCH_ERROR;
771 all = total;
772 all->where = where;
775 *result = all;
776 return MATCH_YES;
780 /* Match a level 5 expression. */
782 static match
783 match_level_5 (gfc_expr ** result)
785 gfc_expr *all, *e, *total;
786 locus where;
787 match m;
788 gfc_intrinsic_op i;
790 m = match_equiv_operand (&all);
791 if (m != MATCH_YES)
792 return m;
794 for (;;)
796 if (next_operator (INTRINSIC_EQV))
797 i = INTRINSIC_EQV;
798 else
800 if (next_operator (INTRINSIC_NEQV))
801 i = INTRINSIC_NEQV;
802 else
803 break;
806 where = gfc_current_locus;
808 m = match_equiv_operand (&e);
809 if (m == MATCH_NO)
810 gfc_error (expression_syntax);
811 if (m != MATCH_YES)
813 gfc_free_expr (all);
814 return MATCH_ERROR;
817 if (i == INTRINSIC_EQV)
818 total = gfc_eqv (all, e);
819 else
820 total = gfc_neqv (all, e);
822 if (total == NULL)
824 gfc_free_expr (all);
825 gfc_free_expr (e);
826 return MATCH_ERROR;
829 all = total;
830 all->where = where;
833 *result = all;
834 return MATCH_YES;
838 /* Match an expression. At this level, we are stringing together
839 level 5 expressions separated by binary operators. */
841 match
842 gfc_match_expr (gfc_expr ** result)
844 gfc_expr *all, *e;
845 gfc_user_op *uop;
846 locus where;
847 match m;
849 m = match_level_5 (&all);
850 if (m != MATCH_YES)
851 return m;
853 for (;;)
855 m = match_defined_operator (&uop);
856 if (m == MATCH_NO)
857 break;
858 if (m == MATCH_ERROR)
860 gfc_free_expr (all);
861 return MATCH_ERROR;
864 where = gfc_current_locus;
866 m = match_level_5 (&e);
867 if (m == MATCH_NO)
868 gfc_error (expression_syntax);
869 if (m != MATCH_YES)
871 gfc_free_expr (all);
872 return MATCH_ERROR;
875 all = build_node (INTRINSIC_USER, &where, all, e);
876 all->value.op.uop = uop;
879 *result = all;
880 return MATCH_YES;