1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
2000-06-30 20:50:40 -03:00
|
|
|
Copyright (c) 2000, BeOpen.com.
|
|
|
|
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
|
|
|
|
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
|
|
|
|
All rights reserved.
|
1996-10-25 11:44:06 -03:00
|
|
|
|
2000-06-30 20:50:40 -03:00
|
|
|
See the file "Misc/COPYRIGHT" for information on usage and
|
|
|
|
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
1991-02-19 08:39:46 -04:00
|
|
|
******************************************************************/
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
/* Compile an expression node to intermediate code */
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* XXX TO DO:
|
1997-01-17 17:04:03 -04:00
|
|
|
XXX add __doc__ attribute == co_doc to code object attributes?
|
|
|
|
XXX (it's currently the first item of the co_const tuple)
|
1990-12-20 11:06:42 -04:00
|
|
|
XXX Generate simple jump for break/return outside 'try...finally'
|
1997-01-17 17:04:03 -04:00
|
|
|
XXX Allow 'continue' inside try-finally
|
|
|
|
XXX New opcode for loading the initial index for a for loop
|
1995-07-18 11:51:37 -03:00
|
|
|
XXX other JAR tricks?
|
1990-12-20 11:06:42 -04:00
|
|
|
*/
|
|
|
|
|
1996-08-24 03:21:31 -03:00
|
|
|
#ifndef NO_PRIVATE_NAME_MANGLING
|
|
|
|
#define PRIVATE_NAME_MANGLING
|
|
|
|
#endif
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
#include "Python.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
#include "node.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include "graminit.h"
|
|
|
|
#include "compile.h"
|
|
|
|
#include "opcode.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "structmember.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2000-07-03 18:39:47 -03:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
1991-04-04 06:42:56 -04:00
|
|
|
|
1997-05-07 14:46:13 -03:00
|
|
|
/* Three symbols from graminit.h are also defined in Python.h, with
|
|
|
|
Py_ prefixes to their names. Python.h can't include graminit.h
|
|
|
|
(which defines too many confusing symbols), but we can check here
|
|
|
|
that they haven't changed (which is very unlikely, but possible). */
|
|
|
|
#if Py_single_input != single_input
|
1997-05-20 19:18:48 -03:00
|
|
|
#error "single_input has changed -- update Py_single_input in Python.h"
|
1997-05-07 14:46:13 -03:00
|
|
|
#endif
|
|
|
|
#if Py_file_input != file_input
|
1997-05-20 19:18:48 -03:00
|
|
|
#error "file_input has changed -- update Py_file_input in Python.h"
|
1997-05-07 14:46:13 -03:00
|
|
|
#endif
|
|
|
|
#if Py_eval_input != eval_input
|
1997-05-20 19:18:48 -03:00
|
|
|
#error "eval_input has changed -- update Py_eval_input in Python.h"
|
1997-05-07 14:46:13 -03:00
|
|
|
#endif
|
|
|
|
|
1997-03-03 15:13:14 -04:00
|
|
|
int Py_OptimizeFlag = 0;
|
|
|
|
|
1996-07-30 13:49:37 -03:00
|
|
|
#define OP_DELETE 0
|
|
|
|
#define OP_ASSIGN 1
|
|
|
|
#define OP_APPLY 2
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
#define OFF(x) offsetof(PyCodeObject, x)
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
static struct memberlist code_memberlist[] = {
|
1995-07-18 11:51:37 -03:00
|
|
|
{"co_argcount", T_INT, OFF(co_argcount), READONLY},
|
|
|
|
{"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
|
1997-01-17 17:04:03 -04:00
|
|
|
{"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
|
1995-07-18 11:51:37 -03:00
|
|
|
{"co_flags", T_INT, OFF(co_flags), READONLY},
|
1992-01-11 22:30:05 -04:00
|
|
|
{"co_code", T_OBJECT, OFF(co_code), READONLY},
|
|
|
|
{"co_consts", T_OBJECT, OFF(co_consts), READONLY},
|
|
|
|
{"co_names", T_OBJECT, OFF(co_names), READONLY},
|
1995-07-18 11:51:37 -03:00
|
|
|
{"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
|
1992-01-11 22:30:05 -04:00
|
|
|
{"co_filename", T_OBJECT, OFF(co_filename), READONLY},
|
1993-03-29 06:43:31 -04:00
|
|
|
{"co_name", T_OBJECT, OFF(co_name), READONLY},
|
1997-01-23 23:43:35 -04:00
|
|
|
{"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
|
|
|
|
{"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
|
1990-12-20 11:06:42 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
code_getattr(PyCodeObject *co, char *name)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyMember_Get((char *)co, code_memberlist, name);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
code_dealloc(PyCodeObject *co)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(co->co_code);
|
|
|
|
Py_XDECREF(co->co_consts);
|
|
|
|
Py_XDECREF(co->co_names);
|
1997-07-25 17:13:49 -03:00
|
|
|
Py_XDECREF(co->co_varnames);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(co->co_filename);
|
|
|
|
Py_XDECREF(co->co_name);
|
1997-07-25 17:13:49 -03:00
|
|
|
Py_XDECREF(co->co_lnotab);
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_DEL(co);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
code_repr(PyCodeObject *co)
|
1992-09-03 17:50:59 -03:00
|
|
|
{
|
|
|
|
char buf[500];
|
|
|
|
int lineno = -1;
|
|
|
|
char *filename = "???";
|
1993-03-29 06:43:31 -04:00
|
|
|
char *name = "???";
|
1998-10-07 16:42:25 -03:00
|
|
|
|
2000-04-06 22:21:36 -03:00
|
|
|
if (co->co_firstlineno != 0)
|
|
|
|
lineno = co->co_firstlineno;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (co->co_filename && PyString_Check(co->co_filename))
|
|
|
|
filename = PyString_AsString(co->co_filename);
|
|
|
|
if (co->co_name && PyString_Check(co->co_name))
|
|
|
|
name = PyString_AsString(co->co_name);
|
2000-06-30 13:20:13 -03:00
|
|
|
sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
|
|
|
|
name, co, filename, lineno);
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyString_FromString(buf);
|
1992-09-03 17:50:59 -03:00
|
|
|
}
|
|
|
|
|
1993-11-05 06:20:10 -04:00
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
code_compare(PyCodeObject *co, PyCodeObject *cp)
|
1993-11-05 06:20:10 -04:00
|
|
|
{
|
|
|
|
int cmp;
|
2000-04-10 13:20:31 -03:00
|
|
|
cmp = PyObject_Compare(co->co_name, cp->co_name);
|
|
|
|
if (cmp) return cmp;
|
1997-08-29 14:12:43 -03:00
|
|
|
cmp = co->co_argcount - cp->co_argcount;
|
1995-07-18 11:51:37 -03:00
|
|
|
if (cmp) return cmp;
|
1997-08-29 14:12:43 -03:00
|
|
|
cmp = co->co_nlocals - cp->co_nlocals;
|
1995-07-18 11:51:37 -03:00
|
|
|
if (cmp) return cmp;
|
1997-08-29 14:12:43 -03:00
|
|
|
cmp = co->co_flags - cp->co_flags;
|
1995-07-18 11:51:37 -03:00
|
|
|
if (cmp) return cmp;
|
1998-10-07 16:42:25 -03:00
|
|
|
cmp = PyObject_Compare(co->co_code, cp->co_code);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (cmp) return cmp;
|
1997-04-29 17:08:16 -03:00
|
|
|
cmp = PyObject_Compare(co->co_consts, cp->co_consts);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (cmp) return cmp;
|
1997-04-29 17:08:16 -03:00
|
|
|
cmp = PyObject_Compare(co->co_names, cp->co_names);
|
1995-07-18 11:51:37 -03:00
|
|
|
if (cmp) return cmp;
|
1997-04-29 17:08:16 -03:00
|
|
|
cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
|
1993-11-05 06:20:10 -04:00
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
2000-07-22 15:47:25 -03:00
|
|
|
code_hash(PyCodeObject *co)
|
1993-11-05 06:20:10 -04:00
|
|
|
{
|
2000-04-10 13:20:31 -03:00
|
|
|
long h, h0, h1, h2, h3, h4;
|
|
|
|
h0 = PyObject_Hash(co->co_name);
|
|
|
|
if (h0 == -1) return -1;
|
1998-10-07 16:42:25 -03:00
|
|
|
h1 = PyObject_Hash(co->co_code);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h1 == -1) return -1;
|
1997-04-29 17:08:16 -03:00
|
|
|
h2 = PyObject_Hash(co->co_consts);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h2 == -1) return -1;
|
1997-04-29 17:08:16 -03:00
|
|
|
h3 = PyObject_Hash(co->co_names);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h3 == -1) return -1;
|
1997-04-29 17:08:16 -03:00
|
|
|
h4 = PyObject_Hash(co->co_varnames);
|
1995-07-18 11:51:37 -03:00
|
|
|
if (h4 == -1) return -1;
|
2000-04-10 13:20:31 -03:00
|
|
|
h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^
|
1995-07-18 11:51:37 -03:00
|
|
|
co->co_argcount ^ co->co_nlocals ^ co->co_flags;
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h == -1) h = -2;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyTypeObject PyCode_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-11-18 13:27:39 -04:00
|
|
|
0,
|
|
|
|
"code",
|
1997-04-29 17:08:16 -03:00
|
|
|
sizeof(PyCodeObject),
|
1990-11-18 13:27:39 -04:00
|
|
|
0,
|
1994-08-29 09:16:12 -03:00
|
|
|
(destructor)code_dealloc, /*tp_dealloc*/
|
1990-11-18 13:27:39 -04:00
|
|
|
0, /*tp_print*/
|
1994-08-29 09:16:12 -03:00
|
|
|
(getattrfunc)code_getattr, /*tp_getattr*/
|
1990-11-18 13:27:39 -04:00
|
|
|
0, /*tp_setattr*/
|
1994-08-29 09:16:12 -03:00
|
|
|
(cmpfunc)code_compare, /*tp_compare*/
|
|
|
|
(reprfunc)code_repr, /*tp_repr*/
|
1990-11-18 13:27:39 -04:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
1994-08-29 09:16:12 -03:00
|
|
|
(hashfunc)code_hash, /*tp_hash*/
|
1990-11-18 13:27:39 -04:00
|
|
|
};
|
|
|
|
|
1997-04-09 16:24:53 -03:00
|
|
|
#define NAME_CHARS \
|
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyCodeObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
PyCode_New(int argcount, int nlocals, int stacksize, int flags,
|
|
|
|
PyObject *code, PyObject *consts, PyObject *names,
|
|
|
|
PyObject *varnames, PyObject *filename, PyObject *name,
|
|
|
|
int firstlineno, PyObject *lnotab)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyCodeObject *co;
|
1990-11-18 13:27:39 -04:00
|
|
|
int i;
|
1998-10-07 16:42:25 -03:00
|
|
|
PyBufferProcs *pb;
|
1990-11-18 13:27:39 -04:00
|
|
|
/* Check argument types */
|
1995-07-18 11:51:37 -03:00
|
|
|
if (argcount < 0 || nlocals < 0 ||
|
1998-10-07 16:42:25 -03:00
|
|
|
code == NULL ||
|
1997-04-29 17:08:16 -03:00
|
|
|
consts == NULL || !PyTuple_Check(consts) ||
|
|
|
|
names == NULL || !PyTuple_Check(names) ||
|
|
|
|
varnames == NULL || !PyTuple_Check(varnames) ||
|
|
|
|
name == NULL || !PyString_Check(name) ||
|
|
|
|
filename == NULL || !PyString_Check(filename) ||
|
|
|
|
lnotab == NULL || !PyString_Check(lnotab)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-11-18 13:27:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1998-10-07 16:42:25 -03:00
|
|
|
pb = code->ob_type->tp_as_buffer;
|
|
|
|
if (pb == NULL ||
|
|
|
|
pb->bf_getreadbuffer == NULL ||
|
|
|
|
pb->bf_getsegcount == NULL ||
|
|
|
|
(*pb->bf_getsegcount)(code, NULL) != 1)
|
|
|
|
{
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-18 04:02:57 -04:00
|
|
|
/* Make sure names and varnames are all strings, & intern them */
|
1997-04-29 17:08:16 -03:00
|
|
|
for (i = PyTuple_Size(names); --i >= 0; ) {
|
|
|
|
PyObject *v = PyTuple_GetItem(names, i);
|
|
|
|
if (v == NULL || !PyString_Check(v)) {
|
|
|
|
PyErr_BadInternalCall();
|
1994-08-29 09:16:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-18 04:02:57 -04:00
|
|
|
PyString_InternInPlace(&PyTuple_GET_ITEM(names, i));
|
1994-08-29 09:16:12 -03:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
for (i = PyTuple_Size(varnames); --i >= 0; ) {
|
|
|
|
PyObject *v = PyTuple_GetItem(varnames, i);
|
|
|
|
if (v == NULL || !PyString_Check(v)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-11-18 13:27:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-18 04:02:57 -04:00
|
|
|
PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i));
|
|
|
|
}
|
|
|
|
/* Intern selected string constants */
|
1997-04-29 17:08:16 -03:00
|
|
|
for (i = PyTuple_Size(consts); --i >= 0; ) {
|
|
|
|
PyObject *v = PyTuple_GetItem(consts, i);
|
1997-01-18 04:02:57 -04:00
|
|
|
char *p;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyString_Check(v))
|
1997-01-18 04:02:57 -04:00
|
|
|
continue;
|
1997-04-29 17:08:16 -03:00
|
|
|
p = PyString_AsString(v);
|
2000-06-28 19:07:35 -03:00
|
|
|
if (strspn(p, NAME_CHARS)
|
|
|
|
!= (size_t)PyString_Size(v))
|
1997-01-18 04:02:57 -04:00
|
|
|
continue;
|
|
|
|
PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
co = PyObject_NEW(PyCodeObject, &PyCode_Type);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (co != NULL) {
|
1995-07-18 11:51:37 -03:00
|
|
|
co->co_argcount = argcount;
|
|
|
|
co->co_nlocals = nlocals;
|
1997-01-17 17:04:03 -04:00
|
|
|
co->co_stacksize = stacksize;
|
1995-07-18 11:51:37 -03:00
|
|
|
co->co_flags = flags;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(code);
|
1998-10-07 16:42:25 -03:00
|
|
|
co->co_code = code;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(consts);
|
1990-11-18 13:27:39 -04:00
|
|
|
co->co_consts = consts;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(names);
|
1990-11-18 13:27:39 -04:00
|
|
|
co->co_names = names;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(varnames);
|
1995-07-18 11:51:37 -03:00
|
|
|
co->co_varnames = varnames;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(filename);
|
1991-06-04 16:41:56 -03:00
|
|
|
co->co_filename = filename;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(name);
|
1993-03-29 06:43:31 -04:00
|
|
|
co->co_name = name;
|
1997-01-23 23:43:35 -04:00
|
|
|
co->co_firstlineno = firstlineno;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(lnotab);
|
1997-01-23 23:43:35 -04:00
|
|
|
co->co_lnotab = lnotab;
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
return co;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Data structure used internally */
|
1991-07-27 18:34:52 -03:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
struct compiling {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *c_code; /* string */
|
|
|
|
PyObject *c_consts; /* list of objects */
|
2000-04-28 13:42:25 -03:00
|
|
|
PyObject *c_const_dict; /* inverse of c_consts */
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *c_names; /* list of strings (names) */
|
2000-04-28 13:42:25 -03:00
|
|
|
PyObject *c_name_dict; /* inverse of c_names */
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *c_globals; /* dictionary (value=None) */
|
|
|
|
PyObject *c_locals; /* dictionary (value=localID) */
|
|
|
|
PyObject *c_varnames; /* list (inverse of c_locals) */
|
1995-07-18 11:51:37 -03:00
|
|
|
int c_nlocals; /* index of next local */
|
|
|
|
int c_argcount; /* number of top-level arguments */
|
|
|
|
int c_flags; /* same as co_flags */
|
1990-11-18 13:27:39 -04:00
|
|
|
int c_nexti; /* index into c_code */
|
|
|
|
int c_errors; /* counts errors occurred */
|
1990-12-20 11:06:42 -04:00
|
|
|
int c_infunction; /* set when compiling a function */
|
1994-08-29 09:16:12 -03:00
|
|
|
int c_interactive; /* generating code for interactive command */
|
1990-12-20 11:06:42 -04:00
|
|
|
int c_loops; /* counts nested loops */
|
1991-07-27 18:34:52 -03:00
|
|
|
int c_begin; /* begin of current loop, for 'continue' */
|
1997-01-17 17:04:03 -04:00
|
|
|
int c_block[CO_MAXBLOCKS]; /* stack of block types */
|
1991-07-27 18:34:52 -03:00
|
|
|
int c_nblocks; /* current block stack level */
|
1990-12-20 11:06:42 -04:00
|
|
|
char *c_filename; /* filename of current node */
|
1993-03-29 06:43:31 -04:00
|
|
|
char *c_name; /* name of object (e.g. function) */
|
1996-09-17 11:32:04 -03:00
|
|
|
int c_lineno; /* Current line number */
|
1997-01-17 17:04:03 -04:00
|
|
|
int c_stacklevel; /* Current stack level */
|
|
|
|
int c_maxstacklevel; /* Maximum stack level */
|
1997-01-23 23:43:35 -04:00
|
|
|
int c_firstlineno;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *c_lnotab; /* Table mapping address to line number */
|
1997-01-23 23:43:35 -04:00
|
|
|
int c_last_addr, c_last_line, c_lnotab_next;
|
1996-08-24 03:21:31 -03:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
char *c_private; /* for private name mangling */
|
|
|
|
#endif
|
1990-11-18 13:27:39 -04:00
|
|
|
};
|
|
|
|
|
1991-07-27 18:34:52 -03:00
|
|
|
|
1996-09-17 11:32:04 -03:00
|
|
|
/* Error message including line number */
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_error(struct compiling *c, PyObject *exc, char *msg)
|
1996-09-17 11:32:04 -03:00
|
|
|
{
|
2000-06-28 19:07:35 -03:00
|
|
|
size_t n = strlen(msg);
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1996-09-17 11:32:04 -03:00
|
|
|
char buffer[30];
|
|
|
|
char *s;
|
1997-01-06 18:56:52 -04:00
|
|
|
c->c_errors++;
|
1996-09-17 11:32:04 -03:00
|
|
|
if (c->c_lineno <= 1) {
|
|
|
|
/* Unknown line number or single interactive command */
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(exc, msg);
|
1996-09-17 11:32:04 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sprintf(buffer, " (line %d)", c->c_lineno);
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer));
|
1996-09-17 11:32:04 -03:00
|
|
|
if (v == NULL)
|
|
|
|
return; /* MemoryError, too bad */
|
1997-04-29 17:08:16 -03:00
|
|
|
s = PyString_AS_STRING((PyStringObject *)v);
|
1996-09-17 11:32:04 -03:00
|
|
|
strcpy(s, msg);
|
|
|
|
strcat(s, buffer);
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetObject(exc, v);
|
|
|
|
Py_DECREF(v);
|
1996-09-17 11:32:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1991-07-27 18:34:52 -03:00
|
|
|
/* Interface to the block stack */
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
block_push(struct compiling *c, int type)
|
1991-07-27 18:34:52 -03:00
|
|
|
{
|
1997-01-17 17:04:03 -04:00
|
|
|
if (c->c_nblocks >= CO_MAXBLOCKS) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"too many statically nested blocks");
|
1991-07-27 18:34:52 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
c->c_block[c->c_nblocks++] = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
block_pop(struct compiling *c, int type)
|
1991-07-27 18:34:52 -03:00
|
|
|
{
|
|
|
|
if (c->c_nblocks > 0)
|
|
|
|
c->c_nblocks--;
|
|
|
|
if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError, "bad block pop");
|
1991-07-27 18:34:52 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-07-18 11:51:37 -03:00
|
|
|
/* Prototype forward declarations */
|
1991-07-27 18:34:52 -03:00
|
|
|
|
2000-07-09 00:09:57 -03:00
|
|
|
static int com_init(struct compiling *, char *);
|
|
|
|
static void com_free(struct compiling *);
|
|
|
|
static void com_push(struct compiling *, int);
|
|
|
|
static void com_pop(struct compiling *, int);
|
|
|
|
static void com_done(struct compiling *);
|
|
|
|
static void com_node(struct compiling *, struct _node *);
|
|
|
|
static void com_factor(struct compiling *, struct _node *);
|
|
|
|
static void com_addbyte(struct compiling *, int);
|
|
|
|
static void com_addint(struct compiling *, int);
|
|
|
|
static void com_addoparg(struct compiling *, int, int);
|
|
|
|
static void com_addfwref(struct compiling *, int, int *);
|
|
|
|
static void com_backpatch(struct compiling *, int);
|
|
|
|
static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
|
|
|
|
static int com_addconst(struct compiling *, PyObject *);
|
|
|
|
static int com_addname(struct compiling *, PyObject *);
|
|
|
|
static void com_addopname(struct compiling *, int, node *);
|
|
|
|
static void com_list(struct compiling *, node *, int);
|
|
|
|
static int com_argdefs(struct compiling *, node *);
|
|
|
|
static int com_newlocal(struct compiling *, char *);
|
|
|
|
static PyCodeObject *icompile(struct _node *, struct compiling *);
|
|
|
|
static PyCodeObject *jcompile(struct _node *, char *,
|
|
|
|
struct compiling *);
|
|
|
|
static PyObject *parsestrplus(node *);
|
|
|
|
static PyObject *parsestr(char *);
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_init(struct compiling *c, char *filename)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-04-28 13:42:25 -03:00
|
|
|
memset((void *)c, '\0', sizeof(struct compiling));
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
|
|
|
|
1000)) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_consts = PyList_New(0)) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
|
|
|
if ((c->c_const_dict = PyDict_New()) == NULL)
|
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_names = PyList_New(0)) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
|
|
|
if ((c->c_name_dict = PyDict_New()) == NULL)
|
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_globals = PyDict_New()) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_locals = PyDict_New()) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_varnames = PyList_New(0)) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
|
|
|
|
1000)) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
1995-07-18 11:51:37 -03:00
|
|
|
c->c_nlocals = 0;
|
|
|
|
c->c_argcount = 0;
|
|
|
|
c->c_flags = 0;
|
1990-11-18 13:27:39 -04:00
|
|
|
c->c_nexti = 0;
|
|
|
|
c->c_errors = 0;
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_infunction = 0;
|
1994-08-29 09:16:12 -03:00
|
|
|
c->c_interactive = 0;
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops = 0;
|
1991-07-27 18:34:52 -03:00
|
|
|
c->c_begin = 0;
|
|
|
|
c->c_nblocks = 0;
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_filename = filename;
|
1993-03-29 06:43:31 -04:00
|
|
|
c->c_name = "?";
|
1996-09-17 11:32:04 -03:00
|
|
|
c->c_lineno = 0;
|
1997-01-17 17:04:03 -04:00
|
|
|
c->c_stacklevel = 0;
|
|
|
|
c->c_maxstacklevel = 0;
|
1997-01-23 23:43:35 -04:00
|
|
|
c->c_firstlineno = 0;
|
|
|
|
c->c_last_addr = 0;
|
|
|
|
c->c_last_line = 0;
|
|
|
|
c-> c_lnotab_next = 0;
|
1990-11-18 13:27:39 -04:00
|
|
|
return 1;
|
|
|
|
|
2000-04-28 13:42:25 -03:00
|
|
|
fail:
|
|
|
|
com_free(c);
|
1990-11-18 13:27:39 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_free(struct compiling *c)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(c->c_code);
|
|
|
|
Py_XDECREF(c->c_consts);
|
2000-04-28 13:42:25 -03:00
|
|
|
Py_XDECREF(c->c_const_dict);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(c->c_names);
|
2000-04-28 13:42:25 -03:00
|
|
|
Py_XDECREF(c->c_name_dict);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(c->c_globals);
|
|
|
|
Py_XDECREF(c->c_locals);
|
|
|
|
Py_XDECREF(c->c_varnames);
|
|
|
|
Py_XDECREF(c->c_lnotab);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1997-01-17 17:04:03 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_push(struct compiling *c, int n)
|
1997-01-17 17:04:03 -04:00
|
|
|
{
|
|
|
|
c->c_stacklevel += n;
|
|
|
|
if (c->c_stacklevel > c->c_maxstacklevel)
|
|
|
|
c->c_maxstacklevel = c->c_stacklevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_pop(struct compiling *c, int n)
|
1997-01-17 17:04:03 -04:00
|
|
|
{
|
|
|
|
if (c->c_stacklevel < n) {
|
1998-08-25 15:22:17 -03:00
|
|
|
/* fprintf(stderr,
|
1997-01-17 17:04:03 -04:00
|
|
|
"%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
|
|
|
|
c->c_filename, c->c_lineno,
|
1998-08-25 15:22:17 -03:00
|
|
|
c->c_nexti, c->c_stacklevel, n); */
|
1997-01-17 17:04:03 -04:00
|
|
|
c->c_stacklevel = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c->c_stacklevel -= n;
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_done(struct compiling *c)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
if (c->c_code != NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
_PyString_Resize(&c->c_code, c->c_nexti);
|
1997-01-23 23:43:35 -04:00
|
|
|
if (c->c_lnotab != NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
_PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addbyte(struct compiling *c, int byte)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int len;
|
1995-07-18 11:51:37 -03:00
|
|
|
/*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
|
1990-11-18 13:27:39 -04:00
|
|
|
if (byte < 0 || byte > 255) {
|
1991-10-20 17:12:38 -03:00
|
|
|
/*
|
1990-12-20 11:06:42 -04:00
|
|
|
fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
|
1995-01-02 15:04:15 -04:00
|
|
|
fatal("com_addbyte: byte out of range");
|
1991-10-20 17:12:38 -03:00
|
|
|
*/
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_addbyte: byte out of range");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
if (c->c_code == NULL)
|
|
|
|
return;
|
1997-04-29 17:08:16 -03:00
|
|
|
len = PyString_Size(c->c_code);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (c->c_nexti >= len) {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (_PyString_Resize(&c->c_code, len+1000) != 0) {
|
1990-11-18 13:27:39 -04:00
|
|
|
c->c_errors++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
PyString_AsString(c->c_code)[c->c_nexti++] = byte;
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addint(struct compiling *c, int x)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1990-11-18 13:35:03 -04:00
|
|
|
com_addbyte(c, x & 0xff);
|
|
|
|
com_addbyte(c, x >> 8); /* XXX x should be positive */
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1997-01-23 23:43:35 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_add_lnotab(struct compiling *c, int addr, int line)
|
1997-01-23 23:43:35 -04:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
char *p;
|
|
|
|
if (c->c_lnotab == NULL)
|
|
|
|
return;
|
1997-04-29 17:08:16 -03:00
|
|
|
size = PyString_Size(c->c_lnotab);
|
1997-01-23 23:43:35 -04:00
|
|
|
if (c->c_lnotab_next+2 > size) {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
|
1997-01-23 23:43:35 -04:00
|
|
|
c->c_errors++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
|
1997-01-23 23:43:35 -04:00
|
|
|
*p++ = addr;
|
|
|
|
*p++ = line;
|
|
|
|
c->c_lnotab_next += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_set_lineno(struct compiling *c, int lineno)
|
1997-01-23 23:43:35 -04:00
|
|
|
{
|
|
|
|
c->c_lineno = lineno;
|
|
|
|
if (c->c_firstlineno == 0) {
|
|
|
|
c->c_firstlineno = c->c_last_line = lineno;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int incr_addr = c->c_nexti - c->c_last_addr;
|
|
|
|
int incr_line = lineno - c->c_last_line;
|
|
|
|
while (incr_addr > 0 || incr_line > 0) {
|
|
|
|
int trunc_addr = incr_addr;
|
|
|
|
int trunc_line = incr_line;
|
|
|
|
if (trunc_addr > 255)
|
|
|
|
trunc_addr = 255;
|
|
|
|
if (trunc_line > 255)
|
|
|
|
trunc_line = 255;
|
|
|
|
com_add_lnotab(c, trunc_addr, trunc_line);
|
|
|
|
incr_addr -= trunc_addr;
|
|
|
|
incr_line -= trunc_line;
|
|
|
|
}
|
|
|
|
c->c_last_addr = c->c_nexti;
|
|
|
|
c->c_last_line = lineno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addoparg(struct compiling *c, int op, int arg)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-03-03 15:13:14 -04:00
|
|
|
if (op == SET_LINENO) {
|
1997-01-23 23:43:35 -04:00
|
|
|
com_set_lineno(c, arg);
|
1997-03-03 15:13:14 -04:00
|
|
|
if (Py_OptimizeFlag)
|
|
|
|
return;
|
1997-01-23 23:43:35 -04:00
|
|
|
}
|
1997-03-03 15:13:14 -04:00
|
|
|
com_addbyte(c, op);
|
|
|
|
com_addint(c, arg);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addfwref(struct compiling *c, int op, int *p_anchor)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
/* 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
|
2000-07-22 15:47:25 -03:00
|
|
|
com_backpatch(struct compiling *c, int anchor)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
|
1990-11-18 13:27:39 -04:00
|
|
|
int target = c->c_nexti;
|
|
|
|
int dist;
|
|
|
|
int prev;
|
|
|
|
for (;;) {
|
|
|
|
/* Make the JUMP instruction at anchor point to target */
|
1990-11-18 13:35:03 -04:00
|
|
|
prev = code[anchor] + (code[anchor+1] << 8);
|
|
|
|
dist = target - (anchor+2);
|
|
|
|
code[anchor] = dist & 0xff;
|
|
|
|
code[anchor+1] = dist >> 8;
|
1990-11-18 13:27:39 -04:00
|
|
|
if (!prev)
|
|
|
|
break;
|
|
|
|
anchor -= prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1991-12-31 09:13:35 -04:00
|
|
|
/* Handle literals and names uniformly */
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-04-28 13:42:25 -03:00
|
|
|
PyObject *w, *t, *np=NULL;
|
|
|
|
long n;
|
|
|
|
|
|
|
|
t = Py_BuildValue("(OO)", v, v->ob_type);
|
|
|
|
if (t == NULL)
|
|
|
|
goto fail;
|
|
|
|
w = PyDict_GetItem(dict, t);
|
|
|
|
if (w != NULL) {
|
|
|
|
n = PyInt_AsLong(w);
|
|
|
|
} else {
|
|
|
|
n = PyList_Size(list);
|
|
|
|
np = PyInt_FromLong(n);
|
|
|
|
if (np == NULL)
|
|
|
|
goto fail;
|
|
|
|
if (PyList_Append(list, v) != 0)
|
|
|
|
goto fail;
|
|
|
|
if (PyDict_SetItem(dict, t, np) != 0)
|
|
|
|
goto fail;
|
|
|
|
Py_DECREF(np);
|
1990-11-18 13:35:03 -04:00
|
|
|
}
|
2000-04-28 13:42:25 -03:00
|
|
|
Py_DECREF(t);
|
1990-11-18 13:35:03 -04:00
|
|
|
return n;
|
2000-04-28 13:42:25 -03:00
|
|
|
fail:
|
|
|
|
Py_XDECREF(np);
|
|
|
|
Py_XDECREF(t);
|
|
|
|
c->c_errors++;
|
|
|
|
return 0;
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addconst(struct compiling *c, PyObject *v)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-04-28 13:42:25 -03:00
|
|
|
return com_add(c, c->c_consts, c->c_const_dict, v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addname(struct compiling *c, PyObject *v)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-04-28 13:42:25 -03:00
|
|
|
return com_add(c, c->c_names, c->c_name_dict, v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1996-08-24 03:21:31 -03:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_mangle(struct compiling *c, char *name, char *buffer, size_t maxlen)
|
1996-08-24 03:21:31 -03:00
|
|
|
{
|
1996-08-24 04:54:27 -03:00
|
|
|
/* Name mangling: __private becomes _classname__private.
|
1996-08-24 03:21:31 -03:00
|
|
|
This is independent from how the name is used. */
|
|
|
|
char *p;
|
2000-06-28 19:07:35 -03:00
|
|
|
size_t nlen, plen;
|
1996-08-24 03:21:31 -03:00
|
|
|
nlen = strlen(name);
|
1996-08-24 04:54:27 -03:00
|
|
|
if (nlen+2 >= maxlen)
|
1996-08-24 03:21:31 -03:00
|
|
|
return 0; /* Don't mangle __extremely_long_names */
|
|
|
|
if (name[nlen-1] == '_' && name[nlen-2] == '_')
|
|
|
|
return 0; /* Don't mangle __whatever__ */
|
|
|
|
p = c->c_private;
|
|
|
|
/* Strip leading underscores from class name */
|
|
|
|
while (*p == '_')
|
|
|
|
p++;
|
|
|
|
if (*p == '\0')
|
|
|
|
return 0; /* Don't mangle if class is just underscores */
|
|
|
|
plen = strlen(p);
|
|
|
|
if (plen + nlen >= maxlen)
|
1996-08-24 04:54:27 -03:00
|
|
|
plen = maxlen-nlen-2; /* Truncate class name if too long */
|
|
|
|
/* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
|
1996-08-24 03:21:31 -03:00
|
|
|
buffer[0] = '_';
|
|
|
|
strncpy(buffer+1, p, plen);
|
1996-08-24 04:54:27 -03:00
|
|
|
strcpy(buffer+1+plen, name);
|
1996-08-24 03:21:31 -03:00
|
|
|
/* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addopnamestr(struct compiling *c, int op, char *name)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1990-11-18 13:27:39 -04:00
|
|
|
int i;
|
1996-08-24 03:21:31 -03:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
char buffer[256];
|
|
|
|
if (name != NULL && name[0] == '_' && name[1] == '_' &&
|
|
|
|
c->c_private != NULL &&
|
2000-06-28 19:07:35 -03:00
|
|
|
com_mangle(c, name, buffer, sizeof(buffer)))
|
1996-08-24 03:21:31 -03:00
|
|
|
name = buffer;
|
|
|
|
#endif
|
1997-01-18 04:02:57 -04:00
|
|
|
if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
|
1990-11-18 13:27:39 -04:00
|
|
|
c->c_errors++;
|
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addname(c, v);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
1991-12-10 09:53:51 -04:00
|
|
|
/* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
|
|
|
|
switch (op) {
|
|
|
|
case LOAD_NAME:
|
|
|
|
case STORE_NAME:
|
|
|
|
case DELETE_NAME:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyDict_GetItemString(c->c_globals, name) != NULL) {
|
1991-12-10 09:53:51 -04:00
|
|
|
switch (op) {
|
|
|
|
case LOAD_NAME: op = LOAD_GLOBAL; break;
|
|
|
|
case STORE_NAME: op = STORE_GLOBAL; break;
|
|
|
|
case DELETE_NAME: op = DELETE_GLOBAL; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, op, i);
|
|
|
|
}
|
|
|
|
|
1995-01-07 08:41:23 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addopname(struct compiling *c, int op, node *n)
|
1995-01-07 08:41:23 -04:00
|
|
|
{
|
|
|
|
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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_MemoryError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"dotted_name too long");
|
1995-02-17 11:04:57 -04:00
|
|
|
name = NULL;
|
1995-01-07 08:41:23 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p != buffer)
|
|
|
|
*p++ = '.';
|
|
|
|
strcpy(p, s);
|
|
|
|
p = strchr(p, '\0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
REQ(n, NAME);
|
|
|
|
name = STR(n);
|
|
|
|
}
|
|
|
|
com_addopnamestr(c, op, name);
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
parsenumber(struct compiling *co, char *s)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-07-09 00:09:57 -03:00
|
|
|
extern double atof(const char *);
|
1991-12-10 09:53:51 -04:00
|
|
|
char *end;
|
1990-11-18 13:27:39 -04:00
|
|
|
long x;
|
1997-02-14 18:58:07 -04:00
|
|
|
double dx;
|
1996-01-11 21:13:16 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
1996-07-20 23:27:43 -03:00
|
|
|
Py_complex c;
|
1996-01-11 21:13:16 -04:00
|
|
|
int imflag;
|
|
|
|
#endif
|
|
|
|
|
1991-04-04 06:42:56 -04:00
|
|
|
errno = 0;
|
1991-12-10 09:53:51 -04:00
|
|
|
end = s + strlen(s) - 1;
|
1996-01-11 21:13:16 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
1996-01-26 16:53:56 -04:00
|
|
|
imflag = *end == 'j' || *end == 'J';
|
1996-01-11 21:13:16 -04:00
|
|
|
#endif
|
1992-03-27 13:28:26 -04:00
|
|
|
if (*end == 'l' || *end == 'L')
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyLong_FromString(s, (char **)0, 0);
|
1992-01-19 12:33:51 -04:00
|
|
|
if (s[0] == '0')
|
1997-04-29 17:08:16 -03:00
|
|
|
x = (long) PyOS_strtoul(s, &end, 0);
|
1992-01-19 12:33:51 -04:00
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
x = PyOS_strtol(s, &end, 0);
|
1991-04-04 06:42:56 -04:00
|
|
|
if (*end == '\0') {
|
|
|
|
if (errno != 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(co, PyExc_OverflowError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"integer literal too large");
|
1991-04-04 06:42:56 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyInt_FromLong(x);
|
1991-04-04 06:42:56 -04:00
|
|
|
}
|
1994-08-29 09:16:12 -03:00
|
|
|
/* XXX Huge floats may silently fail */
|
1996-01-11 21:13:16 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
|
|
|
if (imflag) {
|
|
|
|
c.real = 0.;
|
1997-02-14 18:58:07 -04:00
|
|
|
PyFPE_START_PROTECT("atof", return 0)
|
1996-01-11 21:13:16 -04:00
|
|
|
c.imag = atof(s);
|
1997-03-14 00:32:50 -04:00
|
|
|
PyFPE_END_PROTECT(c)
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyComplex_FromCComplex(c);
|
1996-01-11 21:13:16 -04:00
|
|
|
}
|
1997-10-08 12:23:55 -03:00
|
|
|
else
|
1996-01-11 21:13:16 -04:00
|
|
|
#endif
|
1997-10-08 12:23:55 -03:00
|
|
|
{
|
1997-02-14 18:58:07 -04:00
|
|
|
PyFPE_START_PROTECT("atof", return 0)
|
|
|
|
dx = atof(s);
|
1997-03-14 00:32:50 -04:00
|
|
|
PyFPE_END_PROTECT(dx)
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyFloat_FromDouble(dx);
|
1997-02-14 18:58:07 -04:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
parsestr(char *s)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
2000-06-28 19:07:35 -03:00
|
|
|
size_t len;
|
1990-11-18 13:27:39 -04:00
|
|
|
char *buf;
|
|
|
|
char *p;
|
1994-08-29 09:16:12 -03:00
|
|
|
char *end;
|
1990-11-18 13:27:39 -04:00
|
|
|
int c;
|
1997-04-05 23:41:40 -04:00
|
|
|
int first = *s;
|
|
|
|
int quote = first;
|
2000-03-10 19:01:36 -04:00
|
|
|
int rawmode = 0;
|
|
|
|
int unicode = 0;
|
|
|
|
if (isalpha(quote) || quote == '_') {
|
|
|
|
if (quote == 'u' || quote == 'U') {
|
|
|
|
quote = *++s;
|
|
|
|
unicode = 1;
|
|
|
|
}
|
|
|
|
if (quote == 'r' || quote == 'R') {
|
|
|
|
quote = *++s;
|
|
|
|
rawmode = 1;
|
|
|
|
}
|
|
|
|
}
|
1993-10-26 12:19:44 -03:00
|
|
|
if (quote != '\'' && quote != '\"') {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadInternalCall();
|
1990-11-18 13:27:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
len = strlen(s);
|
2000-06-28 19:07:35 -03:00
|
|
|
if (len > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
1993-10-26 12:19:44 -03:00
|
|
|
if (s[--len] != quote) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadInternalCall();
|
1990-11-18 13:27:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-29 09:16:12 -03:00
|
|
|
if (len >= 4 && s[0] == quote && s[1] == quote) {
|
|
|
|
s += 2;
|
|
|
|
len -= 2;
|
|
|
|
if (s[--len] != quote || s[--len] != quote) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadInternalCall();
|
1994-08-29 09:16:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2000-05-01 14:54:56 -03:00
|
|
|
if (unicode || Py_UnicodeFlag) {
|
2000-03-10 19:01:36 -04:00
|
|
|
if (rawmode)
|
|
|
|
return PyUnicode_DecodeRawUnicodeEscape(
|
|
|
|
s, len, NULL);
|
|
|
|
else
|
|
|
|
return PyUnicode_DecodeUnicodeEscape(
|
|
|
|
s, len, NULL);
|
|
|
|
}
|
|
|
|
else if (rawmode || strchr(s, '\\') == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyString_FromStringAndSize(s, len);
|
2000-03-10 19:01:36 -04:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyString_FromStringAndSize((char *)NULL, len);
|
|
|
|
p = buf = PyString_AsString(v);
|
1994-08-29 09:16:12 -03:00
|
|
|
end = s + len;
|
|
|
|
while (s < end) {
|
1990-11-18 13:27:39 -04:00
|
|
|
if (*s != '\\') {
|
|
|
|
*p++ = *s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
switch (*s++) {
|
|
|
|
/* XXX This assumes ASCII! */
|
1994-08-29 09:16:12 -03:00
|
|
|
case '\n': break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case '\\': *p++ = '\\'; break;
|
|
|
|
case '\'': *p++ = '\''; break;
|
1993-10-26 12:19:44 -03:00
|
|
|
case '\"': *p++ = '\"'; break;
|
1990-11-18 13:27:39 -04:00
|
|
|
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':
|
1995-02-10 13:01:56 -04:00
|
|
|
if (isxdigit(Py_CHARMASK(*s))) {
|
1997-10-20 20:24:07 -03:00
|
|
|
unsigned int x = 0;
|
1990-11-18 13:27:39 -04:00
|
|
|
do {
|
1997-10-20 20:24:07 -03:00
|
|
|
c = Py_CHARMASK(*s);
|
1990-11-18 13:27:39 -04:00
|
|
|
s++;
|
1997-10-20 20:24:07 -03:00
|
|
|
x = (x<<4) & ~0xF;
|
|
|
|
if (isdigit(c))
|
|
|
|
x += c - '0';
|
|
|
|
else if (islower(c))
|
|
|
|
x += 10 + c - 'a';
|
|
|
|
else
|
|
|
|
x += 10 + c - 'A';
|
1995-02-10 13:01:56 -04:00
|
|
|
} while (isxdigit(Py_CHARMASK(*s)));
|
1997-10-20 20:24:07 -03:00
|
|
|
*p++ = x;
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default: *p++ = '\\'; *p++ = s[-1]; break;
|
|
|
|
}
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
_PyString_Resize(&v, (int)(p - buf));
|
1990-11-18 13:27:39 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
parsestrplus(node *n)
|
1995-01-07 08:41:23 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1995-01-07 08:41:23 -04:00
|
|
|
int i;
|
|
|
|
REQ(CHILD(n, 0), STRING);
|
|
|
|
if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
|
|
|
|
/* String literal concatenation */
|
2000-04-13 11:10:44 -03:00
|
|
|
for (i = 1; i < NCH(n); i++) {
|
|
|
|
PyObject *s;
|
|
|
|
s = parsestr(STR(CHILD(n, i)));
|
|
|
|
if (s == NULL)
|
|
|
|
goto onError;
|
|
|
|
if (PyString_Check(v) && PyString_Check(s)) {
|
|
|
|
PyString_ConcatAndDel(&v, s);
|
|
|
|
if (v == NULL)
|
|
|
|
goto onError;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyObject *temp;
|
|
|
|
temp = PyUnicode_Concat(v, s);
|
|
|
|
Py_DECREF(s);
|
|
|
|
if (temp == NULL)
|
|
|
|
goto onError;
|
|
|
|
Py_DECREF(v);
|
|
|
|
v = temp;
|
|
|
|
}
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
2000-04-13 11:10:44 -03:00
|
|
|
|
|
|
|
onError:
|
|
|
|
Py_XDECREF(v);
|
|
|
|
return NULL;
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_list_constructor(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
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);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, len-1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1991-07-27 18:34:52 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_dictmaker(struct compiling *c, node *n)
|
1991-07-27 18:34:52 -03:00
|
|
|
{
|
|
|
|
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);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1991-07-27 18:34:52 -03:00
|
|
|
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);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 3);
|
1991-07-27 18:34:52 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_atom(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
node *ch;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1990-11-18 13:27:39 -04:00
|
|
|
int i;
|
|
|
|
REQ(n, atom);
|
|
|
|
ch = CHILD(n, 0);
|
|
|
|
switch (TYPE(ch)) {
|
|
|
|
case LPAR:
|
1997-01-17 17:04:03 -04:00
|
|
|
if (TYPE(CHILD(n, 1)) == RPAR) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, BUILD_TUPLE, 0);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
else
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
break;
|
|
|
|
case LSQB:
|
1997-01-17 17:04:03 -04:00
|
|
|
if (TYPE(CHILD(n, 1)) == RSQB) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, BUILD_LIST, 0);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
else
|
|
|
|
com_list_constructor(c, CHILD(n, 1));
|
|
|
|
break;
|
1991-07-27 18:34:52 -03:00
|
|
|
case LBRACE: /* '{' [dictmaker] '}' */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, BUILD_MAP, 0);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1991-07-27 18:34:52 -03:00
|
|
|
if (TYPE(CHILD(n, 1)) != RBRACE)
|
|
|
|
com_dictmaker(c, CHILD(n, 1));
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case BACKQUOTE:
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_CONVERT);
|
|
|
|
break;
|
|
|
|
case NUMBER:
|
1996-09-17 11:32:04 -03:00
|
|
|
if ((v = parsenumber(c, STR(ch))) == NULL) {
|
1990-11-18 13:27:39 -04:00
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addconst(c, v);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case STRING:
|
1995-01-07 08:41:23 -04:00
|
|
|
v = parsestrplus(n);
|
1994-08-29 09:16:12 -03:00
|
|
|
if (v == NULL) {
|
1990-11-18 13:27:39 -04:00
|
|
|
c->c_errors++;
|
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addconst(c, v);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case NAME:
|
|
|
|
com_addopname(c, LOAD_NAME, ch);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
default:
|
1997-01-17 17:04:03 -04:00
|
|
|
/* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_atom: unexpected node type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_slice(struct compiling *c, node *n, int op)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
com_node(c, CHILD(n, 2));
|
|
|
|
com_addbyte(c, op+3);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 2);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-06 18:56:52 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_argument(struct compiling *c, node *n, PyObject **pkeywords)
|
1995-07-07 19:53:21 -03:00
|
|
|
{
|
|
|
|
node *m;
|
1997-01-17 17:04:03 -04:00
|
|
|
REQ(n, argument); /* [test '='] test; really [keyword '='] test */
|
1995-07-07 19:53:21 -03:00
|
|
|
if (NCH(n) == 1) {
|
1997-01-06 18:56:52 -04:00
|
|
|
if (*pkeywords != NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1995-07-07 19:53:21 -03:00
|
|
|
"non-keyword arg after keyword arg");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
}
|
1997-01-06 18:56:52 -04:00
|
|
|
return;
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
|
|
|
m = n;
|
|
|
|
do {
|
|
|
|
m = CHILD(m, 0);
|
|
|
|
} while (NCH(m) == 1);
|
|
|
|
if (TYPE(m) != NAME) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"keyword can't be an expression");
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v = PyString_InternFromString(STR(m));
|
1997-01-06 18:56:52 -04:00
|
|
|
if (v != NULL && *pkeywords == NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
*pkeywords = PyDict_New();
|
1997-01-06 18:56:52 -04:00
|
|
|
if (v == NULL || *pkeywords == NULL)
|
1995-07-07 19:53:21 -03:00
|
|
|
c->c_errors++;
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyDict_GetItem(*pkeywords, v) != NULL)
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
1997-01-06 18:56:52 -04:00
|
|
|
"duplicate keyword argument");
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyDict_SetItem(*pkeywords, v, v) != 0)
|
1997-01-06 18:56:52 -04:00
|
|
|
c->c_errors++;
|
1995-07-07 19:53:21 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, v));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
com_node(c, CHILD(n, 2));
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_call_function(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
if (TYPE(n) == RPAR) {
|
1995-07-07 19:53:21 -03:00
|
|
|
com_addoparg(c, CALL_FUNCTION, 0);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *keywords = NULL;
|
1997-01-06 18:56:52 -04:00
|
|
|
int i, na, nk;
|
1998-12-10 12:56:22 -04:00
|
|
|
int lineno = n->n_lineno;
|
2000-03-28 19:49:17 -04:00
|
|
|
int star_flag = 0;
|
|
|
|
int starstar_flag = 0;
|
|
|
|
int opcode;
|
1995-07-07 19:53:21 -03:00
|
|
|
REQ(n, arglist);
|
|
|
|
na = 0;
|
|
|
|
nk = 0;
|
|
|
|
for (i = 0; i < NCH(n); i += 2) {
|
1998-12-10 12:56:22 -04:00
|
|
|
node *ch = CHILD(n, i);
|
2000-03-28 19:49:17 -04:00
|
|
|
if (TYPE(ch) == STAR ||
|
|
|
|
TYPE(ch) == DOUBLESTAR)
|
|
|
|
break;
|
1998-12-10 12:56:22 -04:00
|
|
|
if (ch->n_lineno != lineno) {
|
|
|
|
lineno = ch->n_lineno;
|
|
|
|
com_addoparg(c, SET_LINENO, lineno);
|
|
|
|
}
|
|
|
|
com_argument(c, ch, &keywords);
|
1997-01-06 18:56:52 -04:00
|
|
|
if (keywords == NULL)
|
1995-07-07 19:53:21 -03:00
|
|
|
na++;
|
|
|
|
else
|
|
|
|
nk++;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(keywords);
|
2000-03-28 19:49:17 -04:00
|
|
|
while (i < NCH(n)) {
|
|
|
|
node *tok = CHILD(n, i);
|
|
|
|
node *ch = CHILD(n, i+1);
|
|
|
|
i += 3;
|
|
|
|
switch (TYPE(tok)) {
|
|
|
|
case STAR: star_flag = 1; break;
|
|
|
|
case DOUBLESTAR: starstar_flag = 1; break;
|
|
|
|
}
|
|
|
|
com_node(c, ch);
|
|
|
|
}
|
1995-07-07 19:53:21 -03:00
|
|
|
if (na > 255 || nk > 255) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"more than 255 arguments");
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
2000-03-28 19:49:17 -04:00
|
|
|
if (star_flag || starstar_flag)
|
2000-03-28 20:10:44 -04:00
|
|
|
opcode = CALL_FUNCTION_VAR - 1 +
|
2000-03-28 19:49:17 -04:00
|
|
|
star_flag + (starstar_flag << 1);
|
|
|
|
else
|
|
|
|
opcode = CALL_FUNCTION;
|
|
|
|
com_addoparg(c, opcode, na | (nk << 8));
|
|
|
|
com_pop(c, na + 2*nk + star_flag + starstar_flag);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_select_member(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
com_addopname(c, LOAD_ATTR, n);
|
|
|
|
}
|
|
|
|
|
1996-07-30 13:49:37 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_sliceobj(struct compiling *c, node *n)
|
1996-07-30 13:49:37 -03:00
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
int ns=2; /* number of slice arguments */
|
|
|
|
node *ch;
|
|
|
|
|
|
|
|
/* first argument */
|
|
|
|
if (TYPE(CHILD(n,i)) == COLON) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1996-07-30 13:49:37 -03:00
|
|
|
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++;
|
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1996-07-30 13:49:37 -03:00
|
|
|
/* remaining arguments */
|
|
|
|
for (; i < NCH(n); i++) {
|
|
|
|
ns++;
|
|
|
|
ch=CHILD(n,i);
|
|
|
|
REQ(ch, sliceop);
|
|
|
|
if (NCH(ch) == 1) {
|
|
|
|
/* right argument of ':' missing */
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1996-07-30 13:49:37 -03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
com_node(c, CHILD(ch,1));
|
|
|
|
}
|
|
|
|
com_addoparg(c, BUILD_SLICE, ns);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1 + (ns == 3));
|
1996-07-30 13:49:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_subscript(struct compiling *c, node *n)
|
1996-07-30 13:49:37 -03:00
|
|
|
{
|
|
|
|
node *ch;
|
|
|
|
REQ(n, subscript);
|
|
|
|
ch = CHILD(n,0);
|
|
|
|
/* check for rubber index */
|
1997-01-17 17:04:03 -04:00
|
|
|
if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
|
1996-10-11 13:25:41 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1996-07-30 13:49:37 -03:00
|
|
|
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
|
2000-07-22 15:47:25 -03:00
|
|
|
com_subscriptlist(struct compiling *c, node *n, int assigning)
|
1996-07-30 13:49:37 -03:00
|
|
|
{
|
|
|
|
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.
|
1997-01-17 17:04:03 -04:00
|
|
|
Should have exactly one colon. */
|
1996-07-30 13:49:37 -03:00
|
|
|
if ((TYPE(CHILD(sub, 0)) == COLON
|
|
|
|
|| (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
|
1997-01-17 17:04:03 -04:00
|
|
|
&& (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
|
|
|
|
{
|
1996-07-30 13:49:37 -03:00
|
|
|
if (assigning == OP_APPLY)
|
|
|
|
op = SLICE;
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
op = ((assigning == OP_ASSIGN) ?
|
|
|
|
STORE_SLICE : DELETE_SLICE);
|
1996-07-30 13:49:37 -03:00
|
|
|
com_slice(c, sub, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
if (op == STORE_SLICE)
|
|
|
|
com_pop(c, 2);
|
|
|
|
else if (op == DELETE_SLICE)
|
|
|
|
com_pop(c, 1);
|
1996-07-30 13:49:37 -03:00
|
|
|
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 */
|
1997-01-17 17:04:03 -04:00
|
|
|
if (NCH(n) > 1) {
|
|
|
|
i = (NCH(n)+1) / 2;
|
|
|
|
com_addoparg(c, BUILD_TUPLE, i);
|
|
|
|
com_pop(c, i-1);
|
|
|
|
}
|
|
|
|
if (assigning == OP_APPLY) {
|
1996-07-30 13:49:37 -03:00
|
|
|
op = BINARY_SUBSCR;
|
1997-01-17 17:04:03 -04:00
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
else if (assigning == OP_ASSIGN) {
|
|
|
|
op = STORE_SUBSCR;
|
|
|
|
i = 3;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
op = DELETE_SUBSCR;
|
|
|
|
i = 2;
|
|
|
|
}
|
1996-07-30 13:49:37 -03:00
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, i);
|
1996-07-30 13:49:37 -03:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_apply_trailer(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, trailer);
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case LPAR:
|
|
|
|
com_call_function(c, CHILD(n, 1));
|
|
|
|
break;
|
|
|
|
case DOT:
|
|
|
|
com_select_member(c, CHILD(n, 1));
|
|
|
|
break;
|
|
|
|
case LSQB:
|
1996-07-30 13:49:37 -03:00
|
|
|
com_subscriptlist(c, CHILD(n, 1), OP_APPLY);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_apply_trailer: unknown trailer type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-01-11 21:13:16 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_power(struct compiling *c, node *n)
|
1996-01-11 21:13:16 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
REQ(n, power);
|
|
|
|
com_atom(c, CHILD(n, 0));
|
|
|
|
for (i = 1; i < NCH(n); i++) {
|
|
|
|
if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
|
|
|
|
com_factor(c, CHILD(n, i+1));
|
|
|
|
com_addbyte(c, BINARY_POWER);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1996-01-11 21:13:16 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
com_apply_trailer(c, CHILD(n, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_factor(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, factor);
|
|
|
|
if (TYPE(CHILD(n, 0)) == PLUS) {
|
|
|
|
com_factor(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_POSITIVE);
|
|
|
|
}
|
|
|
|
else if (TYPE(CHILD(n, 0)) == MINUS) {
|
|
|
|
com_factor(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_NEGATIVE);
|
|
|
|
}
|
1991-10-24 11:59:31 -03:00
|
|
|
else if (TYPE(CHILD(n, 0)) == TILDE) {
|
|
|
|
com_factor(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_INVERT);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
else {
|
1996-01-11 21:13:16 -04:00
|
|
|
com_power(c, CHILD(n, 0));
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_term(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, term);
|
|
|
|
com_factor(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_factor(c, CHILD(n, i));
|
|
|
|
switch (TYPE(CHILD(n, i-1))) {
|
|
|
|
case STAR:
|
|
|
|
op = BINARY_MULTIPLY;
|
|
|
|
break;
|
|
|
|
case SLASH:
|
|
|
|
op = BINARY_DIVIDE;
|
|
|
|
break;
|
|
|
|
case PERCENT:
|
|
|
|
op = BINARY_MODULO;
|
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_term: operator not *, / or %");
|
1990-11-18 13:27:39 -04:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_arith_expr(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
1991-10-24 11:59:31 -03:00
|
|
|
REQ(n, arith_expr);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_term(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_term(c, CHILD(n, i));
|
|
|
|
switch (TYPE(CHILD(n, i-1))) {
|
|
|
|
case PLUS:
|
|
|
|
op = BINARY_ADD;
|
|
|
|
break;
|
|
|
|
case MINUS:
|
|
|
|
op = BINARY_SUBTRACT;
|
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_arith_expr: operator not + or -");
|
1991-10-24 11:59:31 -03:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_shift_expr(struct compiling *c, node *n)
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, shift_expr);
|
|
|
|
com_arith_expr(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_arith_expr(c, CHILD(n, i));
|
|
|
|
switch (TYPE(CHILD(n, i-1))) {
|
|
|
|
case LEFTSHIFT:
|
|
|
|
op = BINARY_LSHIFT;
|
|
|
|
break;
|
|
|
|
case RIGHTSHIFT:
|
|
|
|
op = BINARY_RSHIFT;
|
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_shift_expr: operator not << or >>");
|
1991-10-24 11:59:31 -03:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_and_expr(struct compiling *c, node *n)
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, and_expr);
|
|
|
|
com_shift_expr(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_shift_expr(c, CHILD(n, i));
|
|
|
|
if (TYPE(CHILD(n, i-1)) == AMPER) {
|
|
|
|
op = BINARY_AND;
|
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_and_expr: operator not &");
|
1991-10-24 11:59:31 -03:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_xor_expr(struct compiling *c, node *n)
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, xor_expr);
|
|
|
|
com_and_expr(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_and_expr(c, CHILD(n, i));
|
|
|
|
if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
|
|
|
|
op = BINARY_XOR;
|
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_xor_expr: operator not ^");
|
1991-10-24 11:59:31 -03:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_expr(struct compiling *c, node *n)
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, expr);
|
|
|
|
com_xor_expr(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_xor_expr(c, CHILD(n, i));
|
|
|
|
if (TYPE(CHILD(n, i-1)) == VBAR) {
|
|
|
|
op = BINARY_OR;
|
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_expr: expr operator not |");
|
1990-11-18 13:27:39 -04:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum cmp_op
|
2000-07-22 15:47:25 -03:00
|
|
|
cmp_type(node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, comp_op);
|
1991-10-20 17:12:38 -03:00
|
|
|
/* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
|
1990-11-18 13:27:39 -04:00
|
|
|
| 'in' | 'not' 'in' | 'is' | 'is' not' */
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
switch (TYPE(n)) {
|
|
|
|
case LESS: return LT;
|
|
|
|
case GREATER: return GT;
|
1991-10-20 17:12:38 -03:00
|
|
|
case EQEQUAL: /* == */
|
1990-11-18 13:27:39 -04:00
|
|
|
case EQUAL: return EQ;
|
1991-10-20 17:12:38 -03:00
|
|
|
case LESSEQUAL: return LE;
|
|
|
|
case GREATEREQUAL: return GE;
|
|
|
|
case NOTEQUAL: return NE; /* <> or != */
|
1990-11-18 13:27:39 -04:00
|
|
|
case NAME: if (strcmp(STR(n), "in") == 0) return IN;
|
|
|
|
if (strcmp(STR(n), "is") == 0) return IS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (NCH(n) == 2) {
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
|
|
|
|
return NOT_IN;
|
|
|
|
if (strcmp(STR(CHILD(n, 0)), "is") == 0)
|
|
|
|
return IS_NOT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_comparison(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
enum cmp_op op;
|
|
|
|
int anchor;
|
|
|
|
REQ(n, comparison); /* comparison: expr (comp_op expr)* */
|
|
|
|
com_expr(c, CHILD(n, 0));
|
|
|
|
if (NCH(n) == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
The following code is generated for all but the last
|
|
|
|
comparison in a chain:
|
|
|
|
|
|
|
|
label: on stack: opcode: jump to:
|
|
|
|
|
|
|
|
a <code to load b>
|
|
|
|
a, b DUP_TOP
|
|
|
|
a, b, b ROT_THREE
|
|
|
|
b, a, b COMPARE_OP
|
|
|
|
b, 0-or-1 JUMP_IF_FALSE L1
|
|
|
|
b, 1 POP_TOP
|
|
|
|
b
|
|
|
|
|
|
|
|
We are now ready to repeat this sequence for the next
|
|
|
|
comparison in the chain.
|
|
|
|
|
|
|
|
For the last we generate:
|
|
|
|
|
|
|
|
b <code to load c>
|
|
|
|
b, c COMPARE_OP
|
|
|
|
0-or-1
|
|
|
|
|
|
|
|
If there were any jumps to L1 (i.e., there was more than one
|
|
|
|
comparison), we generate:
|
|
|
|
|
|
|
|
0-or-1 JUMP_FORWARD L2
|
|
|
|
L1: b, 0 ROT_TWO
|
|
|
|
0, b POP_TOP
|
|
|
|
0
|
1997-01-17 17:04:03 -04:00
|
|
|
L2: 0-or-1
|
1990-11-18 13:27:39 -04:00
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
anchor = 0;
|
|
|
|
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_expr(c, CHILD(n, i));
|
|
|
|
if (i+2 < NCH(n)) {
|
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, ROT_THREE);
|
|
|
|
}
|
|
|
|
op = cmp_type(CHILD(n, i-1));
|
|
|
|
if (op == BAD) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_comparison: unknown comparison op");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
com_addoparg(c, COMPARE_OP, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (i+2 < NCH(n)) {
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anchor) {
|
|
|
|
int anchor2 = 0;
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &anchor2);
|
|
|
|
com_backpatch(c, anchor);
|
|
|
|
com_addbyte(c, ROT_TWO);
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_backpatch(c, anchor2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_not_test(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, not_test); /* 'not' not_test | comparison */
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_comparison(c, CHILD(n, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_not_test(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_NOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_and_test(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int anchor;
|
|
|
|
REQ(n, and_test); /* not_test ('and' not_test)* */
|
|
|
|
anchor = 0;
|
|
|
|
i = 0;
|
|
|
|
for (;;) {
|
|
|
|
com_not_test(c, CHILD(n, i));
|
|
|
|
if ((i += 2) >= NCH(n))
|
|
|
|
break;
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
if (anchor)
|
|
|
|
com_backpatch(c, anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_test(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-01-17 17:04:03 -04:00
|
|
|
REQ(n, test); /* and_test ('or' and_test)* | lambdef */
|
1993-11-30 10:57:42 -04:00
|
|
|
if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1993-11-30 10:57:42 -04:00
|
|
|
int i;
|
1995-07-18 11:51:37 -03:00
|
|
|
int ndefs = com_argdefs(c, CHILD(n, 0));
|
1997-04-29 17:08:16 -03:00
|
|
|
v = (PyObject *) icompile(CHILD(n, 0), c);
|
1993-11-30 10:57:42 -04:00
|
|
|
if (v == NULL) {
|
|
|
|
c->c_errors++;
|
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addconst(c, v);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1993-11-30 10:57:42 -04:00
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-07-18 11:51:37 -03:00
|
|
|
com_addoparg(c, MAKE_FUNCTION, ndefs);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, ndefs);
|
1993-11-30 10:57:42 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int anchor = 0;
|
|
|
|
int i = 0;
|
|
|
|
for (;;) {
|
|
|
|
com_and_test(c, CHILD(n, i));
|
|
|
|
if ((i += 2) >= NCH(n))
|
|
|
|
break;
|
|
|
|
com_addfwref(c, JUMP_IF_TRUE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1993-11-30 10:57:42 -04:00
|
|
|
}
|
|
|
|
if (anchor)
|
|
|
|
com_backpatch(c, anchor);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_list(struct compiling *c, node *n, int toplevel)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
/* exprlist: expr (',' expr)* [',']; likewise for testlist */
|
1991-12-16 09:05:10 -04:00
|
|
|
if (NCH(n) == 1 && !toplevel) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
len = (NCH(n) + 1) / 2;
|
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
com_addoparg(c, BUILD_TUPLE, len);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, len-1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Begin of assignment compilation */
|
|
|
|
|
2000-07-09 00:09:57 -03:00
|
|
|
static void com_assign_name(struct compiling *, node *, int);
|
|
|
|
static void com_assign(struct compiling *, node *, int);
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assign_attr(struct compiling *c, node *n, int assigning)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, assigning ? 2 : 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assign_trailer(struct compiling *c, node *n, int assigning)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, trailer);
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case LPAR: /* '(' [exprlist] ')' */
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"can't assign to function call");
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case DOT: /* '.' NAME */
|
|
|
|
com_assign_attr(c, CHILD(n, 1), assigning);
|
|
|
|
break;
|
1996-07-30 13:49:37 -03:00
|
|
|
case LSQB: /* '[' subscriptlist ']' */
|
|
|
|
com_subscriptlist(c, CHILD(n, 1), assigning);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError, "unknown trailer type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assign_tuple(struct compiling *c, node *n, int assigning)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (TYPE(n) != testlist)
|
|
|
|
REQ(n, exprlist);
|
1997-01-17 17:04:03 -04:00
|
|
|
if (assigning) {
|
|
|
|
i = (NCH(n)+1)/2;
|
|
|
|
com_addoparg(c, UNPACK_TUPLE, i);
|
|
|
|
com_push(c, i-1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_assign(c, CHILD(n, i), assigning);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assign_list(struct compiling *c, node *n, int assigning)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
1997-01-17 17:04:03 -04:00
|
|
|
if (assigning) {
|
|
|
|
i = (NCH(n)+1)/2;
|
|
|
|
com_addoparg(c, UNPACK_LIST, i);
|
|
|
|
com_push(c, i-1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_assign(c, CHILD(n, i), assigning);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assign_name(struct compiling *c, node *n, int assigning)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, NAME);
|
|
|
|
com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
|
1997-01-17 17:04:03 -04:00
|
|
|
if (assigning)
|
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assign(struct compiling *c, node *n, int assigning)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
/* Loop to avoid trivial recursion */
|
|
|
|
for (;;) {
|
|
|
|
switch (TYPE(n)) {
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
case exprlist:
|
|
|
|
case testlist:
|
|
|
|
if (NCH(n) > 1) {
|
|
|
|
com_assign_tuple(c, n, assigning);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
break;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
case test:
|
|
|
|
case and_test:
|
|
|
|
case not_test:
|
|
|
|
case comparison:
|
|
|
|
case expr:
|
1991-10-24 11:59:31 -03:00
|
|
|
case xor_expr:
|
|
|
|
case and_expr:
|
|
|
|
case shift_expr:
|
|
|
|
case arith_expr:
|
1990-11-18 13:27:39 -04:00
|
|
|
case term:
|
1996-01-11 21:13:16 -04:00
|
|
|
case factor:
|
1990-11-18 13:27:39 -04:00
|
|
|
if (NCH(n) > 1) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to operator");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
break;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1996-01-11 21:13:16 -04:00
|
|
|
case power: /* atom trailer* ('**' power)* */
|
|
|
|
/* ('+'|'-'|'~') factor | atom trailer* */
|
|
|
|
if (TYPE(CHILD(n, 0)) != atom) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to operator");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
1996-01-11 21:13:16 -04:00
|
|
|
if (NCH(n) > 1) { /* trailer or exponent present */
|
1990-11-18 13:27:39 -04:00
|
|
|
int i;
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
for (i = 1; i+1 < NCH(n); i++) {
|
1996-01-11 21:13:16 -04:00
|
|
|
if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to operator");
|
1996-01-11 21:13:16 -04:00
|
|
|
return;
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
com_apply_trailer(c, CHILD(n, i));
|
|
|
|
} /* NB i is still alive */
|
|
|
|
com_assign_trailer(c,
|
|
|
|
CHILD(n, i), assigning);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
break;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
case atom:
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case LPAR:
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
if (TYPE(n) == RPAR) {
|
|
|
|
/* XXX Should allow () = () ??? */
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to ()");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LSQB:
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
if (TYPE(n) == RSQB) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to []");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
com_assign_list(c, n, assigning);
|
|
|
|
return;
|
|
|
|
case NAME:
|
|
|
|
com_assign_name(c, CHILD(n, 0), assigning);
|
|
|
|
return;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to literal");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
1996-08-08 15:51:04 -03:00
|
|
|
|
|
|
|
case lambdef:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"can't assign to lambda");
|
1996-08-08 15:51:04 -03:00
|
|
|
return;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
default:
|
1997-01-17 17:04:03 -04:00
|
|
|
/* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_assign: bad node");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-03-11 14:42:21 -04:00
|
|
|
|
2000-07-09 00:09:57 -03:00
|
|
|
/* Forward */ static node *get_rawdocstring(node *);
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_expr_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1993-10-18 14:06:59 -03:00
|
|
|
REQ(n, expr_stmt); /* testlist ('=' testlist)* */
|
1997-01-17 17:04:03 -04:00
|
|
|
/* Forget it if we have just a doc string here */
|
1997-04-05 23:41:40 -04:00
|
|
|
if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
|
1997-01-17 17:04:03 -04:00
|
|
|
return;
|
1991-07-27 18:34:52 -03:00
|
|
|
com_node(c, CHILD(n, NCH(n)-1));
|
|
|
|
if (NCH(n) == 1) {
|
1994-08-29 09:16:12 -03:00
|
|
|
if (c->c_interactive)
|
|
|
|
com_addbyte(c, PRINT_EXPR);
|
|
|
|
else
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
1991-07-27 18:34:52 -03:00
|
|
|
for (i = 0; i < NCH(n)-2; i+=2) {
|
1997-01-17 17:04:03 -04:00
|
|
|
if (i+2 < NCH(n)-2) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1996-07-30 13:49:37 -03:00
|
|
|
com_assign(c, CHILD(n, i), OP_ASSIGN);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-02 01:24:36 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assert_stmt(struct compiling *c, node *n)
|
1997-04-02 01:24:36 -04:00
|
|
|
{
|
|
|
|
int a = 0, b = 0;
|
|
|
|
int i;
|
|
|
|
REQ(n, assert_stmt); /* 'assert' test [',' test] */
|
|
|
|
/* Generate code like for
|
|
|
|
|
|
|
|
if __debug__:
|
|
|
|
if not <test>:
|
|
|
|
raise AssertionError [, <message>]
|
|
|
|
|
|
|
|
where <message> is the second test, if present.
|
|
|
|
*/
|
|
|
|
if (Py_OptimizeFlag)
|
|
|
|
return;
|
|
|
|
com_addopnamestr(c, LOAD_GLOBAL, "__debug__");
|
|
|
|
com_push(c, 1);
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &a);
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addfwref(c, JUMP_IF_TRUE, &b);
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_pop(c, 1);
|
|
|
|
/* Raise that exception! */
|
|
|
|
com_addopnamestr(c, LOAD_GLOBAL, "AssertionError");
|
|
|
|
com_push(c, 1);
|
|
|
|
i = NCH(n)/2; /* Either 2 or 4 */
|
|
|
|
if (i > 1)
|
|
|
|
com_node(c, CHILD(n, 3));
|
|
|
|
com_addoparg(c, RAISE_VARARGS, i);
|
|
|
|
com_pop(c, i);
|
|
|
|
/* The interpreter does not fall through */
|
|
|
|
/* All jumps converge here */
|
|
|
|
com_backpatch(c, a);
|
|
|
|
com_backpatch(c, b);
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_print_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
1991-07-27 18:34:52 -03:00
|
|
|
REQ(n, print_stmt); /* 'print' (test ',')* [test] */
|
|
|
|
for (i = 1; i < NCH(n); i += 2) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
com_addbyte(c, PRINT_ITEM);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
1991-07-27 18:34:52 -03:00
|
|
|
if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, PRINT_NEWLINE);
|
1990-11-18 13:35:03 -04:00
|
|
|
/* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_return_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1991-07-27 18:34:52 -03:00
|
|
|
REQ(n, return_stmt); /* 'return' [testlist] */
|
1990-12-20 11:06:42 -04:00
|
|
|
if (!c->c_infunction) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError, "'return' outside function");
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
if (NCH(n) < 2) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
else
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_raise_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-01-17 17:04:03 -04:00
|
|
|
int i;
|
1998-04-09 18:39:57 -03:00
|
|
|
REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
|
|
|
|
if (NCH(n) > 1) {
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
if (NCH(n) > 3) {
|
|
|
|
com_node(c, CHILD(n, 3));
|
|
|
|
if (NCH(n) > 5)
|
|
|
|
com_node(c, CHILD(n, 5));
|
|
|
|
}
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
i = NCH(n)/2;
|
|
|
|
com_addoparg(c, RAISE_VARARGS, i);
|
|
|
|
com_pop(c, i);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_import_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
REQ(n, import_stmt);
|
1995-01-07 08:41:23 -04:00
|
|
|
/* 'import' dotted_name (',' dotted_name)* |
|
|
|
|
'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
|
1990-11-18 13:27:39 -04:00
|
|
|
if (STR(CHILD(n, 0))[0] == 'f') {
|
1995-01-07 08:41:23 -04:00
|
|
|
/* 'from' dotted_name 'import' ... */
|
|
|
|
REQ(CHILD(n, 1), dotted_name);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addopname(c, IMPORT_NAME, CHILD(n, 1));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
for (i = 3; i < NCH(n); i += 2)
|
|
|
|
com_addopname(c, IMPORT_FROM, CHILD(n, i));
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
else {
|
1990-11-18 13:35:03 -04:00
|
|
|
/* 'import' ... */
|
1990-11-18 13:27:39 -04:00
|
|
|
for (i = 1; i < NCH(n); i += 2) {
|
1995-01-07 08:41:23 -04:00
|
|
|
REQ(CHILD(n, i), dotted_name);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addopname(c, IMPORT_NAME, CHILD(n, i));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1991-12-10 09:53:51 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_global_stmt(struct compiling *c, node *n)
|
1991-12-10 09:53:51 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
REQ(n, global_stmt);
|
|
|
|
/* 'global' NAME (',' NAME)* */
|
|
|
|
for (i = 1; i < NCH(n); i += 2) {
|
1995-07-18 11:51:37 -03:00
|
|
|
char *s = STR(CHILD(n, i));
|
1996-08-24 04:29:04 -03:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
char buffer[256];
|
|
|
|
if (s != NULL && s[0] == '_' && s[1] == '_' &&
|
|
|
|
c->c_private != NULL &&
|
2000-06-28 19:07:35 -03:00
|
|
|
com_mangle(c, s, buffer, sizeof(buffer)))
|
1996-08-24 04:29:04 -03:00
|
|
|
s = buffer;
|
|
|
|
#endif
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyDict_GetItemString(c->c_locals, s) != NULL) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"name is local and global");
|
1995-07-18 11:51:37 -03:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0)
|
1995-07-18 11:51:37 -03:00
|
|
|
c->c_errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_newlocal_o(struct compiling *c, PyObject *nameval)
|
1995-07-18 11:51:37 -03:00
|
|
|
{
|
|
|
|
int i;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *ival;
|
|
|
|
if (PyList_Size(c->c_varnames) != c->c_nlocals) {
|
1995-07-18 11:51:37 -03:00
|
|
|
/* This is usually caused by an error on a previous call */
|
|
|
|
if (c->c_errors == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"mixed up var name/index");
|
1995-07-18 11:51:37 -03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
ival = PyInt_FromLong(i = c->c_nlocals++);
|
1995-07-18 11:51:37 -03:00
|
|
|
if (ival == NULL)
|
|
|
|
c->c_errors++;
|
1997-04-29 17:08:16 -03:00
|
|
|
else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0)
|
1995-07-18 11:51:37 -03:00
|
|
|
c->c_errors++;
|
1997-04-29 17:08:16 -03:00
|
|
|
else if (PyList_Append(c->c_varnames, nameval) != 0)
|
1995-07-18 11:51:37 -03:00
|
|
|
c->c_errors++;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(ival);
|
1995-07-18 11:51:37 -03:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addlocal_o(struct compiling *c, PyObject *nameval)
|
1995-07-18 11:51:37 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *ival = PyDict_GetItem(c->c_locals, nameval);
|
1995-07-18 11:51:37 -03:00
|
|
|
if (ival != NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyInt_AsLong(ival);
|
1995-07-18 11:51:37 -03:00
|
|
|
return com_newlocal_o(c, nameval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_newlocal(struct compiling *c, char *name)
|
1995-07-18 11:51:37 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *nameval = PyString_InternFromString(name);
|
1995-07-18 11:51:37 -03:00
|
|
|
int i;
|
|
|
|
if (nameval == NULL) {
|
|
|
|
c->c_errors++;
|
|
|
|
return 0;
|
1991-12-10 09:53:51 -04:00
|
|
|
}
|
1995-07-18 11:51:37 -03:00
|
|
|
i = com_newlocal_o(c, nameval);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(nameval);
|
1995-07-18 11:51:37 -03:00
|
|
|
return i;
|
1991-12-10 09:53:51 -04:00
|
|
|
}
|
|
|
|
|
1993-10-18 14:06:59 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_exec_stmt(struct compiling *c, node *n)
|
1993-10-18 14:06:59 -03:00
|
|
|
{
|
|
|
|
REQ(n, exec_stmt);
|
|
|
|
/* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
if (NCH(n) >= 4)
|
|
|
|
com_node(c, CHILD(n, 3));
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1993-10-18 14:06:59 -03:00
|
|
|
if (NCH(n) >= 6)
|
|
|
|
com_node(c, CHILD(n, 5));
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
1993-10-18 14:06:59 -03:00
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1993-10-18 14:06:59 -03:00
|
|
|
com_addbyte(c, EXEC_STMT);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 3);
|
1993-10-18 14:06:59 -03:00
|
|
|
}
|
|
|
|
|
1997-03-11 14:42:21 -04:00
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
is_constant_false(struct compiling *c, node *n)
|
1997-03-11 14:42:21 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1997-03-11 14:42:21 -04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Label to avoid tail recursion */
|
|
|
|
next:
|
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
|
|
|
case suite:
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
/* Fall through */
|
|
|
|
case file_input:
|
|
|
|
for (i = 0; i < NCH(n); i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == stmt) {
|
|
|
|
n = ch;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case stmt:
|
|
|
|
case simple_stmt:
|
|
|
|
case small_stmt:
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
case expr_stmt:
|
|
|
|
case testlist:
|
|
|
|
case test:
|
|
|
|
case and_test:
|
|
|
|
case not_test:
|
|
|
|
case comparison:
|
|
|
|
case expr:
|
|
|
|
case xor_expr:
|
|
|
|
case and_expr:
|
|
|
|
case shift_expr:
|
|
|
|
case arith_expr:
|
|
|
|
case term:
|
|
|
|
case factor:
|
|
|
|
case power:
|
|
|
|
case atom:
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAME:
|
|
|
|
if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NUMBER:
|
|
|
|
v = parsenumber(c, STR(n));
|
|
|
|
if (v == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
1997-03-11 14:42:21 -04:00
|
|
|
break;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
i = PyObject_IsTrue(v);
|
|
|
|
Py_DECREF(v);
|
1997-03-11 14:42:21 -04:00
|
|
|
return i == 0;
|
|
|
|
|
|
|
|
case STRING:
|
1997-03-11 17:25:55 -04:00
|
|
|
v = parsestr(STR(n));
|
1997-03-11 14:42:21 -04:00
|
|
|
if (v == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
1997-03-11 14:42:21 -04:00
|
|
|
break;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
i = PyObject_IsTrue(v);
|
|
|
|
Py_DECREF(v);
|
1997-03-11 14:42:21 -04:00
|
|
|
return i == 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_if_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int anchor = 0;
|
|
|
|
REQ(n, if_stmt);
|
|
|
|
/*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
|
|
|
|
for (i = 0; i+3 < NCH(n); i+=4) {
|
|
|
|
int a = 0;
|
1990-12-20 11:06:42 -04:00
|
|
|
node *ch = CHILD(n, i+1);
|
1997-03-11 14:42:21 -04:00
|
|
|
if (is_constant_false(c, ch))
|
|
|
|
continue;
|
1990-12-20 11:06:42 -04:00
|
|
|
if (i > 0)
|
|
|
|
com_addoparg(c, SET_LINENO, ch->n_lineno);
|
1997-03-11 14:42:21 -04:00
|
|
|
com_node(c, ch);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &a);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, i+3));
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &anchor);
|
|
|
|
com_backpatch(c, a);
|
1997-01-17 17:04:03 -04:00
|
|
|
/* We jump here with an extra entry which we now pop */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
}
|
|
|
|
if (i+2 < NCH(n))
|
|
|
|
com_node(c, CHILD(n, i+2));
|
1997-03-11 14:42:21 -04:00
|
|
|
if (anchor)
|
|
|
|
com_backpatch(c, anchor);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_while_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int break_anchor = 0;
|
|
|
|
int anchor = 0;
|
1991-07-27 18:34:52 -03:00
|
|
|
int save_begin = c->c_begin;
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
|
|
|
|
com_addfwref(c, SETUP_LOOP, &break_anchor);
|
1991-07-27 18:34:52 -03:00
|
|
|
block_push(c, SETUP_LOOP);
|
|
|
|
c->c_begin = c->c_nexti;
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addoparg(c, SET_LINENO, n->n_lineno);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops++;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 3));
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops--;
|
1991-07-27 18:34:52 -03:00
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
c->c_begin = save_begin;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_backpatch(c, anchor);
|
1997-01-17 17:04:03 -04:00
|
|
|
/* We jump here with one entry more on the stack */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_addbyte(c, POP_BLOCK);
|
1991-07-27 18:34:52 -03:00
|
|
|
block_pop(c, SETUP_LOOP);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (NCH(n) > 4)
|
|
|
|
com_node(c, CHILD(n, 6));
|
|
|
|
com_backpatch(c, break_anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_for_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1990-11-18 13:27:39 -04:00
|
|
|
int break_anchor = 0;
|
|
|
|
int anchor = 0;
|
1991-07-27 18:34:52 -03:00
|
|
|
int save_begin = c->c_begin;
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, for_stmt);
|
|
|
|
/* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
|
|
|
|
com_addfwref(c, SETUP_LOOP, &break_anchor);
|
1991-07-27 18:34:52 -03:00
|
|
|
block_push(c, SETUP_LOOP);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 3));
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyInt_FromLong(0L);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (v == NULL)
|
|
|
|
c->c_errors++;
|
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, v));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(v);
|
1991-07-27 18:34:52 -03:00
|
|
|
c->c_begin = c->c_nexti;
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addoparg(c, SET_LINENO, n->n_lineno);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addfwref(c, FOR_LOOP, &anchor);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1996-07-30 13:49:37 -03:00
|
|
|
com_assign(c, CHILD(n, 1), OP_ASSIGN);
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops++;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 5));
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops--;
|
1991-07-27 18:34:52 -03:00
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
c->c_begin = save_begin;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_backpatch(c, anchor);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 2); /* FOR_LOOP has popped these */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, POP_BLOCK);
|
1991-07-27 18:34:52 -03:00
|
|
|
block_pop(c, SETUP_LOOP);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (NCH(n) > 8)
|
|
|
|
com_node(c, CHILD(n, 8));
|
|
|
|
com_backpatch(c, break_anchor);
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:16:12 -03:00
|
|
|
/* Code generated for "try: S finally: Sf" is as follows:
|
1990-11-18 13:35:03 -04:00
|
|
|
|
|
|
|
SETUP_FINALLY L
|
|
|
|
<code for S>
|
|
|
|
POP_BLOCK
|
|
|
|
LOAD_CONST <nil>
|
|
|
|
L: <code for Sf>
|
|
|
|
END_FINALLY
|
|
|
|
|
|
|
|
The special instructions use the block stack. Each block
|
|
|
|
stack entry contains the instruction that created it (here
|
|
|
|
SETUP_FINALLY), the level of the value stack at the time the
|
|
|
|
block stack entry was created, and a label (here L).
|
|
|
|
|
|
|
|
SETUP_FINALLY:
|
|
|
|
Pushes the current value stack level and the label
|
|
|
|
onto the block stack.
|
|
|
|
POP_BLOCK:
|
|
|
|
Pops en entry from the block stack, and pops the value
|
|
|
|
stack until its level is the same as indicated on the
|
|
|
|
block stack. (The label is ignored.)
|
|
|
|
END_FINALLY:
|
1990-12-20 11:06:42 -04:00
|
|
|
Pops a variable number of entries from the *value* stack
|
|
|
|
and re-raises the exception they specify. The number of
|
|
|
|
entries popped depends on the (pseudo) exception type.
|
1990-11-18 13:35:03 -04:00
|
|
|
|
|
|
|
The block stack is unwound when an exception is raised:
|
|
|
|
when a SETUP_FINALLY entry is found, the exception is pushed
|
|
|
|
onto the value stack (and the exception condition is cleared),
|
|
|
|
and the interpreter jumps to the label gotten from the block
|
|
|
|
stack.
|
|
|
|
|
|
|
|
Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
|
1990-12-20 11:06:42 -04:00
|
|
|
(The contents of the value stack is shown in [], with the top
|
|
|
|
at the right; 'tb' is trace-back info, 'val' the exception's
|
|
|
|
associated value, and 'exc' the exception.)
|
1990-11-18 13:35:03 -04:00
|
|
|
|
|
|
|
Value stack Label Instruction Argument
|
|
|
|
[] SETUP_EXCEPT L1
|
|
|
|
[] <code for S>
|
|
|
|
[] POP_BLOCK
|
|
|
|
[] JUMP_FORWARD L0
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
[tb, val, exc] L1: DUP )
|
|
|
|
[tb, val, exc, exc] <evaluate E1> )
|
|
|
|
[tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
|
|
|
|
[tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
|
|
|
|
[tb, val, exc, 1] POP )
|
|
|
|
[tb, val, exc] POP
|
|
|
|
[tb, val] <assign to V1> (or POP if no V1)
|
|
|
|
[tb] POP
|
1990-11-18 13:35:03 -04:00
|
|
|
[] <code for S1>
|
|
|
|
JUMP_FORWARD L0
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
[tb, val, exc, 0] L2: POP
|
|
|
|
[tb, val, exc] DUP
|
1990-11-18 13:35:03 -04:00
|
|
|
.............................etc.......................
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
[tb, val, exc, 0] Ln+1: POP
|
|
|
|
[tb, val, exc] END_FINALLY # re-raise exception
|
1990-11-18 13:35:03 -04:00
|
|
|
|
|
|
|
[] L0: <next statement>
|
|
|
|
|
|
|
|
Of course, parts are not generated if Vi or Ei is not present.
|
|
|
|
*/
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_try_except(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int except_anchor = 0;
|
1994-08-29 09:16:12 -03:00
|
|
|
int end_anchor = 0;
|
|
|
|
int else_anchor = 0;
|
|
|
|
int i;
|
|
|
|
node *ch;
|
1992-04-05 11:24:50 -03:00
|
|
|
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addfwref(c, SETUP_EXCEPT, &except_anchor);
|
|
|
|
block_push(c, SETUP_EXCEPT);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 2));
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addbyte(c, POP_BLOCK);
|
|
|
|
block_pop(c, SETUP_EXCEPT);
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &else_anchor);
|
|
|
|
com_backpatch(c, except_anchor);
|
|
|
|
for (i = 3;
|
|
|
|
i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
|
|
|
|
i += 3) {
|
1997-01-17 17:04:03 -04:00
|
|
|
/* except_clause: 'except' [expr [',' var]] */
|
1994-08-29 09:16:12 -03:00
|
|
|
if (except_anchor == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"default 'except:' must be last");
|
1994-08-29 09:16:12 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
except_anchor = 0;
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 3); /* tb, val, exc pushed by exception */
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addoparg(c, SET_LINENO, ch->n_lineno);
|
|
|
|
if (NCH(ch) > 1) {
|
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
com_node(c, CHILD(ch, 1));
|
|
|
|
com_addoparg(c, COMPARE_OP, EXC_MATCH);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
}
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
if (NCH(ch) > 3)
|
1996-07-30 13:49:37 -03:00
|
|
|
com_assign(c, CHILD(ch, 3), OP_ASSIGN);
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
com_node(c, CHILD(n, i+2));
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &end_anchor);
|
|
|
|
if (except_anchor) {
|
|
|
|
com_backpatch(c, except_anchor);
|
1997-01-17 17:04:03 -04:00
|
|
|
/* We come in with [tb, val, exc, 0] on the
|
|
|
|
stack; one pop and it's the same as
|
|
|
|
expected at the start of the loop */
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addbyte(c, POP_TOP);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
/* We actually come in here with [tb, val, exc] but the
|
|
|
|
END_FINALLY will zap those and jump around.
|
|
|
|
The c_stacklevel does not reflect them so we need not pop
|
|
|
|
anything. */
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addbyte(c, END_FINALLY);
|
|
|
|
com_backpatch(c, else_anchor);
|
|
|
|
if (i < NCH(n))
|
|
|
|
com_node(c, CHILD(n, i+2));
|
|
|
|
com_backpatch(c, end_anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_try_finally(struct compiling *c, node *n)
|
1994-08-29 09:16:12 -03:00
|
|
|
{
|
|
|
|
int finally_anchor = 0;
|
|
|
|
node *ch;
|
|
|
|
|
|
|
|
com_addfwref(c, SETUP_FINALLY, &finally_anchor);
|
|
|
|
block_push(c, SETUP_FINALLY);
|
|
|
|
com_node(c, CHILD(n, 2));
|
|
|
|
com_addbyte(c, POP_BLOCK);
|
|
|
|
block_pop(c, SETUP_FINALLY);
|
|
|
|
block_push(c, END_FINALLY);
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
/* While the generated code pushes only one item,
|
|
|
|
the try-finally handling can enter here with
|
|
|
|
up to three items. OK, here are the details:
|
|
|
|
3 for an exception, 2 for RETURN, 1 for BREAK. */
|
|
|
|
com_push(c, 3);
|
1994-08-29 09:16:12 -03:00
|
|
|
com_backpatch(c, finally_anchor);
|
|
|
|
ch = CHILD(n, NCH(n)-1);
|
|
|
|
com_addoparg(c, SET_LINENO, ch->n_lineno);
|
|
|
|
com_node(c, ch);
|
|
|
|
com_addbyte(c, END_FINALLY);
|
|
|
|
block_pop(c, END_FINALLY);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 3); /* Matches the com_push above */
|
1994-08-29 09:16:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_try_stmt(struct compiling *c, node *n)
|
1994-08-29 09:16:12 -03:00
|
|
|
{
|
|
|
|
REQ(n, try_stmt);
|
|
|
|
/* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
|
|
|
|
| 'try' ':' suite 'finally' ':' suite */
|
|
|
|
if (TYPE(CHILD(n, 3)) != except_clause)
|
|
|
|
com_try_finally(c, n);
|
|
|
|
else
|
|
|
|
com_try_except(c, n);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1997-01-17 17:04:03 -04:00
|
|
|
static node *
|
2000-07-22 15:47:25 -03:00
|
|
|
get_rawdocstring(node *n)
|
1995-01-07 08:41:23 -04:00
|
|
|
{
|
1995-01-25 20:40:09 -04:00
|
|
|
int i;
|
|
|
|
|
1997-01-17 17:04:03 -04:00
|
|
|
/* Label to avoid tail recursion */
|
|
|
|
next:
|
1995-01-07 08:41:23 -04:00
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
|
|
|
case suite:
|
1997-01-17 17:04:03 -04:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
/* Fall through */
|
1995-01-25 20:40:09 -04:00
|
|
|
case file_input:
|
|
|
|
for (i = 0; i < NCH(n); i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
if (TYPE(ch) == stmt) {
|
|
|
|
n = ch;
|
|
|
|
goto next;
|
|
|
|
}
|
1995-01-25 20:40:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1995-01-07 08:41:23 -04:00
|
|
|
case stmt:
|
|
|
|
case simple_stmt:
|
|
|
|
case small_stmt:
|
1997-01-17 17:04:03 -04:00
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
1995-01-07 08:41:23 -04:00
|
|
|
|
|
|
|
case expr_stmt:
|
|
|
|
case testlist:
|
|
|
|
case test:
|
|
|
|
case and_test:
|
|
|
|
case not_test:
|
|
|
|
case comparison:
|
|
|
|
case expr:
|
|
|
|
case xor_expr:
|
|
|
|
case and_expr:
|
|
|
|
case shift_expr:
|
|
|
|
case arith_expr:
|
|
|
|
case term:
|
|
|
|
case factor:
|
1996-01-11 21:13:16 -04:00
|
|
|
case power:
|
1997-01-17 17:04:03 -04:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
|
|
|
}
|
1995-01-07 08:41:23 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case atom:
|
|
|
|
if (TYPE(CHILD(n, 0)) == STRING)
|
1997-01-17 17:04:03 -04:00
|
|
|
return n;
|
1995-01-07 08:41:23 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
get_docstring(node *n)
|
1997-01-17 17:04:03 -04:00
|
|
|
{
|
1999-01-28 11:08:09 -04:00
|
|
|
/* Don't generate doc-strings if run with -OO */
|
|
|
|
if (Py_OptimizeFlag > 1)
|
|
|
|
return NULL;
|
1997-01-17 17:04:03 -04:00
|
|
|
n = get_rawdocstring(n);
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
|
|
|
return parsestrplus(n);
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_suite(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, suite);
|
|
|
|
/* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < NCH(n); i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == stmt)
|
|
|
|
com_node(c, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1992-03-27 13:28:26 -04:00
|
|
|
/* ARGSUSED */
|
1991-07-27 18:34:52 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_continue_stmt(struct compiling *c, node *n)
|
1991-07-27 18:34:52 -03:00
|
|
|
{
|
|
|
|
int i = c->c_nblocks;
|
|
|
|
if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
|
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'continue' not properly in loop");
|
1991-07-27 18:34:52 -03:00
|
|
|
}
|
|
|
|
/* XXX Could allow it inside a 'finally' clause
|
|
|
|
XXX if we could pop the exception still on the stack */
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:16:12 -03:00
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_argdefs(struct compiling *c, node *n)
|
1994-08-29 09:16:12 -03:00
|
|
|
{
|
1995-07-18 11:51:37 -03:00
|
|
|
int i, nch, nargs, ndefs;
|
1994-08-29 09:16:12 -03:00
|
|
|
if (TYPE(n) == lambdef) {
|
|
|
|
/* lambdef: 'lambda' [varargslist] ':' test */
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
|
|
|
|
n = CHILD(n, 2);
|
|
|
|
REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
}
|
|
|
|
if (TYPE(n) != varargslist)
|
1995-07-18 11:51:37 -03:00
|
|
|
return 0;
|
1994-08-29 09:16:12 -03:00
|
|
|
/* varargslist:
|
1995-07-18 11:51:37 -03:00
|
|
|
(fpdef ['=' test] ',')* '*' ....... |
|
1994-08-29 09:16:12 -03:00
|
|
|
fpdef ['=' test] (',' fpdef ['=' test])* [','] */
|
|
|
|
nch = NCH(n);
|
|
|
|
nargs = 0;
|
|
|
|
ndefs = 0;
|
|
|
|
for (i = 0; i < nch; i++) {
|
|
|
|
int t;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (TYPE(CHILD(n, i)) == STAR ||
|
|
|
|
TYPE(CHILD(n, i)) == DOUBLESTAR)
|
1995-07-07 19:53:21 -03:00
|
|
|
break;
|
1994-08-29 09:16:12 -03:00
|
|
|
nargs++;
|
|
|
|
i++;
|
|
|
|
if (i >= nch)
|
1995-09-18 18:44:04 -03:00
|
|
|
t = RPAR; /* Anything except EQUAL or COMMA */
|
|
|
|
else
|
|
|
|
t = TYPE(CHILD(n, i));
|
1994-08-29 09:16:12 -03:00
|
|
|
if (t == EQUAL) {
|
|
|
|
i++;
|
|
|
|
ndefs++;
|
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
i++;
|
|
|
|
if (i >= nch)
|
|
|
|
break;
|
|
|
|
t = TYPE(CHILD(n, i));
|
|
|
|
}
|
|
|
|
else {
|
1998-10-02 10:41:54 -03:00
|
|
|
/* Treat "(a=1, b)" as an error */
|
|
|
|
if (ndefs)
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
1998-10-02 11:06:56 -03:00
|
|
|
"non-default argument follows default argument");
|
1994-08-29 09:16:12 -03:00
|
|
|
}
|
|
|
|
if (t != COMMA)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ndefs;
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_funcdef(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
|
1997-04-29 17:08:16 -03:00
|
|
|
v = (PyObject *)icompile(n, c);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (v == NULL)
|
|
|
|
c->c_errors++;
|
|
|
|
else {
|
|
|
|
int i = com_addconst(c, v);
|
1995-07-18 11:51:37 -03:00
|
|
|
int ndefs = com_argdefs(c, n);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-07-18 11:51:37 -03:00
|
|
|
com_addoparg(c, MAKE_FUNCTION, ndefs);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, ndefs);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addopname(c, STORE_NAME, CHILD(n, 1));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:35:03 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_bases(struct compiling *c, node *n)
|
1991-12-10 09:53:51 -04:00
|
|
|
{
|
1992-03-27 13:28:26 -04:00
|
|
|
int i;
|
1991-12-10 09:53:51 -04:00
|
|
|
REQ(n, testlist);
|
|
|
|
/* testlist: test (',' test)* [','] */
|
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_node(c, CHILD(n, i));
|
1997-01-17 17:04:03 -04:00
|
|
|
i = (NCH(n)+1) / 2;
|
|
|
|
com_addoparg(c, BUILD_TUPLE, i);
|
|
|
|
com_pop(c, i-1);
|
1991-12-10 09:53:51 -04:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_classdef(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1993-05-19 11:50:45 -03:00
|
|
|
int i;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, classdef);
|
1993-05-19 11:50:45 -03:00
|
|
|
/* classdef: class NAME ['(' testlist ')'] ':' suite */
|
1997-01-18 04:02:57 -04:00
|
|
|
if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
|
1993-05-19 11:50:45 -03:00
|
|
|
c->c_errors++;
|
|
|
|
return;
|
1991-12-10 09:53:51 -04:00
|
|
|
}
|
1993-05-19 11:50:45 -03:00
|
|
|
/* Push the class name on the stack */
|
|
|
|
i = com_addconst(c, v);
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1993-05-19 11:50:45 -03:00
|
|
|
/* Push the tuple of base classes on the stack */
|
1997-01-17 17:04:03 -04:00
|
|
|
if (TYPE(CHILD(n, 2)) != LPAR) {
|
1993-05-19 11:50:45 -03:00
|
|
|
com_addoparg(c, BUILD_TUPLE, 0);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1993-05-19 11:50:45 -03:00
|
|
|
else
|
|
|
|
com_bases(c, CHILD(n, 3));
|
1997-04-29 17:08:16 -03:00
|
|
|
v = (PyObject *)icompile(n, c);
|
1990-11-18 13:35:03 -04:00
|
|
|
if (v == NULL)
|
|
|
|
c->c_errors++;
|
|
|
|
else {
|
1993-05-19 11:50:45 -03:00
|
|
|
i = com_addconst(c, v);
|
1990-11-18 13:35:03 -04:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-07-18 11:51:37 -03:00
|
|
|
com_addoparg(c, MAKE_FUNCTION, 0);
|
|
|
|
com_addoparg(c, CALL_FUNCTION, 0);
|
1990-11-18 13:35:03 -04:00
|
|
|
com_addbyte(c, BUILD_CLASS);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 2);
|
1990-11-18 13:35:03 -04:00
|
|
|
com_addopname(c, STORE_NAME, CHILD(n, 1));
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1990-11-18 13:35:03 -04:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_node(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
|
|
|
/* Definition nodes */
|
|
|
|
|
|
|
|
case funcdef:
|
|
|
|
com_funcdef(c, n);
|
|
|
|
break;
|
|
|
|
case classdef:
|
|
|
|
com_classdef(c, n);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Trivial parse tree nodes */
|
|
|
|
|
|
|
|
case stmt:
|
1991-07-27 18:34:52 -03:00
|
|
|
case small_stmt:
|
1990-11-18 13:27:39 -04:00
|
|
|
case flow_stmt:
|
1990-12-20 11:06:42 -04:00
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case simple_stmt:
|
1991-07-27 18:34:52 -03:00
|
|
|
/* small_stmt (';' small_stmt)* [';'] NEWLINE */
|
|
|
|
com_addoparg(c, SET_LINENO, n->n_lineno);
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < NCH(n)-1; i += 2)
|
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
case compound_stmt:
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addoparg(c, SET_LINENO, n->n_lineno);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Statement nodes */
|
|
|
|
|
|
|
|
case expr_stmt:
|
|
|
|
com_expr_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case print_stmt:
|
|
|
|
com_print_stmt(c, n);
|
|
|
|
break;
|
1991-07-27 18:34:52 -03:00
|
|
|
case del_stmt: /* 'del' exprlist */
|
1996-07-30 13:49:37 -03:00
|
|
|
com_assign(c, CHILD(n, 1), OP_DELETE);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case pass_stmt:
|
|
|
|
break;
|
|
|
|
case break_stmt:
|
1990-12-20 11:06:42 -04:00
|
|
|
if (c->c_loops == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'break' outside loop");
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, BREAK_LOOP);
|
|
|
|
break;
|
1991-07-27 18:34:52 -03:00
|
|
|
case continue_stmt:
|
|
|
|
com_continue_stmt(c, n);
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case return_stmt:
|
|
|
|
com_return_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case raise_stmt:
|
|
|
|
com_raise_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case import_stmt:
|
|
|
|
com_import_stmt(c, n);
|
|
|
|
break;
|
1991-12-10 09:53:51 -04:00
|
|
|
case global_stmt:
|
|
|
|
com_global_stmt(c, n);
|
|
|
|
break;
|
1993-10-18 14:06:59 -03:00
|
|
|
case exec_stmt:
|
|
|
|
com_exec_stmt(c, n);
|
|
|
|
break;
|
1997-04-02 01:24:36 -04:00
|
|
|
case assert_stmt:
|
|
|
|
com_assert_stmt(c, n);
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case if_stmt:
|
|
|
|
com_if_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case while_stmt:
|
|
|
|
com_while_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case for_stmt:
|
|
|
|
com_for_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case try_stmt:
|
|
|
|
com_try_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case suite:
|
|
|
|
com_suite(c, n);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Expression nodes */
|
|
|
|
|
|
|
|
case testlist:
|
1991-12-16 09:05:10 -04:00
|
|
|
com_list(c, n, 0);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case test:
|
|
|
|
com_test(c, n);
|
|
|
|
break;
|
|
|
|
case and_test:
|
|
|
|
com_and_test(c, n);
|
|
|
|
break;
|
|
|
|
case not_test:
|
|
|
|
com_not_test(c, n);
|
|
|
|
break;
|
|
|
|
case comparison:
|
|
|
|
com_comparison(c, n);
|
|
|
|
break;
|
|
|
|
case exprlist:
|
1991-12-16 09:05:10 -04:00
|
|
|
com_list(c, n, 0);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case expr:
|
|
|
|
com_expr(c, n);
|
|
|
|
break;
|
1991-10-24 11:59:31 -03:00
|
|
|
case xor_expr:
|
|
|
|
com_xor_expr(c, n);
|
|
|
|
break;
|
|
|
|
case and_expr:
|
|
|
|
com_and_expr(c, n);
|
|
|
|
break;
|
|
|
|
case shift_expr:
|
|
|
|
com_shift_expr(c, n);
|
|
|
|
break;
|
|
|
|
case arith_expr:
|
|
|
|
com_arith_expr(c, n);
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case term:
|
|
|
|
com_term(c, n);
|
|
|
|
break;
|
|
|
|
case factor:
|
|
|
|
com_factor(c, n);
|
|
|
|
break;
|
1996-01-11 21:13:16 -04:00
|
|
|
case power:
|
|
|
|
com_power(c, n);
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case atom:
|
|
|
|
com_atom(c, n);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1997-01-17 17:04:03 -04:00
|
|
|
/* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_node: unexpected node type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-09 00:09:57 -03:00
|
|
|
static void com_fplist(struct compiling *, node *);
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_fpdef(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
|
if (TYPE(CHILD(n, 0)) == LPAR)
|
|
|
|
com_fplist(c, CHILD(n, 1));
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
1995-07-18 11:51:37 -03:00
|
|
|
com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_fplist(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1991-12-31 09:13:35 -04:00
|
|
|
REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
|
1990-11-18 13:27:39 -04:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_fpdef(c, CHILD(n, 0));
|
|
|
|
}
|
|
|
|
else {
|
1997-01-17 17:04:03 -04:00
|
|
|
int i = (NCH(n)+1)/2;
|
|
|
|
com_addoparg(c, UNPACK_TUPLE, i);
|
|
|
|
com_push(c, i-1);
|
1990-11-18 13:27:39 -04:00
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_fpdef(c, CHILD(n, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1992-01-14 14:30:26 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_arglist(struct compiling *c, node *n)
|
1992-01-14 14:30:26 -04:00
|
|
|
{
|
1995-07-18 11:51:37 -03:00
|
|
|
int nch, i;
|
|
|
|
int complex = 0;
|
1997-07-09 22:06:53 -03:00
|
|
|
char nbuf[10];
|
1992-01-14 14:30:26 -04:00
|
|
|
REQ(n, varargslist);
|
1992-01-19 12:33:51 -04:00
|
|
|
/* varargslist:
|
1995-07-18 11:51:37 -03:00
|
|
|
(fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
|
1994-08-29 09:16:12 -03:00
|
|
|
nch = NCH(n);
|
1995-07-18 11:51:37 -03:00
|
|
|
/* Enter all arguments in table of locals */
|
1994-08-29 09:16:12 -03:00
|
|
|
for (i = 0; i < nch; i++) {
|
1995-07-18 11:51:37 -03:00
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
node *fp;
|
|
|
|
char *name;
|
2000-07-13 03:15:04 -03:00
|
|
|
PyObject *nameval;
|
1996-01-11 21:13:16 -04:00
|
|
|
if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
|
1995-07-07 19:53:21 -03:00
|
|
|
break;
|
1995-07-18 11:51:37 -03:00
|
|
|
REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
|
fp = CHILD(ch, 0);
|
|
|
|
if (TYPE(fp) == NAME)
|
|
|
|
name = STR(fp);
|
|
|
|
else {
|
1997-07-09 22:06:53 -03:00
|
|
|
name = nbuf;
|
|
|
|
sprintf(nbuf, ".%d", i);
|
1996-07-20 23:27:43 -03:00
|
|
|
complex = 1;
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
2000-07-13 03:15:04 -03:00
|
|
|
nameval = PyString_InternFromString(name);
|
|
|
|
if (nameval == NULL) {
|
|
|
|
c->c_errors++;
|
|
|
|
}
|
2000-07-13 03:24:29 -03:00
|
|
|
if (PyDict_GetItem(c->c_locals, nameval)) {
|
2000-07-13 03:15:04 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,"duplicate argument in function definition");
|
|
|
|
}
|
|
|
|
com_newlocal_o(c, nameval);
|
|
|
|
Py_DECREF(nameval);
|
1995-07-18 11:51:37 -03:00
|
|
|
c->c_argcount++;
|
|
|
|
if (++i >= nch)
|
1992-01-14 14:30:26 -04:00
|
|
|
break;
|
1995-07-18 11:51:37 -03:00
|
|
|
ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == EQUAL)
|
1994-08-29 09:16:12 -03:00
|
|
|
i += 2;
|
1995-07-18 11:51:37 -03:00
|
|
|
else
|
|
|
|
REQ(ch, COMMA);
|
|
|
|
}
|
|
|
|
/* Handle *arguments */
|
|
|
|
if (i < nch) {
|
|
|
|
node *ch;
|
|
|
|
ch = CHILD(n, i);
|
1996-01-11 21:13:16 -04:00
|
|
|
if (TYPE(ch) != DOUBLESTAR) {
|
|
|
|
REQ(ch, STAR);
|
|
|
|
ch = CHILD(n, i+1);
|
|
|
|
if (TYPE(ch) == NAME) {
|
|
|
|
c->c_flags |= CO_VARARGS;
|
|
|
|
i += 3;
|
|
|
|
com_newlocal(c, STR(ch));
|
|
|
|
}
|
1992-01-14 14:30:26 -04:00
|
|
|
}
|
|
|
|
}
|
1995-07-18 11:51:37 -03:00
|
|
|
/* Handle **keywords */
|
|
|
|
if (i < nch) {
|
|
|
|
node *ch;
|
|
|
|
ch = CHILD(n, i);
|
1996-01-11 21:13:16 -04:00
|
|
|
if (TYPE(ch) != DOUBLESTAR) {
|
|
|
|
REQ(ch, STAR);
|
|
|
|
ch = CHILD(n, i+1);
|
|
|
|
REQ(ch, STAR);
|
|
|
|
ch = CHILD(n, i+2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ch = CHILD(n, i+1);
|
1995-07-18 11:51:37 -03:00
|
|
|
REQ(ch, NAME);
|
|
|
|
c->c_flags |= CO_VARKEYWORDS;
|
|
|
|
com_newlocal(c, STR(ch));
|
|
|
|
}
|
|
|
|
if (complex) {
|
|
|
|
/* Generate code for complex arguments only after
|
|
|
|
having counted the simple arguments */
|
|
|
|
int ilocal = 0;
|
|
|
|
for (i = 0; i < nch; i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
node *fp;
|
1996-01-11 21:13:16 -04:00
|
|
|
if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
|
1994-08-29 09:16:12 -03:00
|
|
|
break;
|
1995-07-18 11:51:37 -03:00
|
|
|
REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
|
fp = CHILD(ch, 0);
|
|
|
|
if (TYPE(fp) != NAME) {
|
|
|
|
com_addoparg(c, LOAD_FAST, ilocal);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-07-18 11:51:37 -03:00
|
|
|
com_fpdef(c, ch);
|
|
|
|
}
|
|
|
|
ilocal++;
|
|
|
|
if (++i >= nch)
|
|
|
|
break;
|
|
|
|
ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == EQUAL)
|
|
|
|
i += 2;
|
|
|
|
else
|
|
|
|
REQ(ch, COMMA);
|
1994-08-29 09:16:12 -03:00
|
|
|
}
|
|
|
|
}
|
1992-01-14 14:30:26 -04:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_file_input(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *doc;
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
|
1995-01-07 08:41:23 -04:00
|
|
|
doc = get_docstring(n);
|
|
|
|
if (doc != NULL) {
|
|
|
|
int i = com_addconst(c, doc);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(doc);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addopnamestr(c, STORE_NAME, "__doc__");
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
for (i = 0; i < NCH(n); i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
|
|
|
|
com_node(c, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Top-level compile-node interface */
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
compile_funcdef(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *doc;
|
1990-11-18 13:27:39 -04:00
|
|
|
node *ch;
|
|
|
|
REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
|
1993-03-29 06:43:31 -04:00
|
|
|
c->c_name = STR(CHILD(n, 1));
|
1995-01-07 08:41:23 -04:00
|
|
|
doc = get_docstring(CHILD(n, 4));
|
|
|
|
if (doc != NULL) {
|
|
|
|
(void) com_addconst(c, doc);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(doc);
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
1995-07-18 11:51:37 -03:00
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
(void) com_addconst(c, Py_None); /* No docstring */
|
1992-01-14 14:30:26 -04:00
|
|
|
ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
|
|
|
|
ch = CHILD(ch, 1); /* ')' | varargslist */
|
1995-07-18 11:51:37 -03:00
|
|
|
if (TYPE(ch) == varargslist)
|
1992-01-14 14:30:26 -04:00
|
|
|
com_arglist(c, ch);
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_infunction = 1;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 4));
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_infunction = 0;
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
compile_lambdef(struct compiling *c, node *n)
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1993-11-30 09:40:46 -04:00
|
|
|
node *ch;
|
1995-07-18 11:51:37 -03:00
|
|
|
REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
|
1994-11-10 18:40:34 -04:00
|
|
|
c->c_name = "<lambda>";
|
1993-11-30 09:40:46 -04:00
|
|
|
|
|
|
|
ch = CHILD(n, 1);
|
1997-04-29 17:08:16 -03:00
|
|
|
(void) com_addconst(c, Py_None); /* No docstring */
|
1995-07-18 11:51:37 -03:00
|
|
|
if (TYPE(ch) == varargslist) {
|
1993-11-30 09:40:46 -04:00
|
|
|
com_arglist(c, ch);
|
1995-07-18 11:51:37 -03:00
|
|
|
ch = CHILD(n, 3);
|
1993-11-30 09:40:46 -04:00
|
|
|
}
|
1995-07-18 11:51:37 -03:00
|
|
|
else
|
|
|
|
ch = CHILD(n, 2);
|
|
|
|
com_node(c, ch);
|
1993-10-26 14:58:25 -03:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1995-01-07 08:41:23 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
compile_classdef(struct compiling *c, node *n)
|
1995-01-07 08:41:23 -04:00
|
|
|
{
|
|
|
|
node *ch;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *doc;
|
1995-01-07 08:41:23 -04:00
|
|
|
REQ(n, classdef);
|
|
|
|
/* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
|
|
|
|
c->c_name = STR(CHILD(n, 1));
|
1996-08-24 03:21:31 -03:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
c->c_private = c->c_name;
|
|
|
|
#endif
|
1995-01-07 08:41:23 -04:00
|
|
|
ch = CHILD(n, NCH(n)-1); /* The suite */
|
|
|
|
doc = get_docstring(ch);
|
|
|
|
if (doc != NULL) {
|
|
|
|
int i = com_addconst(c, doc);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(doc);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addopnamestr(c, STORE_NAME, "__doc__");
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
(void) com_addconst(c, Py_None);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_node(c, ch);
|
|
|
|
com_addbyte(c, LOAD_LOCALS);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
compile_node(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addoparg(c, SET_LINENO, n->n_lineno);
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
1991-01-21 12:09:22 -04:00
|
|
|
case single_input: /* One interactive command */
|
1990-11-18 13:27:39 -04:00
|
|
|
/* NEWLINE | simple_stmt | compound_stmt NEWLINE */
|
1994-08-29 09:16:12 -03:00
|
|
|
c->c_interactive++;
|
1990-11-18 13:27:39 -04:00
|
|
|
n = CHILD(n, 0);
|
|
|
|
if (TYPE(n) != NEWLINE)
|
|
|
|
com_node(c, n);
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
c->c_interactive--;
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
|
1991-01-21 12:09:22 -04:00
|
|
|
case file_input: /* A whole file, or built-in function exec() */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_file_input(c, n);
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
|
1993-11-30 09:40:46 -04:00
|
|
|
case eval_input: /* Built-in function input() */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 0));
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
|
1993-11-30 09:40:46 -04:00
|
|
|
case lambdef: /* anonymous function definition */
|
|
|
|
compile_lambdef(c, n);
|
|
|
|
break;
|
|
|
|
|
1991-01-21 12:09:22 -04:00
|
|
|
case funcdef: /* A function definition */
|
1990-11-18 13:27:39 -04:00
|
|
|
compile_funcdef(c, n);
|
|
|
|
break;
|
|
|
|
|
1991-01-21 12:09:22 -04:00
|
|
|
case classdef: /* A class definition */
|
1995-01-07 08:41:23 -04:00
|
|
|
compile_classdef(c, n);
|
1990-11-18 13:35:03 -04:00
|
|
|
break;
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
default:
|
1997-01-17 17:04:03 -04:00
|
|
|
/* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"compile_node: unexpected node type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-03-30 09:18:41 -04:00
|
|
|
/* Optimization for local variables in functions (and *only* functions).
|
1991-04-04 06:42:56 -04:00
|
|
|
|
1993-03-30 09:18:41 -04:00
|
|
|
This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
|
|
|
|
instructions that refer to local variables with LOAD_FAST etc.
|
|
|
|
The latter instructions are much faster because they don't need to
|
|
|
|
look up the variable name in a dictionary.
|
1994-08-29 09:16:12 -03:00
|
|
|
|
1995-07-18 11:51:37 -03:00
|
|
|
To find all local variables, we check all STORE_NAME, IMPORT_FROM
|
|
|
|
and DELETE_NAME instructions. This yields all local variables,
|
|
|
|
function definitions, class definitions and import statements.
|
|
|
|
Argument names have already been entered into the list by the
|
|
|
|
special processing for the argument list.
|
1994-08-29 09:16:12 -03:00
|
|
|
|
|
|
|
All remaining LOAD_NAME instructions must refer to non-local (global
|
|
|
|
or builtin) variables, so are replaced by LOAD_GLOBAL.
|
|
|
|
|
|
|
|
There are two problems: 'from foo import *' and 'exec' may introduce
|
|
|
|
local variables that we can't know while compiling. If this is the
|
1995-07-18 11:51:37 -03:00
|
|
|
case, we can still optimize bona fide locals (since those
|
|
|
|
statements will be surrounded by fast_2_locals() and
|
|
|
|
locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
|
1994-08-29 09:16:12 -03:00
|
|
|
|
1995-07-18 11:51:37 -03:00
|
|
|
NB: this modifies the string object c->c_code! */
|
1991-04-04 06:42:56 -04:00
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
optimize(struct compiling *c)
|
1991-04-04 06:42:56 -04:00
|
|
|
{
|
1991-04-16 05:39:12 -03:00
|
|
|
unsigned char *next_instr, *cur_instr;
|
1991-04-04 06:42:56 -04:00
|
|
|
int opcode;
|
1996-12-05 19:27:02 -04:00
|
|
|
int oparg = 0;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *name;
|
|
|
|
PyObject *error_type, *error_value, *error_traceback;
|
1993-03-30 09:18:41 -04:00
|
|
|
|
1991-04-04 06:42:56 -04:00
|
|
|
#define NEXTOP() (*next_instr++)
|
|
|
|
#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
|
1997-04-29 17:08:16 -03:00
|
|
|
#define GETITEM(v, i) (PyList_GetItem((v), (i)))
|
1993-03-30 09:18:41 -04:00
|
|
|
#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
1995-07-18 11:51:37 -03:00
|
|
|
|
|
|
|
c->c_flags |= CO_OPTIMIZED;
|
1991-04-04 06:42:56 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
next_instr = (unsigned char *) PyString_AsString(c->c_code);
|
1991-04-04 06:42:56 -04:00
|
|
|
for (;;) {
|
|
|
|
opcode = NEXTOP();
|
|
|
|
if (opcode == STOP_CODE)
|
|
|
|
break;
|
|
|
|
if (HAS_ARG(opcode))
|
|
|
|
oparg = NEXTARG();
|
1995-07-18 11:51:37 -03:00
|
|
|
switch (opcode) {
|
|
|
|
case STORE_NAME:
|
|
|
|
case DELETE_NAME:
|
|
|
|
case IMPORT_FROM:
|
|
|
|
com_addlocal_o(c, GETNAMEOBJ(oparg));
|
|
|
|
break;
|
|
|
|
case EXEC_STMT:
|
|
|
|
c->c_flags &= ~CO_OPTIMIZED;
|
|
|
|
break;
|
1991-04-04 06:42:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyDict_GetItemString(c->c_locals, "*") != NULL)
|
1995-07-18 11:51:37 -03:00
|
|
|
c->c_flags &= ~CO_OPTIMIZED;
|
1993-03-30 09:18:41 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
next_instr = (unsigned char *) PyString_AsString(c->c_code);
|
1991-04-04 06:42:56 -04:00
|
|
|
for (;;) {
|
|
|
|
cur_instr = next_instr;
|
|
|
|
opcode = NEXTOP();
|
|
|
|
if (opcode == STOP_CODE)
|
|
|
|
break;
|
|
|
|
if (HAS_ARG(opcode))
|
|
|
|
oparg = NEXTARG();
|
1993-03-30 09:18:41 -04:00
|
|
|
if (opcode == LOAD_NAME ||
|
|
|
|
opcode == STORE_NAME ||
|
|
|
|
opcode == DELETE_NAME) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1993-03-30 09:18:41 -04:00
|
|
|
int i;
|
1991-04-04 06:42:56 -04:00
|
|
|
name = GETNAMEOBJ(oparg);
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyDict_GetItem(c->c_locals, name);
|
1993-03-30 09:18:41 -04:00
|
|
|
if (v == NULL) {
|
1995-07-18 11:51:37 -03:00
|
|
|
if (opcode == LOAD_NAME &&
|
|
|
|
(c->c_flags&CO_OPTIMIZED))
|
1994-08-29 09:16:12 -03:00
|
|
|
cur_instr[0] = LOAD_GLOBAL;
|
1993-03-30 09:18:41 -04:00
|
|
|
continue;
|
1991-08-16 05:58:43 -03:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
i = PyInt_AsLong(v);
|
1993-03-30 09:18:41 -04:00
|
|
|
switch (opcode) {
|
|
|
|
case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
|
|
|
|
case STORE_NAME: cur_instr[0] = STORE_FAST; break;
|
|
|
|
case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
|
|
|
|
}
|
|
|
|
cur_instr[1] = i & 0xff;
|
|
|
|
cur_instr[2] = (i>>8) & 0xff;
|
1991-04-04 06:42:56 -04:00
|
|
|
}
|
|
|
|
}
|
1993-03-30 09:18:41 -04:00
|
|
|
|
1995-07-18 11:51:37 -03:00
|
|
|
if (c->c_errors == 0)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Restore(error_type, error_value, error_traceback);
|
1991-04-04 06:42:56 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyCodeObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
PyNode_Compile(node *n, char *filename)
|
1996-08-24 03:21:31 -03:00
|
|
|
{
|
|
|
|
return jcompile(n, filename, NULL);
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyCodeObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
icompile(node *n, struct compiling *base)
|
1996-08-24 03:21:31 -03:00
|
|
|
{
|
|
|
|
return jcompile(n, base->c_filename, base);
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyCodeObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
jcompile(node *n, char *filename, struct compiling *base)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
struct compiling sc;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyCodeObject *co;
|
1993-11-30 09:40:46 -04:00
|
|
|
if (!com_init(&sc, filename))
|
1990-11-18 13:27:39 -04:00
|
|
|
return NULL;
|
1996-08-24 03:21:31 -03:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
if (base)
|
|
|
|
sc.c_private = base->c_private;
|
|
|
|
else
|
|
|
|
sc.c_private = NULL;
|
|
|
|
#endif
|
1990-11-18 13:27:39 -04:00
|
|
|
compile_node(&sc, n);
|
|
|
|
com_done(&sc);
|
1995-07-18 11:51:37 -03:00
|
|
|
if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
|
1993-03-30 09:18:41 -04:00
|
|
|
optimize(&sc);
|
1995-07-18 11:51:37 -03:00
|
|
|
sc.c_flags |= CO_NEWLOCALS;
|
|
|
|
}
|
|
|
|
else if (TYPE(n) == classdef)
|
|
|
|
sc.c_flags |= CO_NEWLOCALS;
|
1993-03-29 06:43:31 -04:00
|
|
|
co = NULL;
|
|
|
|
if (sc.c_errors == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *consts, *names, *varnames, *filename, *name;
|
|
|
|
consts = PyList_AsTuple(sc.c_consts);
|
|
|
|
names = PyList_AsTuple(sc.c_names);
|
|
|
|
varnames = PyList_AsTuple(sc.c_varnames);
|
1997-01-18 04:02:57 -04:00
|
|
|
filename = PyString_InternFromString(sc.c_filename);
|
|
|
|
name = PyString_InternFromString(sc.c_name);
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyErr_Occurred())
|
|
|
|
co = PyCode_New(sc.c_argcount,
|
1995-07-18 11:51:37 -03:00
|
|
|
sc.c_nlocals,
|
1997-01-17 17:04:03 -04:00
|
|
|
sc.c_maxstacklevel,
|
1995-07-18 11:51:37 -03:00
|
|
|
sc.c_flags,
|
|
|
|
sc.c_code,
|
|
|
|
consts,
|
|
|
|
names,
|
|
|
|
varnames,
|
|
|
|
filename,
|
1997-01-23 23:43:35 -04:00
|
|
|
name,
|
|
|
|
sc.c_firstlineno,
|
|
|
|
sc.c_lnotab);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(consts);
|
|
|
|
Py_XDECREF(names);
|
|
|
|
Py_XDECREF(varnames);
|
|
|
|
Py_XDECREF(filename);
|
|
|
|
Py_XDECREF(name);
|
1991-06-04 16:41:56 -03:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
com_free(&sc);
|
|
|
|
return co;
|
|
|
|
}
|
1997-01-23 23:43:35 -04:00
|
|
|
|
|
|
|
int
|
2000-07-22 15:47:25 -03:00
|
|
|
PyCode_Addr2Line(PyCodeObject *co, int addrq)
|
1997-01-23 23:43:35 -04:00
|
|
|
{
|
|
|
|
int size = PyString_Size(co->co_lnotab) / 2;
|
1999-09-15 19:48:09 -03:00
|
|
|
unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
|
1997-01-23 23:43:35 -04:00
|
|
|
int line = co->co_firstlineno;
|
|
|
|
int addr = 0;
|
|
|
|
while (--size >= 0) {
|
|
|
|
addr += *p++;
|
|
|
|
if (addr > addrq)
|
|
|
|
break;
|
|
|
|
line += *p++;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|