* ch-exp.y (match_character_literal): Fix case where no
[deliverable/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 else
1018 {
1019 /* Not a character literal. */
1020 return (0);
1021 }
1022 yylval.typed_val.val = ival;
1023 yylval.typed_val.type = builtin_type_chill_char;
1024 lexptr = tokptr;
1025 return (CHARACTER_LITERAL);
1026 }
1027
1028 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1029 Note that according to 5.2.4.2, a single "_" is also a valid integer
1030 literal, however GNU-chill requires there to be at least one "digit"
1031 in any integer literal. */
1032
1033 static int
1034 match_integer_literal ()
1035 {
1036 char *tokptr = lexptr;
1037 int ival;
1038
1039 if (!decode_integer_literal (&ival, &tokptr))
1040 {
1041 return (0);
1042 }
1043 else
1044 {
1045 yylval.typed_val.val = ival;
1046 yylval.typed_val.type = builtin_type_int;
1047 lexptr = tokptr;
1048 return (INTEGER_LITERAL);
1049 }
1050 }
1051
1052 static void convert_float ()
1053 {
1054 #if 0
1055 extern double strtod ();
1056 double d;
1057 char tmp[256];
1058 char *p = yytext, *p1 = tmp;
1059 char c;
1060
1061 while (c = *p++)
1062 {
1063 switch (c)
1064 {
1065 case '_':
1066 break;
1067 case 'E':
1068 case 'd':
1069 case 'D':
1070 *p1++ = 'e';
1071 break;
1072 default:
1073 *p1++ = c;
1074 break;
1075 }
1076 }
1077 *p1 = '\0';
1078 d = strtod (tmp, &p1);
1079 if (*p1)
1080 {
1081 /* add error handling here */
1082 ;
1083 }
1084 yylval.dval = d;
1085 #endif
1086 }
1087
1088 /* Take care of parsing a number (anything that starts with a digit).
1089 Set yylval and return the token type; update lexptr.
1090 LEN is the number of characters in it. */
1091
1092 /*** Needs some error checking for the float case ***/
1093
1094 static int
1095 parse_number ()
1096 {
1097 }
1098
1099 struct token
1100 {
1101 char *operator;
1102 int token;
1103 };
1104
1105 const static struct token tokentab5[] =
1106 {
1107 { "ANDIF", ANDIF }
1108 };
1109
1110 const static struct token tokentab4[] =
1111 {
1112 { "ORIF", ORIF }
1113 };
1114
1115 const static struct token tokentab3[] =
1116 {
1117 { "NOT", NOT },
1118 { "XOR", LOGXOR },
1119 { "AND", LOGAND }
1120 };
1121
1122 const static struct token tokentab2[] =
1123 {
1124 { "//", SLASH_SLASH },
1125 { "/=", NOTEQUAL },
1126 { "<=", LEQ },
1127 { ">=", GTR },
1128 { "IN", IN },
1129 { "OR", LOGIOR }
1130 };
1131
1132 /* Read one token, getting characters through lexptr. */
1133 /* This is where we will check to make sure that the language and the
1134 operators used are compatible. */
1135
1136 static int
1137 yylex ()
1138 {
1139 unsigned int i;
1140 int token;
1141 char *simplename;
1142 struct symbol *sym;
1143
1144 /* Skip over any leading whitespace. */
1145 while (isspace (*lexptr))
1146 {
1147 lexptr++;
1148 }
1149 /* Look for special single character cases which can't be the first
1150 character of some other multicharacter token. */
1151 switch (*lexptr)
1152 {
1153 case '\0':
1154 return (0);
1155 case '.':
1156 case '=':
1157 case ':':
1158 case ';':
1159 case '!':
1160 case '+':
1161 case '-':
1162 case '*':
1163 case '/':
1164 case '(':
1165 case ')':
1166 case '[':
1167 case ']':
1168 return (*lexptr++);
1169 }
1170 /* Look for characters which start a particular kind of multicharacter
1171 token, such as a character literal. */
1172 switch (*lexptr)
1173 {
1174 case 'C':
1175 case 'c':
1176 case '\'':
1177 token = match_character_literal ();
1178 if (token != 0)
1179 {
1180 return (token);
1181 }
1182 break;
1183 }
1184 /* See if it is a special token of length 5. */
1185 for (i = 0; i < sizeof (tokentab5) / sizeof (tokentab5[0]); i++)
1186 {
1187 if (strncmp (lexptr, tokentab5[i].operator, 5) == 0)
1188 {
1189 lexptr += 5;
1190 return (tokentab5[i].token);
1191 }
1192 }
1193 /* See if it is a special token of length 4. */
1194 for (i = 0; i < sizeof (tokentab4) / sizeof (tokentab4[0]); i++)
1195 {
1196 if (strncmp (lexptr, tokentab4[i].operator, 4) == 0)
1197 {
1198 lexptr += 4;
1199 return (tokentab4[i].token);
1200 }
1201 }
1202 /* See if it is a special token of length 3. */
1203 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1204 {
1205 if (strncmp (lexptr, tokentab3[i].operator, 3) == 0)
1206 {
1207 lexptr += 3;
1208 return (tokentab3[i].token);
1209 }
1210 }
1211 /* See if it is a special token of length 2. */
1212 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1213 {
1214 if (strncmp (lexptr, tokentab2[i].operator, 2) == 0)
1215 {
1216 lexptr += 2;
1217 return (tokentab2[i].token);
1218 }
1219 }
1220 /* Look for single character cases which which could be the first
1221 character of some other multicharacter token, but aren't, or we
1222 would already have found it. */
1223 switch (*lexptr)
1224 {
1225 case '/':
1226 case '<':
1227 case '>':
1228 return (*lexptr++);
1229 }
1230 /* Look for other special tokens. */
1231 if (strncmp (lexptr, "TRUE", 4) == 0) /* FIXME: What about lowercase? */
1232 {
1233 yylval.ulval = 1;
1234 lexptr += 4;
1235 return (BOOLEAN_LITERAL);
1236 }
1237 if (strncmp (lexptr, "FALSE", 5) == 0) /* FIXME: What about lowercase? */
1238 {
1239 yylval.ulval = 0;
1240 lexptr += 5;
1241 return (BOOLEAN_LITERAL);
1242 }
1243 token = match_integer_literal ();
1244 if (token != 0)
1245 {
1246 return (token);
1247 }
1248
1249 /* Try to match a simple name string, and if a match is found, then
1250 further classify what sort of name it is and return an appropriate
1251 token. Note that attempting to match a simple name string consumes
1252 the token from lexptr, so we can't back out if we later find that
1253 we can't classify what sort of name it is. */
1254
1255 simplename = match_simple_name_string ();
1256 if (simplename != NULL)
1257 {
1258 sym = lookup_symbol (simplename, expression_context_block,
1259 VAR_NAMESPACE, (int *) NULL,
1260 (struct symtab **) NULL);
1261 if (sym != NULL)
1262 {
1263 yylval.ssym.stoken.ptr = NULL;
1264 yylval.ssym.stoken.length = 0;
1265 yylval.ssym.sym = sym;
1266 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1267 switch (SYMBOL_CLASS (sym))
1268 {
1269 case LOC_BLOCK:
1270 /* Found a procedure name. */
1271 return (GENERAL_PROCEDURE_NAME);
1272 case LOC_STATIC:
1273 /* Found a global or local static variable. */
1274 return (LOCATION_NAME);
1275 }
1276 }
1277 else if (!have_full_symbols () && !have_partial_symbols ())
1278 {
1279 error ("No symbol table is loaded. Use the \"file\" command.");
1280 }
1281 else
1282 {
1283 error ("No symbol \"%s\" in current context.", simplename);
1284 }
1285 }
1286
1287 return (ILLEGAL_TOKEN);
1288 }
1289
1290 static void
1291 yyerror (msg)
1292 char *msg; /* unused */
1293 {
1294 printf ("Parsing: %s\n", lexptr);
1295 if (yychar < 256)
1296 {
1297 error ("Invalid syntax in expression near character '%c'.", yychar);
1298 }
1299 else
1300 {
1301 error ("Invalid syntax in expression");
1302 }
1303 }
1304
1305 \f
1306 static void
1307 chill_printchar (c, stream)
1308 register int c;
1309 FILE *stream;
1310 {
1311 c &= 0xFF; /* Avoid sign bit follies */
1312
1313 if (PRINT_LITERAL_FORM (c))
1314 {
1315 fprintf_filtered (stream, "'%c'", c);
1316 }
1317 else
1318 {
1319 fprintf_filtered (stream, "C'%.2x'", (unsigned int) c);
1320 }
1321 }
1322
1323 /* Print the character string STRING, printing at most LENGTH characters.
1324 Printing stops early if the number hits print_max; repeat counts
1325 are printed as appropriate. Print ellipses at the end if we
1326 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
1327 Note that gdb maintains the length of strings without counting the
1328 terminating null byte, while chill strings are typically written with
1329 an explicit null byte. So we always assume an implied null byte
1330 until gdb is able to maintain non-null terminated strings as well
1331 as null terminated strings (FIXME).
1332 */
1333
1334 static void
1335 chill_printstr (stream, string, length, force_ellipses)
1336 FILE *stream;
1337 char *string;
1338 unsigned int length;
1339 int force_ellipses;
1340 {
1341 register unsigned int i;
1342 unsigned int things_printed = 0;
1343 int in_literal_form = 0;
1344 int in_control_form = 0;
1345 int need_slashslash = 0;
1346 unsigned int c;
1347 extern int repeat_count_threshold;
1348 extern int print_max;
1349
1350 if (length == 0)
1351 {
1352 chill_printchar ('\0', stream);
1353 return;
1354 }
1355
1356 for (i = 0; i < length && things_printed < print_max; ++i)
1357 {
1358 /* Position of the character we are examining
1359 to see whether it is repeated. */
1360 unsigned int rep1;
1361 /* Number of repetitions we have detected so far. */
1362 unsigned int reps;
1363
1364 QUIT;
1365
1366 if (need_slashslash)
1367 {
1368 fputs_filtered ("//", stream);
1369 need_slashslash = 0;
1370 }
1371
1372 rep1 = i + 1;
1373 reps = 1;
1374 while (rep1 < length && string[rep1] == string[i])
1375 {
1376 ++rep1;
1377 ++reps;
1378 }
1379
1380 c = string[i];
1381 if (reps > repeat_count_threshold)
1382 {
1383 if (in_control_form || in_literal_form)
1384 {
1385 fputs_filtered ("'//", stream);
1386 in_control_form = in_literal_form = 0;
1387 }
1388 chill_printchar (c, stream);
1389 fprintf_filtered (stream, "<repeats %u times>", reps);
1390 i = rep1 - 1;
1391 things_printed += repeat_count_threshold;
1392 need_slashslash = 1;
1393 }
1394 else
1395 {
1396 if (PRINT_LITERAL_FORM (c))
1397 {
1398 if (!in_literal_form)
1399 {
1400 if (in_control_form)
1401 {
1402 fputs_filtered ("'//", stream);
1403 in_control_form = 0;
1404 }
1405 fputs_filtered ("'", stream);
1406 in_literal_form = 1;
1407 }
1408 fprintf_filtered (stream, "%c", c);
1409 }
1410 else
1411 {
1412 if (!in_control_form)
1413 {
1414 if (in_literal_form)
1415 {
1416 fputs_filtered ("'//", stream);
1417 in_literal_form = 0;
1418 }
1419 fputs_filtered ("c'", stream);
1420 in_control_form = 1;
1421 }
1422 fprintf_filtered (stream, "%.2x", c);
1423 }
1424 ++things_printed;
1425 }
1426 }
1427
1428 /* Terminate the quotes if necessary. */
1429 if (in_literal_form || in_control_form)
1430 {
1431 fputs_filtered ("'", stream);
1432 }
1433 if (force_ellipses || (i < length))
1434 {
1435 fputs_filtered ("...", stream);
1436 }
1437 }
1438
1439 static struct type *
1440 chill_create_fundamental_type (objfile, typeid)
1441 struct objfile *objfile;
1442 int typeid;
1443 {
1444 register struct type *type = NULL;
1445 register int nbytes;
1446
1447 switch (typeid)
1448 {
1449 default:
1450 /* FIXME: For now, if we are asked to produce a type not in this
1451 language, create the equivalent of a C integer type with the
1452 name "<?type?>". When all the dust settles from the type
1453 reconstruction work, this should probably become an error. */
1454 type = init_type (TYPE_CODE_INT,
1455 TARGET_INT_BIT / TARGET_CHAR_BIT,
1456 0, "<?type?>", objfile);
1457 warning ("internal error: no chill fundamental type %d", typeid);
1458 break;
1459 case FT_BOOLEAN:
1460 type = init_type (TYPE_CODE_BOOL, 1, TYPE_FLAG_UNSIGNED, "BOOL", objfile);
1461 break;
1462 case FT_CHAR:
1463 type = init_type (TYPE_CODE_INT, 1, TYPE_FLAG_UNSIGNED, "CHAR", objfile);
1464 break;
1465 case FT_SIGNED_CHAR:
1466 type = init_type (TYPE_CODE_INT, 1, TYPE_FLAG_SIGNED, "BYTE", objfile);
1467 break;
1468 case FT_UNSIGNED_CHAR:
1469 type = init_type (TYPE_CODE_INT, 1, TYPE_FLAG_UNSIGNED, "UBYTE", objfile);
1470 break;
1471 case FT_SHORT: /* Chill ints are 2 bytes */
1472 type = init_type (TYPE_CODE_INT, 2, TYPE_FLAG_SIGNED, "INT", objfile);
1473 break;
1474 case FT_UNSIGNED_SHORT: /* Chill ints are 2 bytes */
1475 type = init_type (TYPE_CODE_INT, 2, TYPE_FLAG_UNSIGNED, "UINT", objfile);
1476 break;
1477 case FT_INTEGER: /* Chill longs are 4 bytes */
1478 type = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_SIGNED, "LONG", objfile);
1479 break;
1480 case FT_UNSIGNED_INTEGER: /* Chill longs are 4 bytes */
1481 type = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_UNSIGNED, "ULONG", objfile);
1482 break;
1483 case FT_FLOAT:
1484 type = init_type (TYPE_CODE_FLT, 4, 0, "REAL", objfile);
1485 break;
1486 case FT_DBL_PREC_FLOAT:
1487 type = init_type (TYPE_CODE_FLT, 8, 0, "LONG_REAL", objfile);
1488 break;
1489 }
1490 return (type);
1491 }
1492
1493 \f
1494 /* Table of operators and their precedences for printing expressions. */
1495
1496 const static struct op_print chill_op_print_tab[] = {
1497 {"AND", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1498 {"OR", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1499 {"NOT", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1500 {"MOD", BINOP_REM, PREC_MUL, 0},
1501 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1502 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
1503 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1504 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1505 {">=", BINOP_GEQ, PREC_ORDER, 0},
1506 {">", BINOP_GTR, PREC_ORDER, 0},
1507 {"<", BINOP_LESS, PREC_ORDER, 0},
1508 {"+", BINOP_ADD, PREC_ADD, 0},
1509 {"-", BINOP_SUB, PREC_ADD, 0},
1510 {"*", BINOP_MUL, PREC_MUL, 0},
1511 {"/", BINOP_DIV, PREC_MUL, 0},
1512 {"-", UNOP_NEG, PREC_PREFIX, 0},
1513 {NULL, 0, 0, 0}
1514 };
1515
1516 \f
1517 /* The built-in types of Chill. */
1518
1519 struct type *builtin_type_chill_bool;
1520 struct type *builtin_type_chill_char;
1521 struct type *builtin_type_chill_long;
1522 struct type *builtin_type_chill_ulong;
1523 struct type *builtin_type_chill_real;
1524
1525 struct type ** const (chill_builtin_types[]) =
1526 {
1527 &builtin_type_chill_bool,
1528 &builtin_type_chill_char,
1529 &builtin_type_chill_long,
1530 &builtin_type_chill_ulong,
1531 &builtin_type_chill_real,
1532 0
1533 };
1534
1535 const struct language_defn chill_language_defn = {
1536 "chill",
1537 language_chill,
1538 chill_builtin_types,
1539 range_check_on,
1540 type_check_on,
1541 chill_parse, /* parser */
1542 chill_error, /* parser error function */
1543 chill_printchar, /* print a character constant */
1544 chill_printstr, /* function to print a string constant */
1545 chill_create_fundamental_type,/* Create fundamental type in this language */
1546 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1547 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1548 &builtin_type_chill_real, /* longest floating point type */
1549 {"", "B'", "", ""}, /* Binary format info */
1550 {"O'%o", "O'", "o", ""}, /* Octal format info */
1551 {"D'%d", "D'", "d", ""}, /* Decimal format info */
1552 {"H'%x", "H'", "x", ""}, /* Hex format info */
1553 chill_op_print_tab, /* expression operators for printing */
1554 LANG_MAGIC
1555 };
1556
1557 /* Initialization for Chill */
1558
1559 void
1560 _initialize_chill_exp ()
1561 {
1562 builtin_type_chill_bool =
1563 init_type (TYPE_CODE_BOOL, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1564 TYPE_FLAG_UNSIGNED,
1565 "BOOL", (struct objfile *) NULL);
1566 builtin_type_chill_char =
1567 init_type (TYPE_CODE_CHAR, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1568 TYPE_FLAG_UNSIGNED,
1569 "CHAR", (struct objfile *) NULL);
1570 builtin_type_chill_long =
1571 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1572 0,
1573 "LONG", (struct objfile *) NULL);
1574 builtin_type_chill_ulong =
1575 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1576 TYPE_FLAG_UNSIGNED,
1577 "ULONG", (struct objfile *) NULL);
1578 builtin_type_chill_real =
1579 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1580 0,
1581 "LONG_REAL", (struct objfile *) NULL);
1582
1583 add_language (&chill_language_defn);
1584 }
This page took 0.065119 seconds and 5 git commands to generate.