* language.h (PRINT_LITERAL_FORM): New macro that takes character
[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 <voidval> SET_LITERAL
155 %token <voidval> EMPTINESS_LITERAL
156 %token <voidval> CHARACTER_STRING_LITERAL
157 %token <voidval> BIT_STRING_LITERAL
158
159 %token <voidval> STRING
160 %token <voidval> CONSTANT
161 %token <voidval> '.'
162 %token <voidval> ';'
163 %token <voidval> ':'
164 %token <voidval> CASE
165 %token <voidval> OF
166 %token <voidval> ESAC
167 %token <voidval> LOGIOR
168 %token <voidval> ORIF
169 %token <voidval> LOGXOR
170 %token <voidval> LOGAND
171 %token <voidval> ANDIF
172 %token <voidval> '='
173 %token <voidval> NOTEQUAL
174 %token <voidval> '>'
175 %token <voidval> GTR
176 %token <voidval> '<'
177 %token <voidval> LEQ
178 %token <voidval> IN
179 %token <voidval> '+'
180 %token <voidval> '-'
181 %token <voidval> '*'
182 %token <voidval> '/'
183 %token <voidval> SLASH_SLASH
184 %token <voidval> MOD
185 %token <voidval> REM
186 %token <voidval> NOT
187 %token <voidval> POINTER
188 %token <voidval> RECEIVE
189 %token <voidval> SC
190 %token <voidval> '['
191 %token <voidval> ']'
192 %token <voidval> '('
193 %token <voidval> ')'
194 %token <voidval> UP
195 %token <voidval> IF
196 %token <voidval> THEN
197 %token <voidval> ELSE
198 %token <voidval> FI
199 %token <voidval> ELSIF
200 %token <voidval> ILLEGAL_TOKEN
201
202 %type <voidval> location
203 %type <voidval> primitive_value
204 %type <voidval> location_contents
205 %type <voidval> value_name
206 %type <voidval> literal
207 %type <voidval> tuple
208 %type <voidval> value_string_element
209 %type <voidval> value_string_slice
210 %type <voidval> value_array_element
211 %type <voidval> value_array_slice
212 %type <voidval> value_structure_field
213 %type <voidval> expression_conversion
214 %type <voidval> value_procedure_call
215 %type <voidval> value_built_in_routine_call
216 %type <voidval> start_expression
217 %type <voidval> zero_adic_operator
218 %type <voidval> parenthesised_expression
219 %type <voidval> value
220 %type <voidval> undefined_value
221 %type <voidval> expression
222 %type <voidval> conditional_expression
223 %type <voidval> then_alternative
224 %type <voidval> else_alternative
225 %type <voidval> sub_expression
226 %type <voidval> value_case_alternative
227 %type <voidval> operand_0
228 %type <voidval> operand_1
229 %type <voidval> operand_2
230 %type <voidval> operand_3
231 %type <voidval> operand_4
232 %type <voidval> operand_5
233 %type <voidval> operand_6
234 %type <voidval> integer_literal_expression
235 %type <voidval> synonym_name
236 %type <voidval> value_enumeration_name
237 %type <voidval> value_do_with_name
238 %type <voidval> value_receive_name
239 %type <voidval> general_procedure_name
240 %type <voidval> string_primitive_value
241 %type <voidval> start_element
242 %type <voidval> left_element
243 %type <voidval> right_element
244 %type <voidval> slice_size
245 %type <voidval> array_primitive_value
246 %type <voidval> expression_list
247 %type <voidval> lower_element
248 %type <voidval> upper_element
249 %type <voidval> first_element
250 %type <voidval> structure_primitive_value
251 %type <voidval> field_name
252 %type <voidval> mode_name
253 %type <voidval> boolean_expression
254 %type <voidval> case_selector_list
255 %type <voidval> subexpression
256 %type <voidval> case_label_specification
257 %type <voidval> buffer_location
258
259 %%
260
261 /* Z.200, 5.3.1 */
262
263 value : expression
264 {
265 $$ = 0; /* FIXME */
266 }
267 | undefined_value
268 {
269 $$ = 0; /* FIXME */
270 }
271 ;
272
273 undefined_value : FIXME
274 {
275 $$ = 0; /* FIXME */
276 }
277 ;
278
279 /* Z.200, 4.2.1 */
280
281 location : FIXME
282 {
283 $$ = 0; /* FIXME */
284 }
285 ;
286
287 /* Z.200, 5.2.1 */
288
289 primitive_value : location_contents
290 {
291 $$ = 0; /* FIXME */
292 }
293 | value_name
294 {
295 $$ = 0; /* FIXME */
296 }
297 | literal
298 {
299 $$ = 0; /* FIXME */
300 }
301 | tuple
302 {
303 $$ = 0; /* FIXME */
304 }
305 | value_string_element
306 {
307 $$ = 0; /* FIXME */
308 }
309 | value_string_slice
310 {
311 $$ = 0; /* FIXME */
312 }
313 | value_array_element
314 {
315 $$ = 0; /* FIXME */
316 }
317 | value_array_slice
318 {
319 $$ = 0; /* FIXME */
320 }
321 | value_structure_field
322 {
323 $$ = 0; /* FIXME */
324 }
325 | expression_conversion
326 {
327 $$ = 0; /* FIXME */
328 }
329 | value_procedure_call
330 {
331 $$ = 0; /* FIXME */
332 }
333 | value_built_in_routine_call
334 {
335 $$ = 0; /* FIXME */
336 }
337 | start_expression
338 {
339 $$ = 0; /* FIXME */
340 }
341 | zero_adic_operator
342 {
343 $$ = 0; /* FIXME */
344 }
345 | parenthesised_expression
346 {
347 $$ = 0; /* FIXME */
348 }
349 ;
350
351 /* Z.200, 5.2.2 */
352
353 location_contents: location
354 {
355 $$ = 0; /* FIXME */
356 }
357 ;
358
359 /* Z.200, 5.2.3 */
360
361 value_name : synonym_name
362 {
363 $$ = 0; /* FIXME */
364 }
365 | value_enumeration_name
366 {
367 $$ = 0; /* FIXME */
368 }
369 | value_do_with_name
370 {
371 $$ = 0; /* FIXME */
372 }
373 | value_receive_name
374 {
375 $$ = 0; /* FIXME */
376 }
377 | general_procedure_name
378 {
379 $$ = 0; /* FIXME */
380 }
381 ;
382
383 /* Z.200, 5.2.4.1 */
384
385 literal : INTEGER_LITERAL
386 {
387 write_exp_elt_opcode (OP_LONG);
388 write_exp_elt_type ($1.type);
389 write_exp_elt_longcst ((LONGEST) ($1.val));
390 write_exp_elt_opcode (OP_LONG);
391 }
392 | BOOLEAN_LITERAL
393 {
394 write_exp_elt_opcode (OP_BOOL);
395 write_exp_elt_longcst ((LONGEST) $1);
396 write_exp_elt_opcode (OP_BOOL);
397 }
398 | CHARACTER_LITERAL
399 {
400 write_exp_elt_opcode (OP_LONG);
401 write_exp_elt_type ($1.type);
402 write_exp_elt_longcst ((LONGEST) ($1.val));
403 write_exp_elt_opcode (OP_LONG);
404 }
405 | SET_LITERAL
406 {
407 $$ = 0; /* FIXME */
408 }
409 | EMPTINESS_LITERAL
410 {
411 $$ = 0; /* FIXME */
412 }
413 | CHARACTER_STRING_LITERAL
414 {
415 $$ = 0; /* FIXME */
416 }
417 | BIT_STRING_LITERAL
418 {
419 $$ = 0; /* FIXME */
420 }
421 ;
422
423 /* Z.200, 5.2.5 */
424
425 tuple : FIXME
426 {
427 $$ = 0; /* FIXME */
428 }
429 ;
430
431
432 /* Z.200, 5.2.6 */
433
434 value_string_element: string_primitive_value '(' start_element ')'
435 {
436 $$ = 0; /* FIXME */
437 }
438 ;
439
440 /* Z.200, 5.2.7 */
441
442 value_string_slice: string_primitive_value '(' left_element ':' right_element ')'
443 {
444 $$ = 0; /* FIXME */
445 }
446 | string_primitive_value '(' start_element UP slice_size ')'
447 {
448 $$ = 0; /* FIXME */
449 }
450 ;
451
452 /* Z.200, 5.2.8 */
453
454 value_array_element: array_primitive_value '(' expression_list ')'
455 {
456 $$ = 0; /* FIXME */
457 }
458 ;
459
460 /* Z.200, 5.2.9 */
461
462 value_array_slice: array_primitive_value '(' lower_element ':' upper_element ')'
463 {
464 $$ = 0; /* FIXME */
465 }
466 | array_primitive_value '(' first_element UP slice_size '('
467 {
468 $$ = 0; /* FIXME */
469 }
470 ;
471
472 /* Z.200, 5.2.10 */
473
474 value_structure_field: structure_primitive_value '.' field_name
475 {
476 $$ = 0; /* FIXME */
477 }
478 ;
479
480 /* Z.200, 5.2.11 */
481
482 expression_conversion: mode_name '(' expression ')'
483 {
484 $$ = 0; /* FIXME */
485 }
486 ;
487
488 /* Z.200, 5.2.12 */
489
490 value_procedure_call: FIXME
491 {
492 $$ = 0; /* FIXME */
493 }
494 ;
495
496 /* Z.200, 5.2.13 */
497
498 value_built_in_routine_call: FIXME
499 {
500 $$ = 0; /* FIXME */
501 }
502 ;
503
504 /* Z.200, 5.2.14 */
505
506 start_expression: FIXME
507 {
508 $$ = 0; /* FIXME */
509 } /* Not in GNU-Chill */
510 ;
511
512 /* Z.200, 5.2.15 */
513
514 zero_adic_operator: FIXME
515 {
516 $$ = 0; /* FIXME */
517 }
518 ;
519
520 /* Z.200, 5.2.16 */
521
522 parenthesised_expression: '(' expression ')'
523 {
524 $$ = 0; /* FIXME */
525 }
526 ;
527
528 /* Z.200, 5.3.2 */
529
530 expression : operand_0
531 {
532 $$ = 0; /* FIXME */
533 }
534 | conditional_expression
535 {
536 $$ = 0; /* FIXME */
537 }
538 ;
539
540 conditional_expression : IF boolean_expression then_alternative else_alternative FI
541 {
542 $$ = 0; /* FIXME */
543 }
544 | CASE case_selector_list OF value_case_alternative '[' ELSE sub_expression ']' ESAC
545 {
546 $$ = 0; /* FIXME */
547 }
548 ;
549
550 then_alternative: THEN subexpression
551 {
552 $$ = 0; /* FIXME */
553 }
554 ;
555
556 else_alternative: ELSE subexpression
557 {
558 $$ = 0; /* FIXME */
559 }
560 | ELSIF boolean_expression then_alternative else_alternative
561 {
562 $$ = 0; /* FIXME */
563 }
564 ;
565
566 sub_expression : expression
567 {
568 $$ = 0; /* FIXME */
569 }
570 ;
571
572 value_case_alternative: case_label_specification ':' sub_expression ';'
573 {
574 $$ = 0; /* FIXME */
575 }
576 ;
577
578 /* Z.200, 5.3.3 */
579
580 operand_0 : operand_1
581 {
582 $$ = 0; /* FIXME */
583 }
584 | operand_0 LOGIOR operand_1
585 {
586 write_exp_elt_opcode (BINOP_BITWISE_IOR);
587 }
588 | operand_0 ORIF operand_1
589 {
590 $$ = 0; /* FIXME */
591 }
592 | operand_0 LOGXOR operand_1
593 {
594 write_exp_elt_opcode (BINOP_BITWISE_XOR);
595 }
596 ;
597
598 /* Z.200, 5.3.4 */
599
600 operand_1 : operand_2
601 {
602 $$ = 0; /* FIXME */
603 }
604 | operand_1 LOGAND operand_2
605 {
606 write_exp_elt_opcode (BINOP_BITWISE_AND);
607 }
608 | operand_1 ANDIF operand_2
609 {
610 $$ = 0; /* FIXME */
611 }
612 ;
613
614 /* Z.200, 5.3.5 */
615
616 operand_2 : operand_3
617 {
618 $$ = 0; /* FIXME */
619 }
620 | operand_2 '=' operand_3
621 {
622 write_exp_elt_opcode (BINOP_EQUAL);
623 }
624 | operand_2 NOTEQUAL operand_3
625 {
626 write_exp_elt_opcode (BINOP_NOTEQUAL);
627 }
628 | operand_2 '>' operand_3
629 {
630 write_exp_elt_opcode (BINOP_GTR);
631 }
632 | operand_2 GTR operand_3
633 {
634 write_exp_elt_opcode (BINOP_GEQ);
635 }
636 | operand_2 '<' operand_3
637 {
638 write_exp_elt_opcode (BINOP_LESS);
639 }
640 | operand_2 LEQ operand_3
641 {
642 write_exp_elt_opcode (BINOP_LEQ);
643 }
644 | operand_2 IN operand_3
645 {
646 $$ = 0; /* FIXME */
647 }
648 ;
649
650
651 /* Z.200, 5.3.6 */
652
653 operand_3 : operand_4
654 {
655 $$ = 0; /* FIXME */
656 }
657 | operand_3 '+' operand_4
658 {
659 write_exp_elt_opcode (BINOP_ADD);
660 }
661 | operand_3 '-' operand_4
662 {
663 write_exp_elt_opcode (BINOP_SUB);
664 }
665 | operand_3 SLASH_SLASH operand_4
666 {
667 $$ = 0; /* FIXME */
668 }
669 ;
670
671 /* Z.200, 5.3.7 */
672
673 operand_4 : operand_5
674 {
675 $$ = 0; /* FIXME */
676 }
677 | operand_4 '*' operand_5
678 {
679 write_exp_elt_opcode (BINOP_MUL);
680 }
681 | operand_4 '/' operand_5
682 {
683 write_exp_elt_opcode (BINOP_DIV);
684 }
685 | operand_4 MOD operand_5
686 {
687 $$ = 0; /* FIXME */
688 }
689 | operand_4 REM operand_5
690 {
691 $$ = 0; /* FIXME */
692 }
693 ;
694
695 /* Z.200, 5.3.8 */
696
697 operand_5 : operand_6
698 {
699 $$ = 0; /* FIXME */
700 }
701 | '-' operand_6
702 {
703 write_exp_elt_opcode (UNOP_NEG);
704 }
705 | NOT operand_6
706 {
707 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
708 }
709 | '(' integer_literal_expression ')' operand_6
710 {
711 $$ = 0; /* FIXME */
712 }
713 ;
714
715 /* Z.200, 5.3.9 */
716
717 operand_6 : POINTER location
718 {
719 $$ = 0; /* FIXME */
720 }
721 | RECEIVE buffer_location
722 {
723 $$ = 0; /* FIXME */
724 }
725 | primitive_value
726 {
727 $$ = 0; /* FIXME */
728 }
729 ;
730
731
732 /* Z.200, 12.4.3 */
733 /* FIXME: For now we just accept only a single integer literal. */
734
735 integer_literal_expression:
736 INTEGER_LITERAL
737 {
738 $$ = 0;
739 }
740
741 /* Things which still need productions... */
742 synonym_name : FIXME { $$ = 0; }
743 value_enumeration_name : FIXME { $$ = 0; }
744 value_do_with_name : FIXME { $$ = 0; }
745 value_receive_name : FIXME { $$ = 0; }
746 general_procedure_name : FIXME { $$ = 0; }
747 string_primitive_value : FIXME { $$ = 0; }
748 start_element : FIXME { $$ = 0; }
749 left_element : FIXME { $$ = 0; }
750 right_element : FIXME { $$ = 0; }
751 slice_size : FIXME { $$ = 0; }
752 array_primitive_value : FIXME { $$ = 0; }
753 expression_list : FIXME { $$ = 0; }
754 lower_element : FIXME { $$ = 0; }
755 upper_element : FIXME { $$ = 0; }
756 first_element : FIXME { $$ = 0; }
757 structure_primitive_value: FIXME { $$ = 0; }
758 field_name : FIXME { $$ = 0; }
759 mode_name : FIXME { $$ = 0; }
760 boolean_expression : FIXME { $$ = 0; }
761 case_selector_list : FIXME { $$ = 0; }
762 subexpression : FIXME { $$ = 0; }
763 case_label_specification: FIXME { $$ = 0; }
764 buffer_location : FIXME { $$ = 0; }
765
766 %%
767
768 /* Start looking for a value composed of valid digits as set by the base
769 in use. Note that '_' characters are valid anywhere, in any quantity,
770 and are simply ignored. Since we must find at least one valid digit,
771 or reject this token as an integer literal, we keep track of how many
772 digits we have encountered. */
773
774 static int
775 decode_integer_value (base, tokptrptr, ivalptr)
776 int base;
777 char **tokptrptr;
778 int *ivalptr;
779 {
780 char *tokptr = *tokptrptr;
781 int temp;
782 int digits = 0;
783
784 while (*tokptr != '\0')
785 {
786 temp = tolower (*tokptr);
787 tokptr++;
788 switch (temp)
789 {
790 case '_':
791 continue;
792 case '0': case '1': case '2': case '3': case '4':
793 case '5': case '6': case '7': case '8': case '9':
794 temp -= '0';
795 break;
796 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
797 temp -= 'a';
798 temp += 10;
799 break;
800 default:
801 temp = base;
802 break;
803 }
804 if (temp < base)
805 {
806 digits++;
807 *ivalptr *= base;
808 *ivalptr += temp;
809 }
810 else
811 {
812 /* Found something not in domain for current base. */
813 tokptr--; /* Unconsume what gave us indigestion. */
814 break;
815 }
816 }
817
818 /* If we didn't find any digits, then we don't have a valid integer
819 value, so reject the entire token. Otherwise, update the lexical
820 scan pointer, and return non-zero for success. */
821
822 if (digits == 0)
823 {
824 return (0);
825 }
826 else
827 {
828 *tokptrptr = tokptr;
829 return (1);
830 }
831 }
832
833 static int
834 decode_integer_literal (valptr, tokptrptr)
835 int *valptr;
836 char **tokptrptr;
837 {
838 char *tokptr = *tokptrptr;
839 int base = 0;
840 int ival = 0;
841 int digits = 0;
842 int temp;
843 int explicit_base = 0;
844
845 /* Look for an explicit base specifier, which is optional. */
846
847 switch (*tokptr)
848 {
849 case 'd':
850 case 'D':
851 explicit_base++;
852 base = 10;
853 tokptr++;
854 break;
855 case 'b':
856 case 'B':
857 explicit_base++;
858 base = 2;
859 tokptr++;
860 break;
861 case 'h':
862 case 'H':
863 explicit_base++;
864 base = 16;
865 tokptr++;
866 break;
867 case 'o':
868 case 'O':
869 explicit_base++;
870 base = 8;
871 tokptr++;
872 break;
873 default:
874 base = 10;
875 break;
876 }
877
878 /* If we found an explicit base ensure that the character after the
879 explicit base is a single quote. */
880
881 if (explicit_base && (*tokptr++ != '\''))
882 {
883 return (0);
884 }
885
886 /* Attempt to decode whatever follows as an integer value in the
887 indicated base, updating the token pointer in the process and
888 computing the value into ival. Also, if we have an explicit
889 base, then the next character must not be a single quote, or we
890 have a bitstring literal, so reject the entire token in this case.
891 Otherwise, update the lexical scan pointer, and return non-zero
892 for success. */
893
894 if (!decode_integer_value (base, &tokptr, &ival))
895 {
896 return (0);
897 }
898 else if (explicit_base && (*tokptr == '\''))
899 {
900 return (0);
901 }
902 else
903 {
904 *valptr = ival;
905 *tokptrptr = tokptr;
906 return (1);
907 }
908 }
909
910 /* Recognize a character literal. A character literal is single character
911 or a control sequence, enclosed in single quotes. A control sequence
912 is a comma separated list of one or more integer literals, enclosed
913 in parenthesis and introduced with a circumflex character.
914
915 EX: 'a' '^(7)' '^(7,8)'
916
917 As a GNU chill extension, the syntax C'xx' is also recognized as a
918 character literal, where xx is a hex value for the character.
919
920 Returns CHARACTER_LITERAL if a match is found.
921 */
922
923 static int
924 match_character_literal ()
925 {
926 char *tokptr = lexptr;
927 int ival = 0;
928
929 if ((tolower (*tokptr) == 'c') && (*(tokptr + 1) == '\''))
930 {
931 /* We have a GNU chill extension form, so skip the leading "C'",
932 decode the hex value, and then ensure that we have a trailing
933 single quote character. */
934 tokptr += 2;
935 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
936 {
937 return (0);
938 }
939 tokptr++;
940 }
941 else if (*tokptr == '\'')
942 {
943 tokptr++;
944
945 /* Determine which form we have, either a control sequence or the
946 single character form. */
947
948 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
949 {
950 /* Match and decode a control sequence. Return zero if we don't
951 find a valid integer literal, or if the next unconsumed character
952 after the integer literal is not the trailing ')'.
953 FIXME: We currently don't handle the multiple integer literal
954 form. */
955 tokptr += 2;
956 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
957 {
958 return (0);
959 }
960 }
961 else
962 {
963 ival = *tokptr++;
964 }
965
966 /* The trailing quote has not yet been consumed. If we don't find
967 it, then we have no match. */
968
969 if (*tokptr++ != '\'')
970 {
971 return (0);
972 }
973 }
974 yylval.typed_val.val = ival;
975 yylval.typed_val.type = builtin_type_chill_char;
976 lexptr = tokptr;
977 return (CHARACTER_LITERAL);
978 }
979
980 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
981 Note that according to 5.2.4.2, a single "_" is also a valid integer
982 literal, however GNU-chill requires there to be at least one "digit"
983 in any integer literal. */
984
985 static int
986 match_integer_literal ()
987 {
988 char *tokptr = lexptr;
989 int ival;
990
991 if (!decode_integer_literal (&ival, &tokptr))
992 {
993 return (0);
994 }
995 else
996 {
997 yylval.typed_val.val = ival;
998 yylval.typed_val.type = builtin_type_int;
999 lexptr = tokptr;
1000 return (INTEGER_LITERAL);
1001 }
1002 }
1003
1004 static void convert_float ()
1005 {
1006 #if 0
1007 extern double strtod ();
1008 double d;
1009 char tmp[256];
1010 char *p = yytext, *p1 = tmp;
1011 char c;
1012
1013 while (c = *p++)
1014 {
1015 switch (c)
1016 {
1017 case '_':
1018 break;
1019 case 'E':
1020 case 'd':
1021 case 'D':
1022 *p1++ = 'e';
1023 break;
1024 default:
1025 *p1++ = c;
1026 break;
1027 }
1028 }
1029 *p1 = '\0';
1030 d = strtod (tmp, &p1);
1031 if (*p1)
1032 {
1033 /* add error handling here */
1034 ;
1035 }
1036 yylval.dval = d;
1037 #endif
1038 }
1039
1040 /* Take care of parsing a number (anything that starts with a digit).
1041 Set yylval and return the token type; update lexptr.
1042 LEN is the number of characters in it. */
1043
1044 /*** Needs some error checking for the float case ***/
1045
1046 static int
1047 parse_number ()
1048 {
1049 }
1050
1051 struct token
1052 {
1053 char *operator;
1054 int token;
1055 };
1056
1057 const static struct token tokentab5[] =
1058 {
1059 { "ANDIF", ANDIF }
1060 };
1061
1062 const static struct token tokentab4[] =
1063 {
1064 { "ORIF", ORIF }
1065 };
1066
1067 const static struct token tokentab3[] =
1068 {
1069 { "NOT", NOT },
1070 { "XOR", LOGXOR },
1071 { "AND", LOGAND }
1072 };
1073
1074 const static struct token tokentab2[] =
1075 {
1076 { "//", SLASH_SLASH },
1077 { "/=", NOTEQUAL },
1078 { "<=", LEQ },
1079 { ">=", GTR },
1080 { "IN", IN },
1081 { "OR", LOGIOR }
1082 };
1083
1084 /* Read one token, getting characters through lexptr. */
1085 /* This is where we will check to make sure that the language and the
1086 operators used are compatible. */
1087
1088 static int
1089 yylex ()
1090 {
1091 unsigned int i;
1092 int token;
1093
1094 /* Skip over any leading whitespace. */
1095 while (isspace (*lexptr))
1096 {
1097 lexptr++;
1098 }
1099 /* Look for special single character cases which can't be the first
1100 character of some other multicharacter token. */
1101 switch (*lexptr)
1102 {
1103 case '\0':
1104 return (0);
1105 case '.':
1106 case '=':
1107 case ':':
1108 case ';':
1109 case '!':
1110 case '+':
1111 case '-':
1112 case '*':
1113 case '/':
1114 case '(':
1115 case ')':
1116 case '[':
1117 case ']':
1118 return (*lexptr++);
1119 }
1120 /* Look for characters which start a particular kind of multicharacter
1121 token, such as a character literal. */
1122 switch (*lexptr)
1123 {
1124 case 'C':
1125 case 'c':
1126 case '\'':
1127 token = match_character_literal ();
1128 if (token != 0)
1129 {
1130 return (token);
1131 }
1132 break;
1133 }
1134 /* See if it is a special token of length 5. */
1135 for (i = 0; i < sizeof (tokentab5) / sizeof (tokentab5[0]); i++)
1136 {
1137 if (strncmp (lexptr, tokentab5[i].operator, 5) == 0)
1138 {
1139 lexptr += 5;
1140 return (tokentab5[i].token);
1141 }
1142 }
1143 /* See if it is a special token of length 4. */
1144 for (i = 0; i < sizeof (tokentab4) / sizeof (tokentab4[0]); i++)
1145 {
1146 if (strncmp (lexptr, tokentab4[i].operator, 4) == 0)
1147 {
1148 lexptr += 4;
1149 return (tokentab4[i].token);
1150 }
1151 }
1152 /* See if it is a special token of length 3. */
1153 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1154 {
1155 if (strncmp (lexptr, tokentab3[i].operator, 3) == 0)
1156 {
1157 lexptr += 3;
1158 return (tokentab3[i].token);
1159 }
1160 }
1161 /* See if it is a special token of length 2. */
1162 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1163 {
1164 if (strncmp (lexptr, tokentab2[i].operator, 2) == 0)
1165 {
1166 lexptr += 2;
1167 return (tokentab2[i].token);
1168 }
1169 }
1170 /* Look for single character cases which which could be the first
1171 character of some other multicharacter token, but aren't, or we
1172 would already have found it. */
1173 switch (*lexptr)
1174 {
1175 case '/':
1176 case '<':
1177 case '>':
1178 return (*lexptr++);
1179 }
1180 /* Look for other special tokens. */
1181 if (strncmp (lexptr, "TRUE", 4) == 0) /* FIXME: What about lowercase? */
1182 {
1183 yylval.ulval = 1;
1184 lexptr += 4;
1185 return (BOOLEAN_LITERAL);
1186 }
1187 if (strncmp (lexptr, "FALSE", 5) == 0) /* FIXME: What about lowercase? */
1188 {
1189 yylval.ulval = 0;
1190 lexptr += 5;
1191 return (BOOLEAN_LITERAL);
1192 }
1193 token = match_integer_literal ();
1194 if (token != 0);
1195 {
1196 return (token);
1197 }
1198 return (ILLEGAL_TOKEN);
1199 }
1200
1201 static void
1202 yyerror (msg)
1203 char *msg; /* unused */
1204 {
1205 printf ("Parsing: %s\n", lexptr);
1206 if (yychar < 256)
1207 {
1208 error ("Invalid syntax in expression near character '%c'.", yychar);
1209 }
1210 else
1211 {
1212 error ("Invalid syntax in expression");
1213 }
1214 }
1215
1216 \f
1217 static void
1218 chill_printchar (c, stream)
1219 register int c;
1220 FILE *stream;
1221 {
1222 c &= 0xFF; /* Avoid sign bit follies */
1223
1224 if (PRINT_LITERAL_FORM (c))
1225 {
1226 fprintf_filtered (stream, "'%c'", c);
1227 }
1228 else
1229 {
1230 fprintf_filtered (stream, "C'%.2x'", (unsigned int) c);
1231 }
1232 }
1233
1234 /* Print the character string STRING, printing at most LENGTH characters.
1235 Printing stops early if the number hits print_max; repeat counts
1236 are printed as appropriate. Print ellipses at the end if we
1237 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
1238 Note that gdb maintains the length of strings without counting the
1239 terminating null byte, while chill strings are typically written with
1240 an explicit null byte. So we always assume an implied null byte
1241 until gdb is able to maintain non-null terminated strings as well
1242 as null terminated strings (FIXME).
1243 */
1244
1245 static void
1246 chill_printstr (stream, string, length, force_ellipses)
1247 FILE *stream;
1248 char *string;
1249 unsigned int length;
1250 int force_ellipses;
1251 {
1252 register unsigned int i;
1253 unsigned int things_printed = 0;
1254 int in_literal_form = 0;
1255 int in_control_form = 0;
1256 int need_slashslash = 0;
1257 unsigned int c;
1258 extern int repeat_count_threshold;
1259 extern int print_max;
1260
1261 if (length == 0)
1262 {
1263 chill_printchar ('\0', stream);
1264 return;
1265 }
1266
1267 for (i = 0; i < length && things_printed < print_max; ++i)
1268 {
1269 /* Position of the character we are examining
1270 to see whether it is repeated. */
1271 unsigned int rep1;
1272 /* Number of repetitions we have detected so far. */
1273 unsigned int reps;
1274
1275 QUIT;
1276
1277 if (need_slashslash)
1278 {
1279 fputs_filtered ("//", stream);
1280 need_slashslash = 0;
1281 }
1282
1283 rep1 = i + 1;
1284 reps = 1;
1285 while (rep1 < length && string[rep1] == string[i])
1286 {
1287 ++rep1;
1288 ++reps;
1289 }
1290
1291 c = string[i];
1292 if (reps > repeat_count_threshold)
1293 {
1294 if (in_control_form || in_literal_form)
1295 {
1296 fputs_filtered ("'//", stream);
1297 in_control_form = in_literal_form = 0;
1298 }
1299 chill_printchar (c, stream);
1300 fprintf_filtered (stream, "<repeats %u times>", reps);
1301 i = rep1 - 1;
1302 things_printed += repeat_count_threshold;
1303 need_slashslash = 1;
1304 }
1305 else
1306 {
1307 if (PRINT_LITERAL_FORM (c))
1308 {
1309 if (!in_literal_form)
1310 {
1311 if (in_control_form)
1312 {
1313 fputs_filtered ("'//", stream);
1314 in_control_form = 0;
1315 }
1316 fputs_filtered ("'", stream);
1317 in_literal_form = 1;
1318 }
1319 fprintf_filtered (stream, "%c", c);
1320 }
1321 else
1322 {
1323 if (!in_control_form)
1324 {
1325 if (in_literal_form)
1326 {
1327 fputs_filtered ("'//", stream);
1328 in_literal_form = 0;
1329 }
1330 fputs_filtered ("c'", stream);
1331 in_control_form = 1;
1332 }
1333 fprintf_filtered (stream, "%.2x", c);
1334 }
1335 ++things_printed;
1336 }
1337 }
1338
1339 /* Terminate the quotes if necessary. */
1340 if (in_literal_form || in_control_form)
1341 {
1342 fputs_filtered ("'", stream);
1343 }
1344 if (force_ellipses || (i < length))
1345 {
1346 fputs_filtered ("...", stream);
1347 }
1348 }
1349
1350 \f
1351 /* Table of operators and their precedences for printing expressions. */
1352
1353 const static struct op_print chill_op_print_tab[] = {
1354 {"AND", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1355 {"OR", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1356 {"NOT", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1357 {"MOD", BINOP_REM, PREC_MUL, 0},
1358 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1359 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
1360 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1361 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1362 {">=", BINOP_GEQ, PREC_ORDER, 0},
1363 {">", BINOP_GTR, PREC_ORDER, 0},
1364 {"<", BINOP_LESS, PREC_ORDER, 0},
1365 {"+", BINOP_ADD, PREC_ADD, 0},
1366 {"-", BINOP_SUB, PREC_ADD, 0},
1367 {"*", BINOP_MUL, PREC_MUL, 0},
1368 {"/", BINOP_DIV, PREC_MUL, 0},
1369 {"-", UNOP_NEG, PREC_PREFIX, 0},
1370 {NULL, 0, 0, 0}
1371 };
1372
1373 \f
1374 /* The built-in types of Chill. */
1375
1376 struct type *builtin_type_chill_bool;
1377 struct type *builtin_type_chill_char;
1378 struct type *builtin_type_chill_long;
1379 struct type *builtin_type_chill_ulong;
1380 struct type *builtin_type_chill_real;
1381
1382 struct type ** const (chill_builtin_types[]) =
1383 {
1384 &builtin_type_chill_bool,
1385 &builtin_type_chill_char,
1386 &builtin_type_chill_long,
1387 &builtin_type_chill_ulong,
1388 &builtin_type_chill_real,
1389 0
1390 };
1391
1392 const struct language_defn chill_language_defn = {
1393 "chill",
1394 language_chill,
1395 chill_builtin_types,
1396 range_check_on,
1397 type_check_on,
1398 chill_parse, /* parser */
1399 chill_error, /* parser error function */
1400 chill_printchar, /* print a character constant */
1401 chill_printstr, /* function to print a string constant */
1402 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1403 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1404 &builtin_type_chill_real, /* longest floating point type */
1405 {"", "B'", "", ""}, /* Binary format info */
1406 {"O'%o", "O'", "o", ""}, /* Octal format info */
1407 {"D'%d", "D'", "d", ""}, /* Decimal format info */
1408 {"H'%x", "H'", "x", ""}, /* Hex format info */
1409 chill_op_print_tab, /* expression operators for printing */
1410 LANG_MAGIC
1411 };
1412
1413 /* Initialization for Chill */
1414
1415 void
1416 _initialize_chill_exp ()
1417 {
1418 builtin_type_chill_bool =
1419 init_type (TYPE_CODE_BOOL, TARGET_INT_BIT / TARGET_CHAR_BIT,
1420 TYPE_FLAG_UNSIGNED,
1421 "BOOL", (struct objfile *) NULL);
1422 builtin_type_chill_char =
1423 init_type (TYPE_CODE_CHAR, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1424 TYPE_FLAG_UNSIGNED,
1425 "CHAR", (struct objfile *) NULL);
1426 builtin_type_chill_long =
1427 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1428 0,
1429 "LONG", (struct objfile *) NULL);
1430 builtin_type_chill_ulong =
1431 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1432 TYPE_FLAG_UNSIGNED,
1433 "ULONG", (struct objfile *) NULL);
1434 builtin_type_chill_real =
1435 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1436 0,
1437 "LONG_REAL", (struct objfile *) NULL);
1438
1439 add_language (&chill_language_defn);
1440 }
This page took 0.061599 seconds and 4 git commands to generate.