]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/m2-exp.y
Load the udi library procedures.
[thirdparty/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
e35843d4
FF
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. */
3d6b6a90
JG
38
39%{
e35843d4 40
3d6b6a90 41#include "defs.h"
3d6b6a90
JG
42#include "expression.h"
43#include "language.h"
39bf5952 44#include "value.h"
3d6b6a90 45#include "parser-defs.h"
22e39759 46#include "m2-lang.h"
3d6b6a90 47
19d0f3f4
FF
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
d018c8a6 55#define yymaxdepth m2_maxdepth
3d6b6a90
JG
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
4015bfb9
BK
70#define yyerrflag m2_errflag
71#define yynerrs m2_nerrs
39bf5952
JG
72#define yyps m2_ps
73#define yypv m2_pv
74#define yys m2_s
d018c8a6 75#define yy_yys m2_yys
39bf5952
JG
76#define yystate m2_state
77#define yytmp m2_tmp
78#define yyv m2_v
d018c8a6 79#define yy_yyv m2_yyv
39bf5952
JG
80#define yyval m2_val
81#define yylloc m2_lloc
4015bfb9
BK
82#define yyreds m2_reds /* With YYDEBUG defined */
83#define yytoks m2_toks /* With YYDEBUG defined */
3d6b6a90 84
19d0f3f4
FF
85#ifndef YYDEBUG
86#define YYDEBUG 0 /* Default to no yydebug support */
be772100 87#endif
1ab3bf1b 88
19d0f3f4
FF
89int
90yyparse PARAMS ((void));
1ab3bf1b
JG
91
92static int
93yylex PARAMS ((void));
94
22e39759 95void
1ab3bf1b
JG
96yyerror PARAMS ((char *));
97
19d0f3f4
FF
98#if 0
99static char *
100make_qualname PARAMS ((char *, char *));
101#endif
102
103static int
104parse_number PARAMS ((int));
3d6b6a90
JG
105
106/* The sign of the number being parsed. */
e58de8a2 107static int number_sign = 1;
3d6b6a90
JG
108
109/* The block that the module specified by the qualifer on an identifer is
110 contained in, */
e58de8a2
FF
111#if 0
112static struct block *modblock=0;
113#endif
3d6b6a90 114
3d6b6a90
JG
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
368c8614 145%token <ulval> UINT M2_TRUE M2_FALSE CHAR
3d6b6a90
JG
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
088c3a0b 158%token <sval> NAME BLOCKNAME IDENT VARNAME
3d6b6a90
JG
159%token <sval> TYPENAME
160
71302249 161%token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
3d6b6a90
JG
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
088c3a0b 176%left OROR
e58de8a2 177%left LOGICAL_AND '&'
3d6b6a90
JG
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*/
3d6b6a90 188
e35843d4 189\f
368c8614
MT
190%%
191
3d6b6a90
JG
192start : exp
193 | type_exp
194 ;
195
196type_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
205exp : exp '^' %prec UNARY
206 { write_exp_elt_opcode (UNOP_IND); }
207
208exp : '-'
209 { number_sign = -1; }
210 exp %prec UNARY
211 { number_sign = 1;
212 write_exp_elt_opcode (UNOP_NEG); }
213 ;
214
215exp : '+' exp %prec UNARY
216 { write_exp_elt_opcode(UNOP_PLUS); }
217 ;
218
219exp : not_exp exp %prec UNARY
e58de8a2 220 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
3d6b6a90
JG
221 ;
222
223not_exp : NOT
224 | '~'
225 ;
226
227exp : CAP '(' exp ')'
228 { write_exp_elt_opcode (UNOP_CAP); }
229 ;
230
231exp : ORD '(' exp ')'
232 { write_exp_elt_opcode (UNOP_ORD); }
233 ;
234
235exp : ABS '(' exp ')'
236 { write_exp_elt_opcode (UNOP_ABS); }
237 ;
238
239exp : HIGH '(' exp ')'
240 { write_exp_elt_opcode (UNOP_HIGH); }
241 ;
242
71302249 243exp : MIN_FUNC '(' type ')'
3d6b6a90
JG
244 { write_exp_elt_opcode (UNOP_MIN);
245 write_exp_elt_type ($3);
246 write_exp_elt_opcode (UNOP_MIN); }
247 ;
248
71302249 249exp : MAX_FUNC '(' type ')'
3d6b6a90
JG
250 { write_exp_elt_opcode (UNOP_MAX);
251 write_exp_elt_type ($3);
252 write_exp_elt_opcode (UNOP_MIN); }
253 ;
254
255exp : FLOAT_FUNC '(' exp ')'
256 { write_exp_elt_opcode (UNOP_FLOAT); }
257 ;
258
259exp : 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
265exp : CHR '(' exp ')'
266 { write_exp_elt_opcode (UNOP_CHR); }
267 ;
268
269exp : ODD '(' exp ')'
270 { write_exp_elt_opcode (UNOP_ODD); }
271 ;
272
273exp : TRUNC '(' exp ')'
274 { write_exp_elt_opcode (UNOP_TRUNC); }
275 ;
276
277exp : SIZE exp %prec UNARY
278 { write_exp_elt_opcode (UNOP_SIZEOF); }
279 ;
280
281
282exp : INC '(' exp ')'
283 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
284 ;
285
286exp : 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
292exp : DEC '(' exp ')'
293 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
294 ;
295
296exp : 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
302exp : exp DOT NAME
303 { write_exp_elt_opcode (STRUCTOP_STRUCT);
304 write_exp_string ($3);
305 write_exp_elt_opcode (STRUCTOP_STRUCT); }
306 ;
307
308exp : set
309 ;
310
311exp : exp IN set
312 { error("Sets are not implemented.");}
313 ;
314
315exp : INCL '(' exp ',' exp ')'
316 { error("Sets are not implemented.");}
317 ;
318
319exp : EXCL '(' exp ',' exp ')'
320 { error("Sets are not implemented.");}
321
322set : '{' 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...] */
330exp : 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
54bbbfb4 336 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
3d6b6a90 337 write_exp_elt_longcst ((LONGEST) end_arglist());
54bbbfb4 338 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
3d6b6a90
JG
339 ;
340
341exp : 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
351arglist :
352 ;
353
354arglist : exp
355 { arglist_len = 1; }
356 ;
357
358arglist : arglist ',' exp %prec ABOVE_COMMA
359 { arglist_len++; }
360 ;
361
362non_empty_arglist
363 : exp
364 { arglist_len = 1; }
365 ;
366
367non_empty_arglist
368 : non_empty_arglist ',' exp %prec ABOVE_COMMA
369 { arglist_len++; }
370 ;
371
372/* GDB construct */
373exp : '{' 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
379exp : 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
385exp : '(' 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 */
393exp : exp '@' exp
394 { write_exp_elt_opcode (BINOP_REPEAT); }
395 ;
396
397exp : exp '*' exp
398 { write_exp_elt_opcode (BINOP_MUL); }
399 ;
400
401exp : exp '/' exp
402 { write_exp_elt_opcode (BINOP_DIV); }
403 ;
404
405exp : exp DIV exp
406 { write_exp_elt_opcode (BINOP_INTDIV); }
407 ;
408
409exp : exp MOD exp
410 { write_exp_elt_opcode (BINOP_REM); }
411 ;
412
413exp : exp '+' exp
414 { write_exp_elt_opcode (BINOP_ADD); }
415 ;
416
417exp : exp '-' exp
418 { write_exp_elt_opcode (BINOP_SUB); }
419 ;
420
421exp : exp '=' exp
422 { write_exp_elt_opcode (BINOP_EQUAL); }
423 ;
424
425exp : exp NOTEQUAL exp
426 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
427 | exp '#' exp
428 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
429 ;
430
431exp : exp LEQ exp
432 { write_exp_elt_opcode (BINOP_LEQ); }
433 ;
434
435exp : exp GEQ exp
436 { write_exp_elt_opcode (BINOP_GEQ); }
437 ;
438
439exp : exp '<' exp
440 { write_exp_elt_opcode (BINOP_LESS); }
441 ;
442
443exp : exp '>' exp
444 { write_exp_elt_opcode (BINOP_GTR); }
445 ;
446
e58de8a2
FF
447exp : exp LOGICAL_AND exp
448 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
3d6b6a90
JG
449 ;
450
088c3a0b 451exp : exp OROR exp
e58de8a2 452 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
3d6b6a90
JG
453 ;
454
455exp : exp ASSIGN exp
456 { write_exp_elt_opcode (BINOP_ASSIGN); }
457 ;
458
459
460/* Constants */
461
368c8614 462exp : M2_TRUE
3d6b6a90
JG
463 { write_exp_elt_opcode (OP_BOOL);
464 write_exp_elt_longcst ((LONGEST) $1);
465 write_exp_elt_opcode (OP_BOOL); }
466 ;
467
368c8614 468exp : M2_FALSE
3d6b6a90
JG
469 { write_exp_elt_opcode (OP_BOOL);
470 write_exp_elt_longcst ((LONGEST) $1);
471 write_exp_elt_opcode (OP_BOOL); }
472 ;
473
474exp : 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
481exp : 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
490exp : 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
498exp : 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
505exp : variable
506 ;
507
508/* The GDB internal variable $$, et al. */
509exp : LAST
510 { write_exp_elt_opcode (OP_LAST);
511 write_exp_elt_longcst ((LONGEST) $1);
512 write_exp_elt_opcode (OP_LAST); }
513 ;
514
515exp : REGNAME
516 { write_exp_elt_opcode (OP_REGISTER);
517 write_exp_elt_longcst ((LONGEST) $1);
518 write_exp_elt_opcode (OP_REGISTER); }
519 ;
520
521exp : 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
528exp : 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. */
535block : fblock
536 { $$ = SYMBOL_BLOCK_VALUE($1); }
537 ;
538
539fblock : 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 */
548fblock : 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 */
560variable: fblock
561 { write_exp_elt_opcode(OP_VAR_VALUE);
479fdd26 562 write_exp_elt_block (NULL);
3d6b6a90
JG
563 write_exp_elt_sym ($1);
564 write_exp_elt_opcode (OP_VAR_VALUE); }
565 ;
566
567/* GDB internal ($foo) variable */
568variable: 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 */
575variable: 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);
479fdd26
JK
584 /* block_found is set by lookup_symbol. */
585 write_exp_elt_block (block_found);
3d6b6a90
JG
586 write_exp_elt_sym (sym);
587 write_exp_elt_opcode (OP_VAR_VALUE); }
588 ;
589
590/* Base case for variables. */
591variable: 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 switch (sym->class)
603 {
604 case LOC_REGISTER:
605 case LOC_ARG:
606 case LOC_LOCAL:
7d9884b9
JG
607 case LOC_REF_ARG:
608 case LOC_REGPARM:
609 case LOC_LOCAL_ARG:
a1c8d76e
JK
610 case LOC_BASEREG:
611 case LOC_BASEREG_ARG:
3d6b6a90
JG
612 if (innermost_block == 0 ||
613 contained_in (block_found,
614 innermost_block))
615 innermost_block = block_found;
7d9884b9
JG
616 break;
617
618 case LOC_UNDEF:
619 case LOC_CONST:
620 case LOC_STATIC:
621 case LOC_TYPEDEF:
622 case LOC_LABEL: /* maybe should go above? */
623 case LOC_BLOCK:
624 case LOC_CONST_BYTES:
0848ad1c 625 case LOC_OPTIMIZED_OUT:
7d9884b9
JG
626 /* These are listed so gcc -Wall will reveal
627 un-handled cases. */
628 break;
3d6b6a90
JG
629 }
630 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26
JK
631 /* We want to use the selected frame, not
632 another more inner frame which happens to
633 be in the same block. */
634 write_exp_elt_block (NULL);
3d6b6a90
JG
635 write_exp_elt_sym (sym);
636 write_exp_elt_opcode (OP_VAR_VALUE);
637 }
638 else
639 {
1ab3bf1b 640 struct minimal_symbol *msymbol;
3d6b6a90
JG
641 register char *arg = copy_name ($1);
642
1ab3bf1b
JG
643 msymbol = lookup_minimal_symbol (arg,
644 (struct objfile *) NULL);
645 if (msymbol != NULL)
3d6b6a90 646 {
3d6b6a90
JG
647 write_exp_elt_opcode (OP_LONG);
648 write_exp_elt_type (builtin_type_int);
2e4964ad 649 write_exp_elt_longcst ((LONGEST) SYMBOL_VALUE_ADDRESS (msymbol));
3d6b6a90
JG
650 write_exp_elt_opcode (OP_LONG);
651 write_exp_elt_opcode (UNOP_MEMVAL);
1ab3bf1b
JG
652 if (msymbol -> type == mst_data ||
653 msymbol -> type == mst_bss)
3d6b6a90 654 write_exp_elt_type (builtin_type_int);
1ab3bf1b 655 else if (msymbol -> type == mst_text)
3d6b6a90
JG
656 write_exp_elt_type (lookup_function_type (builtin_type_int));
657 else
658 write_exp_elt_type (builtin_type_char);
659 write_exp_elt_opcode (UNOP_MEMVAL);
660 }
1ab3bf1b 661 else if (!have_full_symbols () && !have_partial_symbols ())
3d6b6a90
JG
662 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
663 else
664 error ("No symbol \"%s\" in current context.",
665 copy_name ($1));
666 }
667 }
668 ;
669
670type
671 : TYPENAME
672 { $$ = lookup_typename (copy_name ($1),
673 expression_context_block, 0); }
674
675 ;
676
677%%
678
679#if 0 /* FIXME! */
680int
681overflow(a,b)
682 long a,b;
683{
684 return (MAX_OF_TYPE(builtin_type_m2_int) - b) < a;
685}
686
687int
688uoverflow(a,b)
689 unsigned long a,b;
690{
691 return (MAX_OF_TYPE(builtin_type_m2_card) - b) < a;
692}
693#endif /* FIXME */
694
695/* Take care of parsing a number (anything that starts with a digit).
696 Set yylval and return the token type; update lexptr.
697 LEN is the number of characters in it. */
698
699/*** Needs some error checking for the float case ***/
700
701static int
702parse_number (olen)
703 int olen;
704{
705 register char *p = lexptr;
706 register LONGEST n = 0;
707 register LONGEST prevn = 0;
708 register int c,i,ischar=0;
709 register int base = input_radix;
710 register int len = olen;
3d6b6a90
JG
711 int unsigned_p = number_sign == 1 ? 1 : 0;
712
3d6b6a90
JG
713 if(p[len-1] == 'H')
714 {
715 base = 16;
716 len--;
717 }
718 else if(p[len-1] == 'C' || p[len-1] == 'B')
719 {
720 base = 8;
721 ischar = p[len-1] == 'C';
722 len--;
723 }
724
725 /* Scan the number */
726 for (c = 0; c < len; c++)
727 {
728 if (p[c] == '.' && base == 10)
729 {
730 /* It's a float since it contains a point. */
731 yylval.dval = atof (p);
732 lexptr += len;
733 return FLOAT;
734 }
735 if (p[c] == '.' && base != 10)
736 error("Floating point numbers must be base 10.");
737 if (base == 10 && (p[c] < '0' || p[c] > '9'))
738 error("Invalid digit \'%c\' in number.",p[c]);
739 }
740
741 while (len-- > 0)
742 {
743 c = *p++;
744 n *= base;
745 if( base == 8 && (c == '8' || c == '9'))
746 error("Invalid digit \'%c\' in octal number.",c);
747 if (c >= '0' && c <= '9')
748 i = c - '0';
749 else
750 {
751 if (base == 16 && c >= 'A' && c <= 'F')
752 i = c - 'A' + 10;
753 else
754 return ERROR;
755 }
756 n+=i;
757 if(i >= base)
758 return ERROR;
759 if(!unsigned_p && number_sign == 1 && (prevn >= n))
760 unsigned_p=1; /* Try something unsigned */
761 /* Don't do the range check if n==i and i==0, since that special
762 case will give an overflow error. */
763 if(RANGE_CHECK && n!=i && i)
764 {
765 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
766 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
767 range_error("Overflow on numeric constant.");
768 }
769 prevn=n;
770 }
771
772 lexptr = p;
773 if(*p == 'B' || *p == 'C' || *p == 'H')
774 lexptr++; /* Advance past B,C or H */
775
776 if (ischar)
777 {
778 yylval.ulval = n;
779 return CHAR;
780 }
781 else if ( unsigned_p && number_sign == 1)
782 {
783 yylval.ulval = n;
784 return UINT;
785 }
9dffe475 786 else if((unsigned_p && (n<0))) {
3d6b6a90 787 range_error("Overflow on numeric constant -- number too large.");
9dffe475 788 /* But, this can return if range_check == range_warn. */
3d6b6a90 789 }
9dffe475
JG
790 yylval.lval = n;
791 return INT;
3d6b6a90
JG
792}
793
794
795/* Some tokens */
796
797static struct
798{
799 char name[2];
800 int token;
801} tokentab2[] =
802{
d453b386
PB
803 { {'<', '>'}, NOTEQUAL },
804 { {':', '='}, ASSIGN },
805 { {'<', '='}, LEQ },
806 { {'>', '='}, GEQ },
807 { {':', ':'}, COLONCOLON },
3d6b6a90
JG
808
809};
810
811/* Some specific keywords */
812
813struct keyword {
814 char keyw[10];
815 int token;
816};
817
818static struct keyword keytab[] =
819{
088c3a0b 820 {"OR" , OROR },
3d6b6a90 821 {"IN", IN },/* Note space after IN */
e58de8a2 822 {"AND", LOGICAL_AND},
3d6b6a90
JG
823 {"ABS", ABS },
824 {"CHR", CHR },
825 {"DEC", DEC },
826 {"NOT", NOT },
827 {"DIV", DIV },
828 {"INC", INC },
71302249
JG
829 {"MAX", MAX_FUNC },
830 {"MIN", MIN_FUNC },
3d6b6a90
JG
831 {"MOD", MOD },
832 {"ODD", ODD },
833 {"CAP", CAP },
834 {"ORD", ORD },
835 {"VAL", VAL },
836 {"EXCL", EXCL },
837 {"HIGH", HIGH },
838 {"INCL", INCL },
839 {"SIZE", SIZE },
840 {"FLOAT", FLOAT_FUNC },
841 {"TRUNC", TRUNC },
842};
843
844
845/* Read one token, getting characters through lexptr. */
846
847/* This is where we will check to make sure that the language and the operators used are
848 compatible */
849
850static int
851yylex ()
852{
853 register int c;
854 register int namelen;
855 register int i;
856 register char *tokstart;
857 register char quote;
858
859 retry:
860
861 tokstart = lexptr;
862
863
864 /* See if it is a special token of length 2 */
865 for( i = 0 ; i < sizeof tokentab2 / sizeof tokentab2[0] ; i++)
45fe3db4 866 if(STREQN(tokentab2[i].name, tokstart, 2))
3d6b6a90
JG
867 {
868 lexptr += 2;
869 return tokentab2[i].token;
870 }
871
872 switch (c = *tokstart)
873 {
874 case 0:
875 return 0;
876
877 case ' ':
878 case '\t':
879 case '\n':
880 lexptr++;
881 goto retry;
882
883 case '(':
884 paren_depth++;
885 lexptr++;
886 return c;
887
888 case ')':
889 if (paren_depth == 0)
890 return 0;
891 paren_depth--;
892 lexptr++;
893 return c;
894
895 case ',':
896 if (comma_terminates && paren_depth == 0)
897 return 0;
898 lexptr++;
899 return c;
900
901 case '.':
902 /* Might be a floating point number. */
903 if (lexptr[1] >= '0' && lexptr[1] <= '9')
904 break; /* Falls into number code. */
905 else
906 {
907 lexptr++;
908 return DOT;
909 }
910
911/* These are character tokens that appear as-is in the YACC grammar */
912 case '+':
913 case '-':
914 case '*':
915 case '/':
916 case '^':
917 case '<':
918 case '>':
919 case '[':
920 case ']':
921 case '=':
922 case '{':
923 case '}':
924 case '#':
925 case '@':
926 case '~':
927 case '&':
928 lexptr++;
929 return c;
930
931 case '\'' :
932 case '"':
933 quote = c;
934 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
935 if (c == '\\')
936 {
937 c = tokstart[++namelen];
938 if (c >= '0' && c <= '9')
939 {
940 c = tokstart[++namelen];
941 if (c >= '0' && c <= '9')
942 c = tokstart[++namelen];
943 }
944 }
945 if(c != quote)
946 error("Unterminated string or character constant.");
947 yylval.sval.ptr = tokstart + 1;
948 yylval.sval.length = namelen - 1;
949 lexptr += namelen + 1;
950
951 if(namelen == 2) /* Single character */
952 {
953 yylval.ulval = tokstart[1];
954 return CHAR;
955 }
956 else
957 return STRING;
958 }
959
960 /* Is it a number? */
961 /* Note: We have already dealt with the case of the token '.'.
962 See case '.' above. */
963 if ((c >= '0' && c <= '9'))
964 {
965 /* It's a number. */
966 int got_dot = 0, got_e = 0;
967 register char *p = tokstart;
968 int toktype;
969
970 for (++p ;; ++p)
971 {
972 if (!got_e && (*p == 'e' || *p == 'E'))
973 got_dot = got_e = 1;
974 else if (!got_dot && *p == '.')
975 got_dot = 1;
976 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
977 && (*p == '-' || *p == '+'))
978 /* This is the sign of the exponent, not the end of the
979 number. */
980 continue;
981 else if ((*p < '0' || *p > '9') &&
982 (*p < 'A' || *p > 'F') &&
983 (*p != 'H')) /* Modula-2 hexadecimal number */
984 break;
985 }
986 toktype = parse_number (p - tokstart);
987 if (toktype == ERROR)
988 {
989 char *err_copy = (char *) alloca (p - tokstart + 1);
990
4ed3a9ea 991 memcpy (err_copy, tokstart, p - tokstart);
3d6b6a90
JG
992 err_copy[p - tokstart] = 0;
993 error ("Invalid number \"%s\".", err_copy);
994 }
995 lexptr = p;
996 return toktype;
997 }
998
999 if (!(c == '_' || c == '$'
1000 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1001 /* We must have come across a bad character (e.g. ';'). */
1002 error ("Invalid character '%c' in expression.", c);
1003
1004 /* It's a name. See how long it is. */
1005 namelen = 0;
1006 for (c = tokstart[namelen];
1007 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1008 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1009 c = tokstart[++namelen])
1010 ;
1011
1012 /* The token "if" terminates the expression and is NOT
1013 removed from the input stream. */
1014 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1015 {
1016 return 0;
1017 }
1018
1019 lexptr += namelen;
1020
1021 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1022 and $$digits (equivalent to $<-digits> if you could type that).
1023 Make token type LAST, and put the number (the digits) in yylval. */
1024
1025 if (*tokstart == '$')
1026 {
1027 register int negate = 0;
1028 c = 1;
1029 /* Double dollar means negate the number and add -1 as well.
1030 Thus $$ alone means -1. */
1031 if (namelen >= 2 && tokstart[1] == '$')
1032 {
1033 negate = 1;
1034 c = 2;
1035 }
1036 if (c == namelen)
1037 {
1038 /* Just dollars (one or two) */
1039 yylval.lval = - negate;
1040 return LAST;
1041 }
1042 /* Is the rest of the token digits? */
1043 for (; c < namelen; c++)
1044 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1045 break;
1046 if (c == namelen)
1047 {
1048 yylval.lval = atoi (tokstart + 1 + negate);
1049 if (negate)
1050 yylval.lval = - yylval.lval;
1051 return LAST;
1052 }
1053 }
1054
1055 /* Handle tokens that refer to machine registers:
1056 $ followed by a register name. */
1057
1058 if (*tokstart == '$') {
1059 for (c = 0; c < NUM_REGS; c++)
1060 if (namelen - 1 == strlen (reg_names[c])
45fe3db4 1061 && STREQN (tokstart + 1, reg_names[c], namelen - 1))
3d6b6a90
JG
1062 {
1063 yylval.lval = c;
1064 return REGNAME;
1065 }
1066 for (c = 0; c < num_std_regs; c++)
1067 if (namelen - 1 == strlen (std_regs[c].name)
45fe3db4 1068 && STREQN (tokstart + 1, std_regs[c].name, namelen - 1))
3d6b6a90
JG
1069 {
1070 yylval.lval = std_regs[c].regnum;
1071 return REGNAME;
1072 }
1073 }
1074
1075
1076 /* Lookup special keywords */
1077 for(i = 0 ; i < sizeof(keytab) / sizeof(keytab[0]) ; i++)
45fe3db4 1078 if(namelen == strlen(keytab[i].keyw) && STREQN(tokstart,keytab[i].keyw,namelen))
3d6b6a90
JG
1079 return keytab[i].token;
1080
1081 yylval.sval.ptr = tokstart;
1082 yylval.sval.length = namelen;
1083
1084 /* Any other names starting in $ are debugger internal variables. */
1085
1086 if (*tokstart == '$')
1087 {
1088 yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
1089 return INTERNAL_VAR;
1090 }
1091
1092
1093 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1094 functions. If this is not so, then ...
1095 Use token-type TYPENAME for symbols that happen to be defined
1096 currently as names of types; NAME for other symbols.
1097 The caller is not constrained to care about the distinction. */
1098 {
1099
1100
1101 char *tmp = copy_name (yylval.sval);
1102 struct symbol *sym;
1103
1104 if (lookup_partial_symtab (tmp))
1105 return BLOCKNAME;
1106 sym = lookup_symbol (tmp, expression_context_block,
1107 VAR_NAMESPACE, 0, NULL);
1108 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1109 return BLOCKNAME;
1110 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1111 return TYPENAME;
1112
1113 if(sym)
1114 {
1115 switch(sym->class)
1116 {
1117 case LOC_STATIC:
1118 case LOC_REGISTER:
1119 case LOC_ARG:
1120 case LOC_REF_ARG:
1121 case LOC_REGPARM:
1122 case LOC_LOCAL:
1123 case LOC_LOCAL_ARG:
a1c8d76e
JK
1124 case LOC_BASEREG:
1125 case LOC_BASEREG_ARG:
3d6b6a90
JG
1126 case LOC_CONST:
1127 case LOC_CONST_BYTES:
1128 return NAME;
1129
1130 case LOC_TYPEDEF:
1131 return TYPENAME;
1132
1133 case LOC_BLOCK:
1134 return BLOCKNAME;
1135
1136 case LOC_UNDEF:
1137 error("internal: Undefined class in m2lex()");
1138
1139 case LOC_LABEL:
1140 error("internal: Unforseen case in m2lex()");
1141 }
1142 }
1143 else
1144 {
1145 /* Built-in BOOLEAN type. This is sort of a hack. */
45fe3db4 1146 if(STREQN(tokstart,"TRUE",4))
3d6b6a90
JG
1147 {
1148 yylval.ulval = 1;
368c8614 1149 return M2_TRUE;
3d6b6a90 1150 }
45fe3db4 1151 else if(STREQN(tokstart,"FALSE",5))
3d6b6a90
JG
1152 {
1153 yylval.ulval = 0;
368c8614 1154 return M2_FALSE;
3d6b6a90
JG
1155 }
1156 }
1157
1158 /* Must be another type of name... */
1159 return NAME;
1160 }
1161}
1162
be772100 1163#if 0 /* Unused */
1ab3bf1b 1164static char *
3d6b6a90
JG
1165make_qualname(mod,ident)
1166 char *mod, *ident;
1167{
e35843d4 1168 char *new = malloc(strlen(mod)+strlen(ident)+2);
3d6b6a90
JG
1169
1170 strcpy(new,mod);
1171 strcat(new,".");
1172 strcat(new,ident);
1173 return new;
1174}
be772100 1175#endif /* 0 */
3d6b6a90 1176
22e39759 1177void
1ab3bf1b
JG
1178yyerror(msg)
1179 char *msg; /* unused */
3d6b6a90
JG
1180{
1181 printf("Parsing: %s\n",lexptr);
1182 if (yychar < 256)
1183 error("Invalid syntax in expression near character '%c'.",yychar);
1184 else
f24adda3 1185 error("Invalid syntax in expression");
3d6b6a90 1186}
5d074aa9 1187