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