* findvar.c, value.h (symbol_read_needs_frame): New function.
[platform/upstream/binutils.git] / gdb / m2-exp.y
1 /* YACC grammar for Modula-2 expressions, for GDB.
2    Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
3    Generated from expread.y (now c-exp.y) and contributed by the Department
4    of Computer Science at the State University of New York at Buffalo, 1991.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22 /* Parse a Modula-2 expression from text in a string,
23    and return the result as a  struct expression  pointer.
24    That structure contains arithmetic operations in reverse polish,
25    with constants represented by operations that are followed by special data.
26    See expression.h for the details of the format.
27    What is important here is that it can be built up sequentially
28    during the process of parsing; the lower levels of the tree always
29    come first in the result.
30
31    Note that malloc's and realloc's in this file are transformed to
32    xmalloc and xrealloc respectively by the same sed command in the
33    makefile that remaps any other malloc/realloc inserted by the parser
34    generator.  Doing this with #defines and trying to control the interaction
35    with include files (<malloc.h> and <stdlib.h> for example) just became
36    too messy, particularly when such includes can be inserted at random
37    times by the parser generator. */
38    
39 %{
40
41 #include "defs.h"
42 #include "expression.h"
43 #include "language.h"
44 #include "value.h"
45 #include "parser-defs.h"
46 #include "m2-lang.h"
47
48 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
49    as well as gratuitiously global symbol names, so we can have multiple
50    yacc generated parsers in gdb.  Note that these are only the variables
51    produced by yacc.  If other parser generators (bison, byacc, etc) produce
52    additional global names that conflict at link time, then those parser
53    generators need to be fixed instead of adding those names to this list. */
54
55 #define yymaxdepth m2_maxdepth
56 #define yyparse m2_parse
57 #define yylex   m2_lex
58 #define yyerror m2_error
59 #define yylval  m2_lval
60 #define yychar  m2_char
61 #define yydebug m2_debug
62 #define yypact  m2_pact
63 #define yyr1    m2_r1
64 #define yyr2    m2_r2
65 #define yydef   m2_def
66 #define yychk   m2_chk
67 #define yypgo   m2_pgo
68 #define yyact   m2_act
69 #define yyexca  m2_exca
70 #define yyerrflag m2_errflag
71 #define yynerrs m2_nerrs
72 #define yyps    m2_ps
73 #define yypv    m2_pv
74 #define yys     m2_s
75 #define yy_yys  m2_yys
76 #define yystate m2_state
77 #define yytmp   m2_tmp
78 #define yyv     m2_v
79 #define yy_yyv  m2_yyv
80 #define yyval   m2_val
81 #define yylloc  m2_lloc
82 #define yyreds  m2_reds         /* With YYDEBUG defined */
83 #define yytoks  m2_toks         /* With YYDEBUG defined */
84
85 #ifndef YYDEBUG
86 #define YYDEBUG 0               /* Default to no yydebug support */
87 #endif
88
89 int
90 yyparse PARAMS ((void));
91
92 static int
93 yylex PARAMS ((void));
94
95 void
96 yyerror PARAMS ((char *));
97
98 #if 0
99 static char *
100 make_qualname PARAMS ((char *, char *));
101 #endif
102
103 static int
104 parse_number PARAMS ((int));
105
106 /* The sign of the number being parsed. */
107 static int number_sign = 1;
108
109 /* The block that the module specified by the qualifer on an identifer is
110    contained in, */
111 #if 0
112 static struct block *modblock=0;
113 #endif
114
115 %}
116
117 /* Although the yacc "value" of an expression is not used,
118    since the result is stored in the structure being created,
119    other node types do have values.  */
120
121 %union
122   {
123     LONGEST lval;
124     unsigned LONGEST ulval;
125     double dval;
126     struct symbol *sym;
127     struct type *tval;
128     struct stoken sval;
129     int voidval;
130     struct block *bval;
131     enum exp_opcode opcode;
132     struct internalvar *ivar;
133
134     struct type **tvec;
135     int *ivec;
136   }
137
138 %type <voidval> exp type_exp start set
139 %type <voidval> variable
140 %type <tval> type
141 %type <bval> block 
142 %type <sym> fblock 
143
144 %token <lval> INT HEX ERROR
145 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
146 %token <dval> FLOAT
147
148 /* Both NAME and TYPENAME tokens represent symbols in the input,
149    and both convey their data as strings.
150    But a TYPENAME is a string that happens to be defined as a typedef
151    or builtin type name (such as int or char)
152    and a NAME is any other symbol.
153
154    Contexts where this distinction is not important can use the
155    nonterminal "name", which matches either NAME or TYPENAME.  */
156
157 %token <sval> STRING
158 %token <sval> NAME BLOCKNAME IDENT VARNAME
159 %token <sval> TYPENAME
160
161 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
162 %token INC DEC INCL EXCL
163
164 /* The GDB scope operator */
165 %token COLONCOLON
166
167 %token <lval> LAST REGNAME
168
169 %token <ivar> INTERNAL_VAR
170
171 /* M2 tokens */
172 %left ','
173 %left ABOVE_COMMA
174 %nonassoc ASSIGN
175 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
176 %left OROR
177 %left LOGICAL_AND '&'
178 %left '@'
179 %left '+' '-'
180 %left '*' '/' DIV MOD
181 %right UNARY
182 %right '^' DOT '[' '('
183 %right NOT '~'
184 %left COLONCOLON QID
185 /* This is not an actual token ; it is used for precedence. 
186 %right QID
187 */
188
189 \f
190 %%
191
192 start   :       exp
193         |       type_exp
194         ;
195
196 type_exp:       type
197                 { write_exp_elt_opcode(OP_TYPE);
198                   write_exp_elt_type($1);
199                   write_exp_elt_opcode(OP_TYPE);
200                 }
201         ;
202
203 /* Expressions */
204
205 exp     :       exp '^'   %prec UNARY
206                         { write_exp_elt_opcode (UNOP_IND); }
207
208 exp     :       '-'
209                         { number_sign = -1; }
210                 exp    %prec UNARY
211                         { number_sign = 1;
212                           write_exp_elt_opcode (UNOP_NEG); }
213         ;
214
215 exp     :       '+' exp    %prec UNARY
216                 { write_exp_elt_opcode(UNOP_PLUS); }
217         ;
218
219 exp     :       not_exp exp %prec UNARY
220                         { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
221         ;
222
223 not_exp :       NOT
224         |       '~'
225         ;
226
227 exp     :       CAP '(' exp ')'
228                         { write_exp_elt_opcode (UNOP_CAP); }
229         ;
230
231 exp     :       ORD '(' exp ')'
232                         { write_exp_elt_opcode (UNOP_ORD); }
233         ;
234
235 exp     :       ABS '(' exp ')'
236                         { write_exp_elt_opcode (UNOP_ABS); }
237         ;
238
239 exp     :       HIGH '(' exp ')'
240                         { write_exp_elt_opcode (UNOP_HIGH); }
241         ;
242
243 exp     :       MIN_FUNC '(' type ')'
244                         { write_exp_elt_opcode (UNOP_MIN);
245                           write_exp_elt_type ($3);
246                           write_exp_elt_opcode (UNOP_MIN); }
247         ;
248
249 exp     :       MAX_FUNC '(' type ')'
250                         { write_exp_elt_opcode (UNOP_MAX);
251                           write_exp_elt_type ($3);
252                           write_exp_elt_opcode (UNOP_MIN); }
253         ;
254
255 exp     :       FLOAT_FUNC '(' exp ')'
256                         { write_exp_elt_opcode (UNOP_FLOAT); }
257         ;
258
259 exp     :       VAL '(' type ',' exp ')'
260                         { write_exp_elt_opcode (BINOP_VAL);
261                           write_exp_elt_type ($3);
262                           write_exp_elt_opcode (BINOP_VAL); }
263         ;
264
265 exp     :       CHR '(' exp ')'
266                         { write_exp_elt_opcode (UNOP_CHR); }
267         ;
268
269 exp     :       ODD '(' exp ')'
270                         { write_exp_elt_opcode (UNOP_ODD); }
271         ;
272
273 exp     :       TRUNC '(' exp ')'
274                         { write_exp_elt_opcode (UNOP_TRUNC); }
275         ;
276
277 exp     :       SIZE exp       %prec UNARY
278                         { write_exp_elt_opcode (UNOP_SIZEOF); }
279         ;
280
281
282 exp     :       INC '(' exp ')'
283                         { write_exp_elt_opcode(UNOP_PREINCREMENT); }
284         ;
285
286 exp     :       INC '(' exp ',' exp ')'
287                         { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
288                           write_exp_elt_opcode(BINOP_ADD);
289                           write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
290         ;
291
292 exp     :       DEC '(' exp ')'
293                         { write_exp_elt_opcode(UNOP_PREDECREMENT);}
294         ;
295
296 exp     :       DEC '(' exp ',' exp ')'
297                         { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
298                           write_exp_elt_opcode(BINOP_SUB);
299                           write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
300         ;
301
302 exp     :       exp DOT NAME
303                         { write_exp_elt_opcode (STRUCTOP_STRUCT);
304                           write_exp_string ($3);
305                           write_exp_elt_opcode (STRUCTOP_STRUCT); }
306         ;
307
308 exp     :       set
309         ;
310
311 exp     :       exp IN set
312                         { error("Sets are not implemented.");}
313         ;
314
315 exp     :       INCL '(' exp ',' exp ')'
316                         { error("Sets are not implemented.");}
317         ;
318
319 exp     :       EXCL '(' exp ',' exp ')'
320                         { error("Sets are not implemented.");}
321
322 set     :       '{' arglist '}'
323                         { error("Sets are not implemented.");}
324         |       type '{' arglist '}'
325                         { error("Sets are not implemented.");}
326         ;
327
328
329 /* Modula-2 array subscript notation [a,b,c...] */
330 exp     :       exp '['
331                         /* This function just saves the number of arguments
332                            that follow in the list.  It is *not* specific to
333                            function types */
334                         { start_arglist(); }
335                 non_empty_arglist ']'  %prec DOT
336                         { write_exp_elt_opcode (MULTI_SUBSCRIPT);
337                           write_exp_elt_longcst ((LONGEST) end_arglist());
338                           write_exp_elt_opcode (MULTI_SUBSCRIPT); }
339         ;
340
341 exp     :       exp '('
342                         /* This is to save the value of arglist_len
343                            being accumulated by an outer function call.  */
344                         { start_arglist (); }
345                 arglist ')'     %prec DOT
346                         { write_exp_elt_opcode (OP_FUNCALL);
347                           write_exp_elt_longcst ((LONGEST) end_arglist ());
348                           write_exp_elt_opcode (OP_FUNCALL); }
349         ;
350
351 arglist :
352         ;
353
354 arglist :       exp
355                         { arglist_len = 1; }
356         ;
357
358 arglist :       arglist ',' exp   %prec ABOVE_COMMA
359                         { arglist_len++; }
360         ;
361
362 non_empty_arglist
363         :       exp
364                         { arglist_len = 1; }
365         ;
366
367 non_empty_arglist
368         :       non_empty_arglist ',' exp %prec ABOVE_COMMA
369                         { arglist_len++; }
370         ;
371
372 /* GDB construct */
373 exp     :       '{' type '}' exp  %prec UNARY
374                         { write_exp_elt_opcode (UNOP_MEMVAL);
375                           write_exp_elt_type ($2);
376                           write_exp_elt_opcode (UNOP_MEMVAL); }
377         ;
378
379 exp     :       type '(' exp ')' %prec UNARY
380                         { write_exp_elt_opcode (UNOP_CAST);
381                           write_exp_elt_type ($1);
382                           write_exp_elt_opcode (UNOP_CAST); }
383         ;
384
385 exp     :       '(' exp ')'
386                         { }
387         ;
388
389 /* Binary operators in order of decreasing precedence.  Note that some
390    of these operators are overloaded!  (ie. sets) */
391
392 /* GDB construct */
393 exp     :       exp '@' exp
394                         { write_exp_elt_opcode (BINOP_REPEAT); }
395         ;
396
397 exp     :       exp '*' exp
398                         { write_exp_elt_opcode (BINOP_MUL); }
399         ;
400
401 exp     :       exp '/' exp
402                         { write_exp_elt_opcode (BINOP_DIV); }
403         ;
404
405 exp     :       exp DIV exp
406                         { write_exp_elt_opcode (BINOP_INTDIV); }
407         ;
408
409 exp     :       exp MOD exp
410                         { write_exp_elt_opcode (BINOP_REM); }
411         ;
412
413 exp     :       exp '+' exp
414                         { write_exp_elt_opcode (BINOP_ADD); }
415         ;
416
417 exp     :       exp '-' exp
418                         { write_exp_elt_opcode (BINOP_SUB); }
419         ;
420
421 exp     :       exp '=' exp
422                         { write_exp_elt_opcode (BINOP_EQUAL); }
423         ;
424
425 exp     :       exp NOTEQUAL exp
426                         { write_exp_elt_opcode (BINOP_NOTEQUAL); }
427         |       exp '#' exp
428                         { write_exp_elt_opcode (BINOP_NOTEQUAL); }
429         ;
430
431 exp     :       exp LEQ exp
432                         { write_exp_elt_opcode (BINOP_LEQ); }
433         ;
434
435 exp     :       exp GEQ exp
436                         { write_exp_elt_opcode (BINOP_GEQ); }
437         ;
438
439 exp     :       exp '<' exp
440                         { write_exp_elt_opcode (BINOP_LESS); }
441         ;
442
443 exp     :       exp '>' exp
444                         { write_exp_elt_opcode (BINOP_GTR); }
445         ;
446
447 exp     :       exp LOGICAL_AND exp
448                         { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
449         ;
450
451 exp     :       exp OROR exp
452                         { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
453         ;
454
455 exp     :       exp ASSIGN exp
456                         { write_exp_elt_opcode (BINOP_ASSIGN); }
457         ;
458
459
460 /* Constants */
461
462 exp     :       M2_TRUE
463                         { write_exp_elt_opcode (OP_BOOL);
464                           write_exp_elt_longcst ((LONGEST) $1);
465                           write_exp_elt_opcode (OP_BOOL); }
466         ;
467
468 exp     :       M2_FALSE
469                         { write_exp_elt_opcode (OP_BOOL);
470                           write_exp_elt_longcst ((LONGEST) $1);
471                           write_exp_elt_opcode (OP_BOOL); }
472         ;
473
474 exp     :       INT
475                         { write_exp_elt_opcode (OP_LONG);
476                           write_exp_elt_type (builtin_type_m2_int);
477                           write_exp_elt_longcst ((LONGEST) $1);
478                           write_exp_elt_opcode (OP_LONG); }
479         ;
480
481 exp     :       UINT
482                         {
483                           write_exp_elt_opcode (OP_LONG);
484                           write_exp_elt_type (builtin_type_m2_card);
485                           write_exp_elt_longcst ((LONGEST) $1);
486                           write_exp_elt_opcode (OP_LONG);
487                         }
488         ;
489
490 exp     :       CHAR
491                         { write_exp_elt_opcode (OP_LONG);
492                           write_exp_elt_type (builtin_type_m2_char);
493                           write_exp_elt_longcst ((LONGEST) $1);
494                           write_exp_elt_opcode (OP_LONG); }
495         ;
496
497
498 exp     :       FLOAT
499                         { write_exp_elt_opcode (OP_DOUBLE);
500                           write_exp_elt_type (builtin_type_m2_real);
501                           write_exp_elt_dblcst ($1);
502                           write_exp_elt_opcode (OP_DOUBLE); }
503         ;
504
505 exp     :       variable
506         ;
507
508 /* The GDB internal variable $$, et al. */
509 exp     :       LAST
510                         { write_exp_elt_opcode (OP_LAST);
511                           write_exp_elt_longcst ((LONGEST) $1);
512                           write_exp_elt_opcode (OP_LAST); }
513         ;
514
515 exp     :       REGNAME
516                         { write_exp_elt_opcode (OP_REGISTER);
517                           write_exp_elt_longcst ((LONGEST) $1);
518                           write_exp_elt_opcode (OP_REGISTER); }
519         ;
520
521 exp     :       SIZE '(' type ')'       %prec UNARY
522                         { write_exp_elt_opcode (OP_LONG);
523                           write_exp_elt_type (builtin_type_int);
524                           write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
525                           write_exp_elt_opcode (OP_LONG); }
526         ;
527
528 exp     :       STRING
529                         { write_exp_elt_opcode (OP_M2_STRING);
530                           write_exp_string ($1);
531                           write_exp_elt_opcode (OP_M2_STRING); }
532         ;
533
534 /* This will be used for extensions later.  Like adding modules. */
535 block   :       fblock  
536                         { $$ = SYMBOL_BLOCK_VALUE($1); }
537         ;
538
539 fblock  :       BLOCKNAME
540                         { struct symbol *sym
541                             = lookup_symbol (copy_name ($1), expression_context_block,
542                                              VAR_NAMESPACE, 0, NULL);
543                           $$ = sym;}
544         ;
545                              
546
547 /* GDB scope operator */
548 fblock  :       block COLONCOLON BLOCKNAME
549                         { struct symbol *tem
550                             = lookup_symbol (copy_name ($3), $1,
551                                              VAR_NAMESPACE, 0, NULL);
552                           if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
553                             error ("No function \"%s\" in specified context.",
554                                    copy_name ($3));
555                           $$ = tem;
556                         }
557         ;
558
559 /* Useful for assigning to PROCEDURE variables */
560 variable:       fblock
561                         { write_exp_elt_opcode(OP_VAR_VALUE);
562                           write_exp_elt_block (NULL);
563                           write_exp_elt_sym ($1);
564                           write_exp_elt_opcode (OP_VAR_VALUE); }
565         ;
566
567 /* GDB internal ($foo) variable */
568 variable:       INTERNAL_VAR
569                         { write_exp_elt_opcode (OP_INTERNALVAR);
570                           write_exp_elt_intern ($1);
571                           write_exp_elt_opcode (OP_INTERNALVAR); }
572         ;
573
574 /* GDB scope operator */
575 variable:       block COLONCOLON NAME
576                         { struct symbol *sym;
577                           sym = lookup_symbol (copy_name ($3), $1,
578                                                VAR_NAMESPACE, 0, NULL);
579                           if (sym == 0)
580                             error ("No symbol \"%s\" in specified context.",
581                                    copy_name ($3));
582
583                           write_exp_elt_opcode (OP_VAR_VALUE);
584                           /* block_found is set by lookup_symbol.  */
585                           write_exp_elt_block (block_found);
586                           write_exp_elt_sym (sym);
587                           write_exp_elt_opcode (OP_VAR_VALUE); }
588         ;
589
590 /* Base case for variables. */
591 variable:       NAME
592                         { struct symbol *sym;
593                           int is_a_field_of_this;
594
595                           sym = lookup_symbol (copy_name ($1),
596                                                expression_context_block,
597                                                VAR_NAMESPACE,
598                                                &is_a_field_of_this,
599                                                NULL);
600                           if (sym)
601                             {
602                               if (symbol_read_needs_frame (sym))
603                                 {
604                                   if (innermost_block == 0 ||
605                                       contained_in (block_found, 
606                                                     innermost_block))
607                                     innermost_block = block_found;
608                                 }
609
610                               write_exp_elt_opcode (OP_VAR_VALUE);
611                               /* We want to use the selected frame, not
612                                  another more inner frame which happens to
613                                  be in the same block.  */
614                               write_exp_elt_block (NULL);
615                               write_exp_elt_sym (sym);
616                               write_exp_elt_opcode (OP_VAR_VALUE);
617                             }
618                           else
619                             {
620                               struct minimal_symbol *msymbol;
621                               register char *arg = copy_name ($1);
622
623                               msymbol = lookup_minimal_symbol (arg,
624                                           (struct objfile *) NULL);
625                               if (msymbol != NULL)
626                                 {
627                                   write_exp_elt_opcode (OP_LONG);
628                                   write_exp_elt_type (builtin_type_long);
629                                   write_exp_elt_longcst ((LONGEST) SYMBOL_VALUE_ADDRESS (msymbol));
630                                   write_exp_elt_opcode (OP_LONG);
631                                   write_exp_elt_opcode (UNOP_MEMVAL);
632                                   if (msymbol -> type == mst_data ||
633                                       msymbol -> type == mst_bss)
634                                     write_exp_elt_type (builtin_type_int);
635                                   else if (msymbol -> type == mst_text)
636                                     write_exp_elt_type (lookup_function_type (builtin_type_int));
637                                   else
638                                     write_exp_elt_type (builtin_type_char);
639                                   write_exp_elt_opcode (UNOP_MEMVAL);
640                                 }
641                               else if (!have_full_symbols () && !have_partial_symbols ())
642                                 error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
643                               else
644                                 error ("No symbol \"%s\" in current context.",
645                                        copy_name ($1));
646                             }
647                         }
648         ;
649
650 type
651         :       TYPENAME
652                         { $$ = lookup_typename (copy_name ($1),
653                                                 expression_context_block, 0); }
654
655         ;
656
657 %%
658
659 #if 0  /* FIXME! */
660 int
661 overflow(a,b)
662    long a,b;
663 {
664    return (MAX_OF_TYPE(builtin_type_m2_int) - b) < a;
665 }
666
667 int
668 uoverflow(a,b)
669    unsigned long a,b;
670 {
671    return (MAX_OF_TYPE(builtin_type_m2_card) - b) < a;
672 }
673 #endif /* FIXME */
674
675 /* Take care of parsing a number (anything that starts with a digit).
676    Set yylval and return the token type; update lexptr.
677    LEN is the number of characters in it.  */
678
679 /*** Needs some error checking for the float case ***/
680
681 static int
682 parse_number (olen)
683      int olen;
684 {
685   register char *p = lexptr;
686   register LONGEST n = 0;
687   register LONGEST prevn = 0;
688   register int c,i,ischar=0;
689   register int base = input_radix;
690   register int len = olen;
691   int unsigned_p = number_sign == 1 ? 1 : 0;
692
693   if(p[len-1] == 'H')
694   {
695      base = 16;
696      len--;
697   }
698   else if(p[len-1] == 'C' || p[len-1] == 'B')
699   {
700      base = 8;
701      ischar = p[len-1] == 'C';
702      len--;
703   }
704
705   /* Scan the number */
706   for (c = 0; c < len; c++)
707   {
708     if (p[c] == '.' && base == 10)
709       {
710         /* It's a float since it contains a point.  */
711         yylval.dval = atof (p);
712         lexptr += len;
713         return FLOAT;
714       }
715     if (p[c] == '.' && base != 10)
716        error("Floating point numbers must be base 10.");
717     if (base == 10 && (p[c] < '0' || p[c] > '9'))
718        error("Invalid digit \'%c\' in number.",p[c]);
719  }
720
721   while (len-- > 0)
722     {
723       c = *p++;
724       n *= base;
725       if( base == 8 && (c == '8' || c == '9'))
726          error("Invalid digit \'%c\' in octal number.",c);
727       if (c >= '0' && c <= '9')
728         i = c - '0';
729       else
730         {
731           if (base == 16 && c >= 'A' && c <= 'F')
732             i = c - 'A' + 10;
733           else
734              return ERROR;
735         }
736       n+=i;
737       if(i >= base)
738          return ERROR;
739       if(!unsigned_p && number_sign == 1 && (prevn >= n))
740          unsigned_p=1;          /* Try something unsigned */
741       /* Don't do the range check if n==i and i==0, since that special
742          case will give an overflow error. */
743       if(RANGE_CHECK && n!=i && i)
744       {
745          if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
746             ((!unsigned_p && number_sign==-1) && -prevn <= -n))
747             range_error("Overflow on numeric constant.");
748       }
749          prevn=n;
750     }
751
752   lexptr = p;
753   if(*p == 'B' || *p == 'C' || *p == 'H')
754      lexptr++;                  /* Advance past B,C or H */
755
756   if (ischar)
757   {
758      yylval.ulval = n;
759      return CHAR;
760   }
761   else if ( unsigned_p && number_sign == 1)
762   {
763      yylval.ulval = n;
764      return UINT;
765   }
766   else if((unsigned_p && (n<0))) {
767      range_error("Overflow on numeric constant -- number too large.");
768      /* But, this can return if range_check == range_warn.  */
769   }
770   yylval.lval = n;
771   return INT;
772 }
773
774
775 /* Some tokens */
776
777 static struct
778 {
779    char name[2];
780    int token;
781 } tokentab2[] =
782 {
783     { {'<', '>'},    NOTEQUAL   },
784     { {':', '='},    ASSIGN     },
785     { {'<', '='},    LEQ        },
786     { {'>', '='},    GEQ        },
787     { {':', ':'},    COLONCOLON },
788
789 };
790
791 /* Some specific keywords */
792
793 struct keyword {
794    char keyw[10];
795    int token;
796 };
797
798 static struct keyword keytab[] =
799 {
800     {"OR" ,   OROR       },
801     {"IN",    IN         },/* Note space after IN */
802     {"AND",   LOGICAL_AND},
803     {"ABS",   ABS        },
804     {"CHR",   CHR        },
805     {"DEC",   DEC        },
806     {"NOT",   NOT        },
807     {"DIV",   DIV        },
808     {"INC",   INC        },
809     {"MAX",   MAX_FUNC   },
810     {"MIN",   MIN_FUNC   },
811     {"MOD",   MOD        },
812     {"ODD",   ODD        },
813     {"CAP",   CAP        },
814     {"ORD",   ORD        },
815     {"VAL",   VAL        },
816     {"EXCL",  EXCL       },
817     {"HIGH",  HIGH       },
818     {"INCL",  INCL       },
819     {"SIZE",  SIZE       },
820     {"FLOAT", FLOAT_FUNC },
821     {"TRUNC", TRUNC      },
822 };
823
824
825 /* Read one token, getting characters through lexptr.  */
826
827 /* This is where we will check to make sure that the language and the operators used are
828    compatible  */
829
830 static int
831 yylex ()
832 {
833   register int c;
834   register int namelen;
835   register int i;
836   register char *tokstart;
837   register char quote;
838
839  retry:
840
841   tokstart = lexptr;
842
843
844   /* See if it is a special token of length 2 */
845   for( i = 0 ; i < sizeof tokentab2 / sizeof tokentab2[0] ; i++)
846      if(STREQN(tokentab2[i].name, tokstart, 2))
847      {
848         lexptr += 2;
849         return tokentab2[i].token;
850      }
851
852   switch (c = *tokstart)
853     {
854     case 0:
855       return 0;
856
857     case ' ':
858     case '\t':
859     case '\n':
860       lexptr++;
861       goto retry;
862
863     case '(':
864       paren_depth++;
865       lexptr++;
866       return c;
867
868     case ')':
869       if (paren_depth == 0)
870         return 0;
871       paren_depth--;
872       lexptr++;
873       return c;
874
875     case ',':
876       if (comma_terminates && paren_depth == 0)
877         return 0;
878       lexptr++;
879       return c;
880
881     case '.':
882       /* Might be a floating point number.  */
883       if (lexptr[1] >= '0' && lexptr[1] <= '9')
884         break;                  /* Falls into number code.  */
885       else
886       {
887          lexptr++;
888          return DOT;
889       }
890
891 /* These are character tokens that appear as-is in the YACC grammar */
892     case '+':
893     case '-':
894     case '*':
895     case '/':
896     case '^':
897     case '<':
898     case '>':
899     case '[':
900     case ']':
901     case '=':
902     case '{':
903     case '}':
904     case '#':
905     case '@':
906     case '~':
907     case '&':
908       lexptr++;
909       return c;
910
911     case '\'' :
912     case '"':
913       quote = c;
914       for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
915         if (c == '\\')
916           {
917             c = tokstart[++namelen];
918             if (c >= '0' && c <= '9')
919               {
920                 c = tokstart[++namelen];
921                 if (c >= '0' && c <= '9')
922                   c = tokstart[++namelen];
923               }
924           }
925       if(c != quote)
926          error("Unterminated string or character constant.");
927       yylval.sval.ptr = tokstart + 1;
928       yylval.sval.length = namelen - 1;
929       lexptr += namelen + 1;
930
931       if(namelen == 2)          /* Single character */
932       {
933            yylval.ulval = tokstart[1];
934            return CHAR;
935       }
936       else
937          return STRING;
938     }
939
940   /* Is it a number?  */
941   /* Note:  We have already dealt with the case of the token '.'.
942      See case '.' above.  */
943   if ((c >= '0' && c <= '9'))
944     {
945       /* It's a number.  */
946       int got_dot = 0, got_e = 0;
947       register char *p = tokstart;
948       int toktype;
949
950       for (++p ;; ++p)
951         {
952           if (!got_e && (*p == 'e' || *p == 'E'))
953             got_dot = got_e = 1;
954           else if (!got_dot && *p == '.')
955             got_dot = 1;
956           else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
957                    && (*p == '-' || *p == '+'))
958             /* This is the sign of the exponent, not the end of the
959                number.  */
960             continue;
961           else if ((*p < '0' || *p > '9') &&
962                    (*p < 'A' || *p > 'F') &&
963                    (*p != 'H'))  /* Modula-2 hexadecimal number */
964             break;
965         }
966         toktype = parse_number (p - tokstart);
967         if (toktype == ERROR)
968           {
969             char *err_copy = (char *) alloca (p - tokstart + 1);
970
971             memcpy (err_copy, tokstart, p - tokstart);
972             err_copy[p - tokstart] = 0;
973             error ("Invalid number \"%s\".", err_copy);
974           }
975         lexptr = p;
976         return toktype;
977     }
978
979   if (!(c == '_' || c == '$'
980         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
981     /* We must have come across a bad character (e.g. ';').  */
982     error ("Invalid character '%c' in expression.", c);
983
984   /* It's a name.  See how long it is.  */
985   namelen = 0;
986   for (c = tokstart[namelen];
987        (c == '_' || c == '$' || (c >= '0' && c <= '9')
988         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
989        c = tokstart[++namelen])
990     ;
991
992   /* The token "if" terminates the expression and is NOT
993      removed from the input stream.  */
994   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
995     {
996       return 0;
997     }
998
999   lexptr += namelen;
1000
1001   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1002      and $$digits (equivalent to $<-digits> if you could type that).
1003      Make token type LAST, and put the number (the digits) in yylval.  */
1004
1005   if (*tokstart == '$')
1006     {
1007       register int negate = 0;
1008       c = 1;
1009       /* Double dollar means negate the number and add -1 as well.
1010          Thus $$ alone means -1.  */
1011       if (namelen >= 2 && tokstart[1] == '$')
1012         {
1013           negate = 1;
1014           c = 2;
1015         }
1016       if (c == namelen)
1017         {
1018           /* Just dollars (one or two) */
1019           yylval.lval = - negate;
1020           return LAST;
1021         }
1022       /* Is the rest of the token digits?  */
1023       for (; c < namelen; c++)
1024         if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1025           break;
1026       if (c == namelen)
1027         {
1028           yylval.lval = atoi (tokstart + 1 + negate);
1029           if (negate)
1030             yylval.lval = - yylval.lval;
1031           return LAST;
1032         }
1033     }
1034
1035   /* Handle tokens that refer to machine registers:
1036      $ followed by a register name.  */
1037
1038   if (*tokstart == '$') {
1039     for (c = 0; c < NUM_REGS; c++)
1040       if (namelen - 1 == strlen (reg_names[c])
1041           && STREQN (tokstart + 1, reg_names[c], namelen - 1))
1042         {
1043           yylval.lval = c;
1044           return REGNAME;
1045         }
1046     for (c = 0; c < num_std_regs; c++)
1047      if (namelen - 1 == strlen (std_regs[c].name)
1048          && STREQN (tokstart + 1, std_regs[c].name, namelen - 1))
1049        {
1050          yylval.lval = std_regs[c].regnum;
1051          return REGNAME;
1052        }
1053   }
1054
1055
1056   /*  Lookup special keywords */
1057   for(i = 0 ; i < sizeof(keytab) / sizeof(keytab[0]) ; i++)
1058      if(namelen == strlen(keytab[i].keyw) && STREQN(tokstart,keytab[i].keyw,namelen))
1059            return keytab[i].token;
1060
1061   yylval.sval.ptr = tokstart;
1062   yylval.sval.length = namelen;
1063
1064   /* Any other names starting in $ are debugger internal variables.  */
1065
1066   if (*tokstart == '$')
1067     {
1068       yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
1069       return INTERNAL_VAR;
1070     }
1071
1072
1073   /* Use token-type BLOCKNAME for symbols that happen to be defined as
1074      functions.  If this is not so, then ...
1075      Use token-type TYPENAME for symbols that happen to be defined
1076      currently as names of types; NAME for other symbols.
1077      The caller is not constrained to care about the distinction.  */
1078  {
1079
1080
1081     char *tmp = copy_name (yylval.sval);
1082     struct symbol *sym;
1083
1084     if (lookup_partial_symtab (tmp))
1085       return BLOCKNAME;
1086     sym = lookup_symbol (tmp, expression_context_block,
1087                          VAR_NAMESPACE, 0, NULL);
1088     if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1089       return BLOCKNAME;
1090     if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1091       return TYPENAME;
1092
1093     if(sym)
1094     {
1095        switch(sym->class)
1096        {
1097        case LOC_STATIC:
1098        case LOC_REGISTER:
1099        case LOC_ARG:
1100        case LOC_REF_ARG:
1101        case LOC_REGPARM:
1102        case LOC_REGPARM_ADDR:
1103        case LOC_LOCAL:
1104        case LOC_LOCAL_ARG:
1105        case LOC_BASEREG:
1106        case LOC_BASEREG_ARG:
1107        case LOC_CONST:
1108        case LOC_CONST_BYTES:
1109        case LOC_OPTIMIZED_OUT:
1110           return NAME;
1111
1112        case LOC_TYPEDEF:
1113           return TYPENAME;
1114
1115        case LOC_BLOCK:
1116           return BLOCKNAME;
1117
1118        case LOC_UNDEF:
1119           error("internal:  Undefined class in m2lex()");
1120
1121        case LOC_LABEL:
1122           error("internal:  Unforseen case in m2lex()");
1123        }
1124     }
1125     else
1126     {
1127        /* Built-in BOOLEAN type.  This is sort of a hack. */
1128        if(STREQN(tokstart,"TRUE",4))
1129        {
1130           yylval.ulval = 1;
1131           return M2_TRUE;
1132        }
1133        else if(STREQN(tokstart,"FALSE",5))
1134        {
1135           yylval.ulval = 0;
1136           return M2_FALSE;
1137        }
1138     }
1139
1140     /* Must be another type of name... */
1141     return NAME;
1142  }
1143 }
1144
1145 #if 0           /* Unused */
1146 static char *
1147 make_qualname(mod,ident)
1148    char *mod, *ident;
1149 {
1150    char *new = malloc(strlen(mod)+strlen(ident)+2);
1151
1152    strcpy(new,mod);
1153    strcat(new,".");
1154    strcat(new,ident);
1155    return new;
1156 }
1157 #endif  /* 0 */
1158
1159 void
1160 yyerror(msg)
1161      char *msg; /* unused */
1162 {
1163    printf("Parsing:  %s\n",lexptr);
1164    if (yychar < 256)
1165      error("Invalid syntax in expression near character '%c'.",yychar);
1166    else
1167      error("Invalid syntax in expression");
1168 }
1169