-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathcompile.c
2919 lines (2723 loc) · 61.7 KB
/
compile.c
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 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
/* Compile an expression node to intermediate code */
/* XXX TO DO:
XXX Compute maximum needed stack sizes while compiling;
XXX then frame object can be one malloc and no stack checks are needed
XXX add __doc__ attribute == co_doc to code object attributes
XXX don't execute doc string
XXX Generate simple jump for break/return outside 'try...finally'
XXX get rid of SET_LINENO instructions, use JAR's table trick
XXX (need an option to put them back in, for debugger!)
XXX other JAR tricks?
*/
#include "allobjects.h"
#include "node.h"
#include "token.h"
#include "graminit.h"
#include "compile.h"
#include "opcode.h"
#include "structmember.h"
#include <ctype.h>
#include <errno.h>
#define OP_DELETE 0
#define OP_ASSIGN 1
#define OP_APPLY 2
#define OFF(x) offsetof(codeobject, x)
static struct memberlist code_memberlist[] = {
{"co_argcount", T_INT, OFF(co_argcount), READONLY},
{"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
{"co_flags", T_INT, OFF(co_flags), READONLY},
{"co_code", T_OBJECT, OFF(co_code), READONLY},
{"co_consts", T_OBJECT, OFF(co_consts), READONLY},
{"co_names", T_OBJECT, OFF(co_names), READONLY},
{"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
{"co_filename", T_OBJECT, OFF(co_filename), READONLY},
{"co_name", T_OBJECT, OFF(co_name), READONLY},
{NULL} /* Sentinel */
};
static object *
code_getattr(co, name)
codeobject *co;
char *name;
{
return getmember((char *)co, code_memberlist, name);
}
static void
code_dealloc(co)
codeobject *co;
{
XDECREF(co->co_code);
XDECREF(co->co_consts);
XDECREF(co->co_names);
XDECREF(co->co_filename);
XDECREF(co->co_name);
XDECREF(co->co_varnames);
DEL(co);
}
static object *
code_repr(co)
codeobject *co;
{
char buf[500];
int lineno = -1;
char *p = GETSTRINGVALUE(co->co_code);
char *filename = "???";
char *name = "???";
if (*p == SET_LINENO)
lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
if (co->co_filename && is_stringobject(co->co_filename))
filename = getstringvalue(co->co_filename);
if (co->co_name && is_stringobject(co->co_name))
name = getstringvalue(co->co_name);
sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
name, (long)co, filename, lineno);
return newstringobject(buf);
}
static int
code_compare(co, cp)
codeobject *co, *cp;
{
int cmp;
cmp = cp->co_argcount - cp->co_argcount;
if (cmp) return cmp;
cmp = cp->co_nlocals - cp->co_nlocals;
if (cmp) return cmp;
cmp = cp->co_flags - cp->co_flags;
if (cmp) return cmp;
cmp = cmpobject((object *)co->co_code, (object *)cp->co_code);
if (cmp) return cmp;
cmp = cmpobject(co->co_consts, cp->co_consts);
if (cmp) return cmp;
cmp = cmpobject(co->co_names, cp->co_names);
if (cmp) return cmp;
cmp = cmpobject(co->co_varnames, cp->co_varnames);
return cmp;
}
static long
code_hash(co)
codeobject *co;
{
long h, h1, h2, h3, h4;
h1 = hashobject((object *)co->co_code);
if (h1 == -1) return -1;
h2 = hashobject(co->co_consts);
if (h2 == -1) return -1;
h3 = hashobject(co->co_names);
if (h3 == -1) return -1;
h4 = hashobject(co->co_varnames);
if (h4 == -1) return -1;
h = h1 ^ h2 ^ h3 ^ h4 ^
co->co_argcount ^ co->co_nlocals ^ co->co_flags;
if (h == -1) h = -2;
return h;
}
typeobject Codetype = {
OB_HEAD_INIT(&Typetype)
0,
"code",
sizeof(codeobject),
0,
(destructor)code_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)code_getattr, /*tp_getattr*/
0, /*tp_setattr*/
(cmpfunc)code_compare, /*tp_compare*/
(reprfunc)code_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)code_hash, /*tp_hash*/
};
codeobject *
newcodeobject(argcount, nlocals, flags,
code, consts, names, varnames, filename, name)
int argcount;
int nlocals;
int flags;
object *code;
object *consts;
object *names;
object *varnames;
object *filename;
object *name;
{
codeobject *co;
int i;
/* Check argument types */
if (argcount < 0 || nlocals < 0 ||
code == NULL || !is_stringobject(code) ||
consts == NULL || !is_tupleobject(consts) ||
names == NULL || !is_tupleobject(names) ||
varnames == NULL || !is_tupleobject(varnames) ||
name == NULL || !is_stringobject(name) ||
filename == NULL || !is_stringobject(filename)) {
err_badcall();
return NULL;
}
/* Make sure names and varnames are all strings */
for (i = gettuplesize(names); --i >= 0; ) {
object *v = gettupleitem(names, i);
if (v == NULL || !is_stringobject(v)) {
err_badcall();
return NULL;
}
}
for (i = gettuplesize(varnames); --i >= 0; ) {
object *v = gettupleitem(varnames, i);
if (v == NULL || !is_stringobject(v)) {
err_badcall();
return NULL;
}
}
co = NEWOBJ(codeobject, &Codetype);
if (co != NULL) {
co->co_argcount = argcount;
co->co_nlocals = nlocals;
co->co_flags = flags;
INCREF(code);
co->co_code = (stringobject *)code;
INCREF(consts);
co->co_consts = consts;
INCREF(names);
co->co_names = names;
INCREF(varnames);
co->co_varnames = varnames;
INCREF(filename);
co->co_filename = filename;
INCREF(name);
co->co_name = name;
}
return co;
}
/* Data structure used internally */
#define MAXBLOCKS 20 /* Max static block nesting within a function */
struct compiling {
object *c_code; /* string */
object *c_consts; /* list of objects */
object *c_names; /* list of strings (names) */
object *c_globals; /* dictionary (value=None) */
object *c_locals; /* dictionary (value=localID) */
object *c_varnames; /* list (inverse of c_locals) */
int c_nlocals; /* index of next local */
int c_argcount; /* number of top-level arguments */
int c_flags; /* same as co_flags */
int c_nexti; /* index into c_code */
int c_errors; /* counts errors occurred */
int c_infunction; /* set when compiling a function */
int c_interactive; /* generating code for interactive command */
int c_loops; /* counts nested loops */
int c_begin; /* begin of current loop, for 'continue' */
int c_block[MAXBLOCKS]; /* stack of block types */
int c_nblocks; /* current block stack level */
char *c_filename; /* filename of current node */
char *c_name; /* name of object (e.g. function) */
};
/* Interface to the block stack */
static void
block_push(c, type)
struct compiling *c;
int type;
{
if (c->c_nblocks >= MAXBLOCKS) {
err_setstr(SystemError, "too many statically nested blocks");
c->c_errors++;
}
else {
c->c_block[c->c_nblocks++] = type;
}
}
static void
block_pop(c, type)
struct compiling *c;
int type;
{
if (c->c_nblocks > 0)
c->c_nblocks--;
if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
err_setstr(SystemError, "bad block pop");
c->c_errors++;
}
}
/* Prototype forward declarations */
static int com_init PROTO((struct compiling *, char *));
static void com_free PROTO((struct compiling *));
static void com_done PROTO((struct compiling *));
static void com_node PROTO((struct compiling *, struct _node *));
static void com_factor PROTO((struct compiling *, struct _node *));
static void com_addbyte PROTO((struct compiling *, int));
static void com_addint PROTO((struct compiling *, int));
static void com_addoparg PROTO((struct compiling *, int, int));
static void com_addfwref PROTO((struct compiling *, int, int *));
static void com_backpatch PROTO((struct compiling *, int));
static int com_add PROTO((struct compiling *, object *, object *));
static int com_addconst PROTO((struct compiling *, object *));
static int com_addname PROTO((struct compiling *, object *));
static void com_addopname PROTO((struct compiling *, int, node *));
static void com_list PROTO((struct compiling *, node *, int));
static int com_argdefs PROTO((struct compiling *, node *));
static int com_newlocal PROTO((struct compiling *, char *));
static int
com_init(c, filename)
struct compiling *c;
char *filename;
{
if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL)
goto fail_3;
if ((c->c_consts = newlistobject(0)) == NULL)
goto fail_2;
if ((c->c_names = newlistobject(0)) == NULL)
goto fail_1;
if ((c->c_globals = newdictobject()) == NULL)
goto fail_0;
if ((c->c_locals = newdictobject()) == NULL)
goto fail_00;
if ((c->c_varnames = newlistobject(0)) == NULL)
goto fail_000;
c->c_nlocals = 0;
c->c_argcount = 0;
c->c_flags = 0;
c->c_nexti = 0;
c->c_errors = 0;
c->c_infunction = 0;
c->c_interactive = 0;
c->c_loops = 0;
c->c_begin = 0;
c->c_nblocks = 0;
c->c_filename = filename;
c->c_name = "?";
return 1;
fail_000:
DECREF(c->c_locals);
fail_00:
DECREF(c->c_globals);
fail_0:
DECREF(c->c_names);
fail_1:
DECREF(c->c_consts);
fail_2:
DECREF(c->c_code);
fail_3:
return 0;
}
static void
com_free(c)
struct compiling *c;
{
XDECREF(c->c_code);
XDECREF(c->c_consts);
XDECREF(c->c_names);
XDECREF(c->c_globals);
XDECREF(c->c_locals);
XDECREF(c->c_varnames);
}
static void
com_done(c)
struct compiling *c;
{
if (c->c_code != NULL)
resizestring(&c->c_code, c->c_nexti);
}
static void
com_addbyte(c, byte)
struct compiling *c;
int byte;
{
int len;
/*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
if (byte < 0 || byte > 255) {
/*
fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
fatal("com_addbyte: byte out of range");
*/
err_setstr(SystemError, "com_addbyte: byte out of range");
c->c_errors++;
}
if (c->c_code == NULL)
return;
len = getstringsize(c->c_code);
if (c->c_nexti >= len) {
if (resizestring(&c->c_code, len+1000) != 0) {
c->c_errors++;
return;
}
}
getstringvalue(c->c_code)[c->c_nexti++] = byte;
}
static void
com_addint(c, x)
struct compiling *c;
int x;
{
com_addbyte(c, x & 0xff);
com_addbyte(c, x >> 8); /* XXX x should be positive */
}
static void
com_addoparg(c, op, arg)
struct compiling *c;
int op;
int arg;
{
com_addbyte(c, op);
com_addint(c, arg);
}
static void
com_addfwref(c, op, p_anchor)
struct compiling *c;
int op;
int *p_anchor;
{
/* Compile a forward reference for backpatching */
int here;
int anchor;
com_addbyte(c, op);
here = c->c_nexti;
anchor = *p_anchor;
*p_anchor = here;
com_addint(c, anchor == 0 ? 0 : here - anchor);
}
static void
com_backpatch(c, anchor)
struct compiling *c;
int anchor; /* Must be nonzero */
{
unsigned char *code = (unsigned char *) getstringvalue(c->c_code);
int target = c->c_nexti;
int dist;
int prev;
for (;;) {
/* Make the JUMP instruction at anchor point to target */
prev = code[anchor] + (code[anchor+1] << 8);
dist = target - (anchor+2);
code[anchor] = dist & 0xff;
code[anchor+1] = dist >> 8;
if (!prev)
break;
anchor -= prev;
}
}
/* Handle literals and names uniformly */
static int
com_add(c, list, v)
struct compiling *c;
object *list;
object *v;
{
int n = getlistsize(list);
int i;
for (i = n; --i >= 0; ) {
object *w = getlistitem(list, i);
if (v->ob_type == w->ob_type && cmpobject(v, w) == 0)
return i;
}
if (addlistitem(list, v) != 0)
c->c_errors++;
return n;
}
static int
com_addconst(c, v)
struct compiling *c;
object *v;
{
return com_add(c, c->c_consts, v);
}
static int
com_addname(c, v)
struct compiling *c;
object *v;
{
return com_add(c, c->c_names, v);
}
static void
com_addopnamestr(c, op, name)
struct compiling *c;
int op;
char *name;
{
object *v;
int i;
if (name == NULL || (v = newstringobject(name)) == NULL) {
c->c_errors++;
i = 255;
}
else {
i = com_addname(c, v);
DECREF(v);
}
/* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
switch (op) {
case LOAD_NAME:
case STORE_NAME:
case DELETE_NAME:
if (dictlookup(c->c_globals, name) != NULL) {
switch (op) {
case LOAD_NAME: op = LOAD_GLOBAL; break;
case STORE_NAME: op = STORE_GLOBAL; break;
case DELETE_NAME: op = DELETE_GLOBAL; break;
}
}
}
com_addoparg(c, op, i);
}
static void
com_addopname(c, op, n)
struct compiling *c;
int op;
node *n;
{
char *name;
char buffer[1000];
/* XXX it is possible to write this code without the 1000
chars on the total length of dotted names, I just can't be
bothered right now */
if (TYPE(n) == STAR)
name = "*";
else if (TYPE(n) == dotted_name) {
char *p = buffer;
int i;
name = buffer;
for (i = 0; i < NCH(n); i += 2) {
char *s = STR(CHILD(n, i));
if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
err_setstr(MemoryError,
"dotted_name too long");
name = NULL;
break;
}
if (p != buffer)
*p++ = '.';
strcpy(p, s);
p = strchr(p, '\0');
}
}
else {
REQ(n, NAME);
name = STR(n);
}
com_addopnamestr(c, op, name);
}
static object *
parsenumber(s)
char *s;
{
extern long mystrtol PROTO((const char *, char **, int));
extern unsigned long mystrtoul PROTO((const char *, char **, int));
extern double atof PROTO((const char *));
char *end;
long x;
#ifndef WITHOUT_COMPLEX
Py_complex c;
int imflag;
#endif
errno = 0;
end = s + strlen(s) - 1;
#ifndef WITHOUT_COMPLEX
imflag = *end == 'j' || *end == 'J';
#endif
if (*end == 'l' || *end == 'L')
return long_scan(s, 0);
if (s[0] == '0')
x = (long) mystrtoul(s, &end, 0);
else
x = mystrtol(s, &end, 0);
if (*end == '\0') {
if (errno != 0) {
err_setstr(OverflowError,
"integer literal too large");
return NULL;
}
return newintobject(x);
}
/* XXX Huge floats may silently fail */
#ifndef WITHOUT_COMPLEX
if (imflag) {
c.real = 0.;
c.imag = atof(s);
return newcomplexobject(c);
}
else
#endif
return newfloatobject(atof(s));
}
static object *
parsestr(s)
char *s;
{
object *v;
int len;
char *buf;
char *p;
char *end;
int c;
int quote = *s;
if (quote != '\'' && quote != '\"') {
err_badcall();
return NULL;
}
s++;
len = strlen(s);
if (s[--len] != quote) {
err_badcall();
return NULL;
}
if (len >= 4 && s[0] == quote && s[1] == quote) {
s += 2;
len -= 2;
if (s[--len] != quote || s[--len] != quote) {
err_badcall();
return NULL;
}
}
if (strchr(s, '\\') == NULL)
return newsizedstringobject(s, len);
v = newsizedstringobject((char *)NULL, len);
p = buf = getstringvalue(v);
end = s + len;
while (s < end) {
if (*s != '\\') {
*p++ = *s++;
continue;
}
s++;
switch (*s++) {
/* XXX This assumes ASCII! */
case '\n': break;
case '\\': *p++ = '\\'; break;
case '\'': *p++ = '\''; break;
case '\"': *p++ = '\"'; break;
case 'b': *p++ = '\b'; break;
case 'f': *p++ = '\014'; break; /* FF */
case 't': *p++ = '\t'; break;
case 'n': *p++ = '\n'; break;
case 'r': *p++ = '\r'; break;
case 'v': *p++ = '\013'; break; /* VT */
case 'a': *p++ = '\007'; break; /* BEL, not classic C */
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
c = s[-1] - '0';
if ('0' <= *s && *s <= '7') {
c = (c<<3) + *s++ - '0';
if ('0' <= *s && *s <= '7')
c = (c<<3) + *s++ - '0';
}
*p++ = c;
break;
case 'x':
if (isxdigit(Py_CHARMASK(*s))) {
sscanf(s, "%x", &c);
*p++ = c;
do {
s++;
} while (isxdigit(Py_CHARMASK(*s)));
break;
}
/* FALLTHROUGH */
default: *p++ = '\\'; *p++ = s[-1]; break;
}
}
resizestring(&v, (int)(p - buf));
return v;
}
static object *
parsestrplus(n)
node *n;
{
object *v;
int i;
REQ(CHILD(n, 0), STRING);
if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
/* String literal concatenation */
for (i = 1; i < NCH(n) && v != NULL; i++) {
joinstring_decref(&v, parsestr(STR(CHILD(n, i))));
}
}
return v;
}
static void
com_list_constructor(c, n)
struct compiling *c;
node *n;
{
int len;
int i;
if (TYPE(n) != testlist)
REQ(n, exprlist);
/* exprlist: expr (',' expr)* [',']; likewise for testlist */
len = (NCH(n) + 1) / 2;
for (i = 0; i < NCH(n); i += 2)
com_node(c, CHILD(n, i));
com_addoparg(c, BUILD_LIST, len);
}
static void
com_dictmaker(c, n)
struct compiling *c;
node *n;
{
int i;
/* dictmaker: test ':' test (',' test ':' value)* [','] */
for (i = 0; i+2 < NCH(n); i += 4) {
/* We must arrange things just right for STORE_SUBSCR.
It wants the stack to look like (value) (dict) (key) */
com_addbyte(c, DUP_TOP);
com_node(c, CHILD(n, i+2)); /* value */
com_addbyte(c, ROT_TWO);
com_node(c, CHILD(n, i)); /* key */
com_addbyte(c, STORE_SUBSCR);
}
}
static void
com_atom(c, n)
struct compiling *c;
node *n;
{
node *ch;
object *v;
int i;
REQ(n, atom);
ch = CHILD(n, 0);
switch (TYPE(ch)) {
case LPAR:
if (TYPE(CHILD(n, 1)) == RPAR)
com_addoparg(c, BUILD_TUPLE, 0);
else
com_node(c, CHILD(n, 1));
break;
case LSQB:
if (TYPE(CHILD(n, 1)) == RSQB)
com_addoparg(c, BUILD_LIST, 0);
else
com_list_constructor(c, CHILD(n, 1));
break;
case LBRACE: /* '{' [dictmaker] '}' */
com_addoparg(c, BUILD_MAP, 0);
if (TYPE(CHILD(n, 1)) != RBRACE)
com_dictmaker(c, CHILD(n, 1));
break;
case BACKQUOTE:
com_node(c, CHILD(n, 1));
com_addbyte(c, UNARY_CONVERT);
break;
case NUMBER:
if ((v = parsenumber(STR(ch))) == NULL) {
c->c_errors++;
i = 255;
}
else {
i = com_addconst(c, v);
DECREF(v);
}
com_addoparg(c, LOAD_CONST, i);
break;
case STRING:
v = parsestrplus(n);
if (v == NULL) {
c->c_errors++;
i = 255;
}
else {
i = com_addconst(c, v);
DECREF(v);
}
com_addoparg(c, LOAD_CONST, i);
break;
case NAME:
com_addopname(c, LOAD_NAME, ch);
break;
default:
fprintf(stderr, "node type %d\n", TYPE(ch));
err_setstr(SystemError, "com_atom: unexpected node type");
c->c_errors++;
}
}
static void
com_slice(c, n, op)
struct compiling *c;
node *n;
int op;
{
if (NCH(n) == 1) {
com_addbyte(c, op);
}
else if (NCH(n) == 2) {
if (TYPE(CHILD(n, 0)) != COLON) {
com_node(c, CHILD(n, 0));
com_addbyte(c, op+1);
}
else {
com_node(c, CHILD(n, 1));
com_addbyte(c, op+2);
}
}
else {
com_node(c, CHILD(n, 0));
com_node(c, CHILD(n, 2));
com_addbyte(c, op+3);
}
}
static int
com_argument(c, n, inkeywords)
struct compiling *c;
node *n; /* argument */
int inkeywords;
{
node *m;
REQ(n, argument); /* [test '='] test; really [ keyword '='] keyword */
if (NCH(n) == 1) {
if (inkeywords) {
err_setstr(SyntaxError,
"non-keyword arg after keyword arg");
c->c_errors++;
}
else {
com_node(c, CHILD(n, 0));
}
return 0;
}
m = n;
do {
m = CHILD(m, 0);
} while (NCH(m) == 1);
if (TYPE(m) != NAME) {
err_setstr(SyntaxError, "keyword can't be an expression");
c->c_errors++;
}
else {
object *v = newstringobject(STR(m));
if (v == NULL)
c->c_errors++;
else {
com_addoparg(c, LOAD_CONST, com_addconst(c, v));
DECREF(v);
}
}
com_node(c, CHILD(n, 2));
return 1;
}
static void
com_call_function(c, n)
struct compiling *c;
node *n; /* EITHER arglist OR ')' */
{
if (TYPE(n) == RPAR) {
com_addoparg(c, CALL_FUNCTION, 0);
}
else {
int inkeywords, i, na, nk;
REQ(n, arglist);
inkeywords = 0;
na = 0;
nk = 0;
for (i = 0; i < NCH(n); i += 2) {
inkeywords = com_argument(c, CHILD(n, i), inkeywords);
if (!inkeywords)
na++;
else
nk++;
}
if (na > 255 || nk > 255) {
err_setstr(SyntaxError, "more than 255 arguments");
c->c_errors++;
}
com_addoparg(c, CALL_FUNCTION, na | (nk << 8));
}
}
static void
com_select_member(c, n)
struct compiling *c;
node *n;
{
com_addopname(c, LOAD_ATTR, n);
}
static void
com_sliceobj(c, n)
struct compiling *c;
node *n;
{
int i=0;
int ns=2; /* number of slice arguments */
int first_missing=0;
node *ch;
/* first argument */
if (TYPE(CHILD(n,i)) == COLON) {
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
i++;
}
else {
com_node(c, CHILD(n,i));
i++;
REQ(CHILD(n,i),COLON);
i++;
}
/* second argument */
if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
com_node(c, CHILD(n,i));
i++;
}
else com_addoparg(c, LOAD_CONST, com_addconst(c, None));
/* remaining arguments */
for (; i < NCH(n); i++) {
ns++;
ch=CHILD(n,i);
REQ(ch, sliceop);
if (NCH(ch) == 1) {
/* right argument of ':' missing */
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
}
else
com_node(c, CHILD(ch,1));
}
com_addoparg(c, BUILD_SLICE, ns);
}
static void
com_subscript(c, n)
struct compiling *c;
node *n;
{
node *ch;
REQ(n, subscript);
ch = CHILD(n,0);
/* check for rubber index */
if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT)
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipses));
else {
/* check for slice */
if ((TYPE(ch) == COLON || NCH(n) > 1))
com_sliceobj(c, n);
else {
REQ(ch, test);
com_node(c, ch);
}
}
}
static void
com_subscriptlist(c, n, assigning)
struct compiling *c;
node *n;
int assigning;
{
int i, op;
REQ(n, subscriptlist);
/* Check to make backward compatible slice behavior for '[i:j]' */
if (NCH(n) == 1) {
node *sub = CHILD(n, 0); /* subscript */
/* Make it is a simple slice.
should have exactly one colon. */
if ((TYPE(CHILD(sub, 0)) == COLON
|| (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
&& (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) {
if (assigning == OP_APPLY)
op = SLICE;
else
op = ((assigning == OP_ASSIGN) ? STORE_SLICE : DELETE_SLICE);
com_slice(c, sub, op);
return;
}
}
/* Else normal subscriptlist. Compile each subscript. */
for (i = 0; i < NCH(n); i += 2)
com_subscript(c, CHILD(n, i));
/* Put multiple subscripts into a tuple */
if (NCH(n) > 1)
com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2);
if (assigning == OP_APPLY)
op = BINARY_SUBSCR;