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