-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
1329 lines (1202 loc) · 29.4 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%{
/******************************************************************************
Copyright (c) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
Pavel@Xerox.Com
*****************************************************************************/
/*************************************************************************/
/* NOTE: If you add an #include here, make sure you properly update the */
/* parser.o dependency line in the Makefile. */
/*************************************************************************/
#include "my-ctype.h"
#include "my-math.h"
#include "my-stdlib.h"
#include "my-string.h"
#include "ast.h"
#include "code_gen.h"
#include "config.h"
#include "functions.h"
#include "keywords.h"
#include "list.h"
#include "log.h"
#include "numbers.h"
#include "opcode.h"
#include "parser.h"
#include "program.h"
#include "storage.h"
#include "streams.h"
#include "structures.h"
#include "sym_table.h"
#include "utils.h"
#include "version.h"
static Stmt *prog_start;
static int dollars_ok;
static DB_Version language_version;
static void error(const char *, const char *);
static void warning(const char *, const char *);
static int find_id(char *name);
static void yyerror(const char *s);
static int yylex(void);
static Scatter *scatter_from_arglist(Arg_List *);
static Scatter *add_scatter_item(Scatter *, Scatter *);
static void vet_scatter(Scatter *);
static void push_loop_name(const char *);
static void pop_loop_name(void);
static void suspend_loop_scope(void);
static void resume_loop_scope(void);
enum loop_exit_kind { LOOP_BREAK, LOOP_CONTINUE };
static void check_loop_name(const char *, enum loop_exit_kind);
%}
%union {
Stmt *stmt;
Expr *expr;
int integer;
Objid object;
double *real;
char *string;
enum error error;
Arg_List *args;
Cond_Arm *arm;
Except_Arm *except;
Scatter *scatter;
}
%type <stmt> statements statement elsepart
%type <arm> elseifs
%type <expr> expr default
%type <args> arglist ne_arglist codes
%type <except> except excepts
%type <string> opt_id
%type <scatter> scatter scatter_item
%token <integer> tINTEGER
%token <object> tOBJECT
%token <real> tFLOAT
%token <string> tSTRING tID
%token <error> tERROR
%token tIF tELSE tELSEIF tENDIF tFOR tIN tENDFOR tRETURN tFORK tENDFORK
%token tWHILE tENDWHILE tTRY tENDTRY tEXCEPT tFINALLY tANY tBREAK tCONTINUE
%token tTO tARROW
%right '='
%nonassoc '?' '|'
%left tOR tAND
%left tEQ tNE '<' tLE '>' tGE tIN
%left '+' '-'
%left '*' '/' '%'
%right '^'
%left '!' tUNARYMINUS
%nonassoc '.' ':' '[' '$'
%%
program: statements
{ prog_start = $1; }
;
statements:
/* NOTHING */
{ $$ = 0; }
| statements statement
{
if ($1) {
Stmt *tmp = $1;
while (tmp->next)
tmp = tmp->next;
tmp->next = $2;
$$ = $1;
} else
$$ = $2;
}
;
statement:
tIF '(' expr ')' statements elseifs elsepart tENDIF
{
$$ = alloc_stmt(STMT_COND);
$$->s.cond.arms = alloc_cond_arm($3, $5);
$$->s.cond.arms->next = $6;
$$->s.cond.otherwise = $7;
}
| tFOR tID tIN '(' expr ')'
{
push_loop_name($2);
}
statements tENDFOR
{
$$ = alloc_stmt(STMT_LIST);
$$->s.list.id = find_id($2);
$$->s.list.expr = $5;
$$->s.list.body = $8;
pop_loop_name();
}
| tFOR tID tIN '[' expr tTO expr ']'
{
push_loop_name($2);
}
statements tENDFOR
{
$$ = alloc_stmt(STMT_RANGE);
$$->s.range.id = find_id($2);
$$->s.range.from = $5;
$$->s.range.to = $7;
$$->s.range.body = $10;
pop_loop_name();
}
| tWHILE '(' expr ')'
{
push_loop_name(0);
}
statements tENDWHILE
{
$$ = alloc_stmt(STMT_WHILE);
$$->s.loop.id = -1;
$$->s.loop.condition = $3;
$$->s.loop.body = $6;
pop_loop_name();
}
| tWHILE tID '(' expr ')'
{
push_loop_name($2);
}
statements tENDWHILE
{
$$ = alloc_stmt(STMT_WHILE);
$$->s.loop.id = find_id($2);
$$->s.loop.condition = $4;
$$->s.loop.body = $7;
pop_loop_name();
}
| tFORK '(' expr ')'
{
suspend_loop_scope();
}
statements tENDFORK
{
$$ = alloc_stmt(STMT_FORK);
$$->s.fork.id = -1;
$$->s.fork.time = $3;
$$->s.fork.body = $6;
resume_loop_scope();
}
| tFORK tID '(' expr ')'
{
suspend_loop_scope();
}
statements tENDFORK
{
$$ = alloc_stmt(STMT_FORK);
$$->s.fork.id = find_id($2);
$$->s.fork.time = $4;
$$->s.fork.body = $7;
resume_loop_scope();
}
| expr ';'
{
$$ = alloc_stmt(STMT_EXPR);
$$->s.expr = $1;
}
| tBREAK ';'
{
check_loop_name(0, LOOP_BREAK);
$$ = alloc_stmt(STMT_BREAK);
$$->s.exit = -1;
}
| tBREAK tID ';'
{
check_loop_name($2, LOOP_BREAK);
$$ = alloc_stmt(STMT_BREAK);
$$->s.exit = find_id($2);
}
| tCONTINUE ';'
{
check_loop_name(0, LOOP_CONTINUE);
$$ = alloc_stmt(STMT_CONTINUE);
$$->s.exit = -1;
}
| tCONTINUE tID ';'
{
check_loop_name($2, LOOP_CONTINUE);
$$ = alloc_stmt(STMT_CONTINUE);
$$->s.exit = find_id($2);
}
| tRETURN expr ';'
{
$$ = alloc_stmt(STMT_RETURN);
$$->s.expr = $2;
}
| tRETURN ';'
{
$$ = alloc_stmt(STMT_RETURN);
$$->s.expr = 0;
}
| ';'
{ $$ = 0; }
| tTRY statements excepts tENDTRY
{
$$ = alloc_stmt(STMT_TRY_EXCEPT);
$$->s.catch.body = $2;
$$->s.catch.excepts = $3;
}
| tTRY statements tFINALLY statements tENDTRY
{
$$ = alloc_stmt(STMT_TRY_FINALLY);
$$->s.finally.body = $2;
$$->s.finally.handler = $4;
}
;
elseifs:
/* NOTHING */
{ $$ = 0; }
| elseifs tELSEIF '(' expr ')' statements
{
Cond_Arm *this_arm = alloc_cond_arm($4, $6);
if ($1) {
Cond_Arm *tmp = $1;
while (tmp->next)
tmp = tmp->next;
tmp->next = this_arm;
$$ = $1;
} else
$$ = this_arm;
}
;
elsepart:
/* NOTHING */
{ $$ = 0; }
| tELSE statements
{ $$ = $2; }
;
excepts:
tEXCEPT except
{ $$ = $2; }
| excepts tEXCEPT
{
Except_Arm *tmp = $1;
int count = 1;
while (tmp->next) {
tmp = tmp->next;
count++;
}
if (!tmp->codes)
yyerror("Unreachable EXCEPT clause");
else if (count > 255)
yyerror("Too many EXCEPT clauses (max. 255)");
}
except
{
Except_Arm *tmp = $1;
while (tmp->next)
tmp = tmp->next;
tmp->next = $4;
$$ = $1;
}
except:
opt_id '(' codes ')' statements
{ $$ = alloc_except($1 ? find_id($1) : -1, $3, $5); }
opt_id:
/* NOTHING */
{ $$ = 0; }
| tID
{ $$ = $1; }
expr:
tINTEGER
{
$$ = alloc_var(TYPE_INT);
$$->e.var.v.num = $1;
}
| tFLOAT
{
$$ = alloc_var(TYPE_FLOAT);
$$->e.var.v.fnum = $1;
}
| tSTRING
{
$$ = alloc_var(TYPE_STR);
$$->e.var.v.str = $1;
}
| tOBJECT
{
$$ = alloc_var(TYPE_OBJ);
$$->e.var.v.obj = $1;
}
| tERROR
{
$$ = alloc_var(TYPE_ERR);
$$->e.var.v.err = $1;
}
| tID
{
$$ = alloc_expr(EXPR_ID);
$$->e.id = find_id($1);
}
| '$' tID
{
/* Treat $foo like #0.("foo") */
Expr *obj = alloc_var(TYPE_OBJ);
Expr *prop = alloc_var(TYPE_STR);
obj->e.var.v.obj = 0;
prop->e.var.v.str = $2;
$$ = alloc_binary(EXPR_PROP, obj, prop);
}
| expr '.' tID
{
/* Treat foo.bar like foo.("bar") for simplicity */
Expr *prop = alloc_var(TYPE_STR);
prop->e.var.v.str = $3;
$$ = alloc_binary(EXPR_PROP, $1, prop);
}
| expr '.' '(' expr ')'
{
$$ = alloc_binary(EXPR_PROP, $1, $4);
}
| expr ':' tID '(' arglist ')'
{
/* treat foo:bar(args) like foo:("bar")(args) */
Expr *verb = alloc_var(TYPE_STR);
verb->e.var.v.str = $3;
$$ = alloc_verb($1, verb, $5);
}
| '$' tID '(' arglist ')'
{
/* treat $bar(args) like #0:("bar")(args) */
Expr *obj = alloc_var(TYPE_OBJ);
Expr *verb = alloc_var(TYPE_STR);
obj->e.var.v.obj = 0;
verb->e.var.v.str = $2;
$$ = alloc_verb(obj, verb, $4);
}
| expr ':' '(' expr ')' '(' arglist ')'
{
$$ = alloc_verb($1, $4, $7);
}
| expr '[' dollars_up expr ']'
{
dollars_ok--;
$$ = alloc_binary(EXPR_INDEX, $1, $4);
}
| expr '[' dollars_up expr tTO expr ']'
{
dollars_ok--;
$$ = alloc_expr(EXPR_RANGE);
$$->e.range.base = $1;
$$->e.range.from = $4;
$$->e.range.to = $6;
}
| '$'
{
if (!dollars_ok)
yyerror("Illegal context for `$' expression.");
$$ = alloc_expr(EXPR_LENGTH);
}
| expr '=' expr
{
Expr *e = $1;
if (e->kind == EXPR_LIST) {
e->kind = EXPR_SCATTER;
if (e->e.list) {
e->e.scatter = scatter_from_arglist(e->e.list);
vet_scatter(e->e.scatter);
} else
yyerror("Empty list in scattering assignment.");
} else {
if (e->kind == EXPR_RANGE)
e = e->e.range.base;
while (e->kind == EXPR_INDEX)
e = e->e.bin.lhs;
if (e->kind != EXPR_ID && e->kind != EXPR_PROP)
yyerror("Illegal expression on left side of"
" assignment.");
}
$$ = alloc_binary(EXPR_ASGN, $1, $3);
}
| '{' scatter '}' '=' expr
{
Expr *e = alloc_expr(EXPR_SCATTER);
e->e.scatter = $2;
vet_scatter($2);
$$ = alloc_binary(EXPR_ASGN, e, $5);
}
| tID '(' arglist ')'
{
unsigned f_no;
$$ = alloc_expr(EXPR_CALL);
if ((f_no = number_func_by_name($1)) == FUNC_NOT_FOUND) {
/* Replace with call_function("$1", @args) */
Expr *fname = alloc_var(TYPE_STR);
Arg_List *a = alloc_arg_list(ARG_NORMAL, fname);
fname->e.var.v.str = $1;
a->next = $3;
warning("Unknown built-in function: ", $1);
$$->e.call.func = number_func_by_name("call_function");
$$->e.call.args = a;
} else {
$$->e.call.func = f_no;
$$->e.call.args = $3;
dealloc_string($1);
}
}
| expr '+' expr
{
$$ = alloc_binary(EXPR_PLUS, $1, $3);
}
| expr '-' expr
{
$$ = alloc_binary(EXPR_MINUS, $1, $3);
}
| expr '*' expr
{
$$ = alloc_binary(EXPR_TIMES, $1, $3);
}
| expr '/' expr
{
$$ = alloc_binary(EXPR_DIVIDE, $1, $3);
}
| expr '%' expr
{
$$ = alloc_binary(EXPR_MOD, $1, $3);
}
| expr '^' expr
{
$$ = alloc_binary(EXPR_EXP, $1, $3);
}
| expr tAND expr
{
$$ = alloc_binary(EXPR_AND, $1, $3);
}
| expr tOR expr
{
$$ = alloc_binary(EXPR_OR, $1, $3);
}
| expr tEQ expr
{
$$ = alloc_binary(EXPR_EQ, $1, $3);
}
| expr tNE expr
{
$$ = alloc_binary(EXPR_NE, $1, $3);
}
| expr '<' expr
{
$$ = alloc_binary(EXPR_LT, $1, $3);
}
| expr tLE expr
{
$$ = alloc_binary(EXPR_LE, $1, $3);
}
| expr '>' expr
{
$$ = alloc_binary(EXPR_GT, $1, $3);
}
| expr tGE expr
{
$$ = alloc_binary(EXPR_GE, $1, $3);
}
| expr tIN expr
{
$$ = alloc_binary(EXPR_IN, $1, $3);
}
| '-' expr %prec tUNARYMINUS
{
if ($2->kind == EXPR_VAR
&& ($2->e.var.type == TYPE_INT
|| $2->e.var.type == TYPE_FLOAT)) {
switch ($2->e.var.type) {
case TYPE_INT:
$2->e.var.v.num = -$2->e.var.v.num;
break;
case TYPE_FLOAT:
*($2->e.var.v.fnum) = - (*($2->e.var.v.fnum));
break;
default:
break;
}
$$ = $2;
} else {
$$ = alloc_expr(EXPR_NEGATE);
$$->e.expr = $2;
}
}
| '!' expr
{
$$ = alloc_expr(EXPR_NOT);
$$->e.expr = $2;
}
| '(' expr ')'
{ $$ = $2; }
| '{' arglist '}'
{
$$ = alloc_expr(EXPR_LIST);
$$->e.list = $2;
}
| expr '?' expr '|' expr
{
$$ = alloc_expr(EXPR_COND);
$$->e.cond.condition = $1;
$$->e.cond.consequent = $3;
$$->e.cond.alternate = $5;
}
| '`' expr '!' codes default '\''
{
$$ = alloc_expr(EXPR_CATCH);
$$->e.catch.try = $2;
$$->e.catch.codes = $4;
$$->e.catch.except = $5;
}
;
dollars_up:
/* NOTHING */
{ dollars_ok++; }
codes:
tANY
{ $$ = 0; }
| ne_arglist
{ $$ = $1; }
default:
/* NOTHING */
{ $$ = 0; }
| tARROW expr
{ $$ = $2; }
arglist:
/* NOTHING */
{ $$ = 0; }
| ne_arglist
{ $$ = $1; }
;
ne_arglist:
expr
{ $$ = alloc_arg_list(ARG_NORMAL, $1); }
| '@' expr
{ $$ = alloc_arg_list(ARG_SPLICE, $2); }
| ne_arglist ',' expr
{
Arg_List *this_arg = alloc_arg_list(ARG_NORMAL, $3);
if ($1) {
Arg_List *tmp = $1;
while (tmp->next)
tmp = tmp->next;
tmp->next = this_arg;
$$ = $1;
} else
$$ = this_arg;
}
| ne_arglist ',' '@' expr
{
Arg_List *this_arg = alloc_arg_list(ARG_SPLICE, $4);
if ($1) {
Arg_List *tmp = $1;
while (tmp->next)
tmp = tmp->next;
tmp->next = this_arg;
$$ = $1;
} else
$$ = this_arg;
}
;
scatter:
ne_arglist ',' scatter_item
{
Scatter *sc = scatter_from_arglist($1);
if (sc)
$$ = add_scatter_item(sc, $3);
else
$$ = $3;
}
| scatter ',' scatter_item
{
$$ = add_scatter_item($1, $3);
}
| scatter ',' tID
{
$$ = add_scatter_item($1, alloc_scatter(SCAT_REQUIRED,
find_id($3), 0));
}
| scatter ',' '@' tID
{
$$ = add_scatter_item($1, alloc_scatter(SCAT_REST,
find_id($4), 0));
}
| scatter_item
{ $$ = $1; }
;
scatter_item:
'?' tID
{
$$ = alloc_scatter(SCAT_OPTIONAL, find_id($2), 0);
}
| '?' tID '=' expr
{
$$ = alloc_scatter(SCAT_OPTIONAL, find_id($2), $4);
}
;
%%
static int lineno, nerrors, must_rename_keywords;
static Parser_Client client;
static void *client_data;
static Names *local_names;
static int
find_id(char *name)
{
int slot = find_or_add_name(&local_names, name);
dealloc_string(name);
return slot;
}
static void
yyerror(const char *s)
{
error(s, 0);
}
static const char *
fmt_error(const char *s, const char *t)
{
static Stream *str = 0;
if (str == 0)
str = new_stream(100);
if (t)
stream_printf(str, "Line %d: %s%s", lineno, s, t);
else
stream_printf(str, "Line %d: %s", lineno, s);
return reset_stream(str);
}
static void
error(const char *s, const char *t)
{
nerrors++;
(*(client.error))(client_data, fmt_error(s, t));
}
static void
warning(const char *s, const char *t)
{
if (client.warning)
(*(client.warning))(client_data, fmt_error(s, t));
else
error(s, t);
}
static int unget_buffer[5], unget_count;
static int
lex_getc(void)
{
if (unget_count > 0)
return unget_buffer[--unget_count];
else
return (*(client.getch))(client_data);
}
static void
lex_ungetc(int c)
{
unget_buffer[unget_count++] = c;
}
static int
follow(int expect, int ifyes, int ifno) /* look ahead for >=, etc. */
{
int c = lex_getc();
if (c == expect)
return ifyes;
lex_ungetc(c);
return ifno;
}
static Stream *token_stream = 0;
static int
yylex(void)
{
int c;
reset_stream(token_stream);
start_over:
do {
c = lex_getc();
if (c == '\n') lineno++;
} while (isspace(c));
if (c == '/') {
c = lex_getc();
if (c == '*') {
for (;;) {
c = lex_getc();
if (c == '*') {
c = lex_getc();
if (c == '/')
goto start_over;
}
if (c == EOF) {
yyerror("End of program while in a comment");
return c;
}
}
} else {
lex_ungetc(c);
return '/';
}
}
if (c == '#') {
int negative = 0;
Objid oid = 0;
c = lex_getc();
if (c == '-') {
negative = 1;
c = lex_getc();
}
if (!isdigit(c)) {
yyerror("Malformed object number");
lex_ungetc(c);
return 0;
}
do {
oid = oid * 10 + (c - '0');
c = lex_getc();
} while (isdigit(c));
lex_ungetc(c);
yylval.object = negative ? -oid : oid;
return tOBJECT;
}
if (isdigit(c) || (c == '.' && language_version >= DBV_Float)) {
int n = 0;
int type = tINTEGER;
while (isdigit(c)) {
n = n * 10 + (c - '0');
stream_add_char(token_stream, c);
c = lex_getc();
}
if (language_version >= DBV_Float && c == '.') {
/* maybe floating-point (but maybe `..') */
int cc;
lex_ungetc(cc = lex_getc()); /* peek ahead */
if (isdigit(cc)) { /* definitely floating-point */
type = tFLOAT;
do {
stream_add_char(token_stream, c);
c = lex_getc();
} while (isdigit(c));
} else if (stream_length(token_stream) == 0)
/* no digits before or after `.'; not a number at all */
goto normal_dot;
else if (cc != '.') {
/* Some digits before dot, not `..' */
type = tFLOAT;
stream_add_char(token_stream, c);
c = lex_getc();
}
}
if (language_version >= DBV_Float && (c == 'e' || c == 'E')) {
/* better be an exponent */
type = tFLOAT;
stream_add_char(token_stream, c);
c = lex_getc();
if (c == '+' || c == '-') {
stream_add_char(token_stream, c);
c = lex_getc();
}
if (!isdigit(c)) {
yyerror("Malformed floating-point literal");
lex_ungetc(c);
return 0;
}
do {
stream_add_char(token_stream, c);
c = lex_getc();
} while (isdigit(c));
}
lex_ungetc(c);
if (type == tINTEGER)
yylval.integer = n;
else {
double d;
d = strtod(reset_stream(token_stream), 0);
if (!IS_REAL(d)) {
yyerror("Floating-point literal out of range");
d = 0.0;
}
yylval.real = alloc_float(d);
}
return type;
}
if (isalpha(c) || c == '_') {
char *buf;
Keyword *k;
stream_add_char(token_stream, c);
while (isalnum(c = lex_getc()) || c == '_')
stream_add_char(token_stream, c);
lex_ungetc(c);
buf = reset_stream(token_stream);
k = find_keyword(buf);
if (k) {
if (k->version <= language_version) {
int t = k->token;
if (t == tERROR)
yylval.error = k->error;
return t;
} else { /* New keyword being used as an identifier */
if (!must_rename_keywords)
warning("Renaming old use of new keyword: ", buf);
must_rename_keywords = 1;
}
}
yylval.string = alloc_string(buf);
return tID;
}
if (c == '"') {
while(1) {
c = lex_getc();
if (c == '"')
break;
if (c == '\\')
c = lex_getc();
if (c == '\n' || c == EOF) {
yyerror("Missing quote");
break;
}
stream_add_char(token_stream, c);
}
yylval.string = alloc_string(reset_stream(token_stream));
return tSTRING;
}
switch(c) {
case '>': return follow('=', tGE, '>');
case '<': return follow('=', tLE, '<');
case '=': return ((c = follow('=', tEQ, 0))
? c
: follow('>', tARROW, '='));
case '!': return follow('=', tNE, '!');
case '|': return follow('|', tOR, '|');
case '&': return follow('&', tAND, '&');
normal_dot:
case '.': return follow('.', tTO, '.');
default: return c;
}
}
static Scatter *
add_scatter_item(Scatter *first, Scatter *last)
{
Scatter *tmp = first;
while (tmp->next)
tmp = tmp->next;
tmp->next = last;
return first;
}
static Scatter *
scatter_from_arglist(Arg_List *a)
{
Scatter *sc = 0, **scp;
Arg_List *anext;
for (scp = ≻ a; a = anext, scp = &((*scp)->next)) {
if (a->expr->kind == EXPR_ID) {
*scp = alloc_scatter(a->kind == ARG_NORMAL ? SCAT_REQUIRED
: SCAT_REST,
a->expr->e.id, 0);
anext = a->next;
dealloc_node(a->expr);
dealloc_node(a);
} else {
yyerror("Scattering assignment targets must be simple variables.");
return 0;
}
}
return sc;
}
static void
vet_scatter(Scatter *sc)
{
int seen_rest = 0, count = 0;
for (; sc; sc = sc->next) {
if (sc->kind == SCAT_REST) {
if (seen_rest)
yyerror("More than one `@' target in scattering assignment.");
else
seen_rest = 1;
}
count++;