* ch-exp.y (GENERAL_PROCEDURE_NAME, LOCATION_NAME): New
[binutils-gdb.git] / gdb / ch-exp.y
1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* Parse a Chill expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
28
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator.
36
37 Also note that the language accepted by this parser is more liberal
38 than the one accepted by an actual Chill compiler. For example, the
39 language rule that a simple name string can not be one of the reserved
40 simple name strings is not enforced (e.g "case" is not treated as a
41 reserved name). Another example is that Chill is a strongly typed
42 language, and certain expressions that violate the type constraints
43 may still be evaluated if gdb can do so in a meaningful manner, while
44 such expressions would be rejected by the compiler. The reason for
45 this more liberal behavior is the philosophy that the debugger
46 is intended to be a tool that is used by the programmer when things
47 go wrong, and as such, it should provide as few artificial barriers
48 to it's use as possible. If it can do something meaningful, even
49 something that violates language contraints that are enforced by the
50 compiler, it should do so without complaint.
51
52 */
53
54 %{
55
56 #include <stdio.h>
57 #include <string.h>
58 #include "defs.h"
59 #include "symtab.h"
60 #include "gdbtypes.h"
61 #include "frame.h"
62 #include "expression.h"
63 #include "language.h"
64 #include "value.h"
65 #include "parser-defs.h"
66 #include "bfd.h"
67 #include "symfile.h"
68 #include "objfiles.h"
69
70 /* These MUST be included in any grammar file!!!! Please choose unique names!
71 Note that this are a combined list of variables that can be produced
72 by any one of bison, byacc, or yacc. */
73 #define yymaxdepth chill_maxdepth
74 #define yyparse chill_parse
75 #define yylex chill_lex
76 #define yyerror chill_error
77 #define yylval chill_lval
78 #define yychar chill_char
79 #define yydebug chill_debug
80 #define yypact chill_pact
81 #define yyr1 chill_r1
82 #define yyr2 chill_r2
83 #define yydef chill_def
84 #define yychk chill_chk
85 #define yypgo chill_pgo
86 #define yyact chill_act
87 #define yyexca chill_exca
88 #define yyerrflag chill_errflag
89 #define yynerrs chill_nerrs
90 #define yyps chill_ps
91 #define yypv chill_pv
92 #define yys chill_s
93 #define yy_yys chill_yys
94 #define yystate chill_state
95 #define yytmp chill_tmp
96 #define yyv chill_v
97 #define yy_yyv chill_yyv
98 #define yyval chill_val
99 #define yylloc chill_lloc
100 #define yyss chill_yyss /* byacc */
101 #define yyssp chill_yysp /* byacc */
102 #define yyvs chill_yyvs /* byacc */
103 #define yyvsp chill_yyvsp /* byacc */
104
105 static int
106 yylex PARAMS ((void));
107
108 static void
109 yyerror PARAMS ((char *));
110
111 int
112 yyparse PARAMS ((void));
113
114 /* #define YYDEBUG 1 */
115
116 %}
117
118 /* Although the yacc "value" of an expression is not used,
119 since the result is stored in the structure being created,
120 other node types do have values. */
121
122 %union
123 {
124 LONGEST lval;
125 unsigned LONGEST ulval;
126 struct {
127 LONGEST val;
128 struct type *type;
129 } typed_val;
130 double dval;
131 struct symbol *sym;
132 struct type *tval;
133 struct stoken sval;
134 struct ttype tsym;
135 struct symtoken ssym;
136 int voidval;
137 struct block *bval;
138 enum exp_opcode opcode;
139 struct internalvar *ivar;
140
141 struct type **tvec;
142 int *ivec;
143 }
144
145 %{
146 static int parse_number PARAMS ((void));
147 %}
148
149 %token <voidval> FIXME
150
151 %token <typed_val> INTEGER_LITERAL
152 %token <ulval> BOOLEAN_LITERAL
153 %token <typed_val> CHARACTER_LITERAL
154 %token <ssym> GENERAL_PROCEDURE_NAME
155 %token <ssym> LOCATION_NAME
156 %token <voidval> SET_LITERAL
157 %token <voidval> EMPTINESS_LITERAL
158 %token <voidval> CHARACTER_STRING_LITERAL
159 %token <voidval> BIT_STRING_LITERAL
160
161 %token <voidval> STRING
162 %token <voidval> CONSTANT
163 %token <voidval> '.'
164 %token <voidval> ';'
165 %token <voidval> ':'
166 %token <voidval> CASE
167 %token <voidval> OF
168 %token <voidval> ESAC
169 %token <voidval> LOGIOR
170 %token <voidval> ORIF
171 %token <voidval> LOGXOR
172 %token <voidval> LOGAND
173 %token <voidval> ANDIF
174 %token <voidval> '='
175 %token <voidval> NOTEQUAL
176 %token <voidval> '>'
177 %token <voidval> GTR
178 %token <voidval> '<'
179 %token <voidval> LEQ
180 %token <voidval> IN
181 %token <voidval> '+'
182 %token <voidval> '-'
183 %token <voidval> '*'
184 %token <voidval> '/'
185 %token <voidval> SLASH_SLASH
186 %token <voidval> MOD
187 %token <voidval> REM
188 %token <voidval> NOT
189 %token <voidval> POINTER
190 %token <voidval> RECEIVE
191 %token <voidval> SC
192 %token <voidval> '['
193 %token <voidval> ']'
194 %token <voidval> '('
195 %token <voidval> ')'
196 %token <voidval> UP
197 %token <voidval> IF
198 %token <voidval> THEN
199 %token <voidval> ELSE
200 %token <voidval> FI
201 %token <voidval> ELSIF
202 %token <voidval> ILLEGAL_TOKEN
203
204 %type <voidval> location
205 %type <voidval> access_name
206 %type <voidval> primitive_value
207 %type <voidval> location_contents
208 %type <voidval> value_name
209 %type <voidval> literal
210 %type <voidval> tuple
211 %type <voidval> value_string_element
212 %type <voidval> value_string_slice
213 %type <voidval> value_array_element
214 %type <voidval> value_array_slice
215 %type <voidval> value_structure_field
216 %type <voidval> expression_conversion
217 %type <voidval> value_procedure_call
218 %type <voidval> value_built_in_routine_call
219 %type <voidval> start_expression
220 %type <voidval> zero_adic_operator
221 %type <voidval> parenthesised_expression
222 %type <voidval> value
223 %type <voidval> undefined_value
224 %type <voidval> expression
225 %type <voidval> conditional_expression
226 %type <voidval> then_alternative
227 %type <voidval> else_alternative
228 %type <voidval> sub_expression
229 %type <voidval> value_case_alternative
230 %type <voidval> operand_0
231 %type <voidval> operand_1
232 %type <voidval> operand_2
233 %type <voidval> operand_3
234 %type <voidval> operand_4
235 %type <voidval> operand_5
236 %type <voidval> operand_6
237 %type <voidval> integer_literal_expression
238 %type <voidval> synonym_name
239 %type <voidval> value_enumeration_name
240 %type <voidval> value_do_with_name
241 %type <voidval> value_receive_name
242 %type <voidval> string_primitive_value
243 %type <voidval> start_element
244 %type <voidval> left_element
245 %type <voidval> right_element
246 %type <voidval> slice_size
247 %type <voidval> array_primitive_value
248 %type <voidval> expression_list
249 %type <voidval> lower_element
250 %type <voidval> upper_element
251 %type <voidval> first_element
252 %type <voidval> structure_primitive_value
253 %type <voidval> field_name
254 %type <voidval> mode_name
255 %type <voidval> boolean_expression
256 %type <voidval> case_selector_list
257 %type <voidval> subexpression
258 %type <voidval> case_label_specification
259 %type <voidval> buffer_location
260
261 %%
262
263 /* Z.200, 5.3.1 */
264
265 value : expression
266 {
267 $$ = 0; /* FIXME */
268 }
269 | undefined_value
270 {
271 $$ = 0; /* FIXME */
272 }
273 ;
274
275 undefined_value : FIXME
276 {
277 $$ = 0; /* FIXME */
278 }
279 ;
280
281 /* Z.200, 4.2.1 */
282
283 location : access_name
284 {
285 $$ = 0; /* FIXME */
286 }
287 | FIXME
288 {
289 $$ = 0; /* FIXME */
290 }
291 ;
292
293 /* Z.200, 4.2.2 */
294
295 access_name : LOCATION_NAME
296 {
297 write_exp_elt_opcode (OP_VAR_VALUE);
298 write_exp_elt_sym ($1.sym);
299 write_exp_elt_opcode (OP_VAR_VALUE);
300 }
301 | FIXME
302 {
303 $$ = 0; /* FIXME */
304 }
305 ;
306
307 /* Z.200, 5.2.1 */
308
309 primitive_value : location_contents
310 {
311 $$ = 0; /* FIXME */
312 }
313 | value_name
314 {
315 $$ = 0; /* FIXME */
316 }
317 | literal
318 {
319 $$ = 0; /* FIXME */
320 }
321 | tuple
322 {
323 $$ = 0; /* FIXME */
324 }
325 | value_string_element
326 {
327 $$ = 0; /* FIXME */
328 }
329 | value_string_slice
330 {
331 $$ = 0; /* FIXME */
332 }
333 | value_array_element
334 {
335 $$ = 0; /* FIXME */
336 }
337 | value_array_slice
338 {
339 $$ = 0; /* FIXME */
340 }
341 | value_structure_field
342 {
343 $$ = 0; /* FIXME */
344 }
345 | expression_conversion
346 {
347 $$ = 0; /* FIXME */
348 }
349 | value_procedure_call
350 {
351 $$ = 0; /* FIXME */
352 }
353 | value_built_in_routine_call
354 {
355 $$ = 0; /* FIXME */
356 }
357 | start_expression
358 {
359 $$ = 0; /* FIXME */
360 }
361 | zero_adic_operator
362 {
363 $$ = 0; /* FIXME */
364 }
365 | parenthesised_expression
366 {
367 $$ = 0; /* FIXME */
368 }
369 ;
370
371 /* Z.200, 5.2.2 */
372
373 location_contents: location
374 {
375 $$ = 0; /* FIXME */
376 }
377 ;
378
379 /* Z.200, 5.2.3 */
380
381 value_name : synonym_name
382 {
383 $$ = 0; /* FIXME */
384 }
385 | value_enumeration_name
386 {
387 $$ = 0; /* FIXME */
388 }
389 | value_do_with_name
390 {
391 $$ = 0; /* FIXME */
392 }
393 | value_receive_name
394 {
395 $$ = 0; /* FIXME */
396 }
397 | GENERAL_PROCEDURE_NAME
398 {
399 write_exp_elt_opcode (OP_VAR_VALUE);
400 write_exp_elt_sym ($1.sym);
401 write_exp_elt_opcode (OP_VAR_VALUE);
402 }
403 ;
404
405 /* Z.200, 5.2.4.1 */
406
407 literal : INTEGER_LITERAL
408 {
409 write_exp_elt_opcode (OP_LONG);
410 write_exp_elt_type ($1.type);
411 write_exp_elt_longcst ((LONGEST) ($1.val));
412 write_exp_elt_opcode (OP_LONG);
413 }
414 | BOOLEAN_LITERAL
415 {
416 write_exp_elt_opcode (OP_BOOL);
417 write_exp_elt_longcst ((LONGEST) $1);
418 write_exp_elt_opcode (OP_BOOL);
419 }
420 | CHARACTER_LITERAL
421 {
422 write_exp_elt_opcode (OP_LONG);
423 write_exp_elt_type ($1.type);
424 write_exp_elt_longcst ((LONGEST) ($1.val));
425 write_exp_elt_opcode (OP_LONG);
426 }
427 | SET_LITERAL
428 {
429 $$ = 0; /* FIXME */
430 }
431 | EMPTINESS_LITERAL
432 {
433 $$ = 0; /* FIXME */
434 }
435 | CHARACTER_STRING_LITERAL
436 {
437 $$ = 0; /* FIXME */
438 }
439 | BIT_STRING_LITERAL
440 {
441 $$ = 0; /* FIXME */
442 }
443 ;
444
445 /* Z.200, 5.2.5 */
446
447 tuple : FIXME
448 {
449 $$ = 0; /* FIXME */
450 }
451 ;
452
453
454 /* Z.200, 5.2.6 */
455
456 value_string_element: string_primitive_value '(' start_element ')'
457 {
458 $$ = 0; /* FIXME */
459 }
460 ;
461
462 /* Z.200, 5.2.7 */
463
464 value_string_slice: string_primitive_value '(' left_element ':' right_element ')'
465 {
466 $$ = 0; /* FIXME */
467 }
468 | string_primitive_value '(' start_element UP slice_size ')'
469 {
470 $$ = 0; /* FIXME */
471 }
472 ;
473
474 /* Z.200, 5.2.8 */
475
476 value_array_element: array_primitive_value '(' expression_list ')'
477 {
478 $$ = 0; /* FIXME */
479 }
480 ;
481
482 /* Z.200, 5.2.9 */
483
484 value_array_slice: array_primitive_value '(' lower_element ':' upper_element ')'
485 {
486 $$ = 0; /* FIXME */
487 }
488 | array_primitive_value '(' first_element UP slice_size '('
489 {
490 $$ = 0; /* FIXME */
491 }
492 ;
493
494 /* Z.200, 5.2.10 */
495
496 value_structure_field: structure_primitive_value '.' field_name
497 {
498 $$ = 0; /* FIXME */
499 }
500 ;
501
502 /* Z.200, 5.2.11 */
503
504 expression_conversion: mode_name '(' expression ')'
505 {
506 $$ = 0; /* FIXME */
507 }
508 ;
509
510 /* Z.200, 5.2.12 */
511
512 value_procedure_call: FIXME
513 {
514 $$ = 0; /* FIXME */
515 }
516 ;
517
518 /* Z.200, 5.2.13 */
519
520 value_built_in_routine_call: FIXME
521 {
522 $$ = 0; /* FIXME */
523 }
524 ;
525
526 /* Z.200, 5.2.14 */
527
528 start_expression: FIXME
529 {
530 $$ = 0; /* FIXME */
531 } /* Not in GNU-Chill */
532 ;
533
534 /* Z.200, 5.2.15 */
535
536 zero_adic_operator: FIXME
537 {
538 $$ = 0; /* FIXME */
539 }
540 ;
541
542 /* Z.200, 5.2.16 */
543
544 parenthesised_expression: '(' expression ')'
545 {
546 $$ = 0; /* FIXME */
547 }
548 ;
549
550 /* Z.200, 5.3.2 */
551
552 expression : operand_0
553 {
554 $$ = 0; /* FIXME */
555 }
556 | conditional_expression
557 {
558 $$ = 0; /* FIXME */
559 }
560 ;
561
562 conditional_expression : IF boolean_expression then_alternative else_alternative FI
563 {
564 $$ = 0; /* FIXME */
565 }
566 | CASE case_selector_list OF value_case_alternative '[' ELSE sub_expression ']' ESAC
567 {
568 $$ = 0; /* FIXME */
569 }
570 ;
571
572 then_alternative: THEN subexpression
573 {
574 $$ = 0; /* FIXME */
575 }
576 ;
577
578 else_alternative: ELSE subexpression
579 {
580 $$ = 0; /* FIXME */
581 }
582 | ELSIF boolean_expression then_alternative else_alternative
583 {
584 $$ = 0; /* FIXME */
585 }
586 ;
587
588 sub_expression : expression
589 {
590 $$ = 0; /* FIXME */
591 }
592 ;
593
594 value_case_alternative: case_label_specification ':' sub_expression ';'
595 {
596 $$ = 0; /* FIXME */
597 }
598 ;
599
600 /* Z.200, 5.3.3 */
601
602 operand_0 : operand_1
603 {
604 $$ = 0; /* FIXME */
605 }
606 | operand_0 LOGIOR operand_1
607 {
608 write_exp_elt_opcode (BINOP_BITWISE_IOR);
609 }
610 | operand_0 ORIF operand_1
611 {
612 $$ = 0; /* FIXME */
613 }
614 | operand_0 LOGXOR operand_1
615 {
616 write_exp_elt_opcode (BINOP_BITWISE_XOR);
617 }
618 ;
619
620 /* Z.200, 5.3.4 */
621
622 operand_1 : operand_2
623 {
624 $$ = 0; /* FIXME */
625 }
626 | operand_1 LOGAND operand_2
627 {
628 write_exp_elt_opcode (BINOP_BITWISE_AND);
629 }
630 | operand_1 ANDIF operand_2
631 {
632 $$ = 0; /* FIXME */
633 }
634 ;
635
636 /* Z.200, 5.3.5 */
637
638 operand_2 : operand_3
639 {
640 $$ = 0; /* FIXME */
641 }
642 | operand_2 '=' operand_3
643 {
644 write_exp_elt_opcode (BINOP_EQUAL);
645 }
646 | operand_2 NOTEQUAL operand_3
647 {
648 write_exp_elt_opcode (BINOP_NOTEQUAL);
649 }
650 | operand_2 '>' operand_3
651 {
652 write_exp_elt_opcode (BINOP_GTR);
653 }
654 | operand_2 GTR operand_3
655 {
656 write_exp_elt_opcode (BINOP_GEQ);
657 }
658 | operand_2 '<' operand_3
659 {
660 write_exp_elt_opcode (BINOP_LESS);
661 }
662 | operand_2 LEQ operand_3
663 {
664 write_exp_elt_opcode (BINOP_LEQ);
665 }
666 | operand_2 IN operand_3
667 {
668 $$ = 0; /* FIXME */
669 }
670 ;
671
672
673 /* Z.200, 5.3.6 */
674
675 operand_3 : operand_4
676 {
677 $$ = 0; /* FIXME */
678 }
679 | operand_3 '+' operand_4
680 {
681 write_exp_elt_opcode (BINOP_ADD);
682 }
683 | operand_3 '-' operand_4
684 {
685 write_exp_elt_opcode (BINOP_SUB);
686 }
687 | operand_3 SLASH_SLASH operand_4
688 {
689 $$ = 0; /* FIXME */
690 }
691 ;
692
693 /* Z.200, 5.3.7 */
694
695 operand_4 : operand_5
696 {
697 $$ = 0; /* FIXME */
698 }
699 | operand_4 '*' operand_5
700 {
701 write_exp_elt_opcode (BINOP_MUL);
702 }
703 | operand_4 '/' operand_5
704 {
705 write_exp_elt_opcode (BINOP_DIV);
706 }
707 | operand_4 MOD operand_5
708 {
709 $$ = 0; /* FIXME */
710 }
711 | operand_4 REM operand_5
712 {
713 $$ = 0; /* FIXME */
714 }
715 ;
716
717 /* Z.200, 5.3.8 */
718
719 operand_5 : operand_6
720 {
721 $$ = 0; /* FIXME */
722 }
723 | '-' operand_6
724 {
725 write_exp_elt_opcode (UNOP_NEG);
726 }
727 | NOT operand_6
728 {
729 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
730 }
731 | '(' integer_literal_expression ')' operand_6
732 {
733 $$ = 0; /* FIXME */
734 }
735 ;
736
737 /* Z.200, 5.3.9 */
738
739 operand_6 : POINTER location
740 {
741 $$ = 0; /* FIXME */
742 }
743 | RECEIVE buffer_location
744 {
745 $$ = 0; /* FIXME */
746 }
747 | primitive_value
748 {
749 $$ = 0; /* FIXME */
750 }
751 ;
752
753
754 /* Z.200, 12.4.3 */
755 /* FIXME: For now we just accept only a single integer literal. */
756
757 integer_literal_expression:
758 INTEGER_LITERAL
759 {
760 $$ = 0;
761 }
762
763 /* Things which still need productions... */
764 synonym_name : FIXME { $$ = 0; }
765 value_enumeration_name : FIXME { $$ = 0; }
766 value_do_with_name : FIXME { $$ = 0; }
767 value_receive_name : FIXME { $$ = 0; }
768 string_primitive_value : FIXME { $$ = 0; }
769 start_element : FIXME { $$ = 0; }
770 left_element : FIXME { $$ = 0; }
771 right_element : FIXME { $$ = 0; }
772 slice_size : FIXME { $$ = 0; }
773 array_primitive_value : FIXME { $$ = 0; }
774 expression_list : FIXME { $$ = 0; }
775 lower_element : FIXME { $$ = 0; }
776 upper_element : FIXME { $$ = 0; }
777 first_element : FIXME { $$ = 0; }
778 structure_primitive_value: FIXME { $$ = 0; }
779 field_name : FIXME { $$ = 0; }
780 mode_name : FIXME { $$ = 0; }
781 boolean_expression : FIXME { $$ = 0; }
782 case_selector_list : FIXME { $$ = 0; }
783 subexpression : FIXME { $$ = 0; }
784 case_label_specification: FIXME { $$ = 0; }
785 buffer_location : FIXME { $$ = 0; }
786
787 %%
788
789 /* Try to consume a simple name string token. If successful, returns
790 a pointer to a nullbyte terminated copy of the name that can be used
791 in symbol table lookups. If not successful, returns NULL. */
792
793 static char *
794 match_simple_name_string ()
795 {
796 char *tokptr = lexptr;
797
798 if (isalpha (*tokptr))
799 {
800 do {
801 tokptr++;
802 } while (isalpha (*tokptr) || isdigit (*tokptr) || (*tokptr == '_'));
803 yylval.sval.ptr = lexptr;
804 yylval.sval.length = tokptr - lexptr;
805 lexptr = tokptr;
806 return (copy_name (yylval.sval));
807 }
808 return (NULL);
809 }
810
811 /* Start looking for a value composed of valid digits as set by the base
812 in use. Note that '_' characters are valid anywhere, in any quantity,
813 and are simply ignored. Since we must find at least one valid digit,
814 or reject this token as an integer literal, we keep track of how many
815 digits we have encountered. */
816
817 static int
818 decode_integer_value (base, tokptrptr, ivalptr)
819 int base;
820 char **tokptrptr;
821 int *ivalptr;
822 {
823 char *tokptr = *tokptrptr;
824 int temp;
825 int digits = 0;
826
827 while (*tokptr != '\0')
828 {
829 temp = tolower (*tokptr);
830 tokptr++;
831 switch (temp)
832 {
833 case '_':
834 continue;
835 case '0': case '1': case '2': case '3': case '4':
836 case '5': case '6': case '7': case '8': case '9':
837 temp -= '0';
838 break;
839 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
840 temp -= 'a';
841 temp += 10;
842 break;
843 default:
844 temp = base;
845 break;
846 }
847 if (temp < base)
848 {
849 digits++;
850 *ivalptr *= base;
851 *ivalptr += temp;
852 }
853 else
854 {
855 /* Found something not in domain for current base. */
856 tokptr--; /* Unconsume what gave us indigestion. */
857 break;
858 }
859 }
860
861 /* If we didn't find any digits, then we don't have a valid integer
862 value, so reject the entire token. Otherwise, update the lexical
863 scan pointer, and return non-zero for success. */
864
865 if (digits == 0)
866 {
867 return (0);
868 }
869 else
870 {
871 *tokptrptr = tokptr;
872 return (1);
873 }
874 }
875
876 static int
877 decode_integer_literal (valptr, tokptrptr)
878 int *valptr;
879 char **tokptrptr;
880 {
881 char *tokptr = *tokptrptr;
882 int base = 0;
883 int ival = 0;
884 int digits = 0;
885 int temp;
886 int explicit_base = 0;
887
888 /* Look for an explicit base specifier, which is optional. */
889
890 switch (*tokptr)
891 {
892 case 'd':
893 case 'D':
894 explicit_base++;
895 base = 10;
896 tokptr++;
897 break;
898 case 'b':
899 case 'B':
900 explicit_base++;
901 base = 2;
902 tokptr++;
903 break;
904 case 'h':
905 case 'H':
906 explicit_base++;
907 base = 16;
908 tokptr++;
909 break;
910 case 'o':
911 case 'O':
912 explicit_base++;
913 base = 8;
914 tokptr++;
915 break;
916 default:
917 base = 10;
918 break;
919 }
920
921 /* If we found an explicit base ensure that the character after the
922 explicit base is a single quote. */
923
924 if (explicit_base && (*tokptr++ != '\''))
925 {
926 return (0);
927 }
928
929 /* Attempt to decode whatever follows as an integer value in the
930 indicated base, updating the token pointer in the process and
931 computing the value into ival. Also, if we have an explicit
932 base, then the next character must not be a single quote, or we
933 have a bitstring literal, so reject the entire token in this case.
934 Otherwise, update the lexical scan pointer, and return non-zero
935 for success. */
936
937 if (!decode_integer_value (base, &tokptr, &ival))
938 {
939 return (0);
940 }
941 else if (explicit_base && (*tokptr == '\''))
942 {
943 return (0);
944 }
945 else
946 {
947 *valptr = ival;
948 *tokptrptr = tokptr;
949 return (1);
950 }
951 }
952
953 /* Recognize a character literal. A character literal is single character
954 or a control sequence, enclosed in single quotes. A control sequence
955 is a comma separated list of one or more integer literals, enclosed
956 in parenthesis and introduced with a circumflex character.
957
958 EX: 'a' '^(7)' '^(7,8)'
959
960 As a GNU chill extension, the syntax C'xx' is also recognized as a
961 character literal, where xx is a hex value for the character.
962
963 Returns CHARACTER_LITERAL if a match is found.
964 */
965
966 static int
967 match_character_literal ()
968 {
969 char *tokptr = lexptr;
970 int ival = 0;
971
972 if ((tolower (*tokptr) == 'c') && (*(tokptr + 1) == '\''))
973 {
974 /* We have a GNU chill extension form, so skip the leading "C'",
975 decode the hex value, and then ensure that we have a trailing
976 single quote character. */
977 tokptr += 2;
978 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
979 {
980 return (0);
981 }
982 tokptr++;
983 }
984 else if (*tokptr == '\'')
985 {
986 tokptr++;
987
988 /* Determine which form we have, either a control sequence or the
989 single character form. */
990
991 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
992 {
993 /* Match and decode a control sequence. Return zero if we don't
994 find a valid integer literal, or if the next unconsumed character
995 after the integer literal is not the trailing ')'.
996 FIXME: We currently don't handle the multiple integer literal
997 form. */
998 tokptr += 2;
999 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1000 {
1001 return (0);
1002 }
1003 }
1004 else
1005 {
1006 ival = *tokptr++;
1007 }
1008
1009 /* The trailing quote has not yet been consumed. If we don't find
1010 it, then we have no match. */
1011
1012 if (*tokptr++ != '\'')
1013 {
1014 return (0);
1015 }
1016 }
1017 yylval.typed_val.val = ival;
1018 yylval.typed_val.type = builtin_type_chill_char;
1019 lexptr = tokptr;
1020 return (CHARACTER_LITERAL);
1021 }
1022
1023 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1024 Note that according to 5.2.4.2, a single "_" is also a valid integer
1025 literal, however GNU-chill requires there to be at least one "digit"
1026 in any integer literal. */
1027
1028 static int
1029 match_integer_literal ()
1030 {
1031 char *tokptr = lexptr;
1032 int ival;
1033
1034 if (!decode_integer_literal (&ival, &tokptr))
1035 {
1036 return (0);
1037 }
1038 else
1039 {
1040 yylval.typed_val.val = ival;
1041 yylval.typed_val.type = builtin_type_int;
1042 lexptr = tokptr;
1043 return (INTEGER_LITERAL);
1044 }
1045 }
1046
1047 static void convert_float ()
1048 {
1049 #if 0
1050 extern double strtod ();
1051 double d;
1052 char tmp[256];
1053 char *p = yytext, *p1 = tmp;
1054 char c;
1055
1056 while (c = *p++)
1057 {
1058 switch (c)
1059 {
1060 case '_':
1061 break;
1062 case 'E':
1063 case 'd':
1064 case 'D':
1065 *p1++ = 'e';
1066 break;
1067 default:
1068 *p1++ = c;
1069 break;
1070 }
1071 }
1072 *p1 = '\0';
1073 d = strtod (tmp, &p1);
1074 if (*p1)
1075 {
1076 /* add error handling here */
1077 ;
1078 }
1079 yylval.dval = d;
1080 #endif
1081 }
1082
1083 /* Take care of parsing a number (anything that starts with a digit).
1084 Set yylval and return the token type; update lexptr.
1085 LEN is the number of characters in it. */
1086
1087 /*** Needs some error checking for the float case ***/
1088
1089 static int
1090 parse_number ()
1091 {
1092 }
1093
1094 struct token
1095 {
1096 char *operator;
1097 int token;
1098 };
1099
1100 const static struct token tokentab5[] =
1101 {
1102 { "ANDIF", ANDIF }
1103 };
1104
1105 const static struct token tokentab4[] =
1106 {
1107 { "ORIF", ORIF }
1108 };
1109
1110 const static struct token tokentab3[] =
1111 {
1112 { "NOT", NOT },
1113 { "XOR", LOGXOR },
1114 { "AND", LOGAND }
1115 };
1116
1117 const static struct token tokentab2[] =
1118 {
1119 { "//", SLASH_SLASH },
1120 { "/=", NOTEQUAL },
1121 { "<=", LEQ },
1122 { ">=", GTR },
1123 { "IN", IN },
1124 { "OR", LOGIOR }
1125 };
1126
1127 /* Read one token, getting characters through lexptr. */
1128 /* This is where we will check to make sure that the language and the
1129 operators used are compatible. */
1130
1131 static int
1132 yylex ()
1133 {
1134 unsigned int i;
1135 int token;
1136 char *simplename;
1137 struct symbol *sym;
1138
1139 /* Skip over any leading whitespace. */
1140 while (isspace (*lexptr))
1141 {
1142 lexptr++;
1143 }
1144 /* Look for special single character cases which can't be the first
1145 character of some other multicharacter token. */
1146 switch (*lexptr)
1147 {
1148 case '\0':
1149 return (0);
1150 case '.':
1151 case '=':
1152 case ':':
1153 case ';':
1154 case '!':
1155 case '+':
1156 case '-':
1157 case '*':
1158 case '/':
1159 case '(':
1160 case ')':
1161 case '[':
1162 case ']':
1163 return (*lexptr++);
1164 }
1165 /* Look for characters which start a particular kind of multicharacter
1166 token, such as a character literal. */
1167 switch (*lexptr)
1168 {
1169 case 'C':
1170 case 'c':
1171 case '\'':
1172 token = match_character_literal ();
1173 if (token != 0)
1174 {
1175 return (token);
1176 }
1177 break;
1178 }
1179 /* See if it is a special token of length 5. */
1180 for (i = 0; i < sizeof (tokentab5) / sizeof (tokentab5[0]); i++)
1181 {
1182 if (strncmp (lexptr, tokentab5[i].operator, 5) == 0)
1183 {
1184 lexptr += 5;
1185 return (tokentab5[i].token);
1186 }
1187 }
1188 /* See if it is a special token of length 4. */
1189 for (i = 0; i < sizeof (tokentab4) / sizeof (tokentab4[0]); i++)
1190 {
1191 if (strncmp (lexptr, tokentab4[i].operator, 4) == 0)
1192 {
1193 lexptr += 4;
1194 return (tokentab4[i].token);
1195 }
1196 }
1197 /* See if it is a special token of length 3. */
1198 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1199 {
1200 if (strncmp (lexptr, tokentab3[i].operator, 3) == 0)
1201 {
1202 lexptr += 3;
1203 return (tokentab3[i].token);
1204 }
1205 }
1206 /* See if it is a special token of length 2. */
1207 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1208 {
1209 if (strncmp (lexptr, tokentab2[i].operator, 2) == 0)
1210 {
1211 lexptr += 2;
1212 return (tokentab2[i].token);
1213 }
1214 }
1215 /* Look for single character cases which which could be the first
1216 character of some other multicharacter token, but aren't, or we
1217 would already have found it. */
1218 switch (*lexptr)
1219 {
1220 case '/':
1221 case '<':
1222 case '>':
1223 return (*lexptr++);
1224 }
1225 /* Look for other special tokens. */
1226 if (strncmp (lexptr, "TRUE", 4) == 0) /* FIXME: What about lowercase? */
1227 {
1228 yylval.ulval = 1;
1229 lexptr += 4;
1230 return (BOOLEAN_LITERAL);
1231 }
1232 if (strncmp (lexptr, "FALSE", 5) == 0) /* FIXME: What about lowercase? */
1233 {
1234 yylval.ulval = 0;
1235 lexptr += 5;
1236 return (BOOLEAN_LITERAL);
1237 }
1238 token = match_integer_literal ();
1239 if (token != 0)
1240 {
1241 return (token);
1242 }
1243
1244 /* Try to match a simple name string, and if a match is found, then
1245 further classify what sort of name it is and return an appropriate
1246 token. Note that attempting to match a simple name string consumes
1247 the token from lexptr, so we can't back out if we later find that
1248 we can't classify what sort of name it is. */
1249
1250 simplename = match_simple_name_string ();
1251 if (simplename != NULL)
1252 {
1253 sym = lookup_symbol (simplename, expression_context_block,
1254 VAR_NAMESPACE, (int *) NULL,
1255 (struct symtab **) NULL);
1256 if (sym != NULL)
1257 {
1258 yylval.ssym.stoken.ptr = NULL;
1259 yylval.ssym.stoken.length = 0;
1260 yylval.ssym.sym = sym;
1261 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1262 switch (SYMBOL_CLASS (sym))
1263 {
1264 case LOC_BLOCK:
1265 /* Found a procedure name. */
1266 return (GENERAL_PROCEDURE_NAME);
1267 case LOC_STATIC:
1268 /* Found a global or local static variable. */
1269 return (LOCATION_NAME);
1270 }
1271 }
1272 else if (!have_full_symbols () && !have_partial_symbols ())
1273 {
1274 error ("No symbol table is loaded. Use the \"file\" command.");
1275 }
1276 else
1277 {
1278 error ("No symbol \"%s\" in current context.", simplename);
1279 }
1280 }
1281
1282 return (ILLEGAL_TOKEN);
1283 }
1284
1285 static void
1286 yyerror (msg)
1287 char *msg; /* unused */
1288 {
1289 printf ("Parsing: %s\n", lexptr);
1290 if (yychar < 256)
1291 {
1292 error ("Invalid syntax in expression near character '%c'.", yychar);
1293 }
1294 else
1295 {
1296 error ("Invalid syntax in expression");
1297 }
1298 }
1299
1300 \f
1301 static void
1302 chill_printchar (c, stream)
1303 register int c;
1304 FILE *stream;
1305 {
1306 c &= 0xFF; /* Avoid sign bit follies */
1307
1308 if (PRINT_LITERAL_FORM (c))
1309 {
1310 fprintf_filtered (stream, "'%c'", c);
1311 }
1312 else
1313 {
1314 fprintf_filtered (stream, "C'%.2x'", (unsigned int) c);
1315 }
1316 }
1317
1318 /* Print the character string STRING, printing at most LENGTH characters.
1319 Printing stops early if the number hits print_max; repeat counts
1320 are printed as appropriate. Print ellipses at the end if we
1321 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
1322 Note that gdb maintains the length of strings without counting the
1323 terminating null byte, while chill strings are typically written with
1324 an explicit null byte. So we always assume an implied null byte
1325 until gdb is able to maintain non-null terminated strings as well
1326 as null terminated strings (FIXME).
1327 */
1328
1329 static void
1330 chill_printstr (stream, string, length, force_ellipses)
1331 FILE *stream;
1332 char *string;
1333 unsigned int length;
1334 int force_ellipses;
1335 {
1336 register unsigned int i;
1337 unsigned int things_printed = 0;
1338 int in_literal_form = 0;
1339 int in_control_form = 0;
1340 int need_slashslash = 0;
1341 unsigned int c;
1342 extern int repeat_count_threshold;
1343 extern int print_max;
1344
1345 if (length == 0)
1346 {
1347 chill_printchar ('\0', stream);
1348 return;
1349 }
1350
1351 for (i = 0; i < length && things_printed < print_max; ++i)
1352 {
1353 /* Position of the character we are examining
1354 to see whether it is repeated. */
1355 unsigned int rep1;
1356 /* Number of repetitions we have detected so far. */
1357 unsigned int reps;
1358
1359 QUIT;
1360
1361 if (need_slashslash)
1362 {
1363 fputs_filtered ("//", stream);
1364 need_slashslash = 0;
1365 }
1366
1367 rep1 = i + 1;
1368 reps = 1;
1369 while (rep1 < length && string[rep1] == string[i])
1370 {
1371 ++rep1;
1372 ++reps;
1373 }
1374
1375 c = string[i];
1376 if (reps > repeat_count_threshold)
1377 {
1378 if (in_control_form || in_literal_form)
1379 {
1380 fputs_filtered ("'//", stream);
1381 in_control_form = in_literal_form = 0;
1382 }
1383 chill_printchar (c, stream);
1384 fprintf_filtered (stream, "<repeats %u times>", reps);
1385 i = rep1 - 1;
1386 things_printed += repeat_count_threshold;
1387 need_slashslash = 1;
1388 }
1389 else
1390 {
1391 if (PRINT_LITERAL_FORM (c))
1392 {
1393 if (!in_literal_form)
1394 {
1395 if (in_control_form)
1396 {
1397 fputs_filtered ("'//", stream);
1398 in_control_form = 0;
1399 }
1400 fputs_filtered ("'", stream);
1401 in_literal_form = 1;
1402 }
1403 fprintf_filtered (stream, "%c", c);
1404 }
1405 else
1406 {
1407 if (!in_control_form)
1408 {
1409 if (in_literal_form)
1410 {
1411 fputs_filtered ("'//", stream);
1412 in_literal_form = 0;
1413 }
1414 fputs_filtered ("c'", stream);
1415 in_control_form = 1;
1416 }
1417 fprintf_filtered (stream, "%.2x", c);
1418 }
1419 ++things_printed;
1420 }
1421 }
1422
1423 /* Terminate the quotes if necessary. */
1424 if (in_literal_form || in_control_form)
1425 {
1426 fputs_filtered ("'", stream);
1427 }
1428 if (force_ellipses || (i < length))
1429 {
1430 fputs_filtered ("...", stream);
1431 }
1432 }
1433
1434 \f
1435 /* Table of operators and their precedences for printing expressions. */
1436
1437 const static struct op_print chill_op_print_tab[] = {
1438 {"AND", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1439 {"OR", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1440 {"NOT", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1441 {"MOD", BINOP_REM, PREC_MUL, 0},
1442 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1443 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
1444 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1445 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1446 {">=", BINOP_GEQ, PREC_ORDER, 0},
1447 {">", BINOP_GTR, PREC_ORDER, 0},
1448 {"<", BINOP_LESS, PREC_ORDER, 0},
1449 {"+", BINOP_ADD, PREC_ADD, 0},
1450 {"-", BINOP_SUB, PREC_ADD, 0},
1451 {"*", BINOP_MUL, PREC_MUL, 0},
1452 {"/", BINOP_DIV, PREC_MUL, 0},
1453 {"-", UNOP_NEG, PREC_PREFIX, 0},
1454 {NULL, 0, 0, 0}
1455 };
1456
1457 \f
1458 /* The built-in types of Chill. */
1459
1460 struct type *builtin_type_chill_bool;
1461 struct type *builtin_type_chill_char;
1462 struct type *builtin_type_chill_long;
1463 struct type *builtin_type_chill_ulong;
1464 struct type *builtin_type_chill_real;
1465
1466 struct type ** const (chill_builtin_types[]) =
1467 {
1468 &builtin_type_chill_bool,
1469 &builtin_type_chill_char,
1470 &builtin_type_chill_long,
1471 &builtin_type_chill_ulong,
1472 &builtin_type_chill_real,
1473 0
1474 };
1475
1476 const struct language_defn chill_language_defn = {
1477 "chill",
1478 language_chill,
1479 chill_builtin_types,
1480 range_check_on,
1481 type_check_on,
1482 chill_parse, /* parser */
1483 chill_error, /* parser error function */
1484 chill_printchar, /* print a character constant */
1485 chill_printstr, /* function to print a string constant */
1486 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1487 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1488 &builtin_type_chill_real, /* longest floating point type */
1489 {"", "B'", "", ""}, /* Binary format info */
1490 {"O'%o", "O'", "o", ""}, /* Octal format info */
1491 {"D'%d", "D'", "d", ""}, /* Decimal format info */
1492 {"H'%x", "H'", "x", ""}, /* Hex format info */
1493 chill_op_print_tab, /* expression operators for printing */
1494 LANG_MAGIC
1495 };
1496
1497 /* Initialization for Chill */
1498
1499 void
1500 _initialize_chill_exp ()
1501 {
1502 builtin_type_chill_bool =
1503 init_type (TYPE_CODE_BOOL, TARGET_INT_BIT / TARGET_CHAR_BIT,
1504 TYPE_FLAG_UNSIGNED,
1505 "BOOL", (struct objfile *) NULL);
1506 builtin_type_chill_char =
1507 init_type (TYPE_CODE_CHAR, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1508 TYPE_FLAG_UNSIGNED,
1509 "CHAR", (struct objfile *) NULL);
1510 builtin_type_chill_long =
1511 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1512 0,
1513 "LONG", (struct objfile *) NULL);
1514 builtin_type_chill_ulong =
1515 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1516 TYPE_FLAG_UNSIGNED,
1517 "ULONG", (struct objfile *) NULL);
1518 builtin_type_chill_real =
1519 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1520 0,
1521 "LONG_REAL", (struct objfile *) NULL);
1522
1523 add_language (&chill_language_defn);
1524 }