1991-02-19 12:39:46 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:12:13 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 12:39:46 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 12:39:46 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
/* Compile an expression node to intermediate code */
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
/* XXX TO DO:
|
1997-01-17 21:04:03 +00: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 15:06:42 +00:00
|
|
|
XXX Generate simple jump for break/return outside 'try...finally'
|
1997-01-17 21:04:03 +00:00
|
|
|
XXX Allow 'continue' inside try-finally
|
|
|
|
XXX New opcode for loading the initial index for a for loop
|
1995-07-18 14:51:37 +00:00
|
|
|
XXX other JAR tricks?
|
1990-12-20 15:06:42 +00:00
|
|
|
*/
|
|
|
|
|
1996-08-24 06:21:31 +00:00
|
|
|
#ifndef NO_PRIVATE_NAME_MANGLING
|
|
|
|
#define PRIVATE_NAME_MANGLING
|
|
|
|
#endif
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
#include "Python.h"
|
1990-12-20 15:06:42 +00:00
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
#include "node.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include "graminit.h"
|
|
|
|
#include "compile.h"
|
|
|
|
#include "opcode.h"
|
1990-12-20 15:06:42 +00:00
|
|
|
#include "structmember.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
1991-04-04 10:42:56 +00:00
|
|
|
|
1997-05-07 17:46:13 +00: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 22:18:48 +00:00
|
|
|
#error "single_input has changed -- update Py_single_input in Python.h"
|
1997-05-07 17:46:13 +00:00
|
|
|
#endif
|
|
|
|
#if Py_file_input != file_input
|
1997-05-20 22:18:48 +00:00
|
|
|
#error "file_input has changed -- update Py_file_input in Python.h"
|
1997-05-07 17:46:13 +00:00
|
|
|
#endif
|
|
|
|
#if Py_eval_input != eval_input
|
1997-05-20 22:18:48 +00:00
|
|
|
#error "eval_input has changed -- update Py_eval_input in Python.h"
|
1997-05-07 17:46:13 +00:00
|
|
|
#endif
|
|
|
|
|
1997-03-03 19:13:14 +00:00
|
|
|
int Py_OptimizeFlag = 0;
|
|
|
|
|
1996-07-30 16:49:37 +00:00
|
|
|
#define OP_DELETE 0
|
|
|
|
#define OP_ASSIGN 1
|
|
|
|
#define OP_APPLY 2
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
#define OFF(x) offsetof(PyCodeObject, x)
|
1990-12-20 15:06:42 +00:00
|
|
|
|
|
|
|
static struct memberlist code_memberlist[] = {
|
1995-07-18 14:51:37 +00:00
|
|
|
{"co_argcount", T_INT, OFF(co_argcount), READONLY},
|
|
|
|
{"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
|
1997-01-17 21:04:03 +00:00
|
|
|
{"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
|
1995-07-18 14:51:37 +00:00
|
|
|
{"co_flags", T_INT, OFF(co_flags), READONLY},
|
1992-01-12 02:30:05 +00: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 14:51:37 +00:00
|
|
|
{"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
|
1992-01-12 02:30:05 +00:00
|
|
|
{"co_filename", T_OBJECT, OFF(co_filename), READONLY},
|
1993-03-29 10:43:31 +00:00
|
|
|
{"co_name", T_OBJECT, OFF(co_name), READONLY},
|
1997-01-24 03:43:35 +00:00
|
|
|
{"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
|
|
|
|
{"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
|
1990-12-20 15:06:42 +00:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1990-12-20 15:06:42 +00:00
|
|
|
code_getattr(co, name)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
1990-12-20 15:06:42 +00:00
|
|
|
char *name;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyMember_Get((char *)co, code_memberlist, name);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
|
|
|
|
static void
|
1990-12-20 15:06:42 +00:00
|
|
|
code_dealloc(co)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_XDECREF(co->co_code);
|
|
|
|
Py_XDECREF(co->co_consts);
|
|
|
|
Py_XDECREF(co->co_names);
|
1997-07-25 20:13:49 +00:00
|
|
|
Py_XDECREF(co->co_varnames);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_XDECREF(co->co_filename);
|
|
|
|
Py_XDECREF(co->co_name);
|
1997-07-25 20:13:49 +00:00
|
|
|
Py_XDECREF(co->co_lnotab);
|
1997-04-29 20:08:16 +00:00
|
|
|
PyMem_DEL(co);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1992-09-03 20:50:59 +00:00
|
|
|
code_repr(co)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
1992-09-03 20:50:59 +00:00
|
|
|
{
|
|
|
|
char buf[500];
|
|
|
|
int lineno = -1;
|
1998-10-07 19:42:25 +00:00
|
|
|
unsigned char *p;
|
1992-09-03 20:50:59 +00:00
|
|
|
char *filename = "???";
|
1993-03-29 10:43:31 +00:00
|
|
|
char *name = "???";
|
1998-10-07 19:42:25 +00:00
|
|
|
|
|
|
|
_PyCode_GETCODEPTR(co, &p);
|
1992-09-03 20:50:59 +00:00
|
|
|
if (*p == SET_LINENO)
|
|
|
|
lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
|
1997-04-29 20:08:16 +00: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);
|
1993-03-29 10:43:31 +00:00
|
|
|
sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
|
|
|
|
name, (long)co, filename, lineno);
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyString_FromString(buf);
|
1992-09-03 20:50:59 +00:00
|
|
|
}
|
|
|
|
|
1993-11-05 10:20:10 +00:00
|
|
|
static int
|
|
|
|
code_compare(co, cp)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co, *cp;
|
1993-11-05 10:20:10 +00:00
|
|
|
{
|
|
|
|
int cmp;
|
1997-08-29 17:12:43 +00:00
|
|
|
cmp = co->co_argcount - cp->co_argcount;
|
1995-07-18 14:51:37 +00:00
|
|
|
if (cmp) return cmp;
|
1997-08-29 17:12:43 +00:00
|
|
|
cmp = co->co_nlocals - cp->co_nlocals;
|
1995-07-18 14:51:37 +00:00
|
|
|
if (cmp) return cmp;
|
1997-08-29 17:12:43 +00:00
|
|
|
cmp = co->co_flags - cp->co_flags;
|
1995-07-18 14:51:37 +00:00
|
|
|
if (cmp) return cmp;
|
1998-10-07 19:42:25 +00:00
|
|
|
cmp = PyObject_Compare(co->co_code, cp->co_code);
|
1993-11-05 10:20:10 +00:00
|
|
|
if (cmp) return cmp;
|
1997-04-29 20:08:16 +00:00
|
|
|
cmp = PyObject_Compare(co->co_consts, cp->co_consts);
|
1993-11-05 10:20:10 +00:00
|
|
|
if (cmp) return cmp;
|
1997-04-29 20:08:16 +00:00
|
|
|
cmp = PyObject_Compare(co->co_names, cp->co_names);
|
1995-07-18 14:51:37 +00:00
|
|
|
if (cmp) return cmp;
|
1997-04-29 20:08:16 +00:00
|
|
|
cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
|
1993-11-05 10:20:10 +00:00
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
code_hash(co)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
1993-11-05 10:20:10 +00:00
|
|
|
{
|
1995-07-18 14:51:37 +00:00
|
|
|
long h, h1, h2, h3, h4;
|
1998-10-07 19:42:25 +00:00
|
|
|
h1 = PyObject_Hash(co->co_code);
|
1993-11-05 10:20:10 +00:00
|
|
|
if (h1 == -1) return -1;
|
1997-04-29 20:08:16 +00:00
|
|
|
h2 = PyObject_Hash(co->co_consts);
|
1993-11-05 10:20:10 +00:00
|
|
|
if (h2 == -1) return -1;
|
1997-04-29 20:08:16 +00:00
|
|
|
h3 = PyObject_Hash(co->co_names);
|
1993-11-05 10:20:10 +00:00
|
|
|
if (h3 == -1) return -1;
|
1997-04-29 20:08:16 +00:00
|
|
|
h4 = PyObject_Hash(co->co_varnames);
|
1995-07-18 14:51:37 +00:00
|
|
|
if (h4 == -1) return -1;
|
|
|
|
h = h1 ^ h2 ^ h3 ^ h4 ^
|
|
|
|
co->co_argcount ^ co->co_nlocals ^ co->co_flags;
|
1993-11-05 10:20:10 +00:00
|
|
|
if (h == -1) h = -2;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
PyTypeObject PyCode_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-11-18 17:27:39 +00:00
|
|
|
0,
|
|
|
|
"code",
|
1997-04-29 20:08:16 +00:00
|
|
|
sizeof(PyCodeObject),
|
1990-11-18 17:27:39 +00:00
|
|
|
0,
|
1994-08-29 12:16:12 +00:00
|
|
|
(destructor)code_dealloc, /*tp_dealloc*/
|
1990-11-18 17:27:39 +00:00
|
|
|
0, /*tp_print*/
|
1994-08-29 12:16:12 +00:00
|
|
|
(getattrfunc)code_getattr, /*tp_getattr*/
|
1990-11-18 17:27:39 +00:00
|
|
|
0, /*tp_setattr*/
|
1994-08-29 12:16:12 +00:00
|
|
|
(cmpfunc)code_compare, /*tp_compare*/
|
|
|
|
(reprfunc)code_repr, /*tp_repr*/
|
1990-11-18 17:27:39 +00:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
1994-08-29 12:16:12 +00:00
|
|
|
(hashfunc)code_hash, /*tp_hash*/
|
1990-11-18 17:27:39 +00:00
|
|
|
};
|
|
|
|
|
1997-04-09 19:24:53 +00:00
|
|
|
#define NAME_CHARS \
|
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *
|
|
|
|
PyCode_New(argcount, nlocals, stacksize, flags,
|
1997-01-24 03:43:35 +00:00
|
|
|
code, consts, names, varnames, filename, name,
|
|
|
|
firstlineno, lnotab)
|
1995-07-18 14:51:37 +00:00
|
|
|
int argcount;
|
|
|
|
int nlocals;
|
1997-01-17 21:04:03 +00:00
|
|
|
int stacksize;
|
1995-07-18 14:51:37 +00:00
|
|
|
int flags;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *code;
|
|
|
|
PyObject *consts;
|
|
|
|
PyObject *names;
|
|
|
|
PyObject *varnames;
|
|
|
|
PyObject *filename;
|
|
|
|
PyObject *name;
|
1997-01-24 03:43:35 +00:00
|
|
|
int firstlineno;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *lnotab;
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
1990-11-18 17:27:39 +00:00
|
|
|
int i;
|
1998-10-07 19:42:25 +00:00
|
|
|
PyBufferProcs *pb;
|
1990-11-18 17:27:39 +00:00
|
|
|
/* Check argument types */
|
1995-07-18 14:51:37 +00:00
|
|
|
if (argcount < 0 || nlocals < 0 ||
|
1998-10-07 19:42:25 +00:00
|
|
|
code == NULL ||
|
1997-04-29 20:08:16 +00: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 17:27:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1998-10-07 19:42:25 +00: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 08:02:57 +00:00
|
|
|
/* Make sure names and varnames are all strings, & intern them */
|
1997-04-29 20:08:16 +00: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 12:16:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-18 08:02:57 +00:00
|
|
|
PyString_InternInPlace(&PyTuple_GET_ITEM(names, i));
|
1994-08-29 12:16:12 +00:00
|
|
|
}
|
1997-04-29 20:08:16 +00: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 17:27:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-18 08:02:57 +00:00
|
|
|
PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i));
|
|
|
|
}
|
|
|
|
/* Intern selected string constants */
|
1997-04-29 20:08:16 +00:00
|
|
|
for (i = PyTuple_Size(consts); --i >= 0; ) {
|
|
|
|
PyObject *v = PyTuple_GetItem(consts, i);
|
1997-01-18 08:02:57 +00:00
|
|
|
char *p;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyString_Check(v))
|
1997-01-18 08:02:57 +00:00
|
|
|
continue;
|
1997-04-29 20:08:16 +00:00
|
|
|
p = PyString_AsString(v);
|
1997-04-09 19:24:53 +00:00
|
|
|
if ((int)strspn(p, NAME_CHARS)
|
1997-04-29 20:08:16 +00:00
|
|
|
!= PyString_Size(v))
|
1997-01-18 08:02:57 +00:00
|
|
|
continue;
|
|
|
|
PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
co = PyObject_NEW(PyCodeObject, &PyCode_Type);
|
1990-11-18 17:27:39 +00:00
|
|
|
if (co != NULL) {
|
1995-07-18 14:51:37 +00:00
|
|
|
co->co_argcount = argcount;
|
|
|
|
co->co_nlocals = nlocals;
|
1997-01-17 21:04:03 +00:00
|
|
|
co->co_stacksize = stacksize;
|
1995-07-18 14:51:37 +00:00
|
|
|
co->co_flags = flags;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(code);
|
1998-10-07 19:42:25 +00:00
|
|
|
co->co_code = code;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(consts);
|
1990-11-18 17:27:39 +00:00
|
|
|
co->co_consts = consts;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(names);
|
1990-11-18 17:27:39 +00:00
|
|
|
co->co_names = names;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(varnames);
|
1995-07-18 14:51:37 +00:00
|
|
|
co->co_varnames = varnames;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(filename);
|
1991-06-04 19:41:56 +00:00
|
|
|
co->co_filename = filename;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(name);
|
1993-03-29 10:43:31 +00:00
|
|
|
co->co_name = name;
|
1997-01-24 03:43:35 +00:00
|
|
|
co->co_firstlineno = firstlineno;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(lnotab);
|
1997-01-24 03:43:35 +00:00
|
|
|
co->co_lnotab = lnotab;
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
return co;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Data structure used internally */
|
1991-07-27 21:34:52 +00:00
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
struct compiling {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *c_code; /* string */
|
|
|
|
PyObject *c_consts; /* list of objects */
|
|
|
|
PyObject *c_names; /* list of strings (names) */
|
|
|
|
PyObject *c_globals; /* dictionary (value=None) */
|
|
|
|
PyObject *c_locals; /* dictionary (value=localID) */
|
|
|
|
PyObject *c_varnames; /* list (inverse of c_locals) */
|
1995-07-18 14:51:37 +00: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 17:27:39 +00:00
|
|
|
int c_nexti; /* index into c_code */
|
|
|
|
int c_errors; /* counts errors occurred */
|
1990-12-20 15:06:42 +00:00
|
|
|
int c_infunction; /* set when compiling a function */
|
1994-08-29 12:16:12 +00:00
|
|
|
int c_interactive; /* generating code for interactive command */
|
1990-12-20 15:06:42 +00:00
|
|
|
int c_loops; /* counts nested loops */
|
1991-07-27 21:34:52 +00:00
|
|
|
int c_begin; /* begin of current loop, for 'continue' */
|
1997-01-17 21:04:03 +00:00
|
|
|
int c_block[CO_MAXBLOCKS]; /* stack of block types */
|
1991-07-27 21:34:52 +00:00
|
|
|
int c_nblocks; /* current block stack level */
|
1990-12-20 15:06:42 +00:00
|
|
|
char *c_filename; /* filename of current node */
|
1993-03-29 10:43:31 +00:00
|
|
|
char *c_name; /* name of object (e.g. function) */
|
1996-09-17 14:32:04 +00:00
|
|
|
int c_lineno; /* Current line number */
|
1997-01-17 21:04:03 +00:00
|
|
|
int c_stacklevel; /* Current stack level */
|
|
|
|
int c_maxstacklevel; /* Maximum stack level */
|
1997-01-24 03:43:35 +00:00
|
|
|
int c_firstlineno;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *c_lnotab; /* Table mapping address to line number */
|
1997-01-24 03:43:35 +00:00
|
|
|
int c_last_addr, c_last_line, c_lnotab_next;
|
1996-08-24 06:21:31 +00:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
char *c_private; /* for private name mangling */
|
|
|
|
#endif
|
1990-11-18 17:27:39 +00:00
|
|
|
};
|
|
|
|
|
1991-07-27 21:34:52 +00:00
|
|
|
|
1996-09-17 14:32:04 +00:00
|
|
|
/* Error message including line number */
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_error(c, exc, msg)
|
|
|
|
struct compiling *c;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *exc;
|
1996-09-17 14:32:04 +00:00
|
|
|
char *msg;
|
|
|
|
{
|
|
|
|
int n = strlen(msg);
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1996-09-17 14:32:04 +00:00
|
|
|
char buffer[30];
|
|
|
|
char *s;
|
1997-01-06 22:56:52 +00:00
|
|
|
c->c_errors++;
|
1996-09-17 14:32:04 +00:00
|
|
|
if (c->c_lineno <= 1) {
|
|
|
|
/* Unknown line number or single interactive command */
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetString(exc, msg);
|
1996-09-17 14:32:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sprintf(buffer, " (line %d)", c->c_lineno);
|
1997-04-29 20:08:16 +00:00
|
|
|
v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer));
|
1996-09-17 14:32:04 +00:00
|
|
|
if (v == NULL)
|
|
|
|
return; /* MemoryError, too bad */
|
1997-04-29 20:08:16 +00:00
|
|
|
s = PyString_AS_STRING((PyStringObject *)v);
|
1996-09-17 14:32:04 +00:00
|
|
|
strcpy(s, msg);
|
|
|
|
strcat(s, buffer);
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetObject(exc, v);
|
|
|
|
Py_DECREF(v);
|
1996-09-17 14:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1991-07-27 21:34:52 +00:00
|
|
|
/* Interface to the block stack */
|
|
|
|
|
|
|
|
static void
|
|
|
|
block_push(c, type)
|
|
|
|
struct compiling *c;
|
|
|
|
int type;
|
|
|
|
{
|
1997-01-17 21:04:03 +00:00
|
|
|
if (c->c_nblocks >= CO_MAXBLOCKS) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"too many statically nested blocks");
|
1991-07-27 21:34:52 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
c->c_block[c->c_nblocks++] = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
block_pop(c, type)
|
|
|
|
struct compiling *c;
|
|
|
|
int type;
|
|
|
|
{
|
|
|
|
if (c->c_nblocks > 0)
|
|
|
|
c->c_nblocks--;
|
|
|
|
if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError, "bad block pop");
|
1991-07-27 21:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-07-18 14:51:37 +00:00
|
|
|
/* Prototype forward declarations */
|
1991-07-27 21:34:52 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static int com_init Py_PROTO((struct compiling *, char *));
|
|
|
|
static void com_free Py_PROTO((struct compiling *));
|
|
|
|
static void com_push Py_PROTO((struct compiling *, int));
|
|
|
|
static void com_pop Py_PROTO((struct compiling *, int));
|
|
|
|
static void com_done Py_PROTO((struct compiling *));
|
|
|
|
static void com_node Py_PROTO((struct compiling *, struct _node *));
|
|
|
|
static void com_factor Py_PROTO((struct compiling *, struct _node *));
|
|
|
|
static void com_addbyte Py_PROTO((struct compiling *, int));
|
|
|
|
static void com_addint Py_PROTO((struct compiling *, int));
|
|
|
|
static void com_addoparg Py_PROTO((struct compiling *, int, int));
|
|
|
|
static void com_addfwref Py_PROTO((struct compiling *, int, int *));
|
|
|
|
static void com_backpatch Py_PROTO((struct compiling *, int));
|
|
|
|
static int com_add Py_PROTO((struct compiling *, PyObject *, PyObject *));
|
|
|
|
static int com_addconst Py_PROTO((struct compiling *, PyObject *));
|
|
|
|
static int com_addname Py_PROTO((struct compiling *, PyObject *));
|
|
|
|
static void com_addopname Py_PROTO((struct compiling *, int, node *));
|
|
|
|
static void com_list Py_PROTO((struct compiling *, node *, int));
|
|
|
|
static int com_argdefs Py_PROTO((struct compiling *, node *));
|
|
|
|
static int com_newlocal Py_PROTO((struct compiling *, char *));
|
|
|
|
static PyCodeObject *icompile Py_PROTO((struct _node *, struct compiling *));
|
|
|
|
static PyCodeObject *jcompile Py_PROTO((struct _node *, char *,
|
|
|
|
struct compiling *));
|
|
|
|
static PyObject *parsestrplus Py_PROTO((node *));
|
|
|
|
static PyObject *parsestr Py_PROTO((char *));
|
1990-11-18 17:27:39 +00:00
|
|
|
|
|
|
|
static int
|
1993-11-30 13:40:46 +00:00
|
|
|
com_init(c, filename)
|
1990-11-18 17:27:39 +00:00
|
|
|
struct compiling *c;
|
1990-12-20 15:06:42 +00:00
|
|
|
char *filename;
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
|
|
|
|
1000)) == NULL)
|
1990-11-18 17:27:39 +00:00
|
|
|
goto fail_3;
|
1997-04-29 20:08:16 +00:00
|
|
|
if ((c->c_consts = PyList_New(0)) == NULL)
|
1990-11-18 17:27:39 +00:00
|
|
|
goto fail_2;
|
1997-04-29 20:08:16 +00:00
|
|
|
if ((c->c_names = PyList_New(0)) == NULL)
|
1990-11-18 17:27:39 +00:00
|
|
|
goto fail_1;
|
1997-04-29 20:08:16 +00:00
|
|
|
if ((c->c_globals = PyDict_New()) == NULL)
|
1991-12-10 13:53:51 +00:00
|
|
|
goto fail_0;
|
1997-04-29 20:08:16 +00:00
|
|
|
if ((c->c_locals = PyDict_New()) == NULL)
|
1995-07-18 14:51:37 +00:00
|
|
|
goto fail_00;
|
1997-04-29 20:08:16 +00:00
|
|
|
if ((c->c_varnames = PyList_New(0)) == NULL)
|
1995-07-18 14:51:37 +00:00
|
|
|
goto fail_000;
|
1997-04-29 20:08:16 +00:00
|
|
|
if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
|
|
|
|
1000)) == NULL)
|
1997-01-24 03:43:35 +00:00
|
|
|
goto fail_0000;
|
1995-07-18 14:51:37 +00:00
|
|
|
c->c_nlocals = 0;
|
|
|
|
c->c_argcount = 0;
|
|
|
|
c->c_flags = 0;
|
1990-11-18 17:27:39 +00:00
|
|
|
c->c_nexti = 0;
|
|
|
|
c->c_errors = 0;
|
1990-12-20 15:06:42 +00:00
|
|
|
c->c_infunction = 0;
|
1994-08-29 12:16:12 +00:00
|
|
|
c->c_interactive = 0;
|
1990-12-20 15:06:42 +00:00
|
|
|
c->c_loops = 0;
|
1991-07-27 21:34:52 +00:00
|
|
|
c->c_begin = 0;
|
|
|
|
c->c_nblocks = 0;
|
1990-12-20 15:06:42 +00:00
|
|
|
c->c_filename = filename;
|
1993-03-29 10:43:31 +00:00
|
|
|
c->c_name = "?";
|
1996-09-17 14:32:04 +00:00
|
|
|
c->c_lineno = 0;
|
1997-01-17 21:04:03 +00:00
|
|
|
c->c_stacklevel = 0;
|
|
|
|
c->c_maxstacklevel = 0;
|
1997-01-24 03:43:35 +00:00
|
|
|
c->c_firstlineno = 0;
|
|
|
|
c->c_last_addr = 0;
|
|
|
|
c->c_last_line = 0;
|
|
|
|
c-> c_lnotab_next = 0;
|
1990-11-18 17:27:39 +00:00
|
|
|
return 1;
|
|
|
|
|
1997-01-24 03:43:35 +00:00
|
|
|
fail_0000:
|
1999-12-20 20:40:12 +00:00
|
|
|
Py_DECREF(c->c_varnames);
|
1995-07-18 14:51:37 +00:00
|
|
|
fail_000:
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(c->c_locals);
|
1995-07-18 14:51:37 +00:00
|
|
|
fail_00:
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(c->c_globals);
|
1991-12-10 13:53:51 +00:00
|
|
|
fail_0:
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(c->c_names);
|
1990-11-18 17:27:39 +00:00
|
|
|
fail_1:
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(c->c_consts);
|
1990-11-18 17:27:39 +00:00
|
|
|
fail_2:
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(c->c_code);
|
1990-11-18 17:27:39 +00:00
|
|
|
fail_3:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_free(c)
|
|
|
|
struct compiling *c;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_XDECREF(c->c_code);
|
|
|
|
Py_XDECREF(c->c_consts);
|
|
|
|
Py_XDECREF(c->c_names);
|
|
|
|
Py_XDECREF(c->c_globals);
|
|
|
|
Py_XDECREF(c->c_locals);
|
|
|
|
Py_XDECREF(c->c_varnames);
|
|
|
|
Py_XDECREF(c->c_lnotab);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
1997-01-17 21:04:03 +00:00
|
|
|
static void
|
|
|
|
com_push(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
c->c_stacklevel += n;
|
|
|
|
if (c->c_stacklevel > c->c_maxstacklevel)
|
|
|
|
c->c_maxstacklevel = c->c_stacklevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_pop(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
if (c->c_stacklevel < n) {
|
1998-08-25 18:22:17 +00:00
|
|
|
/* fprintf(stderr,
|
1997-01-17 21:04:03 +00:00
|
|
|
"%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
|
|
|
|
c->c_filename, c->c_lineno,
|
1998-08-25 18:22:17 +00:00
|
|
|
c->c_nexti, c->c_stacklevel, n); */
|
1997-01-17 21:04:03 +00:00
|
|
|
c->c_stacklevel = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c->c_stacklevel -= n;
|
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_done(c)
|
|
|
|
struct compiling *c;
|
|
|
|
{
|
|
|
|
if (c->c_code != NULL)
|
1997-04-29 20:08:16 +00:00
|
|
|
_PyString_Resize(&c->c_code, c->c_nexti);
|
1997-01-24 03:43:35 +00:00
|
|
|
if (c->c_lnotab != NULL)
|
1997-04-29 20:08:16 +00:00
|
|
|
_PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_addbyte(c, byte)
|
|
|
|
struct compiling *c;
|
|
|
|
int byte;
|
|
|
|
{
|
|
|
|
int len;
|
1995-07-18 14:51:37 +00:00
|
|
|
/*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
|
1990-11-18 17:27:39 +00:00
|
|
|
if (byte < 0 || byte > 255) {
|
1991-10-20 20:12:38 +00:00
|
|
|
/*
|
1990-12-20 15:06:42 +00:00
|
|
|
fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
|
1995-01-02 19:04:15 +00:00
|
|
|
fatal("com_addbyte: byte out of range");
|
1991-10-20 20:12:38 +00:00
|
|
|
*/
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_addbyte: byte out of range");
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
if (c->c_code == NULL)
|
|
|
|
return;
|
1997-04-29 20:08:16 +00:00
|
|
|
len = PyString_Size(c->c_code);
|
1990-11-18 17:27:39 +00:00
|
|
|
if (c->c_nexti >= len) {
|
1997-04-29 20:08:16 +00:00
|
|
|
if (_PyString_Resize(&c->c_code, len+1000) != 0) {
|
1990-11-18 17:27:39 +00:00
|
|
|
c->c_errors++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
PyString_AsString(c->c_code)[c->c_nexti++] = byte;
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_addint(c, x)
|
|
|
|
struct compiling *c;
|
|
|
|
int x;
|
|
|
|
{
|
1990-11-18 17:35:03 +00:00
|
|
|
com_addbyte(c, x & 0xff);
|
|
|
|
com_addbyte(c, x >> 8); /* XXX x should be positive */
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
1997-01-24 03:43:35 +00:00
|
|
|
static void
|
|
|
|
com_add_lnotab(c, addr, line)
|
|
|
|
struct compiling *c;
|
|
|
|
int addr;
|
|
|
|
int line;
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
char *p;
|
|
|
|
if (c->c_lnotab == NULL)
|
|
|
|
return;
|
1997-04-29 20:08:16 +00:00
|
|
|
size = PyString_Size(c->c_lnotab);
|
1997-01-24 03:43:35 +00:00
|
|
|
if (c->c_lnotab_next+2 > size) {
|
1997-04-29 20:08:16 +00:00
|
|
|
if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
|
1997-01-24 03:43:35 +00:00
|
|
|
c->c_errors++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
|
1997-01-24 03:43:35 +00:00
|
|
|
*p++ = addr;
|
|
|
|
*p++ = line;
|
|
|
|
c->c_lnotab_next += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_set_lineno(c, lineno)
|
|
|
|
struct compiling *c;
|
|
|
|
int lineno;
|
|
|
|
{
|
|
|
|
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 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_addoparg(c, op, arg)
|
|
|
|
struct compiling *c;
|
|
|
|
int op;
|
|
|
|
int arg;
|
|
|
|
{
|
1997-03-03 19:13:14 +00:00
|
|
|
if (op == SET_LINENO) {
|
1997-01-24 03:43:35 +00:00
|
|
|
com_set_lineno(c, arg);
|
1997-03-03 19:13:14 +00:00
|
|
|
if (Py_OptimizeFlag)
|
|
|
|
return;
|
1997-01-24 03:43:35 +00:00
|
|
|
}
|
1997-03-03 19:13:14 +00:00
|
|
|
com_addbyte(c, op);
|
|
|
|
com_addint(c, arg);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_addfwref(c, op, p_anchor)
|
|
|
|
struct compiling *c;
|
|
|
|
int op;
|
|
|
|
int *p_anchor;
|
|
|
|
{
|
|
|
|
/* Compile a forward reference for backpatching */
|
|
|
|
int here;
|
|
|
|
int anchor;
|
|
|
|
com_addbyte(c, op);
|
|
|
|
here = c->c_nexti;
|
|
|
|
anchor = *p_anchor;
|
|
|
|
*p_anchor = here;
|
|
|
|
com_addint(c, anchor == 0 ? 0 : here - anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_backpatch(c, anchor)
|
|
|
|
struct compiling *c;
|
|
|
|
int anchor; /* Must be nonzero */
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
|
1990-11-18 17:27:39 +00:00
|
|
|
int target = c->c_nexti;
|
|
|
|
int dist;
|
|
|
|
int prev;
|
|
|
|
for (;;) {
|
|
|
|
/* Make the JUMP instruction at anchor point to target */
|
1990-11-18 17:35:03 +00:00
|
|
|
prev = code[anchor] + (code[anchor+1] << 8);
|
|
|
|
dist = target - (anchor+2);
|
|
|
|
code[anchor] = dist & 0xff;
|
|
|
|
code[anchor+1] = dist >> 8;
|
1990-11-18 17:27:39 +00:00
|
|
|
if (!prev)
|
|
|
|
break;
|
|
|
|
anchor -= prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1991-12-31 13:13:35 +00:00
|
|
|
/* Handle literals and names uniformly */
|
1990-11-18 17:27:39 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
com_add(c, list, v)
|
|
|
|
struct compiling *c;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *list;
|
|
|
|
PyObject *v;
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
int n = PyList_Size(list);
|
1990-11-18 17:35:03 +00:00
|
|
|
int i;
|
1998-04-09 21:39:57 +00:00
|
|
|
/* XXX This is quadratic in the number of names per compilation unit.
|
|
|
|
XXX Should use a dictionary. */
|
1990-11-18 17:35:03 +00:00
|
|
|
for (i = n; --i >= 0; ) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *w = PyList_GetItem(list, i);
|
|
|
|
if (v->ob_type == w->ob_type && PyObject_Compare(v, w) == 0)
|
1990-11-18 17:35:03 +00:00
|
|
|
return i;
|
|
|
|
}
|
1997-05-23 00:06:51 +00:00
|
|
|
/* Check for error from PyObject_Compare */
|
|
|
|
if (PyErr_Occurred() || PyList_Append(list, v) != 0)
|
1990-11-18 17:27:39 +00:00
|
|
|
c->c_errors++;
|
1990-11-18 17:35:03 +00:00
|
|
|
return n;
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
com_addconst(c, v)
|
|
|
|
struct compiling *c;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
|
|
|
return com_add(c, c->c_consts, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
com_addname(c, v)
|
|
|
|
struct compiling *c;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
|
|
|
return com_add(c, c->c_names, v);
|
|
|
|
}
|
|
|
|
|
1996-08-24 06:21:31 +00:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
static int
|
|
|
|
com_mangle(c, name, buffer, maxlen)
|
|
|
|
struct compiling *c;
|
|
|
|
char *name;
|
|
|
|
char *buffer;
|
|
|
|
int maxlen;
|
|
|
|
{
|
1996-08-24 07:54:27 +00:00
|
|
|
/* Name mangling: __private becomes _classname__private.
|
1996-08-24 06:21:31 +00:00
|
|
|
This is independent from how the name is used. */
|
|
|
|
char *p;
|
|
|
|
int nlen, plen;
|
|
|
|
nlen = strlen(name);
|
1996-08-24 07:54:27 +00:00
|
|
|
if (nlen+2 >= maxlen)
|
1996-08-24 06:21:31 +00: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 07:54:27 +00:00
|
|
|
plen = maxlen-nlen-2; /* Truncate class name if too long */
|
|
|
|
/* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
|
1996-08-24 06:21:31 +00:00
|
|
|
buffer[0] = '_';
|
|
|
|
strncpy(buffer+1, p, plen);
|
1996-08-24 07:54:27 +00:00
|
|
|
strcpy(buffer+1+plen, name);
|
1996-08-24 06:21:31 +00:00
|
|
|
/* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
1995-01-07 12:41:23 +00:00
|
|
|
com_addopnamestr(c, op, name)
|
1990-11-18 17:27:39 +00:00
|
|
|
struct compiling *c;
|
|
|
|
int op;
|
1995-01-07 12:41:23 +00:00
|
|
|
char *name;
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1990-11-18 17:27:39 +00:00
|
|
|
int i;
|
1996-08-24 06:21:31 +00:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
char buffer[256];
|
|
|
|
if (name != NULL && name[0] == '_' && name[1] == '_' &&
|
|
|
|
c->c_private != NULL &&
|
|
|
|
com_mangle(c, name, buffer, (int)sizeof(buffer)))
|
|
|
|
name = buffer;
|
|
|
|
#endif
|
1997-01-18 08:02:57 +00:00
|
|
|
if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
|
1990-11-18 17:27:39 +00:00
|
|
|
c->c_errors++;
|
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addname(c, v);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(v);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
1991-12-10 13:53:51 +00:00
|
|
|
/* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
|
|
|
|
switch (op) {
|
|
|
|
case LOAD_NAME:
|
|
|
|
case STORE_NAME:
|
|
|
|
case DELETE_NAME:
|
1997-04-29 20:08:16 +00:00
|
|
|
if (PyDict_GetItemString(c->c_globals, name) != NULL) {
|
1991-12-10 13:53:51 +00: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 17:27:39 +00:00
|
|
|
com_addoparg(c, op, i);
|
|
|
|
}
|
|
|
|
|
1995-01-07 12:41:23 +00:00
|
|
|
static void
|
|
|
|
com_addopname(c, op, n)
|
|
|
|
struct compiling *c;
|
|
|
|
int op;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char buffer[1000];
|
|
|
|
/* XXX it is possible to write this code without the 1000
|
|
|
|
chars on the total length of dotted names, I just can't be
|
|
|
|
bothered right now */
|
|
|
|
if (TYPE(n) == STAR)
|
|
|
|
name = "*";
|
|
|
|
else if (TYPE(n) == dotted_name) {
|
|
|
|
char *p = buffer;
|
|
|
|
int i;
|
|
|
|
name = buffer;
|
|
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
|
|
char *s = STR(CHILD(n, i));
|
|
|
|
if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_MemoryError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"dotted_name too long");
|
1995-02-17 15:04:57 +00:00
|
|
|
name = NULL;
|
1995-01-07 12:41:23 +00: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 20:08:16 +00:00
|
|
|
static PyObject *
|
1996-09-17 14:32:04 +00:00
|
|
|
parsenumber(co, s)
|
|
|
|
struct compiling *co;
|
1990-11-18 17:27:39 +00:00
|
|
|
char *s;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
extern double atof Py_PROTO((const char *));
|
1991-12-10 13:53:51 +00:00
|
|
|
char *end;
|
1990-11-18 17:27:39 +00:00
|
|
|
long x;
|
1997-02-14 22:58:07 +00:00
|
|
|
double dx;
|
1996-01-12 01:13:16 +00:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
1996-07-21 02:27:43 +00:00
|
|
|
Py_complex c;
|
1996-01-12 01:13:16 +00:00
|
|
|
int imflag;
|
|
|
|
#endif
|
|
|
|
|
1991-04-04 10:42:56 +00:00
|
|
|
errno = 0;
|
1991-12-10 13:53:51 +00:00
|
|
|
end = s + strlen(s) - 1;
|
1996-01-12 01:13:16 +00:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
1996-01-26 20:53:56 +00:00
|
|
|
imflag = *end == 'j' || *end == 'J';
|
1996-01-12 01:13:16 +00:00
|
|
|
#endif
|
1992-03-27 17:28:26 +00:00
|
|
|
if (*end == 'l' || *end == 'L')
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyLong_FromString(s, (char **)0, 0);
|
1992-01-19 16:33:51 +00:00
|
|
|
if (s[0] == '0')
|
1997-04-29 20:08:16 +00:00
|
|
|
x = (long) PyOS_strtoul(s, &end, 0);
|
1992-01-19 16:33:51 +00:00
|
|
|
else
|
1997-04-29 20:08:16 +00:00
|
|
|
x = PyOS_strtol(s, &end, 0);
|
1991-04-04 10:42:56 +00:00
|
|
|
if (*end == '\0') {
|
|
|
|
if (errno != 0) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(co, PyExc_OverflowError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"integer literal too large");
|
1991-04-04 10:42:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyInt_FromLong(x);
|
1991-04-04 10:42:56 +00:00
|
|
|
}
|
1994-08-29 12:16:12 +00:00
|
|
|
/* XXX Huge floats may silently fail */
|
1996-01-12 01:13:16 +00:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
|
|
|
if (imflag) {
|
|
|
|
c.real = 0.;
|
1997-02-14 22:58:07 +00:00
|
|
|
PyFPE_START_PROTECT("atof", return 0)
|
1996-01-12 01:13:16 +00:00
|
|
|
c.imag = atof(s);
|
1997-03-14 04:32:50 +00:00
|
|
|
PyFPE_END_PROTECT(c)
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyComplex_FromCComplex(c);
|
1996-01-12 01:13:16 +00:00
|
|
|
}
|
1997-10-08 15:23:55 +00:00
|
|
|
else
|
1996-01-12 01:13:16 +00:00
|
|
|
#endif
|
1997-10-08 15:23:55 +00:00
|
|
|
{
|
1997-02-14 22:58:07 +00:00
|
|
|
PyFPE_START_PROTECT("atof", return 0)
|
|
|
|
dx = atof(s);
|
1997-03-14 04:32:50 +00:00
|
|
|
PyFPE_END_PROTECT(dx)
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyFloat_FromDouble(dx);
|
1997-02-14 22:58:07 +00:00
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1990-11-18 17:27:39 +00:00
|
|
|
parsestr(s)
|
|
|
|
char *s;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1990-11-18 17:27:39 +00:00
|
|
|
int len;
|
|
|
|
char *buf;
|
|
|
|
char *p;
|
1994-08-29 12:16:12 +00:00
|
|
|
char *end;
|
1990-11-18 17:27:39 +00:00
|
|
|
int c;
|
1997-04-06 03:41:40 +00:00
|
|
|
int first = *s;
|
|
|
|
int quote = first;
|
|
|
|
if (isalpha(quote) || quote == '_')
|
|
|
|
quote = *++s;
|
1993-10-26 15:19:44 +00:00
|
|
|
if (quote != '\'' && quote != '\"') {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_BadInternalCall();
|
1990-11-18 17:27:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
len = strlen(s);
|
1993-10-26 15:19:44 +00:00
|
|
|
if (s[--len] != quote) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_BadInternalCall();
|
1990-11-18 17:27:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-29 12:16:12 +00:00
|
|
|
if (len >= 4 && s[0] == quote && s[1] == quote) {
|
|
|
|
s += 2;
|
|
|
|
len -= 2;
|
|
|
|
if (s[--len] != quote || s[--len] != quote) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_BadInternalCall();
|
1994-08-29 12:16:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1997-04-06 03:41:40 +00:00
|
|
|
if (first != quote || strchr(s, '\\') == NULL)
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyString_FromStringAndSize(s, len);
|
|
|
|
v = PyString_FromStringAndSize((char *)NULL, len);
|
|
|
|
p = buf = PyString_AsString(v);
|
1994-08-29 12:16:12 +00:00
|
|
|
end = s + len;
|
|
|
|
while (s < end) {
|
1990-11-18 17:27:39 +00:00
|
|
|
if (*s != '\\') {
|
|
|
|
*p++ = *s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
switch (*s++) {
|
|
|
|
/* XXX This assumes ASCII! */
|
1994-08-29 12:16:12 +00:00
|
|
|
case '\n': break;
|
1990-11-18 17:27:39 +00:00
|
|
|
case '\\': *p++ = '\\'; break;
|
|
|
|
case '\'': *p++ = '\''; break;
|
1993-10-26 15:19:44 +00:00
|
|
|
case '\"': *p++ = '\"'; break;
|
1990-11-18 17:27:39 +00: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 17:01:56 +00:00
|
|
|
if (isxdigit(Py_CHARMASK(*s))) {
|
1997-10-20 23:24:07 +00:00
|
|
|
unsigned int x = 0;
|
1990-11-18 17:27:39 +00:00
|
|
|
do {
|
1997-10-20 23:24:07 +00:00
|
|
|
c = Py_CHARMASK(*s);
|
1990-11-18 17:27:39 +00:00
|
|
|
s++;
|
1997-10-20 23:24:07 +00: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 17:01:56 +00:00
|
|
|
} while (isxdigit(Py_CHARMASK(*s)));
|
1997-10-20 23:24:07 +00:00
|
|
|
*p++ = x;
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default: *p++ = '\\'; *p++ = s[-1]; break;
|
|
|
|
}
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
_PyString_Resize(&v, (int)(p - buf));
|
1990-11-18 17:27:39 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-07 12:41:23 +00:00
|
|
|
parsestrplus(n)
|
|
|
|
node *n;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1995-01-07 12:41:23 +00:00
|
|
|
int i;
|
|
|
|
REQ(CHILD(n, 0), STRING);
|
|
|
|
if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
|
|
|
|
/* String literal concatenation */
|
|
|
|
for (i = 1; i < NCH(n) && v != NULL; i++) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyString_ConcatAndDel(&v, parsestr(STR(CHILD(n, i))));
|
1995-01-07 12:41:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_list_constructor(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
if (TYPE(n) != testlist)
|
|
|
|
REQ(n, exprlist);
|
|
|
|
/* exprlist: expr (',' expr)* [',']; likewise for testlist */
|
|
|
|
len = (NCH(n) + 1) / 2;
|
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
com_addoparg(c, BUILD_LIST, len);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, len-1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
1991-07-27 21:34:52 +00:00
|
|
|
static void
|
|
|
|
com_dictmaker(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
/* dictmaker: test ':' test (',' test ':' value)* [','] */
|
|
|
|
for (i = 0; i+2 < NCH(n); i += 4) {
|
|
|
|
/* We must arrange things just right for STORE_SUBSCR.
|
|
|
|
It wants the stack to look like (value) (dict) (key) */
|
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1991-07-27 21:34:52 +00: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 21:04:03 +00:00
|
|
|
com_pop(c, 3);
|
1991-07-27 21:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_atom(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
node *ch;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1990-11-18 17:27:39 +00:00
|
|
|
int i;
|
|
|
|
REQ(n, atom);
|
|
|
|
ch = CHILD(n, 0);
|
|
|
|
switch (TYPE(ch)) {
|
|
|
|
case LPAR:
|
1997-01-17 21:04:03 +00:00
|
|
|
if (TYPE(CHILD(n, 1)) == RPAR) {
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addoparg(c, BUILD_TUPLE, 0);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
else
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
break;
|
|
|
|
case LSQB:
|
1997-01-17 21:04:03 +00:00
|
|
|
if (TYPE(CHILD(n, 1)) == RSQB) {
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addoparg(c, BUILD_LIST, 0);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
else
|
|
|
|
com_list_constructor(c, CHILD(n, 1));
|
|
|
|
break;
|
1991-07-27 21:34:52 +00:00
|
|
|
case LBRACE: /* '{' [dictmaker] '}' */
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addoparg(c, BUILD_MAP, 0);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1991-07-27 21:34:52 +00:00
|
|
|
if (TYPE(CHILD(n, 1)) != RBRACE)
|
|
|
|
com_dictmaker(c, CHILD(n, 1));
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
case BACKQUOTE:
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_CONVERT);
|
|
|
|
break;
|
|
|
|
case NUMBER:
|
1996-09-17 14:32:04 +00:00
|
|
|
if ((v = parsenumber(c, STR(ch))) == NULL) {
|
1990-11-18 17:27:39 +00:00
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addconst(c, v);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(v);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
case STRING:
|
1995-01-07 12:41:23 +00:00
|
|
|
v = parsestrplus(n);
|
1994-08-29 12:16:12 +00:00
|
|
|
if (v == NULL) {
|
1990-11-18 17:27:39 +00:00
|
|
|
c->c_errors++;
|
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addconst(c, v);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(v);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
case NAME:
|
|
|
|
com_addopname(c, LOAD_NAME, ch);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
1997-01-17 21:04:03 +00:00
|
|
|
/* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_atom: unexpected node type");
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_slice(c, n, op)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
int op;
|
|
|
|
{
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_addbyte(c, op);
|
|
|
|
}
|
|
|
|
else if (NCH(n) == 2) {
|
|
|
|
if (TYPE(CHILD(n, 0)) != COLON) {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
com_addbyte(c, op+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, op+2);
|
|
|
|
}
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
com_node(c, CHILD(n, 2));
|
|
|
|
com_addbyte(c, op+3);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 2);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-06 22:56:52 +00:00
|
|
|
static void
|
|
|
|
com_argument(c, n, pkeywords)
|
1995-07-07 22:53:21 +00:00
|
|
|
struct compiling *c;
|
|
|
|
node *n; /* argument */
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject **pkeywords;
|
1995-07-07 22:53:21 +00:00
|
|
|
{
|
|
|
|
node *m;
|
1997-01-17 21:04:03 +00:00
|
|
|
REQ(n, argument); /* [test '='] test; really [keyword '='] test */
|
1995-07-07 22:53:21 +00:00
|
|
|
if (NCH(n) == 1) {
|
1997-01-06 22:56:52 +00:00
|
|
|
if (*pkeywords != NULL) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1995-07-07 22:53:21 +00:00
|
|
|
"non-keyword arg after keyword arg");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
}
|
1997-01-06 22:56:52 +00:00
|
|
|
return;
|
1995-07-07 22:53:21 +00:00
|
|
|
}
|
|
|
|
m = n;
|
|
|
|
do {
|
|
|
|
m = CHILD(m, 0);
|
|
|
|
} while (NCH(m) == 1);
|
|
|
|
if (TYPE(m) != NAME) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"keyword can't be an expression");
|
1995-07-07 22:53:21 +00:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v = PyString_InternFromString(STR(m));
|
1997-01-06 22:56:52 +00:00
|
|
|
if (v != NULL && *pkeywords == NULL)
|
1997-04-29 20:08:16 +00:00
|
|
|
*pkeywords = PyDict_New();
|
1997-01-06 22:56:52 +00:00
|
|
|
if (v == NULL || *pkeywords == NULL)
|
1995-07-07 22:53:21 +00:00
|
|
|
c->c_errors++;
|
|
|
|
else {
|
1997-04-29 20:08:16 +00:00
|
|
|
if (PyDict_GetItem(*pkeywords, v) != NULL)
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
1997-01-06 22:56:52 +00:00
|
|
|
"duplicate keyword argument");
|
|
|
|
else
|
1997-04-29 20:08:16 +00:00
|
|
|
if (PyDict_SetItem(*pkeywords, v, v) != 0)
|
1997-01-06 22:56:52 +00:00
|
|
|
c->c_errors++;
|
1995-07-07 22:53:21 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, v));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(v);
|
1995-07-07 22:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
com_node(c, CHILD(n, 2));
|
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_call_function(c, n)
|
|
|
|
struct compiling *c;
|
1995-07-07 22:53:21 +00:00
|
|
|
node *n; /* EITHER arglist OR ')' */
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
|
|
|
if (TYPE(n) == RPAR) {
|
1995-07-07 22:53:21 +00:00
|
|
|
com_addoparg(c, CALL_FUNCTION, 0);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *keywords = NULL;
|
1997-01-06 22:56:52 +00:00
|
|
|
int i, na, nk;
|
1998-12-10 16:56:22 +00:00
|
|
|
int lineno = n->n_lineno;
|
1995-07-07 22:53:21 +00:00
|
|
|
REQ(n, arglist);
|
|
|
|
na = 0;
|
|
|
|
nk = 0;
|
|
|
|
for (i = 0; i < NCH(n); i += 2) {
|
1998-12-10 16:56:22 +00:00
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
if (ch->n_lineno != lineno) {
|
|
|
|
lineno = ch->n_lineno;
|
|
|
|
com_addoparg(c, SET_LINENO, lineno);
|
|
|
|
}
|
|
|
|
com_argument(c, ch, &keywords);
|
1997-01-06 22:56:52 +00:00
|
|
|
if (keywords == NULL)
|
1995-07-07 22:53:21 +00:00
|
|
|
na++;
|
|
|
|
else
|
|
|
|
nk++;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_XDECREF(keywords);
|
1995-07-07 22:53:21 +00:00
|
|
|
if (na > 255 || nk > 255) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"more than 255 arguments");
|
1995-07-07 22:53:21 +00:00
|
|
|
}
|
|
|
|
com_addoparg(c, CALL_FUNCTION, na | (nk << 8));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, na + 2*nk);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_select_member(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
com_addopname(c, LOAD_ATTR, n);
|
|
|
|
}
|
|
|
|
|
1996-07-30 16:49:37 +00:00
|
|
|
static void
|
|
|
|
com_sliceobj(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
int ns=2; /* number of slice arguments */
|
|
|
|
node *ch;
|
|
|
|
|
|
|
|
/* first argument */
|
|
|
|
if (TYPE(CHILD(n,i)) == COLON) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1996-07-30 16:49:37 +00: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 21:04:03 +00:00
|
|
|
else {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1996-07-30 16:49:37 +00: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 20:08:16 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1996-07-30 16:49:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
com_node(c, CHILD(ch,1));
|
|
|
|
}
|
|
|
|
com_addoparg(c, BUILD_SLICE, ns);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1 + (ns == 3));
|
1996-07-30 16:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_subscript(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
node *ch;
|
|
|
|
REQ(n, subscript);
|
|
|
|
ch = CHILD(n,0);
|
|
|
|
/* check for rubber index */
|
1997-01-17 21:04:03 +00:00
|
|
|
if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
|
1996-10-11 16:25:41 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1996-07-30 16:49:37 +00: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
|
|
|
|
com_subscriptlist(c, n, assigning)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
int assigning;
|
|
|
|
{
|
|
|
|
int i, op;
|
|
|
|
REQ(n, subscriptlist);
|
|
|
|
/* Check to make backward compatible slice behavior for '[i:j]' */
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
node *sub = CHILD(n, 0); /* subscript */
|
|
|
|
/* Make it is a simple slice.
|
1997-01-17 21:04:03 +00:00
|
|
|
Should have exactly one colon. */
|
1996-07-30 16:49:37 +00:00
|
|
|
if ((TYPE(CHILD(sub, 0)) == COLON
|
|
|
|
|| (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
|
1997-01-17 21:04:03 +00:00
|
|
|
&& (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
|
|
|
|
{
|
1996-07-30 16:49:37 +00:00
|
|
|
if (assigning == OP_APPLY)
|
|
|
|
op = SLICE;
|
|
|
|
else
|
1997-04-29 20:08:16 +00:00
|
|
|
op = ((assigning == OP_ASSIGN) ?
|
|
|
|
STORE_SLICE : DELETE_SLICE);
|
1996-07-30 16:49:37 +00:00
|
|
|
com_slice(c, sub, op);
|
1997-01-17 21:04:03 +00:00
|
|
|
if (op == STORE_SLICE)
|
|
|
|
com_pop(c, 2);
|
|
|
|
else if (op == DELETE_SLICE)
|
|
|
|
com_pop(c, 1);
|
1996-07-30 16:49:37 +00: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 21:04:03 +00: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 16:49:37 +00:00
|
|
|
op = BINARY_SUBSCR;
|
1997-01-17 21:04:03 +00:00
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
else if (assigning == OP_ASSIGN) {
|
|
|
|
op = STORE_SUBSCR;
|
|
|
|
i = 3;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
op = DELETE_SUBSCR;
|
|
|
|
i = 2;
|
|
|
|
}
|
1996-07-30 16:49:37 +00:00
|
|
|
com_addbyte(c, op);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, i);
|
1996-07-30 16:49:37 +00:00
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_apply_trailer(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 16:49:37 +00:00
|
|
|
com_subscriptlist(c, CHILD(n, 1), OP_APPLY);
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"com_apply_trailer: unknown trailer type");
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-01-12 01:13:16 +00:00
|
|
|
static void
|
|
|
|
com_power(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1996-01-12 01:13:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
com_apply_trailer(c, CHILD(n, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_factor(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 14:59:31 +00:00
|
|
|
else if (TYPE(CHILD(n, 0)) == TILDE) {
|
|
|
|
com_factor(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_INVERT);
|
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
else {
|
1996-01-12 01:13:16 +00:00
|
|
|
com_power(c, CHILD(n, 0));
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_term(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"com_term: operator not *, / or %");
|
1990-11-18 17:27:39 +00:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1991-10-24 14:59:31 +00:00
|
|
|
com_arith_expr(c, n)
|
1990-11-18 17:27:39 +00:00
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
1991-10-24 14:59:31 +00:00
|
|
|
REQ(n, arith_expr);
|
1990-11-18 17:27:39 +00: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 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"com_arith_expr: operator not + or -");
|
1991-10-24 14:59:31 +00:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1991-10-24 14:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_shift_expr(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"com_shift_expr: operator not << or >>");
|
1991-10-24 14:59:31 +00:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1991-10-24 14:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_and_expr(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"com_and_expr: operator not &");
|
1991-10-24 14:59:31 +00:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1991-10-24 14:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_xor_expr(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"com_xor_expr: operator not ^");
|
1991-10-24 14:59:31 +00:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1991-10-24 14:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_expr(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"com_expr: expr operator not |");
|
1990-11-18 17:27:39 +00:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum cmp_op
|
|
|
|
cmp_type(n)
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
REQ(n, comp_op);
|
1991-10-20 20:12:38 +00:00
|
|
|
/* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
|
1990-11-18 17:27:39 +00: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 20:12:38 +00:00
|
|
|
case EQEQUAL: /* == */
|
1990-11-18 17:27:39 +00:00
|
|
|
case EQUAL: return EQ;
|
1991-10-20 20:12:38 +00:00
|
|
|
case LESSEQUAL: return LE;
|
|
|
|
case GREATEREQUAL: return GE;
|
|
|
|
case NOTEQUAL: return NE; /* <> or != */
|
1990-11-18 17:27:39 +00: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
|
|
|
|
com_comparison(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 21:04:03 +00:00
|
|
|
L2: 0-or-1
|
1990-11-18 17:27:39 +00: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 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addbyte(c, ROT_THREE);
|
|
|
|
}
|
|
|
|
op = cmp_type(CHILD(n, i-1));
|
|
|
|
if (op == BAD) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"com_comparison: unknown comparison op");
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
com_addoparg(c, COMPARE_OP, op);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
if (i+2 < NCH(n)) {
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00: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
|
|
|
|
com_not_test(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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
|
|
|
|
com_and_test(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
if (anchor)
|
|
|
|
com_backpatch(c, anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_test(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1997-01-17 21:04:03 +00:00
|
|
|
REQ(n, test); /* and_test ('or' and_test)* | lambdef */
|
1993-11-30 14:57:42 +00:00
|
|
|
if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1993-11-30 14:57:42 +00:00
|
|
|
int i;
|
1995-07-18 14:51:37 +00:00
|
|
|
int ndefs = com_argdefs(c, CHILD(n, 0));
|
1997-04-29 20:08:16 +00:00
|
|
|
v = (PyObject *) icompile(CHILD(n, 0), c);
|
1993-11-30 14:57:42 +00:00
|
|
|
if (v == NULL) {
|
|
|
|
c->c_errors++;
|
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addconst(c, v);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(v);
|
1993-11-30 14:57:42 +00:00
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1995-07-18 14:51:37 +00:00
|
|
|
com_addoparg(c, MAKE_FUNCTION, ndefs);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, ndefs);
|
1993-11-30 14:57:42 +00: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 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1993-11-30 14:57:42 +00:00
|
|
|
}
|
|
|
|
if (anchor)
|
|
|
|
com_backpatch(c, anchor);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1991-12-16 13:05:10 +00:00
|
|
|
com_list(c, n, toplevel)
|
1990-11-18 17:27:39 +00:00
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
1991-12-16 13:05:10 +00:00
|
|
|
int toplevel; /* If nonzero, *always* build a tuple */
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
|
|
|
/* exprlist: expr (',' expr)* [',']; likewise for testlist */
|
1991-12-16 13:05:10 +00:00
|
|
|
if (NCH(n) == 1 && !toplevel) {
|
1990-11-18 17:27:39 +00: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 21:04:03 +00:00
|
|
|
com_pop(c, len-1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Begin of assignment compilation */
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static void com_assign_name Py_PROTO((struct compiling *, node *, int));
|
|
|
|
static void com_assign Py_PROTO((struct compiling *, node *, int));
|
1990-11-18 17:27:39 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
com_assign_attr(c, n, assigning)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
int assigning;
|
|
|
|
{
|
|
|
|
com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, assigning ? 2 : 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_assign_trailer(c, n, assigning)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
int assigning;
|
|
|
|
{
|
|
|
|
REQ(n, trailer);
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case LPAR: /* '(' [exprlist] ')' */
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"can't assign to function call");
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
case DOT: /* '.' NAME */
|
|
|
|
com_assign_attr(c, CHILD(n, 1), assigning);
|
|
|
|
break;
|
1996-07-30 16:49:37 +00:00
|
|
|
case LSQB: /* '[' subscriptlist ']' */
|
|
|
|
com_subscriptlist(c, CHILD(n, 1), assigning);
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError, "unknown trailer type");
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_assign_tuple(c, n, assigning)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
int assigning;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (TYPE(n) != testlist)
|
|
|
|
REQ(n, exprlist);
|
1997-01-17 21:04:03 +00:00
|
|
|
if (assigning) {
|
|
|
|
i = (NCH(n)+1)/2;
|
|
|
|
com_addoparg(c, UNPACK_TUPLE, i);
|
|
|
|
com_push(c, i-1);
|
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_assign(c, CHILD(n, i), assigning);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_assign_list(c, n, assigning)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
int assigning;
|
|
|
|
{
|
|
|
|
int i;
|
1997-01-17 21:04:03 +00:00
|
|
|
if (assigning) {
|
|
|
|
i = (NCH(n)+1)/2;
|
|
|
|
com_addoparg(c, UNPACK_LIST, i);
|
|
|
|
com_push(c, i-1);
|
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_assign(c, CHILD(n, i), assigning);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_assign_name(c, n, assigning)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
int assigning;
|
|
|
|
{
|
|
|
|
REQ(n, NAME);
|
|
|
|
com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
|
1997-01-17 21:04:03 +00:00
|
|
|
if (assigning)
|
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_assign(c, n, assigning)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
int assigning;
|
|
|
|
{
|
|
|
|
/* Loop to avoid trivial recursion */
|
|
|
|
for (;;) {
|
|
|
|
switch (TYPE(n)) {
|
1990-11-18 17:35:03 +00:00
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
case exprlist:
|
|
|
|
case testlist:
|
|
|
|
if (NCH(n) > 1) {
|
|
|
|
com_assign_tuple(c, n, assigning);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
break;
|
1990-11-18 17:35:03 +00:00
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
case test:
|
|
|
|
case and_test:
|
|
|
|
case not_test:
|
|
|
|
case comparison:
|
|
|
|
case expr:
|
1991-10-24 14:59:31 +00:00
|
|
|
case xor_expr:
|
|
|
|
case and_expr:
|
|
|
|
case shift_expr:
|
|
|
|
case arith_expr:
|
1990-11-18 17:27:39 +00:00
|
|
|
case term:
|
1996-01-12 01:13:16 +00:00
|
|
|
case factor:
|
1990-11-18 17:27:39 +00:00
|
|
|
if (NCH(n) > 1) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"can't assign to operator");
|
1990-11-18 17:27:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
break;
|
1990-11-18 17:35:03 +00:00
|
|
|
|
1996-01-12 01:13:16 +00:00
|
|
|
case power: /* atom trailer* ('**' power)* */
|
|
|
|
/* ('+'|'-'|'~') factor | atom trailer* */
|
|
|
|
if (TYPE(CHILD(n, 0)) != atom) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"can't assign to operator");
|
1990-11-18 17:27:39 +00:00
|
|
|
return;
|
|
|
|
}
|
1996-01-12 01:13:16 +00:00
|
|
|
if (NCH(n) > 1) { /* trailer or exponent present */
|
1990-11-18 17:27:39 +00:00
|
|
|
int i;
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
for (i = 1; i+1 < NCH(n); i++) {
|
1996-01-12 01:13:16 +00:00
|
|
|
if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"can't assign to operator");
|
1996-01-12 01:13:16 +00:00
|
|
|
return;
|
|
|
|
}
|
1990-11-18 17:27:39 +00: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 17:35:03 +00:00
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
case atom:
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case LPAR:
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
if (TYPE(n) == RPAR) {
|
|
|
|
/* XXX Should allow () = () ??? */
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"can't assign to ()");
|
1990-11-18 17:27:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LSQB:
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
if (TYPE(n) == RSQB) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"can't assign to []");
|
1990-11-18 17:27:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
com_assign_list(c, n, assigning);
|
|
|
|
return;
|
|
|
|
case NAME:
|
|
|
|
com_assign_name(c, CHILD(n, 0), assigning);
|
|
|
|
return;
|
|
|
|
default:
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"can't assign to literal");
|
1990-11-18 17:27:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
1996-08-08 18:51:04 +00:00
|
|
|
|
|
|
|
case lambdef:
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"can't assign to lambda");
|
1996-08-08 18:51:04 +00:00
|
|
|
return;
|
1990-11-18 17:35:03 +00:00
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
default:
|
1997-01-17 21:04:03 +00:00
|
|
|
/* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_assign: bad node");
|
1990-11-18 17:27:39 +00:00
|
|
|
return;
|
1990-11-18 17:35:03 +00:00
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-03-11 18:42:21 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
/* Forward */ static node *get_rawdocstring Py_PROTO((node *));
|
1990-11-18 17:27:39 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
com_expr_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1993-10-18 17:06:59 +00:00
|
|
|
REQ(n, expr_stmt); /* testlist ('=' testlist)* */
|
1997-01-17 21:04:03 +00:00
|
|
|
/* Forget it if we have just a doc string here */
|
1997-04-06 03:41:40 +00:00
|
|
|
if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
|
1997-01-17 21:04:03 +00:00
|
|
|
return;
|
1991-07-27 21:34:52 +00:00
|
|
|
com_node(c, CHILD(n, NCH(n)-1));
|
|
|
|
if (NCH(n) == 1) {
|
1994-08-29 12:16:12 +00:00
|
|
|
if (c->c_interactive)
|
|
|
|
com_addbyte(c, PRINT_EXPR);
|
|
|
|
else
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
1991-07-27 21:34:52 +00:00
|
|
|
for (i = 0; i < NCH(n)-2; i+=2) {
|
1997-01-17 21:04:03 +00:00
|
|
|
if (i+2 < NCH(n)-2) {
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1996-07-30 16:49:37 +00:00
|
|
|
com_assign(c, CHILD(n, i), OP_ASSIGN);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-02 05:24:36 +00:00
|
|
|
static void
|
|
|
|
com_assert_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_print_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int i;
|
1991-07-27 21:34:52 +00:00
|
|
|
REQ(n, print_stmt); /* 'print' (test ',')* [test] */
|
|
|
|
for (i = 1; i < NCH(n); i += 2) {
|
1990-11-18 17:27:39 +00:00
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
com_addbyte(c, PRINT_ITEM);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
1991-07-27 21:34:52 +00:00
|
|
|
if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addbyte(c, PRINT_NEWLINE);
|
1990-11-18 17:35:03 +00:00
|
|
|
/* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_return_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1991-07-27 21:34:52 +00:00
|
|
|
REQ(n, return_stmt); /* 'return' [testlist] */
|
1990-12-20 15:06:42 +00:00
|
|
|
if (!c->c_infunction) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError, "'return' outside function");
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
1997-01-17 21:04:03 +00:00
|
|
|
if (NCH(n) < 2) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
else
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_raise_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1997-01-17 21:04:03 +00:00
|
|
|
int i;
|
1998-04-09 21:39:57 +00: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 22:53:21 +00:00
|
|
|
}
|
1997-01-17 21:04:03 +00:00
|
|
|
i = NCH(n)/2;
|
|
|
|
com_addoparg(c, RAISE_VARARGS, i);
|
|
|
|
com_pop(c, i);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_import_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
REQ(n, import_stmt);
|
1995-01-07 12:41:23 +00:00
|
|
|
/* 'import' dotted_name (',' dotted_name)* |
|
|
|
|
'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
|
1990-11-18 17:27:39 +00:00
|
|
|
if (STR(CHILD(n, 0))[0] == 'f') {
|
1995-01-07 12:41:23 +00:00
|
|
|
/* 'from' dotted_name 'import' ... */
|
|
|
|
REQ(CHILD(n, 1), dotted_name);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addopname(c, IMPORT_NAME, CHILD(n, 1));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1990-11-18 17:27:39 +00: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 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
else {
|
1990-11-18 17:35:03 +00:00
|
|
|
/* 'import' ... */
|
1990-11-18 17:27:39 +00:00
|
|
|
for (i = 1; i < NCH(n); i += 2) {
|
1995-01-07 12:41:23 +00:00
|
|
|
REQ(CHILD(n, i), dotted_name);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addopname(c, IMPORT_NAME, CHILD(n, i));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1995-01-07 12:41:23 +00:00
|
|
|
com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1991-12-10 13:53:51 +00:00
|
|
|
static void
|
|
|
|
com_global_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
REQ(n, global_stmt);
|
|
|
|
/* 'global' NAME (',' NAME)* */
|
|
|
|
for (i = 1; i < NCH(n); i += 2) {
|
1995-07-18 14:51:37 +00:00
|
|
|
char *s = STR(CHILD(n, i));
|
1996-08-24 07:29:04 +00:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
char buffer[256];
|
|
|
|
if (s != NULL && s[0] == '_' && s[1] == '_' &&
|
|
|
|
c->c_private != NULL &&
|
|
|
|
com_mangle(c, s, buffer, (int)sizeof(buffer)))
|
|
|
|
s = buffer;
|
|
|
|
#endif
|
1997-04-29 20:08:16 +00:00
|
|
|
if (PyDict_GetItemString(c->c_locals, s) != NULL) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"name is local and global");
|
1995-07-18 14:51:37 +00:00
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0)
|
1995-07-18 14:51:37 +00:00
|
|
|
c->c_errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
com_newlocal_o(c, nameval)
|
|
|
|
struct compiling *c;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *nameval;
|
1995-07-18 14:51:37 +00:00
|
|
|
{
|
|
|
|
int i;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *ival;
|
|
|
|
if (PyList_Size(c->c_varnames) != c->c_nlocals) {
|
1995-07-18 14:51:37 +00:00
|
|
|
/* This is usually caused by an error on a previous call */
|
|
|
|
if (c->c_errors == 0) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"mixed up var name/index");
|
1995-07-18 14:51:37 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
ival = PyInt_FromLong(i = c->c_nlocals++);
|
1995-07-18 14:51:37 +00:00
|
|
|
if (ival == NULL)
|
|
|
|
c->c_errors++;
|
1997-04-29 20:08:16 +00:00
|
|
|
else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0)
|
1995-07-18 14:51:37 +00:00
|
|
|
c->c_errors++;
|
1997-04-29 20:08:16 +00:00
|
|
|
else if (PyList_Append(c->c_varnames, nameval) != 0)
|
1995-07-18 14:51:37 +00:00
|
|
|
c->c_errors++;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_XDECREF(ival);
|
1995-07-18 14:51:37 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
com_addlocal_o(c, nameval)
|
|
|
|
struct compiling *c;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *nameval;
|
1995-07-18 14:51:37 +00:00
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *ival = PyDict_GetItem(c->c_locals, nameval);
|
1995-07-18 14:51:37 +00:00
|
|
|
if (ival != NULL)
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyInt_AsLong(ival);
|
1995-07-18 14:51:37 +00:00
|
|
|
return com_newlocal_o(c, nameval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
com_newlocal(c, name)
|
|
|
|
struct compiling *c;
|
|
|
|
char *name;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *nameval = PyString_InternFromString(name);
|
1995-07-18 14:51:37 +00:00
|
|
|
int i;
|
|
|
|
if (nameval == NULL) {
|
|
|
|
c->c_errors++;
|
|
|
|
return 0;
|
1991-12-10 13:53:51 +00:00
|
|
|
}
|
1995-07-18 14:51:37 +00:00
|
|
|
i = com_newlocal_o(c, nameval);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(nameval);
|
1995-07-18 14:51:37 +00:00
|
|
|
return i;
|
1991-12-10 13:53:51 +00:00
|
|
|
}
|
|
|
|
|
1993-10-18 17:06:59 +00:00
|
|
|
static void
|
|
|
|
com_exec_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 21:04:03 +00:00
|
|
|
else {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1993-10-18 17:06:59 +00:00
|
|
|
if (NCH(n) >= 6)
|
|
|
|
com_node(c, CHILD(n, 5));
|
1997-01-17 21:04:03 +00:00
|
|
|
else {
|
1993-10-18 17:06:59 +00:00
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1993-10-18 17:06:59 +00:00
|
|
|
com_addbyte(c, EXEC_STMT);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 3);
|
1993-10-18 17:06:59 +00:00
|
|
|
}
|
|
|
|
|
1997-03-11 18:42:21 +00:00
|
|
|
static int
|
|
|
|
is_constant_false(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1997-03-11 18:42:21 +00: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 20:08:16 +00:00
|
|
|
PyErr_Clear();
|
1997-03-11 18:42:21 +00:00
|
|
|
break;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
i = PyObject_IsTrue(v);
|
|
|
|
Py_DECREF(v);
|
1997-03-11 18:42:21 +00:00
|
|
|
return i == 0;
|
|
|
|
|
|
|
|
case STRING:
|
1997-03-11 21:25:55 +00:00
|
|
|
v = parsestr(STR(n));
|
1997-03-11 18:42:21 +00:00
|
|
|
if (v == NULL) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_Clear();
|
1997-03-11 18:42:21 +00:00
|
|
|
break;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
i = PyObject_IsTrue(v);
|
|
|
|
Py_DECREF(v);
|
1997-03-11 18:42:21 +00:00
|
|
|
return i == 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_if_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 15:06:42 +00:00
|
|
|
node *ch = CHILD(n, i+1);
|
1997-03-11 18:42:21 +00:00
|
|
|
if (is_constant_false(c, ch))
|
|
|
|
continue;
|
1990-12-20 15:06:42 +00:00
|
|
|
if (i > 0)
|
|
|
|
com_addoparg(c, SET_LINENO, ch->n_lineno);
|
1997-03-11 18:42:21 +00:00
|
|
|
com_node(c, ch);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &a);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_node(c, CHILD(n, i+3));
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &anchor);
|
|
|
|
com_backpatch(c, a);
|
1997-01-17 21:04:03 +00:00
|
|
|
/* We jump here with an extra entry which we now pop */
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
}
|
|
|
|
if (i+2 < NCH(n))
|
|
|
|
com_node(c, CHILD(n, i+2));
|
1997-03-11 18:42:21 +00:00
|
|
|
if (anchor)
|
|
|
|
com_backpatch(c, anchor);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_while_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int break_anchor = 0;
|
|
|
|
int anchor = 0;
|
1991-07-27 21:34:52 +00:00
|
|
|
int save_begin = c->c_begin;
|
1990-11-18 17:27:39 +00:00
|
|
|
REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
|
|
|
|
com_addfwref(c, SETUP_LOOP, &break_anchor);
|
1991-07-27 21:34:52 +00:00
|
|
|
block_push(c, SETUP_LOOP);
|
|
|
|
c->c_begin = c->c_nexti;
|
1990-12-20 15:06:42 +00:00
|
|
|
com_addoparg(c, SET_LINENO, n->n_lineno);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-12-20 15:06:42 +00:00
|
|
|
c->c_loops++;
|
1990-11-18 17:27:39 +00:00
|
|
|
com_node(c, CHILD(n, 3));
|
1990-12-20 15:06:42 +00:00
|
|
|
c->c_loops--;
|
1991-07-27 21:34:52 +00:00
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
c->c_begin = save_begin;
|
1990-11-18 17:27:39 +00:00
|
|
|
com_backpatch(c, anchor);
|
1997-01-17 21:04:03 +00:00
|
|
|
/* We jump here with one entry more on the stack */
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_addbyte(c, POP_BLOCK);
|
1991-07-27 21:34:52 +00:00
|
|
|
block_pop(c, SETUP_LOOP);
|
1990-11-18 17:27:39 +00:00
|
|
|
if (NCH(n) > 4)
|
|
|
|
com_node(c, CHILD(n, 6));
|
|
|
|
com_backpatch(c, break_anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_for_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1990-11-18 17:27:39 +00:00
|
|
|
int break_anchor = 0;
|
|
|
|
int anchor = 0;
|
1991-07-27 21:34:52 +00:00
|
|
|
int save_begin = c->c_begin;
|
1990-11-18 17:27:39 +00:00
|
|
|
REQ(n, for_stmt);
|
|
|
|
/* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
|
|
|
|
com_addfwref(c, SETUP_LOOP, &break_anchor);
|
1991-07-27 21:34:52 +00:00
|
|
|
block_push(c, SETUP_LOOP);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_node(c, CHILD(n, 3));
|
1997-04-29 20:08:16 +00:00
|
|
|
v = PyInt_FromLong(0L);
|
1990-11-18 17:27:39 +00:00
|
|
|
if (v == NULL)
|
|
|
|
c->c_errors++;
|
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, v));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_XDECREF(v);
|
1991-07-27 21:34:52 +00:00
|
|
|
c->c_begin = c->c_nexti;
|
1990-12-20 15:06:42 +00:00
|
|
|
com_addoparg(c, SET_LINENO, n->n_lineno);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addfwref(c, FOR_LOOP, &anchor);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1996-07-30 16:49:37 +00:00
|
|
|
com_assign(c, CHILD(n, 1), OP_ASSIGN);
|
1990-12-20 15:06:42 +00:00
|
|
|
c->c_loops++;
|
1990-11-18 17:27:39 +00:00
|
|
|
com_node(c, CHILD(n, 5));
|
1990-12-20 15:06:42 +00:00
|
|
|
c->c_loops--;
|
1991-07-27 21:34:52 +00:00
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
c->c_begin = save_begin;
|
1990-11-18 17:27:39 +00:00
|
|
|
com_backpatch(c, anchor);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 2); /* FOR_LOOP has popped these */
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addbyte(c, POP_BLOCK);
|
1991-07-27 21:34:52 +00:00
|
|
|
block_pop(c, SETUP_LOOP);
|
1990-11-18 17:27:39 +00:00
|
|
|
if (NCH(n) > 8)
|
|
|
|
com_node(c, CHILD(n, 8));
|
|
|
|
com_backpatch(c, break_anchor);
|
|
|
|
}
|
|
|
|
|
1994-08-29 12:16:12 +00:00
|
|
|
/* Code generated for "try: S finally: Sf" is as follows:
|
1990-11-18 17:35:03 +00: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 15:06:42 +00: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 17:35:03 +00: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 15:06:42 +00: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 17:35:03 +00:00
|
|
|
|
|
|
|
Value stack Label Instruction Argument
|
|
|
|
[] SETUP_EXCEPT L1
|
|
|
|
[] <code for S>
|
|
|
|
[] POP_BLOCK
|
|
|
|
[] JUMP_FORWARD L0
|
|
|
|
|
1990-12-20 15:06:42 +00: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 17:35:03 +00:00
|
|
|
[] <code for S1>
|
|
|
|
JUMP_FORWARD L0
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
[tb, val, exc, 0] L2: POP
|
|
|
|
[tb, val, exc] DUP
|
1990-11-18 17:35:03 +00:00
|
|
|
.............................etc.......................
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
[tb, val, exc, 0] Ln+1: POP
|
|
|
|
[tb, val, exc] END_FINALLY # re-raise exception
|
1990-11-18 17:35:03 +00:00
|
|
|
|
|
|
|
[] L0: <next statement>
|
|
|
|
|
|
|
|
Of course, parts are not generated if Vi or Ei is not present.
|
|
|
|
*/
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
1994-08-29 12:16:12 +00:00
|
|
|
com_try_except(c, n)
|
1990-11-18 17:27:39 +00:00
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int except_anchor = 0;
|
1994-08-29 12:16:12 +00:00
|
|
|
int end_anchor = 0;
|
|
|
|
int else_anchor = 0;
|
|
|
|
int i;
|
|
|
|
node *ch;
|
1992-04-05 14:24:50 +00:00
|
|
|
|
1994-08-29 12:16:12 +00:00
|
|
|
com_addfwref(c, SETUP_EXCEPT, &except_anchor);
|
|
|
|
block_push(c, SETUP_EXCEPT);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_node(c, CHILD(n, 2));
|
1994-08-29 12:16:12 +00: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 21:04:03 +00:00
|
|
|
/* except_clause: 'except' [expr [',' var]] */
|
1994-08-29 12:16:12 +00:00
|
|
|
if (except_anchor == 0) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"default 'except:' must be last");
|
1994-08-29 12:16:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
except_anchor = 0;
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 3); /* tb, val, exc pushed by exception */
|
1994-08-29 12:16:12 +00:00
|
|
|
com_addoparg(c, SET_LINENO, ch->n_lineno);
|
|
|
|
if (NCH(ch) > 1) {
|
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1994-08-29 12:16:12 +00:00
|
|
|
com_node(c, CHILD(ch, 1));
|
|
|
|
com_addoparg(c, COMPARE_OP, EXC_MATCH);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1994-08-29 12:16:12 +00:00
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1994-08-29 12:16:12 +00:00
|
|
|
}
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1994-08-29 12:16:12 +00:00
|
|
|
if (NCH(ch) > 3)
|
1996-07-30 16:49:37 +00:00
|
|
|
com_assign(c, CHILD(ch, 3), OP_ASSIGN);
|
1997-01-17 21:04:03 +00:00
|
|
|
else {
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
1994-08-29 12:16:12 +00:00
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1994-08-29 12:16:12 +00: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 21:04:03 +00: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 15:06:42 +00:00
|
|
|
com_addbyte(c, POP_TOP);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
1997-01-17 21:04:03 +00: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 12:16:12 +00: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
|
|
|
|
com_try_finally(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 20:08:16 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 21:04:03 +00: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 12:16:12 +00: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 21:04:03 +00:00
|
|
|
com_pop(c, 3); /* Matches the com_push above */
|
1994-08-29 12:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_try_stmt(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
1997-01-17 21:04:03 +00:00
|
|
|
static node *
|
|
|
|
get_rawdocstring(n)
|
1995-01-07 12:41:23 +00:00
|
|
|
node *n;
|
|
|
|
{
|
1995-01-26 00:40:09 +00:00
|
|
|
int i;
|
|
|
|
|
1997-01-17 21:04:03 +00:00
|
|
|
/* Label to avoid tail recursion */
|
|
|
|
next:
|
1995-01-07 12:41:23 +00:00
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
|
|
|
case suite:
|
1997-01-17 21:04:03 +00:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
1995-01-07 12:41:23 +00:00
|
|
|
}
|
1997-01-17 21:04:03 +00:00
|
|
|
/* Fall through */
|
1995-01-26 00:40:09 +00:00
|
|
|
case file_input:
|
|
|
|
for (i = 0; i < NCH(n); i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
1997-01-17 21:04:03 +00:00
|
|
|
if (TYPE(ch) == stmt) {
|
|
|
|
n = ch;
|
|
|
|
goto next;
|
|
|
|
}
|
1995-01-26 00:40:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1995-01-07 12:41:23 +00:00
|
|
|
case stmt:
|
|
|
|
case simple_stmt:
|
|
|
|
case small_stmt:
|
1997-01-17 21:04:03 +00:00
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
1995-01-07 12:41:23 +00: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-12 01:13:16 +00:00
|
|
|
case power:
|
1997-01-17 21:04:03 +00:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
|
|
|
}
|
1995-01-07 12:41:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case atom:
|
|
|
|
if (TYPE(CHILD(n, 0)) == STRING)
|
1997-01-17 21:04:03 +00:00
|
|
|
return n;
|
1995-01-07 12:41:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1997-01-17 21:04:03 +00:00
|
|
|
get_docstring(n)
|
|
|
|
node *n;
|
|
|
|
{
|
1999-01-28 15:08:09 +00:00
|
|
|
/* Don't generate doc-strings if run with -OO */
|
|
|
|
if (Py_OptimizeFlag > 1)
|
|
|
|
return NULL;
|
1997-01-17 21:04:03 +00:00
|
|
|
n = get_rawdocstring(n);
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
|
|
|
return parsestrplus(n);
|
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_suite(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 17:28:26 +00:00
|
|
|
/* ARGSUSED */
|
1991-07-27 21:34:52 +00:00
|
|
|
static void
|
|
|
|
com_continue_stmt(c, n)
|
|
|
|
struct compiling *c;
|
1992-03-27 17:28:26 +00:00
|
|
|
node *n; /* Not used, but passed for consistency */
|
1991-07-27 21:34:52 +00: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 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'continue' not properly in loop");
|
1991-07-27 21:34:52 +00:00
|
|
|
}
|
|
|
|
/* XXX Could allow it inside a 'finally' clause
|
|
|
|
XXX if we could pop the exception still on the stack */
|
|
|
|
}
|
|
|
|
|
1994-08-29 12:16:12 +00:00
|
|
|
static int
|
1995-07-18 14:51:37 +00:00
|
|
|
com_argdefs(c, n)
|
1994-08-29 12:16:12 +00:00
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1995-07-18 14:51:37 +00:00
|
|
|
int i, nch, nargs, ndefs;
|
1994-08-29 12:16:12 +00: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 14:51:37 +00:00
|
|
|
return 0;
|
1994-08-29 12:16:12 +00:00
|
|
|
/* varargslist:
|
1995-07-18 14:51:37 +00:00
|
|
|
(fpdef ['=' test] ',')* '*' ....... |
|
1994-08-29 12:16:12 +00:00
|
|
|
fpdef ['=' test] (',' fpdef ['=' test])* [','] */
|
|
|
|
nch = NCH(n);
|
|
|
|
nargs = 0;
|
|
|
|
ndefs = 0;
|
|
|
|
for (i = 0; i < nch; i++) {
|
|
|
|
int t;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (TYPE(CHILD(n, i)) == STAR ||
|
|
|
|
TYPE(CHILD(n, i)) == DOUBLESTAR)
|
1995-07-07 22:53:21 +00:00
|
|
|
break;
|
1994-08-29 12:16:12 +00:00
|
|
|
nargs++;
|
|
|
|
i++;
|
|
|
|
if (i >= nch)
|
1995-09-18 21:44:04 +00:00
|
|
|
t = RPAR; /* Anything except EQUAL or COMMA */
|
|
|
|
else
|
|
|
|
t = TYPE(CHILD(n, i));
|
1994-08-29 12:16:12 +00: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 13:41:54 +00:00
|
|
|
/* Treat "(a=1, b)" as an error */
|
|
|
|
if (ndefs)
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
1998-10-02 14:06:56 +00:00
|
|
|
"non-default argument follows default argument");
|
1994-08-29 12:16:12 +00:00
|
|
|
}
|
|
|
|
if (t != COMMA)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ndefs;
|
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_funcdef(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1990-11-18 17:27:39 +00:00
|
|
|
REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
|
1997-04-29 20:08:16 +00:00
|
|
|
v = (PyObject *)icompile(n, c);
|
1990-11-18 17:27:39 +00:00
|
|
|
if (v == NULL)
|
|
|
|
c->c_errors++;
|
|
|
|
else {
|
|
|
|
int i = com_addconst(c, v);
|
1995-07-18 14:51:37 +00:00
|
|
|
int ndefs = com_argdefs(c, n);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1995-07-18 14:51:37 +00:00
|
|
|
com_addoparg(c, MAKE_FUNCTION, ndefs);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, ndefs);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addopname(c, STORE_NAME, CHILD(n, 1));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(v);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 17:35:03 +00:00
|
|
|
static void
|
1993-05-19 14:50:45 +00:00
|
|
|
com_bases(c, n)
|
1991-12-10 13:53:51 +00:00
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1992-03-27 17:28:26 +00:00
|
|
|
int i;
|
1991-12-10 13:53:51 +00:00
|
|
|
REQ(n, testlist);
|
|
|
|
/* testlist: test (',' test)* [','] */
|
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_node(c, CHILD(n, i));
|
1997-01-17 21:04:03 +00:00
|
|
|
i = (NCH(n)+1) / 2;
|
|
|
|
com_addoparg(c, BUILD_TUPLE, i);
|
|
|
|
com_pop(c, i-1);
|
1991-12-10 13:53:51 +00:00
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_classdef(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1993-05-19 14:50:45 +00:00
|
|
|
int i;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1990-11-18 17:27:39 +00:00
|
|
|
REQ(n, classdef);
|
1993-05-19 14:50:45 +00:00
|
|
|
/* classdef: class NAME ['(' testlist ')'] ':' suite */
|
1997-01-18 08:02:57 +00:00
|
|
|
if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
|
1993-05-19 14:50:45 +00:00
|
|
|
c->c_errors++;
|
|
|
|
return;
|
1991-12-10 13:53:51 +00:00
|
|
|
}
|
1993-05-19 14:50:45 +00:00
|
|
|
/* Push the class name on the stack */
|
|
|
|
i = com_addconst(c, v);
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(v);
|
1993-05-19 14:50:45 +00:00
|
|
|
/* Push the tuple of base classes on the stack */
|
1997-01-17 21:04:03 +00:00
|
|
|
if (TYPE(CHILD(n, 2)) != LPAR) {
|
1993-05-19 14:50:45 +00:00
|
|
|
com_addoparg(c, BUILD_TUPLE, 0);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1993-05-19 14:50:45 +00:00
|
|
|
else
|
|
|
|
com_bases(c, CHILD(n, 3));
|
1997-04-29 20:08:16 +00:00
|
|
|
v = (PyObject *)icompile(n, c);
|
1990-11-18 17:35:03 +00:00
|
|
|
if (v == NULL)
|
|
|
|
c->c_errors++;
|
|
|
|
else {
|
1993-05-19 14:50:45 +00:00
|
|
|
i = com_addconst(c, v);
|
1990-11-18 17:35:03 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1995-07-18 14:51:37 +00:00
|
|
|
com_addoparg(c, MAKE_FUNCTION, 0);
|
|
|
|
com_addoparg(c, CALL_FUNCTION, 0);
|
1990-11-18 17:35:03 +00:00
|
|
|
com_addbyte(c, BUILD_CLASS);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 2);
|
1990-11-18 17:35:03 +00:00
|
|
|
com_addopname(c, STORE_NAME, CHILD(n, 1));
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(v);
|
1990-11-18 17:35:03 +00:00
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_node(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
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 21:34:52 +00:00
|
|
|
case small_stmt:
|
1990-11-18 17:27:39 +00:00
|
|
|
case flow_stmt:
|
1990-12-20 15:06:42 +00:00
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case simple_stmt:
|
1991-07-27 21:34:52 +00: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 17:27:39 +00:00
|
|
|
case compound_stmt:
|
1990-12-20 15:06:42 +00:00
|
|
|
com_addoparg(c, SET_LINENO, n->n_lineno);
|
1990-11-18 17:27:39 +00: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 21:34:52 +00:00
|
|
|
case del_stmt: /* 'del' exprlist */
|
1996-07-30 16:49:37 +00:00
|
|
|
com_assign(c, CHILD(n, 1), OP_DELETE);
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
case pass_stmt:
|
|
|
|
break;
|
|
|
|
case break_stmt:
|
1990-12-20 15:06:42 +00:00
|
|
|
if (c->c_loops == 0) {
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'break' outside loop");
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addbyte(c, BREAK_LOOP);
|
|
|
|
break;
|
1991-07-27 21:34:52 +00:00
|
|
|
case continue_stmt:
|
|
|
|
com_continue_stmt(c, n);
|
|
|
|
break;
|
1990-11-18 17:27:39 +00: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 13:53:51 +00:00
|
|
|
case global_stmt:
|
|
|
|
com_global_stmt(c, n);
|
|
|
|
break;
|
1993-10-18 17:06:59 +00:00
|
|
|
case exec_stmt:
|
|
|
|
com_exec_stmt(c, n);
|
|
|
|
break;
|
1997-04-02 05:24:36 +00:00
|
|
|
case assert_stmt:
|
|
|
|
com_assert_stmt(c, n);
|
|
|
|
break;
|
1990-11-18 17:27:39 +00: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 13:05:10 +00:00
|
|
|
com_list(c, n, 0);
|
1990-11-18 17:27:39 +00: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 13:05:10 +00:00
|
|
|
com_list(c, n, 0);
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
case expr:
|
|
|
|
com_expr(c, n);
|
|
|
|
break;
|
1991-10-24 14:59:31 +00: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 17:27:39 +00:00
|
|
|
case term:
|
|
|
|
com_term(c, n);
|
|
|
|
break;
|
|
|
|
case factor:
|
|
|
|
com_factor(c, n);
|
|
|
|
break;
|
1996-01-12 01:13:16 +00:00
|
|
|
case power:
|
|
|
|
com_power(c, n);
|
|
|
|
break;
|
1990-11-18 17:27:39 +00:00
|
|
|
case atom:
|
|
|
|
com_atom(c, n);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1997-01-17 21:04:03 +00:00
|
|
|
/* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_node: unexpected node type");
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static void com_fplist Py_PROTO((struct compiling *, node *));
|
1990-11-18 17:27:39 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
com_fpdef(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
|
if (TYPE(CHILD(n, 0)) == LPAR)
|
|
|
|
com_fplist(c, CHILD(n, 1));
|
1997-01-17 21:04:03 +00:00
|
|
|
else {
|
1995-07-18 14:51:37 +00:00
|
|
|
com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_fplist(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1991-12-31 13:13:35 +00:00
|
|
|
REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
|
1990-11-18 17:27:39 +00:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_fpdef(c, CHILD(n, 0));
|
|
|
|
}
|
|
|
|
else {
|
1997-01-17 21:04:03 +00:00
|
|
|
int i = (NCH(n)+1)/2;
|
|
|
|
com_addoparg(c, UNPACK_TUPLE, i);
|
|
|
|
com_push(c, i-1);
|
1990-11-18 17:27:39 +00:00
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_fpdef(c, CHILD(n, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1992-01-14 18:30:26 +00:00
|
|
|
static void
|
|
|
|
com_arglist(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1995-07-18 14:51:37 +00:00
|
|
|
int nch, i;
|
|
|
|
int complex = 0;
|
1997-07-10 01:06:53 +00:00
|
|
|
char nbuf[10];
|
1992-01-14 18:30:26 +00:00
|
|
|
REQ(n, varargslist);
|
1992-01-19 16:33:51 +00:00
|
|
|
/* varargslist:
|
1995-07-18 14:51:37 +00:00
|
|
|
(fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
|
1994-08-29 12:16:12 +00:00
|
|
|
nch = NCH(n);
|
1995-07-18 14:51:37 +00:00
|
|
|
/* Enter all arguments in table of locals */
|
1994-08-29 12:16:12 +00:00
|
|
|
for (i = 0; i < nch; i++) {
|
1995-07-18 14:51:37 +00:00
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
node *fp;
|
|
|
|
char *name;
|
1996-01-12 01:13:16 +00:00
|
|
|
if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
|
1995-07-07 22:53:21 +00:00
|
|
|
break;
|
1995-07-18 14:51:37 +00:00
|
|
|
REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
|
fp = CHILD(ch, 0);
|
|
|
|
if (TYPE(fp) == NAME)
|
|
|
|
name = STR(fp);
|
|
|
|
else {
|
1997-07-10 01:06:53 +00:00
|
|
|
name = nbuf;
|
|
|
|
sprintf(nbuf, ".%d", i);
|
1996-07-21 02:27:43 +00:00
|
|
|
complex = 1;
|
1995-07-07 22:53:21 +00:00
|
|
|
}
|
1995-07-18 14:51:37 +00:00
|
|
|
com_newlocal(c, name);
|
|
|
|
c->c_argcount++;
|
|
|
|
if (++i >= nch)
|
1992-01-14 18:30:26 +00:00
|
|
|
break;
|
1995-07-18 14:51:37 +00:00
|
|
|
ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == EQUAL)
|
1994-08-29 12:16:12 +00:00
|
|
|
i += 2;
|
1995-07-18 14:51:37 +00:00
|
|
|
else
|
|
|
|
REQ(ch, COMMA);
|
|
|
|
}
|
|
|
|
/* Handle *arguments */
|
|
|
|
if (i < nch) {
|
|
|
|
node *ch;
|
|
|
|
ch = CHILD(n, i);
|
1996-01-12 01:13:16 +00: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 18:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
1995-07-18 14:51:37 +00:00
|
|
|
/* Handle **keywords */
|
|
|
|
if (i < nch) {
|
|
|
|
node *ch;
|
|
|
|
ch = CHILD(n, i);
|
1996-01-12 01:13:16 +00: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 14:51:37 +00: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-12 01:13:16 +00:00
|
|
|
if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
|
1994-08-29 12:16:12 +00:00
|
|
|
break;
|
1995-07-18 14:51:37 +00:00
|
|
|
REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
|
fp = CHILD(ch, 0);
|
|
|
|
if (TYPE(fp) != NAME) {
|
|
|
|
com_addoparg(c, LOAD_FAST, ilocal);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1995-07-18 14:51:37 +00: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 12:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
1992-01-14 18:30:26 +00:00
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
com_file_input(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
int i;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *doc;
|
1990-11-18 17:27:39 +00:00
|
|
|
REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
|
1995-01-07 12:41:23 +00:00
|
|
|
doc = get_docstring(n);
|
|
|
|
if (doc != NULL) {
|
|
|
|
int i = com_addconst(c, doc);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(doc);
|
1995-01-07 12:41:23 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1995-01-07 12:41:23 +00:00
|
|
|
com_addopnamestr(c, STORE_NAME, "__doc__");
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1995-01-07 12:41:23 +00:00
|
|
|
}
|
1990-11-18 17:27:39 +00: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
|
|
|
|
compile_funcdef(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *doc;
|
1990-11-18 17:27:39 +00:00
|
|
|
node *ch;
|
|
|
|
REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
|
1993-03-29 10:43:31 +00:00
|
|
|
c->c_name = STR(CHILD(n, 1));
|
1995-01-07 12:41:23 +00:00
|
|
|
doc = get_docstring(CHILD(n, 4));
|
|
|
|
if (doc != NULL) {
|
|
|
|
(void) com_addconst(c, doc);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(doc);
|
1995-01-07 12:41:23 +00:00
|
|
|
}
|
1995-07-18 14:51:37 +00:00
|
|
|
else
|
1997-04-29 20:08:16 +00:00
|
|
|
(void) com_addconst(c, Py_None); /* No docstring */
|
1992-01-14 18:30:26 +00:00
|
|
|
ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
|
|
|
|
ch = CHILD(ch, 1); /* ')' | varargslist */
|
1995-07-18 14:51:37 +00:00
|
|
|
if (TYPE(ch) == varargslist)
|
1992-01-14 18:30:26 +00:00
|
|
|
com_arglist(c, ch);
|
1990-12-20 15:06:42 +00:00
|
|
|
c->c_infunction = 1;
|
1990-11-18 17:27:39 +00:00
|
|
|
com_node(c, CHILD(n, 4));
|
1990-12-20 15:06:42 +00:00
|
|
|
c->c_infunction = 0;
|
1997-04-29 20:08:16 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
1993-10-26 17:58:25 +00:00
|
|
|
static void
|
1993-11-30 13:40:46 +00:00
|
|
|
compile_lambdef(c, n)
|
1993-10-26 17:58:25 +00:00
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1993-11-30 13:40:46 +00:00
|
|
|
node *ch;
|
1995-07-18 14:51:37 +00:00
|
|
|
REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
|
1994-11-10 22:40:34 +00:00
|
|
|
c->c_name = "<lambda>";
|
1993-11-30 13:40:46 +00:00
|
|
|
|
|
|
|
ch = CHILD(n, 1);
|
1997-04-29 20:08:16 +00:00
|
|
|
(void) com_addconst(c, Py_None); /* No docstring */
|
1995-07-18 14:51:37 +00:00
|
|
|
if (TYPE(ch) == varargslist) {
|
1993-11-30 13:40:46 +00:00
|
|
|
com_arglist(c, ch);
|
1995-07-18 14:51:37 +00:00
|
|
|
ch = CHILD(n, 3);
|
1993-11-30 13:40:46 +00:00
|
|
|
}
|
1995-07-18 14:51:37 +00:00
|
|
|
else
|
|
|
|
ch = CHILD(n, 2);
|
|
|
|
com_node(c, ch);
|
1993-10-26 17:58:25 +00:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1993-10-26 17:58:25 +00:00
|
|
|
}
|
|
|
|
|
1995-01-07 12:41:23 +00:00
|
|
|
static void
|
|
|
|
compile_classdef(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
|
|
|
node *ch;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *doc;
|
1995-01-07 12:41:23 +00:00
|
|
|
REQ(n, classdef);
|
|
|
|
/* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
|
|
|
|
c->c_name = STR(CHILD(n, 1));
|
1996-08-24 06:21:31 +00:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
c->c_private = c->c_name;
|
|
|
|
#endif
|
1995-01-07 12:41:23 +00: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 20:08:16 +00:00
|
|
|
Py_DECREF(doc);
|
1995-01-07 12:41:23 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1995-01-07 12:41:23 +00:00
|
|
|
com_addopnamestr(c, STORE_NAME, "__doc__");
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1995-01-07 12:41:23 +00:00
|
|
|
}
|
|
|
|
else
|
1997-04-29 20:08:16 +00:00
|
|
|
(void) com_addconst(c, Py_None);
|
1995-01-07 12:41:23 +00:00
|
|
|
com_node(c, ch);
|
|
|
|
com_addbyte(c, LOAD_LOCALS);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1995-01-07 12:41:23 +00:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1995-01-07 12:41:23 +00:00
|
|
|
}
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
static void
|
|
|
|
compile_node(c, n)
|
|
|
|
struct compiling *c;
|
|
|
|
node *n;
|
|
|
|
{
|
1990-12-20 15:06:42 +00:00
|
|
|
com_addoparg(c, SET_LINENO, n->n_lineno);
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
1991-01-21 16:09:22 +00:00
|
|
|
case single_input: /* One interactive command */
|
1990-11-18 17:27:39 +00:00
|
|
|
/* NEWLINE | simple_stmt | compound_stmt NEWLINE */
|
1994-08-29 12:16:12 +00:00
|
|
|
c->c_interactive++;
|
1990-11-18 17:27:39 +00:00
|
|
|
n = CHILD(n, 0);
|
|
|
|
if (TYPE(n) != NEWLINE)
|
|
|
|
com_node(c, n);
|
1997-04-29 20:08:16 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1990-12-20 15:06:42 +00:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1994-08-29 12:16:12 +00:00
|
|
|
c->c_interactive--;
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
|
1991-01-21 16:09:22 +00:00
|
|
|
case file_input: /* A whole file, or built-in function exec() */
|
1990-11-18 17:27:39 +00:00
|
|
|
com_file_input(c, n);
|
1997-04-29 20:08:16 +00:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 21:04:03 +00:00
|
|
|
com_push(c, 1);
|
1990-12-20 15:06:42 +00:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
|
1993-11-30 13:40:46 +00:00
|
|
|
case eval_input: /* Built-in function input() */
|
1990-11-18 17:27:39 +00:00
|
|
|
com_node(c, CHILD(n, 0));
|
1990-12-20 15:06:42 +00:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 21:04:03 +00:00
|
|
|
com_pop(c, 1);
|
1990-11-18 17:27:39 +00:00
|
|
|
break;
|
|
|
|
|
1993-11-30 13:40:46 +00:00
|
|
|
case lambdef: /* anonymous function definition */
|
|
|
|
compile_lambdef(c, n);
|
|
|
|
break;
|
|
|
|
|
1991-01-21 16:09:22 +00:00
|
|
|
case funcdef: /* A function definition */
|
1990-11-18 17:27:39 +00:00
|
|
|
compile_funcdef(c, n);
|
|
|
|
break;
|
|
|
|
|
1991-01-21 16:09:22 +00:00
|
|
|
case classdef: /* A class definition */
|
1995-01-07 12:41:23 +00:00
|
|
|
compile_classdef(c, n);
|
1990-11-18 17:35:03 +00:00
|
|
|
break;
|
|
|
|
|
1990-11-18 17:27:39 +00:00
|
|
|
default:
|
1997-01-17 21:04:03 +00:00
|
|
|
/* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
|
1997-04-29 20:08:16 +00:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 14:32:04 +00:00
|
|
|
"compile_node: unexpected node type");
|
1990-11-18 17:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-03-30 13:18:41 +00:00
|
|
|
/* Optimization for local variables in functions (and *only* functions).
|
1991-04-04 10:42:56 +00:00
|
|
|
|
1993-03-30 13:18:41 +00: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 12:16:12 +00:00
|
|
|
|
1995-07-18 14:51:37 +00: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 12:16:12 +00: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 14:51:37 +00: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 12:16:12 +00:00
|
|
|
|
1995-07-18 14:51:37 +00:00
|
|
|
NB: this modifies the string object c->c_code! */
|
1991-04-04 10:42:56 +00:00
|
|
|
|
|
|
|
static void
|
1993-03-30 13:18:41 +00:00
|
|
|
optimize(c)
|
|
|
|
struct compiling *c;
|
1991-04-04 10:42:56 +00:00
|
|
|
{
|
1991-04-16 08:39:12 +00:00
|
|
|
unsigned char *next_instr, *cur_instr;
|
1991-04-04 10:42:56 +00:00
|
|
|
int opcode;
|
1996-12-05 23:27:02 +00:00
|
|
|
int oparg = 0;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *name;
|
|
|
|
PyObject *error_type, *error_value, *error_traceback;
|
1993-03-30 13:18:41 +00:00
|
|
|
|
1991-04-04 10:42:56 +00:00
|
|
|
#define NEXTOP() (*next_instr++)
|
|
|
|
#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
|
1997-04-29 20:08:16 +00:00
|
|
|
#define GETITEM(v, i) (PyList_GetItem((v), (i)))
|
1993-03-30 13:18:41 +00:00
|
|
|
#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
1995-07-18 14:51:37 +00:00
|
|
|
|
|
|
|
c->c_flags |= CO_OPTIMIZED;
|
1991-04-04 10:42:56 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
next_instr = (unsigned char *) PyString_AsString(c->c_code);
|
1991-04-04 10:42:56 +00:00
|
|
|
for (;;) {
|
|
|
|
opcode = NEXTOP();
|
|
|
|
if (opcode == STOP_CODE)
|
|
|
|
break;
|
|
|
|
if (HAS_ARG(opcode))
|
|
|
|
oparg = NEXTARG();
|
1995-07-18 14:51:37 +00: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 10:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
if (PyDict_GetItemString(c->c_locals, "*") != NULL)
|
1995-07-18 14:51:37 +00:00
|
|
|
c->c_flags &= ~CO_OPTIMIZED;
|
1993-03-30 13:18:41 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
next_instr = (unsigned char *) PyString_AsString(c->c_code);
|
1991-04-04 10:42:56 +00:00
|
|
|
for (;;) {
|
|
|
|
cur_instr = next_instr;
|
|
|
|
opcode = NEXTOP();
|
|
|
|
if (opcode == STOP_CODE)
|
|
|
|
break;
|
|
|
|
if (HAS_ARG(opcode))
|
|
|
|
oparg = NEXTARG();
|
1993-03-30 13:18:41 +00:00
|
|
|
if (opcode == LOAD_NAME ||
|
|
|
|
opcode == STORE_NAME ||
|
|
|
|
opcode == DELETE_NAME) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v;
|
1993-03-30 13:18:41 +00:00
|
|
|
int i;
|
1991-04-04 10:42:56 +00:00
|
|
|
name = GETNAMEOBJ(oparg);
|
1997-04-29 20:08:16 +00:00
|
|
|
v = PyDict_GetItem(c->c_locals, name);
|
1993-03-30 13:18:41 +00:00
|
|
|
if (v == NULL) {
|
1995-07-18 14:51:37 +00:00
|
|
|
if (opcode == LOAD_NAME &&
|
|
|
|
(c->c_flags&CO_OPTIMIZED))
|
1994-08-29 12:16:12 +00:00
|
|
|
cur_instr[0] = LOAD_GLOBAL;
|
1993-03-30 13:18:41 +00:00
|
|
|
continue;
|
1991-08-16 08:58:43 +00:00
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
i = PyInt_AsLong(v);
|
1993-03-30 13:18:41 +00: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 10:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
1993-03-30 13:18:41 +00:00
|
|
|
|
1995-07-18 14:51:37 +00:00
|
|
|
if (c->c_errors == 0)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_Restore(error_type, error_value, error_traceback);
|
1991-04-04 10:42:56 +00:00
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *
|
|
|
|
PyNode_Compile(n, filename)
|
1990-11-18 17:27:39 +00:00
|
|
|
node *n;
|
1990-12-20 15:06:42 +00:00
|
|
|
char *filename;
|
1996-08-24 06:21:31 +00:00
|
|
|
{
|
|
|
|
return jcompile(n, filename, NULL);
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyCodeObject *
|
1996-08-24 06:21:31 +00:00
|
|
|
icompile(n, base)
|
|
|
|
node *n;
|
|
|
|
struct compiling *base;
|
|
|
|
{
|
|
|
|
return jcompile(n, base->c_filename, base);
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyCodeObject *
|
1996-08-24 06:21:31 +00:00
|
|
|
jcompile(n, filename, base)
|
|
|
|
node *n;
|
|
|
|
char *filename;
|
|
|
|
struct compiling *base;
|
1990-11-18 17:27:39 +00:00
|
|
|
{
|
|
|
|
struct compiling sc;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
1993-11-30 13:40:46 +00:00
|
|
|
if (!com_init(&sc, filename))
|
1990-11-18 17:27:39 +00:00
|
|
|
return NULL;
|
1996-08-24 06:21:31 +00:00
|
|
|
#ifdef PRIVATE_NAME_MANGLING
|
|
|
|
if (base)
|
|
|
|
sc.c_private = base->c_private;
|
|
|
|
else
|
|
|
|
sc.c_private = NULL;
|
|
|
|
#endif
|
1990-11-18 17:27:39 +00:00
|
|
|
compile_node(&sc, n);
|
|
|
|
com_done(&sc);
|
1995-07-18 14:51:37 +00:00
|
|
|
if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
|
1993-03-30 13:18:41 +00:00
|
|
|
optimize(&sc);
|
1995-07-18 14:51:37 +00:00
|
|
|
sc.c_flags |= CO_NEWLOCALS;
|
|
|
|
}
|
|
|
|
else if (TYPE(n) == classdef)
|
|
|
|
sc.c_flags |= CO_NEWLOCALS;
|
1993-03-29 10:43:31 +00:00
|
|
|
co = NULL;
|
|
|
|
if (sc.c_errors == 0) {
|
1997-04-29 20:08:16 +00: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 08:02:57 +00:00
|
|
|
filename = PyString_InternFromString(sc.c_filename);
|
|
|
|
name = PyString_InternFromString(sc.c_name);
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyErr_Occurred())
|
|
|
|
co = PyCode_New(sc.c_argcount,
|
1995-07-18 14:51:37 +00:00
|
|
|
sc.c_nlocals,
|
1997-01-17 21:04:03 +00:00
|
|
|
sc.c_maxstacklevel,
|
1995-07-18 14:51:37 +00:00
|
|
|
sc.c_flags,
|
|
|
|
sc.c_code,
|
|
|
|
consts,
|
|
|
|
names,
|
|
|
|
varnames,
|
|
|
|
filename,
|
1997-01-24 03:43:35 +00:00
|
|
|
name,
|
|
|
|
sc.c_firstlineno,
|
|
|
|
sc.c_lnotab);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_XDECREF(consts);
|
|
|
|
Py_XDECREF(names);
|
|
|
|
Py_XDECREF(varnames);
|
|
|
|
Py_XDECREF(filename);
|
|
|
|
Py_XDECREF(name);
|
1991-06-04 19:41:56 +00:00
|
|
|
}
|
1990-11-18 17:27:39 +00:00
|
|
|
com_free(&sc);
|
|
|
|
return co;
|
|
|
|
}
|
1997-01-24 03:43:35 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
PyCode_Addr2Line(co, addrq)
|
|
|
|
PyCodeObject *co;
|
|
|
|
int addrq;
|
|
|
|
{
|
|
|
|
int size = PyString_Size(co->co_lnotab) / 2;
|
1999-09-15 22:48:09 +00:00
|
|
|
unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
|
1997-01-24 03:43:35 +00:00
|
|
|
int line = co->co_firstlineno;
|
|
|
|
int addr = 0;
|
|
|
|
while (--size >= 0) {
|
|
|
|
addr += *p++;
|
|
|
|
if (addr > addrq)
|
|
|
|
break;
|
|
|
|
line += *p++;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|