blob: 0074cd03f4e8c1538ed7740d4c5b89f50d954507 [file] [log] [blame]
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001/* Evaluate compiled expression nodes */
2
3#include <stdio.h>
4#include <ctype.h>
5#include "string.h"
6
7#include "PROTO.h"
8#include "object.h"
9#include "objimpl.h"
10#include "intobject.h"
11#include "stringobject.h"
12#include "tupleobject.h"
13#include "listobject.h"
14#include "dictobject.h"
15#include "builtinobject.h"
16#include "methodobject.h"
17#include "moduleobject.h"
18#include "context.h"
19#include "funcobject.h"
20#include "classobject.h"
21#include "token.h"
22#include "graminit.h"
23#include "run.h"
24#include "import.h"
25#include "support.h"
26#include "sysmodule.h"
27#include "compile.h"
28#include "opcode.h"
29
30/* List access macros */
31#ifdef NDEBUG
32#define GETITEM(v, i) GETLISTITEM((listobject *)(v), (i))
33#define GETITEMNAME(v, i) GETSTRINGVALUE((stringobject *)GETITEM((v), (i)))
34#else
35#define GETITEM(v, i) getlistitem((v), (i))
36#define GETITEMNAME(v, i) getstringvalue(getlistitem((v), (i)))
37#endif
38
39typedef struct {
40 int b_type; /* what kind of block this is */
41 int b_handler; /* where to jump to find handler */
42 int b_level; /* value stack level to pop to */
43} block;
44
45typedef struct _frame {
46 OB_HEAD
47 struct _frame *f_back; /* previous frame, or NULL */
48 codeobject *f_code; /* code segment */
49 object *f_locals; /* local symbol table (dictobject) */
50 object *f_globals; /* global symbol table (dictobject) */
51 object **f_valuestack; /* malloc'ed array */
52 block *f_blockstack; /* malloc'ed array */
53 int f_nvalues; /* size of f_valuestack */
54 int f_nblocks; /* size of f_blockstack */
55 int f_ivalue; /* index in f_valuestack */
56 int f_iblock; /* index in f_blockstack */
57 int f_nexti; /* index in f_code (next instruction) */
58} frameobject;
59
60#define is_frameobject(op) ((op)->ob_type == &Frametype)
61
62static void
63frame_dealloc(f)
64 frameobject *f;
65{
66 XDECREF(f->f_back);
67 XDECREF(f->f_code);
68 XDECREF(f->f_locals);
69 XDECREF(f->f_globals);
70 XDEL(f->f_valuestack);
71 XDEL(f->f_blockstack);
72 DEL(f);
73}
74typeobject Frametype = {
75 OB_HEAD_INIT(&Typetype)
76 0,
77 "frame",
78 sizeof(frameobject),
79 0,
80 frame_dealloc, /*tp_dealloc*/
81 0, /*tp_print*/
82 0, /*tp_getattr*/
83 0, /*tp_setattr*/
84 0, /*tp_compare*/
85 0, /*tp_repr*/
86 0, /*tp_as_number*/
87 0, /*tp_as_sequence*/
88 0, /*tp_as_mapping*/
89};
90
91static frameobject * newframeobject PROTO(
92 (frameobject *, codeobject *, object *, object *, int, int));
93
94static frameobject *
95newframeobject(back, code, locals, globals, nvalues, nblocks)
96 frameobject *back;
97 codeobject *code;
98 object *locals;
99 object *globals;
100 int nvalues;
101 int nblocks;
102{
103 frameobject *f;
104 if ((back != NULL && !is_frameobject(back)) ||
105 code == NULL || !is_codeobject(code) ||
106 locals == NULL || !is_dictobject(locals) ||
107 globals == NULL || !is_dictobject(globals) ||
108 nvalues < 0 || nblocks < 0) {
109 err_badcall();
110 return NULL;
111 }
112 f = NEWOBJ(frameobject, &Frametype);
113 if (f != NULL) {
114 if (back)
115 INCREF(back);
116 f->f_back = back;
117 INCREF(code);
118 f->f_code = code;
119 INCREF(locals);
120 f->f_locals = locals;
121 INCREF(globals);
122 f->f_globals = globals;
123 f->f_valuestack = NEW(object *, nvalues+1);
124 f->f_blockstack = NEW(block, nblocks+1);
125 f->f_nvalues = nvalues;
126 f->f_nblocks = nblocks;
127 f->f_ivalue = f->f_iblock = f->f_nexti = 0;
128 if (f->f_valuestack == NULL || f->f_blockstack == NULL) {
129 err_nomem();
130 DECREF(f);
131 f = NULL;
132 }
133 }
134 return f;
135}
136
Guido van Rossume9736fc1990-11-18 17:33:06 +0000137#define GETUSTRINGVALUE(s) ((unsigned char *)GETSTRINGVALUE(s))
138
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000139#define Push(f, v) ((f)->f_valuestack[(f)->f_ivalue++] = (v))
140#define Pop(f) ((f)->f_valuestack[--(f)->f_ivalue])
141#define Top(f) ((f)->f_valuestack[(f)->f_ivalue-1])
142#define Empty(f) ((f)->f_ivalue == 0)
143#define Full(f) ((f)->f_ivalue == (f)->f_nvalues)
Guido van Rossume9736fc1990-11-18 17:33:06 +0000144#define Nextbyte(f) (GETUSTRINGVALUE((f)->f_code->co_code)[(f)->f_nexti++])
145#define Peekbyte(f) (GETUSTRINGVALUE((f)->f_code->co_code)[(f)->f_nexti])
146#define Peekint(f) \
147 (GETUSTRINGVALUE((f)->f_code->co_code)[(f)->f_nexti] + \
148 GETUSTRINGVALUE((f)->f_code->co_code)[(f)->f_nexti+1])
149#define Prevbyte(f) (GETUSTRINGVALUE((f)->f_code->co_code)[(f)->f_nexti-1])
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000150#define Jumpto(f, x) ((f)->f_nexti = (x))
151#define Jumpby(f, x) ((f)->f_nexti += (x))
152#define Getconst(f, i) (GETITEM((f)->f_code->co_consts, (i)))
153#define Getname(f, i) (GETITEMNAME((f)->f_code->co_names, (i)))
154
155/* Corresponding functions, for debugging */
156
157static void
158push(f, v)
159 frameobject *f;
160 object *v;
161{
162 if (Full(f)) {
163 printf("stack overflow\n");
164 abort();
165 }
166 Push(f, v);
167}
168
169static object *
170pop(f)
171 frameobject *f;
172{
173 if (Empty(f)) {
174 printf("stack underflow\n");
175 abort();
176 }
177 return Pop(f);
178}
179
180static object *
181top(f)
182 frameobject *f;
183{
184 if (Empty(f)) {
185 printf("stack underflow\n");
186 abort();
187 }
188 return Top(f);
189}
190
191static int
192nextbyte(f)
193 frameobject *f;
194{
195 stringobject *code = (f)->f_code->co_code;
196 if (f->f_nexti >= getstringsize((object *)code)) {
197 printf("ran off end of instructions\n");
198 abort();
199 }
Guido van Rossume9736fc1990-11-18 17:33:06 +0000200 return GETUSTRINGVALUE(code)[f->f_nexti++];
201}
202
203static int
204nextint(f)
205 frameobject *f;
206{
207 int a, b;
208#ifdef NDEBUG
209 a = Nextbyte(f);
210 b = Nextbyte(f);
211#else
212 a = nextbyte(f);
213 b = nextbyte(f);
214#endif
215 return a + (b << 8);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000216}
217
218/* Tracing versions */
219
220static void
221trace_push(f, v)
222 frameobject *f;
223 object *v;
224{
225 printf("\tpush ");
226 printobject(v, stdout, 0);
227 printf("\n");
228 push(f, v);
229}
230
231static object *
232trace_pop(f)
233 frameobject *f;
234{
235 object *v;
236 v = pop(f);
237 printf("\tpop ");
238 printobject(v, stdout, 0);
239 printf("\n");
240 return v;
241}
242
243static object *
244trace_top(f)
245 frameobject *f;
246{
247 object *v;
248 v = top(f);
249 printf("\ttop ");
250 printobject(v, stdout, 0);
251 printf("\n");
252 return v;
253}
254
255static int
256trace_nextop(f)
257 frameobject *f;
258{
259 int op;
260 int arg;
261
262 printf("%d: ", f->f_nexti);
263 op = nextbyte(f);
264 if (op < HAVE_ARGUMENT)
265 printf("op %3d\n", op);
266 else {
Guido van Rossume9736fc1990-11-18 17:33:06 +0000267 arg = Peekint(f);
268 printf("op %d arg %d\n", op, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000269 }
270 return op;
271}
272
273/* Block management */
274
275static void
276setup_block(f, type, handler)
277 frameobject *f;
278 int type;
279 int handler;
280{
281 block *b;
282 if (f->f_iblock >= f->f_nblocks) {
283 printf("block stack overflow\n");
284 abort();
285 }
286 b = &f->f_blockstack[f->f_iblock++];
287 b->b_type = type;
288 b->b_level = f->f_ivalue;
289 b->b_handler = handler + f->f_nexti;
290}
291
292static block *
293pop_block(f)
294 frameobject *f;
295{
296 block *b;
297 if (f->f_iblock <= 0) {
298 printf("block stack underflow\n");
299 abort();
300 }
301 b = &f->f_blockstack[--f->f_iblock];
302 while (f->f_ivalue > b->b_level) {
303 object *v = Pop(f);
304 XDECREF(v);
305 }
306 return b;
307}
308
309
310/* XXX Mixing "print ...," and direct file I/O on stdin/stdout
311 XXX has some bad consequences. The needspace flag should
312 XXX really be part of the file object. */
313
314static int needspace;
315
316void
317flushline()
318{
319 FILE *fp = sysgetfile("stdout", stdout);
320 if (needspace) {
321 fprintf(fp, "\n");
322 needspace = 0;
323 }
324}
325
326static object *
327checkerror(ctx, v)
328 context *ctx;
329 object *v;
330{
331 if (v == NULL)
332 puterrno(ctx);
333 return v;
334}
335
336static object *
337add(ctx, v, w)
338 context *ctx;
339 object *v, *w;
340{
341 if (v->ob_type->tp_as_number != NULL)
342 v = (*v->ob_type->tp_as_number->nb_add)(v, w);
343 else if (v->ob_type->tp_as_sequence != NULL)
344 v = (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
345 else {
346 type_error(ctx, "+ not supported by operands");
347 return NULL;
348 }
349 return checkerror(ctx, v);
350}
351
352static object *
353sub(ctx, v, w)
354 context *ctx;
355 object *v, *w;
356{
357 if (v->ob_type->tp_as_number != NULL)
358 return checkerror(ctx,
359 (*v->ob_type->tp_as_number->nb_subtract)(v, w));
360 type_error(ctx, "bad operand type(s) for -");
361 return NULL;
362}
363
364static object *
365mul(ctx, v, w)
366 context *ctx;
367 object *v, *w;
368{
369 typeobject *tp;
370 if (is_intobject(v) && w->ob_type->tp_as_sequence != NULL) {
371 /* int*sequence -- swap v and w */
372 object *tmp = v;
373 v = w;
374 w = tmp;
375 }
376 tp = v->ob_type;
377 if (tp->tp_as_number != NULL)
378 return checkerror(ctx, (*tp->tp_as_number->nb_multiply)(v, w));
379 if (tp->tp_as_sequence != NULL) {
380 if (!is_intobject(w)) {
381 type_error(ctx, "can't multiply sequence with non-int");
382 return NULL;
383 }
384 if (tp->tp_as_sequence->sq_repeat == NULL) {
385 type_error(ctx, "sequence does not support *");
386 return NULL;
387 }
388 return checkerror(ctx, (*tp->tp_as_sequence->sq_repeat)
389 (v, (int)getintvalue(w)));
390 }
391 type_error(ctx, "bad operand type(s) for *");
392 return NULL;
393}
394
395static object *
396div(ctx, v, w)
397 context *ctx;
398 object *v, *w;
399{
400 if (v->ob_type->tp_as_number != NULL)
401 return checkerror(ctx,
402 (*v->ob_type->tp_as_number->nb_divide)(v, w));
403 type_error(ctx, "bad operand type(s) for /");
404 return NULL;
405}
406
407static object *
408rem(ctx, v, w)
409 context *ctx;
410 object *v, *w;
411{
412 if (v->ob_type->tp_as_number != NULL)
413 return checkerror(ctx,
414 (*v->ob_type->tp_as_number->nb_remainder)(v, w));
415 type_error(ctx, "bad operand type(s) for %");
416 return NULL;
417}
418
419static object *
420neg(ctx, v)
421 context *ctx;
422 object *v;
423{
424 if (v->ob_type->tp_as_number != NULL)
425 return checkerror(ctx,
426 (*v->ob_type->tp_as_number->nb_negative)(v));
427 type_error(ctx, "bad operand type(s) for unary -");
428 return NULL;
429}
430
431static object *
432pos(ctx, v)
433 context *ctx;
434 object *v;
435{
436 if (v->ob_type->tp_as_number != NULL)
437 return checkerror(ctx,
438 (*v->ob_type->tp_as_number->nb_positive)(v));
439 type_error(ctx, "bad operand type(s) for unary +");
440 return NULL;
441}
442
443static object *
444not(ctx, v)
445 context *ctx;
446 object *v;
447{
448 int outcome = testbool(ctx, v);
449 if (ctx->ctx_exception)
450 return NULL;
451 return checkerror(ctx, newintobject((long) !outcome));
452}
453
454static object *
455call_builtin(ctx, func, args)
456 context *ctx;
457 object *func;
458 object *args;
459{
460 if (is_builtinobject(func)) {
461 function funcptr = getbuiltinfunction(func);
462 return (*funcptr)(ctx, args);
463 }
464 if (is_methodobject(func)) {
465 method meth = getmethod(func);
466 object *self = getself(func);
467 return checkerror(ctx, (*meth)(self, args));
468 }
469 if (is_classobject(func)) {
470 if (args != NULL) {
471 type_error(ctx, "classobject() allows no arguments");
472 return NULL;
473 }
474 return checkerror(ctx, newclassmemberobject(func));
475 }
476 type_error(ctx, "call of non-function");
477 return NULL;
478}
479
480static object *eval_compiled PROTO((context *, codeobject *, object *, int));
481
482/* XXX Eventually, this should not call eval_compiled recursively
483 but create a new frame */
484
485static object *
486call_function(ctx, func, args)
487 context *ctx;
488 object *func;
489 object *args;
490{
491 object *newargs = NULL;
492 object *savelocals, *newlocals, *saveglobals;
493 object *c, *v;
494
495 if (is_classmethodobject(func)) {
496 object *self = classmethodgetself(func);
497 func = classmethodgetfunc(func);
498 if (args == NULL) {
499 args = self;
500 }
501 else {
502 newargs = checkerror(ctx, newtupleobject(2));
503 if (newargs == NULL)
504 return NULL;
505 INCREF(self);
506 INCREF(args);
507 settupleitem(newargs, 0, self);
508 settupleitem(newargs, 1, args);
509 args = newargs;
510 }
511 }
512 else {
513 if (!is_funcobject(func)) {
514 type_error(ctx, "call of non-function");
515 return NULL;
516 }
517 }
518
519 c = checkerror(ctx, getfunccode(func));
520 if (c == NULL) {
521 XDECREF(newargs);
522 return NULL;
523 }
524 if (!is_codeobject(c)) {
525 printf("Bad code\n");
526 abort();
527 }
528 newlocals = checkerror(ctx, newdictobject());
529 if (newlocals == NULL) {
530 XDECREF(newargs);
531 return NULL;
532 }
533
534 savelocals = ctx->ctx_locals;
535 ctx->ctx_locals = newlocals;
536 saveglobals = ctx->ctx_globals;
537 ctx->ctx_globals = getfuncglobals(func);
538
539 v = eval_compiled(ctx, (codeobject *)c, args, 1);
540
541 DECREF(ctx->ctx_locals);
542 ctx->ctx_locals = savelocals;
543 ctx->ctx_globals = saveglobals;
544
545 XDECREF(newargs);
546
547 return v;
548}
549
550static object *
551apply_subscript(ctx, v, w)
552 context *ctx;
553 object *v, *w;
554{
555 typeobject *tp = v->ob_type;
556 if (tp->tp_as_sequence == NULL && tp->tp_as_mapping == NULL) {
557 type_error(ctx, "unsubscriptable object");
558 return NULL;
559 }
560 if (tp->tp_as_sequence != NULL) {
561 int i;
562 if (!is_intobject(w)) {
563 type_error(ctx, "sequence subscript not int");
564 return NULL;
565 }
566 i = getintvalue(w);
567 return checkerror(ctx, (*tp->tp_as_sequence->sq_item)(v, i));
568 }
569 return checkerror(ctx, (*tp->tp_as_mapping->mp_subscript)(v, w));
570}
571
572static object *
573loop_subscript(ctx, v, w)
574 context *ctx;
575 object *v, *w;
576{
577 sequence_methods *sq = v->ob_type->tp_as_sequence;
578 int i, n;
579 if (sq == NULL) {
580 type_error(ctx, "loop over non-sequence");
581 return NULL;
582 }
583 i = getintvalue(w);
584 n = (*sq->sq_length)(v);
585 if (i >= n)
586 return NULL; /* End of loop */
587 return checkerror(ctx, (*sq->sq_item)(v, i));
588}
589
590static int
591slice_index(ctx, v, isize, pi)
592 context *ctx;
593 object *v;
594 int isize;
595 int *pi;
596{
597 if (v != NULL) {
598 if (!is_intobject(v)) {
599 type_error(ctx, "slice index must be int");
600 return 0;
601 }
602 *pi = getintvalue(v);
603 if (*pi < 0)
604 *pi += isize;
605 }
606 return 1;
607}
608
609static object *
610apply_slice(ctx, u, v, w) /* u[v:w] */
611 context *ctx;
612 object *u, *v, *w;
613{
614 typeobject *tp = u->ob_type;
615 int ilow, ihigh, isize;
616 if (tp->tp_as_sequence == NULL) {
617 type_error(ctx, "only sequences can be sliced");
618 return NULL;
619 }
620 ilow = 0;
621 isize = ihigh = (*tp->tp_as_sequence->sq_length)(u);
622 if (!slice_index(ctx, v, isize, &ilow))
623 return NULL;
624 if (!slice_index(ctx, w, isize, &ihigh))
625 return NULL;
626 return checkerror(ctx, (*tp->tp_as_sequence->sq_slice)(u, ilow, ihigh));
627}
628static void
629assign_subscript(ctx, w, key, v)
630 context *ctx;
631 object *w;
632 object *key;
633 object *v;
634{
635 typeobject *tp = w->ob_type;
636 sequence_methods *sq;
637 mapping_methods *mp;
638 int (*func)();
639 int err;
640 if ((sq = tp->tp_as_sequence) != NULL &&
641 (func = sq->sq_ass_item) != NULL) {
642 if (!is_intobject(key)) {
643 type_error(ctx, "sequence subscript must be integer");
644 return;
645 }
646 err = (*func)(w, (int)getintvalue(key), v);
647 }
648 else if ((mp = tp->tp_as_mapping) != NULL &&
649 (func = mp->mp_ass_subscript) != NULL) {
650 err = (*func)(w, key, v);
651 }
652 else {
653 type_error(ctx, "can't assign to this subscripted object");
654 return;
655 }
656 if (err != 0)
657 puterrno(ctx);
658}
659
660static void
661assign_slice(ctx, u, v, w, x) /* u[v:w] = x */
662 context *ctx;
663 object *u, *v, *w, *x;
664{
665 typeobject *tp = u->ob_type;
666 int ilow, ihigh, isize;
667 if (tp->tp_as_sequence == NULL ||
668 tp->tp_as_sequence->sq_ass_slice == NULL) {
669 type_error(ctx, "unassignable slice");
670 return;
671 }
672 ilow = 0;
673 isize = ihigh = (*tp->tp_as_sequence->sq_length)(u);
674 if (!slice_index(ctx, v, isize, &ilow))
675 return;
676 if (!slice_index(ctx, w, isize, &ihigh))
677 return;
678 if ((*tp->tp_as_sequence->sq_ass_slice)(u, ilow, ihigh, x) != 0)
679 puterrno(ctx);
680}
681
682static int
683cmp_exception(err, v)
684 object *err, *v;
685{
686 if (is_tupleobject(v)) {
687 int i, n;
688 n = gettuplesize(v);
689 for (i = 0; i < n; i++) {
690 if (err == gettupleitem(v, i))
691 return 1;
692 }
693 return 0;
694 }
695 return err == v;
696}
697
698static object *
699cmp_outcome(ctx, op, v, w)
700 register context *ctx;
701 enum cmp_op op;
702 register object *v;
703 register object *w;
704{
705 register int cmp;
706 register int res = 0;
707 switch (op) {
708 case IN:
709 case NOT_IN:
710 cmp = cmp_member(ctx, v, w);
711 break;
712 case IS:
713 case IS_NOT:
714 cmp = (v == w);
715 break;
716 case EXC_MATCH:
717 cmp = cmp_exception(v, w);
718 break;
719 default:
720 cmp = cmp_values(ctx, v, w);
721 }
722 if (ctx->ctx_exception)
723 return NULL;
724 switch (op) {
725 case EXC_MATCH:
726 case IS:
727 case IN: res = cmp; break;
728 case IS_NOT:
729 case NOT_IN: res = !cmp; break;
730 case LT: res = cmp < 0; break;
731 case LE: res = cmp <= 0; break;
732 case EQ: res = cmp == 0; break;
733 case NE: res = cmp != 0; break;
734 case GT: res = cmp > 0; break;
735 case GE: res = cmp >= 0; break;
736 /* XXX no default? */
737 }
738 v = res ? True : False;
739 INCREF(v);
740 return v;
741}
742
Guido van Rossume9736fc1990-11-18 17:33:06 +0000743static void
744import_from(ctx, v, name)
745 context *ctx;
746 object *v;
747 char *name;
748{
749 object *w, *x;
750 w = getmoduledict(v);
751 if (name[0] == '*') {
752 int i;
753 int n = getdictsize(w);
754 for (i = 0; i < n; i++) {
755 name = getdictkey(w, i);
756 if (name == NULL || name[0] == '_')
757 continue;
758 x = dictlookup(w, name);
759 if (x == NULL) {
760 /* XXX can't happen? */
761 name_error(ctx, name);
762 break;
763 }
764 if (dictinsert(ctx->ctx_locals, name, x) != 0) {
765 puterrno(ctx);
766 break;
767 }
768 }
769 }
770 else {
771 x = dictlookup(w, name);
772 if (x == NULL)
773 name_error(ctx, name);
774 else if (dictinsert(ctx->ctx_locals, name, x) != 0)
775 puterrno(ctx);
776 }
777}
778
779static object *
780build_class(ctx, v, w)
781 context *ctx;
782 object *v; /* None or tuple containing base classes */
783 object *w; /* dictionary */
784{
785 if (is_tupleobject(v)) {
786 int i;
787 for (i = gettuplesize(v); --i >= 0; ) {
788 object *x = gettupleitem(v, i);
789 if (!is_classobject(x)) {
790 type_error(ctx, "base is not a class object");
791 return NULL;
792 }
793 }
794 }
795 else {
796 v = NULL;
797 }
798 if (!is_dictobject(w)) {
799 sys_error(ctx, "build_class with non-dictionary");
800 return NULL;
801 }
802 return checkerror(ctx, newclassobject(v, w));
803}
804
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000805static object *
806eval_compiled(ctx, co, arg, needvalue)
807 context *ctx;
808 codeobject *co;
809 object *arg;
810 int needvalue;
811{
812 frameobject *f;
813 register object *v;
814 register object *w;
815 register object *u;
816 register object *x;
817 char *name;
Guido van Rossume9736fc1990-11-18 17:33:06 +0000818 int i, op;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000819 FILE *fp;
820#ifndef NDEBUG
821 int trace = dictlookup(ctx->ctx_globals, "__trace") != NULL;
822#endif
823
824 f = newframeobject(
825 (frameobject *)NULL, /*back*/
826 co, /*code*/
827 ctx->ctx_locals, /*locals*/
828 ctx->ctx_globals, /*globals*/
829 50, /*nvalues*/
830 20); /*nblocks*/
831 if (f == NULL) {
832 puterrno(ctx);
833 return NULL;
834 }
835
836#define EMPTY() Empty(f)
837#define FULL() Full(f)
838#define GETCONST(i) Getconst(f, i)
839#define GETNAME(i) Getname(f, i)
840#define JUMPTO(x) Jumpto(f, x)
841#define JUMPBY(x) Jumpby(f, x)
842
843#ifdef NDEBUG
844
845#define PUSH(v) Push(f, v)
846#define TOP() Top(f)
847#define POP() Pop(f)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000848
849#else
850
851#define PUSH(v) if(trace) trace_push(f, v); else push(f, v)
852#define TOP() (trace ? trace_top(f) : top(f))
853#define POP() (trace ? trace_pop(f) : pop(f))
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000854
855#endif
856
857 if (arg != NULL) {
858 INCREF(arg);
859 PUSH(arg);
860 }
861
862 while (f->f_nexti < getstringsize((object *)f->f_code->co_code) &&
863 !ctx->ctx_exception) {
Guido van Rossume9736fc1990-11-18 17:33:06 +0000864#ifdef NDEBUG
865 op = Nextbyte(f);
866#else
867 op = trace ? trace_nextop(f) : nextbyte(f);
868#endif
869 if (op >= HAVE_ARGUMENT)
870 i = nextint(f);
871 switch (op) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000872
873 case DUP_TOP:
874 v = TOP();
875 INCREF(v);
876 PUSH(v);
877 break;
878
879 case POP_TOP:
880 v = POP();
881 DECREF(v);
882 break;
883
884 case ROT_TWO:
885 v = POP();
886 w = POP();
887 PUSH(v);
888 PUSH(w);
889 break;
890
891 case ROT_THREE:
892 v = POP();
893 w = POP();
894 x = POP();
895 PUSH(v);
896 PUSH(x);
897 PUSH(w);
898 break;
899
900 case UNARY_POSITIVE:
901 v = POP();
902 u = pos(ctx, v);
903 DECREF(v);
904 PUSH(u);
905 break;
906
907 case UNARY_NEGATIVE:
908 v = POP();
909 u = neg(ctx, v);
910 DECREF(v);
911 PUSH(u);
912 break;
913
914 case UNARY_NOT:
915 v = POP();
916 u = not(ctx, v);
917 DECREF(v);
918 PUSH(u);
919 break;
920
921 case UNARY_CONVERT:
922 v = POP();
923 u = checkerror(ctx, reprobject(v));
924 DECREF(v);
925 PUSH(u);
926 break;
927
928 case UNARY_CALL:
929 v = POP();
Guido van Rossume9736fc1990-11-18 17:33:06 +0000930 if (is_classmethodobject(v) || is_funcobject(v))
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000931 u = call_function(ctx, v, (object *)NULL);
932 else
933 u = call_builtin(ctx, v, (object *)NULL);
934 DECREF(v);
935 PUSH(u);
936 break;
937
938 case BINARY_MULTIPLY:
939 w = POP();
940 v = POP();
941 u = mul(ctx, v, w);
942 DECREF(v);
943 DECREF(w);
944 PUSH(u);
945 break;
946
947 case BINARY_DIVIDE:
948 w = POP();
949 v = POP();
950 u = div(ctx, v, w);
951 DECREF(v);
952 DECREF(w);
953 PUSH(u);
954 break;
955
956 case BINARY_MODULO:
957 w = POP();
958 v = POP();
959 u = rem(ctx, v, w);
960 DECREF(v);
961 DECREF(w);
962 PUSH(u);
963 break;
964
965 case BINARY_ADD:
966 w = POP();
967 v = POP();
968 u = add(ctx, v, w);
969 DECREF(v);
970 DECREF(w);
971 PUSH(u);
972 break;
973
974 case BINARY_SUBTRACT:
975 w = POP();
976 v = POP();
977 u = sub(ctx, v, w);
978 DECREF(v);
979 DECREF(w);
980 PUSH(u);
981 break;
982
983 case BINARY_SUBSCR:
984 w = POP();
985 v = POP();
986 u = apply_subscript(ctx, v, w);
987 DECREF(v);
988 DECREF(w);
989 PUSH(u);
990 break;
991
992 case BINARY_CALL:
993 w = POP();
994 v = POP();
Guido van Rossume9736fc1990-11-18 17:33:06 +0000995 if (is_classmethodobject(v) || is_funcobject(v))
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000996 u = call_function(ctx, v, w);
997 else
998 u = call_builtin(ctx, v, w);
999 DECREF(v);
1000 DECREF(w);
1001 PUSH(u);
1002 break;
1003
Guido van Rossume9736fc1990-11-18 17:33:06 +00001004 case SLICE+0:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001005 case SLICE+1:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001006 case SLICE+2:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001007 case SLICE+3:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001008 op -= SLICE;
1009 if (op & 2)
1010 w = POP();
1011 else
1012 w = NULL;
1013 if (op & 1)
1014 v = POP();
1015 else
1016 v = NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001017 u = POP();
1018 x = apply_slice(ctx, u, v, w);
1019 DECREF(u);
Guido van Rossume9736fc1990-11-18 17:33:06 +00001020 XDECREF(v);
1021 XDECREF(w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001022 PUSH(x);
1023 break;
1024
Guido van Rossume9736fc1990-11-18 17:33:06 +00001025 case STORE_SLICE+0:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001026 case STORE_SLICE+1:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001027 case STORE_SLICE+2:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001028 case STORE_SLICE+3:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001029 op -= SLICE;
1030 if (op & 2)
1031 w = POP();
1032 else
1033 w = NULL;
1034 if (op & 1)
1035 v = POP();
1036 else
1037 v = NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001038 u = POP();
1039 x = POP();
1040 assign_slice(ctx, u, v, w, x); /* u[v:w] = x */
1041 DECREF(x);
1042 DECREF(u);
Guido van Rossume9736fc1990-11-18 17:33:06 +00001043 XDECREF(v);
1044 XDECREF(w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001045 break;
1046
Guido van Rossume9736fc1990-11-18 17:33:06 +00001047 case DELETE_SLICE+0:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001048 case DELETE_SLICE+1:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001049 case DELETE_SLICE+2:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001050 case DELETE_SLICE+3:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001051 op -= SLICE;
1052 if (op & 2)
1053 w = POP();
1054 else
1055 w = NULL;
1056 if (op & 1)
1057 v = POP();
1058 else
1059 v = NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001060 u = POP();
1061 x = NULL;
1062 assign_slice(ctx, u, v, w, x); /* del u[v:w] */
1063 DECREF(u);
Guido van Rossume9736fc1990-11-18 17:33:06 +00001064 XDECREF(v);
1065 XDECREF(w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001066 break;
1067
1068 case STORE_SUBSCR:
1069 w = POP();
1070 v = POP();
1071 u = POP();
1072 /* v[w] = u */
1073 assign_subscript(ctx, v, w, u);
1074 DECREF(u);
1075 DECREF(v);
1076 DECREF(w);
1077 break;
1078
1079 case DELETE_SUBSCR:
1080 w = POP();
1081 v = POP();
1082 /* del v[w] */
1083 assign_subscript(ctx, v, w, (object *)NULL);
1084 DECREF(v);
1085 DECREF(w);
1086 break;
1087
1088 case PRINT_EXPR:
1089 v = POP();
1090 fp = sysgetfile("stdout", stdout);
1091 /* Print value except if procedure result */
1092 if (v != None) {
1093 flushline();
1094 printobject(v, fp, 0);
1095 fprintf(fp, "\n");
1096 }
1097 DECREF(v);
1098 break;
1099
1100 case PRINT_ITEM:
1101 v = POP();
1102 fp = sysgetfile("stdout", stdout);
1103 if (needspace)
1104 fprintf(fp, " ");
1105 if (is_stringobject(v)) {
1106 char *s = getstringvalue(v);
1107 int len = getstringsize(v);
1108 fwrite(s, 1, len, fp);
1109 if (len > 0 && s[len-1] == '\n')
1110 needspace = 0;
1111 else
1112 needspace = 1;
1113 }
1114 else {
1115 printobject(v, fp, 0);
1116 needspace = 1;
1117 }
1118 DECREF(v);
1119 break;
1120
1121 case PRINT_NEWLINE:
1122 fp = sysgetfile("stdout", stdout);
1123 fprintf(fp, "\n");
1124 needspace = 0;
1125 break;
1126
1127 case BREAK_LOOP:
1128 raise_pseudo(ctx, BREAK_PSEUDO);
1129 break;
1130
1131 case RAISE_EXCEPTION:
1132 v = POP();
1133 w = POP();
1134 if (!is_stringobject(w)) {
1135 DECREF(v);
1136 DECREF(v);
1137 type_error(ctx, "exceptions must be strings");
1138 }
1139 else {
1140 raise_exception(ctx, w, v);
1141 }
1142 break;
1143
Guido van Rossume9736fc1990-11-18 17:33:06 +00001144 case LOAD_LOCALS:
1145 v = ctx->ctx_locals;
1146 INCREF(v);
1147 PUSH(v);
1148 break;
1149
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001150 case RETURN_VALUE:
1151 v = POP();
1152 raise_pseudo(ctx, RETURN_PSEUDO);
1153 ctx->ctx_errval = v;
1154 break;
1155
1156 case REQUIRE_ARGS:
1157 if (EMPTY())
1158 type_error(ctx,
1159 "function expects argument(s)");
1160 break;
1161
1162 case REFUSE_ARGS:
1163 if (!EMPTY())
1164 type_error(ctx,
1165 "function expects no argument(s)");
1166 break;
1167
1168 case BUILD_FUNCTION:
1169 v = POP();
1170 x = checkerror(ctx, newfuncobject(v, ctx->ctx_globals));
1171 DECREF(v);
1172 PUSH(x);
1173 break;
1174
1175 case POP_BLOCK:
1176 (void) pop_block(f);
1177 break;
1178
1179 case END_FINALLY:
1180 v = POP();
1181 w = POP();
1182 if (is_intobject(v)) {
1183 raise_pseudo(ctx, (int)getintvalue(v));
1184 DECREF(v);
1185 if (w == None)
1186 DECREF(w);
1187 else
1188 ctx->ctx_errval = w;
1189 }
1190 else if (is_stringobject(v))
1191 raise_exception(ctx, v, w);
1192 else if (v == None) {
1193 DECREF(v);
1194 DECREF(w);
1195 }
1196 else {
1197 sys_error(ctx, "'finally' pops bad exception");
1198 }
1199 break;
1200
Guido van Rossume9736fc1990-11-18 17:33:06 +00001201 case BUILD_CLASS:
1202 w = POP();
1203 v = POP();
1204 x = build_class(ctx, v, w);
1205 PUSH(x);
1206 DECREF(v);
1207 DECREF(w);
1208 break;
1209
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001210 case STORE_NAME:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001211 name = GETNAME(i);
1212 v = POP();
1213 if (dictinsert(ctx->ctx_locals, name, v) != 0)
1214 mem_error(ctx, "insert in symbol table");
1215 DECREF(v);
1216 break;
1217
1218 case DELETE_NAME:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001219 name = GETNAME(i);
1220 if (dictremove(ctx->ctx_locals, name) != 0)
1221 name_error(ctx, name);
1222 break;
1223
1224 case UNPACK_TUPLE:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001225 v = POP();
1226 if (!is_tupleobject(v)) {
1227 type_error(ctx, "unpack non-tuple");
1228 }
Guido van Rossume9736fc1990-11-18 17:33:06 +00001229 else if (gettuplesize(v) != i) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001230 error(ctx, "unpack tuple of wrong size");
1231 }
1232 else {
Guido van Rossume9736fc1990-11-18 17:33:06 +00001233 for (; --i >= 0; ) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001234 w = gettupleitem(v, i);
1235 INCREF(w);
1236 PUSH(w);
1237 }
1238 }
1239 DECREF(v);
1240 break;
1241
1242 case UNPACK_LIST:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001243 v = POP();
1244 if (!is_listobject(v)) {
1245 type_error(ctx, "unpack non-list");
1246 }
Guido van Rossume9736fc1990-11-18 17:33:06 +00001247 else if (getlistsize(v) != i) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001248 error(ctx, "unpack tuple of wrong size");
1249 }
1250 else {
Guido van Rossume9736fc1990-11-18 17:33:06 +00001251 for (; --i >= 0; ) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001252 w = getlistitem(v, i);
1253 INCREF(w);
1254 PUSH(w);
1255 }
1256 }
1257 DECREF(v);
1258 break;
1259
1260 case STORE_ATTR:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001261 name = GETNAME(i);
1262 v = POP();
1263 u = POP();
1264 /* v.name = u */
1265 if (v->ob_type->tp_setattr == NULL) {
1266 type_error(ctx, "object without writable attributes");
1267 }
1268 else {
1269 if ((*v->ob_type->tp_setattr)(v, name, u) != 0)
1270 puterrno(ctx);
1271 }
1272 DECREF(v);
1273 DECREF(u);
1274 break;
1275
1276 case DELETE_ATTR:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001277 name = GETNAME(i);
1278 v = POP();
1279 /* del v.name */
1280 if (v->ob_type->tp_setattr == NULL) {
1281 type_error(ctx,
1282 "object without writable attributes");
1283 }
1284 else {
1285 if ((*v->ob_type->tp_setattr)
1286 (v, name, (object *)NULL) != 0)
1287 puterrno(ctx);
1288 }
1289 DECREF(v);
1290 break;
1291
1292 case LOAD_CONST:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001293 v = GETCONST(i);
1294 INCREF(v);
1295 PUSH(v);
1296 break;
1297
1298 case LOAD_NAME:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001299 name = GETNAME(i);
1300 v = dictlookup(ctx->ctx_locals, name);
1301 if (v == NULL) {
1302 v = dictlookup(ctx->ctx_globals, name);
1303 if (v == NULL)
1304 v = dictlookup(ctx->ctx_builtins, name);
1305 }
1306 if (v == NULL)
1307 name_error(ctx, name);
1308 /* XXX could optimize */
1309 else
1310 INCREF(v);
1311 PUSH(v);
1312 break;
1313
1314 case BUILD_TUPLE:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001315 v = checkerror(ctx, newtupleobject(i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001316 if (v != NULL) {
Guido van Rossume9736fc1990-11-18 17:33:06 +00001317 for (; --i >= 0;) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001318 w = POP();
1319 settupleitem(v, i, w);
1320 }
1321 }
1322 PUSH(v);
1323 break;
1324
1325 case BUILD_LIST:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001326 v = checkerror(ctx, newlistobject(i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001327 if (v != NULL) {
Guido van Rossume9736fc1990-11-18 17:33:06 +00001328 for (; --i >= 0;) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001329 w = POP();
1330 setlistitem(v, i, w);
1331 }
1332 }
1333 PUSH(v);
1334 break;
1335
1336 case BUILD_MAP:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001337 v = checkerror(ctx, newdictobject());
1338 PUSH(v);
1339 break;
1340
1341 case LOAD_ATTR:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001342 name = GETNAME(i);
1343 v = POP();
1344 if (v->ob_type->tp_getattr == NULL) {
1345 type_error(ctx, "attribute-less object");
1346 u = NULL;
1347 }
1348 else {
1349 u = checkerror(ctx,
1350 (*v->ob_type->tp_getattr)(v, name));
1351 }
1352 DECREF(v);
1353 PUSH(u);
1354 break;
1355
1356 case COMPARE_OP:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001357 w = POP();
1358 v = POP();
Guido van Rossume9736fc1990-11-18 17:33:06 +00001359 u = cmp_outcome(ctx, (enum cmp_op)i, v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001360 DECREF(v);
1361 DECREF(w);
1362 PUSH(u);
1363 break;
1364
1365 case IMPORT_NAME:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001366 name = GETNAME(i);
1367 u = import_module(ctx, name);
1368 if (u != NULL) {
1369 INCREF(u);
1370 PUSH(u);
1371 }
1372 break;
1373
1374 case IMPORT_FROM:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001375 name = GETNAME(i);
1376 v = TOP();
Guido van Rossume9736fc1990-11-18 17:33:06 +00001377 import_from(ctx, v, name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001378 break;
1379
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001380 case JUMP_FORWARD:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001381 JUMPBY(i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001382 break;
1383
1384 case JUMP_IF_FALSE:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001385 if (!testbool(ctx, TOP()))
Guido van Rossume9736fc1990-11-18 17:33:06 +00001386 JUMPBY(i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001387 break;
1388
1389 case JUMP_IF_TRUE:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001390 if (testbool(ctx, TOP()))
Guido van Rossume9736fc1990-11-18 17:33:06 +00001391 JUMPBY(i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001392 break;
1393
1394 case JUMP_ABSOLUTE:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001395 JUMPTO(i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001396 /* XXX Should check for interrupts more often? */
1397 if (intrcheck())
1398 intr_error(ctx);
1399 break;
1400
1401 case FOR_LOOP:
1402 /* for v in s: ...
1403 On entry: stack contains s, i.
1404 On exit: stack contains s, i+1, s[i];
1405 but if loop exhausted:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001406 s, i are popped, and we jump */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001407 w = POP(); /* Loop index */
1408 v = POP(); /* Sequence object */
1409 x = loop_subscript(ctx, v, w);
1410 if (x != NULL) {
1411 PUSH(v);
1412 u = checkerror(ctx,
1413 newintobject(getintvalue(w)+1));
1414 PUSH(u);
1415 DECREF(w);
1416 PUSH(x);
1417 }
1418 else {
1419 DECREF(v);
1420 DECREF(w);
Guido van Rossume9736fc1990-11-18 17:33:06 +00001421 JUMPBY(i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001422 }
1423 break;
1424
1425 case SETUP_LOOP:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001426 setup_block(f, SETUP_LOOP, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001427 break;
1428
1429 case SETUP_EXCEPT:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001430 setup_block(f, SETUP_EXCEPT, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001431 break;
1432
1433 case SETUP_FINALLY:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001434 setup_block(f, SETUP_FINALLY, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001435 break;
1436
1437 default:
Guido van Rossume9736fc1990-11-18 17:33:06 +00001438 printf("opcode %d\n", op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001439 sys_error(ctx, "eval_compiled: unknown opcode");
1440 break;
1441
1442 }
1443
1444 /* Unwind block stack if an exception occurred */
1445
1446 while (ctx->ctx_exception && f->f_iblock > 0) {
1447 block *b = pop_block(f);
1448 if (b->b_type == SETUP_LOOP &&
1449 ctx->ctx_exception == BREAK_PSEUDO) {
1450 clear_exception(ctx);
1451 JUMPTO(b->b_handler);
1452 break;
1453 }
1454 else if (b->b_type == SETUP_FINALLY ||
1455 b->b_type == SETUP_EXCEPT &&
1456 ctx->ctx_exception == CATCHABLE_EXCEPTION) {
1457 v = ctx->ctx_errval;
1458 if (v == NULL)
1459 v = None;
1460 INCREF(v);
1461 PUSH(v);
1462 v = ctx->ctx_error;
1463 if (v == NULL)
1464 v = newintobject(ctx->ctx_exception);
1465 else
1466 INCREF(v);
1467 PUSH(v);
1468 clear_exception(ctx);
1469 JUMPTO(b->b_handler);
1470 break;
1471 }
1472 }
1473 }
1474
1475 if (ctx->ctx_exception) {
1476 while (!EMPTY()) {
1477 v = POP();
1478 XDECREF(v);
1479 }
1480 v = NULL;
1481 if (ctx->ctx_exception == RETURN_PSEUDO) {
1482 if (needvalue) {
1483 v = ctx->ctx_errval;
1484 INCREF(v);
1485 clear_exception(ctx);
1486 }
1487 else {
1488 /* XXX Can detect this statically! */
1489 type_error(ctx, "unexpected return statement");
1490 }
1491 }
1492 }
1493 else {
1494 if (needvalue)
1495 v = POP();
1496 else
1497 v = NULL;
1498 if (!EMPTY()) {
1499 sys_error(ctx, "stack not cleaned up");
1500 XDECREF(v);
1501 while (!EMPTY()) {
1502 v = POP();
1503 XDECREF(v);
1504 }
1505 v = NULL;
1506 }
1507 }
1508
1509#undef EMPTY
1510#undef FULL
1511#undef GETCONST
1512#undef GETNAME
1513#undef JUMPTO
1514#undef JUMPBY
1515
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001516#undef POP
1517#undef TOP
1518#undef PUSH
1519
1520 DECREF(f);
1521 return v;
1522
1523}
1524
1525/* Provisional interface until everything is compilable */
1526
1527#include "node.h"
1528
1529static object *
1530eval_or_exec(ctx, n, arg, needvalue)
1531 context *ctx;
1532 node *n;
1533 object *arg;
1534 int needvalue;
1535{
1536 object *v;
1537 codeobject *co = compile(n);
Guido van Rossume9736fc1990-11-18 17:33:06 +00001538 freenode(n); /* XXX A rather strange place to do this! */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001539 if (co == NULL) {
1540 puterrno(ctx);
1541 return NULL;
1542 }
1543 v = eval_compiled(ctx, co, arg, needvalue);
1544 DECREF(co);
1545 return v;
1546}
1547
1548object *
1549eval_node(ctx, n)
1550 context *ctx;
1551 node *n;
1552{
1553 return eval_or_exec(ctx, n, (object *)NULL, 1/*needvalue*/);
1554}
1555
1556void
1557exec_node(ctx, n)
1558 context *ctx;
1559 node *n;
1560{
1561 (void) eval_or_exec(ctx, n, (object *)NULL, 0/*needvalue*/);
1562}