mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Initial revision
This commit is contained in:
parent
c636014c43
commit
85a5fbbdfe
78 changed files with 13589 additions and 0 deletions
1
Include/assert.h
Normal file
1
Include/assert.h
Normal file
|
@ -0,0 +1 @@
|
|||
#define assert(e) { if (!(e)) { printf("Assertion failed\n"); abort(); } }
|
22
Include/bitset.h
Normal file
22
Include/bitset.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
/* Bitset interface */
|
||||
|
||||
#define BYTE char
|
||||
|
||||
typedef BYTE *bitset;
|
||||
|
||||
bitset newbitset PROTO((int nbits));
|
||||
void delbitset PROTO((bitset bs));
|
||||
/* int testbit PROTO((bitset bs, int ibit)); /* Now a macro, see below */
|
||||
int addbit PROTO((bitset bs, int ibit)); /* Returns 0 if already set */
|
||||
int samebitset PROTO((bitset bs1, bitset bs2, int nbits));
|
||||
void mergebitset PROTO((bitset bs1, bitset bs2, int nbits));
|
||||
|
||||
#define BITSPERBYTE (8*sizeof(BYTE))
|
||||
#define NBYTES(nbits) (((nbits) + BITSPERBYTE - 1) / BITSPERBYTE)
|
||||
|
||||
#define BIT2BYTE(ibit) ((ibit) / BITSPERBYTE)
|
||||
#define BIT2SHIFT(ibit) ((ibit) % BITSPERBYTE)
|
||||
#define BIT2MASK(ibit) (1 << BIT2SHIFT(ibit))
|
||||
#define BYTE2BIT(ibyte) ((ibyte) * BITSPERBYTE)
|
||||
|
||||
#define testbit(ss, ibit) (((ss)[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0)
|
15
Include/cgensupport.h
Normal file
15
Include/cgensupport.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
/* Definitions used by cgen output */
|
||||
|
||||
typedef char *string;
|
||||
|
||||
#define mknewlongobject(x) newintobject(x)
|
||||
#define mknewshortobject(x) newintobject((long)x)
|
||||
#define mknewfloatobject(x) newfloatobject(x)
|
||||
|
||||
extern object *mknewcharobject PROTO((int c));
|
||||
|
||||
extern int getiobjectarg PROTO((object *args, int nargs, int i, object **p_a));
|
||||
extern int getilongarg PROTO((object *args, int nargs, int i, long *p_a));
|
||||
extern int getishortarg PROTO((object *args, int nargs, int i, short *p_a));
|
||||
extern int getifloatarg PROTO((object *args, int nargs, int i, float *p_a));
|
||||
extern int getistringarg PROTO((object *args, int nargs, int i, string *p_a));
|
20
Include/classobject.h
Normal file
20
Include/classobject.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
/* Class object interface */
|
||||
|
||||
/*
|
||||
Classes are really hacked in at the last moment.
|
||||
It should be possible to use other object types as base classes,
|
||||
but currently it isn't. We'll see if we can fix that later, sigh...
|
||||
*/
|
||||
|
||||
extern typeobject Classtype, Classmembertype, Classmethodtype;
|
||||
|
||||
#define is_classobject(op) ((op)->ob_type == &Classtype)
|
||||
#define is_classmemberobject(op) ((op)->ob_type == &Classmembertype)
|
||||
#define is_classmethodobject(op) ((op)->ob_type == &Classmethodtype)
|
||||
|
||||
extern object *newclassobject PROTO((node *, object *, object *));
|
||||
extern object *newclassmemberobject PROTO((object *));
|
||||
extern object *newclassmethodobject PROTO((object *, object *));
|
||||
|
||||
extern object *classmethodgetfunc PROTO((object *));
|
||||
extern object *classmethodgetself PROTO((object *));
|
25
Include/dictobject.h
Normal file
25
Include/dictobject.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
Dictionary object type -- mapping from char * to object.
|
||||
NB: the key is given as a char *, not as a stringobject.
|
||||
These functions set errno for errors. Functions dictremove() and
|
||||
dictinsert() return nonzero for errors, getdictsize() returns -1,
|
||||
the others NULL. A successful call to dictinsert() calls INCREF()
|
||||
for the inserted item.
|
||||
*/
|
||||
|
||||
extern typeobject Dicttype;
|
||||
|
||||
#define is_dictobject(op) ((op)->ob_type == &Dicttype)
|
||||
|
||||
extern object *newdictobject PROTO((void));
|
||||
extern object *dictlookup PROTO((object *dp, char *key));
|
||||
extern int dictinsert PROTO((object *dp, char *key, object *item));
|
||||
extern int dictremove PROTO((object *dp, char *key));
|
||||
extern int getdictsize PROTO((object *dp));
|
||||
extern char *getdictkey PROTO((object *dp, int i));
|
||||
|
||||
/* New interface with (string)object * instead of char * arguments */
|
||||
extern object *dict2lookup PROTO((object *dp, object *key));
|
||||
extern int dict2insert PROTO((object *dp, object *key, object *item));
|
||||
extern int dict2remove PROTO((object *dp, object *key));
|
||||
extern object *getdict2key PROTO((object *dp, int i));
|
12
Include/errcode.h
Normal file
12
Include/errcode.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
/* Error codes passed around between file input, tokenizer, parser and
|
||||
interpreter. This was necessary so we can turn them into Python
|
||||
exceptions at a higher level. */
|
||||
|
||||
#define E_OK 10 /* No error */
|
||||
#define E_EOF 11 /* (Unexpected) EOF read */
|
||||
#define E_INTR 12 /* Interrupted */
|
||||
#define E_TOKEN 13 /* Bad token */
|
||||
#define E_SYNTAX 14 /* Syntax error */
|
||||
#define E_NOMEM 15 /* Ran out of memory */
|
||||
#define E_DONE 16 /* Parsing complete */
|
||||
#define E_ERROR 17 /* Execution error */
|
17
Include/errors.h
Executable file
17
Include/errors.h
Executable file
|
@ -0,0 +1,17 @@
|
|||
/* Error handling definitions */
|
||||
|
||||
void err_set PROTO((object *));
|
||||
void err_setval PROTO((object *, object *));
|
||||
void err_setstr PROTO((object *, char *));
|
||||
int err_occurred PROTO((void));
|
||||
void err_get PROTO((object **, object **));
|
||||
void err_clear PROTO((void));
|
||||
|
||||
/* Predefined exceptions (in run.c) */
|
||||
object *RuntimeError; /* Raised by error() */
|
||||
object *EOFError; /* Raised by eof_error() */
|
||||
object *TypeError; /* Rased by type_error() */
|
||||
object *MemoryError; /* Raised by mem_error() */
|
||||
object *NameError; /* Raised by name_error() */
|
||||
object *SystemError; /* Raised by sys_error() */
|
||||
object *KeyboardInterrupt; /* Raised by intr_error() */
|
9
Include/fileobject.h
Normal file
9
Include/fileobject.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
/* File object interface */
|
||||
|
||||
extern typeobject Filetype;
|
||||
|
||||
#define is_fileobject(op) ((op)->ob_type == &Filetype)
|
||||
|
||||
extern object *newfileobject PROTO((char *, char *));
|
||||
extern object *newopenfileobject PROTO((FILE *, char *, char *));
|
||||
extern FILE *getfilefile PROTO((object *));
|
20
Include/floatobject.h
Normal file
20
Include/floatobject.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
/* Float object interface */
|
||||
|
||||
/*
|
||||
floatobject represents a (double precision) floating point number.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
OB_HEAD
|
||||
double ob_fval;
|
||||
} floatobject;
|
||||
|
||||
extern typeobject Floattype;
|
||||
|
||||
#define is_floatobject(op) ((op)->ob_type == &Floattype)
|
||||
|
||||
extern object *newfloatobject PROTO((double));
|
||||
extern double getfloatvalue PROTO((object *));
|
||||
|
||||
/* Macro, trading safety for speed */
|
||||
#define GETFLOATVALUE(op) ((op)->ob_fval)
|
9
Include/funcobject.h
Normal file
9
Include/funcobject.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
/* Function object interface */
|
||||
|
||||
extern typeobject Functype;
|
||||
|
||||
#define is_funcobject(op) ((op)->ob_type == &Functype)
|
||||
|
||||
extern object *newfuncobject PROTO((node *, object *));
|
||||
extern node *getfuncnode PROTO((object *));
|
||||
extern object *getfuncglobals PROTO((object *));
|
43
Include/graminit.h
Normal file
43
Include/graminit.h
Normal file
|
@ -0,0 +1,43 @@
|
|||
#define single_input 256
|
||||
#define file_input 257
|
||||
#define expr_input 258
|
||||
#define eval_input 259
|
||||
#define funcdef 260
|
||||
#define parameters 261
|
||||
#define fplist 262
|
||||
#define fpdef 263
|
||||
#define stmt 264
|
||||
#define simple_stmt 265
|
||||
#define expr_stmt 266
|
||||
#define print_stmt 267
|
||||
#define del_stmt 268
|
||||
#define dir_stmt 269
|
||||
#define pass_stmt 270
|
||||
#define flow_stmt 271
|
||||
#define break_stmt 272
|
||||
#define return_stmt 273
|
||||
#define raise_stmt 274
|
||||
#define import_stmt 275
|
||||
#define compound_stmt 276
|
||||
#define if_stmt 277
|
||||
#define while_stmt 278
|
||||
#define for_stmt 279
|
||||
#define try_stmt 280
|
||||
#define except_clause 281
|
||||
#define suite 282
|
||||
#define test 283
|
||||
#define and_test 284
|
||||
#define not_test 285
|
||||
#define comparison 286
|
||||
#define comp_op 287
|
||||
#define expr 288
|
||||
#define term 289
|
||||
#define factor 290
|
||||
#define atom 291
|
||||
#define trailer 292
|
||||
#define subscript 293
|
||||
#define exprlist 294
|
||||
#define testlist 295
|
||||
#define classdef 296
|
||||
#define baselist 297
|
||||
#define arguments 298
|
78
Include/grammar.h
Normal file
78
Include/grammar.h
Normal file
|
@ -0,0 +1,78 @@
|
|||
/* Grammar interface */
|
||||
|
||||
#include "bitset.h" /* Sigh... */
|
||||
|
||||
/* A label of an arc */
|
||||
|
||||
typedef struct _label {
|
||||
int lb_type;
|
||||
char *lb_str;
|
||||
} label;
|
||||
|
||||
#define EMPTY 0 /* Label number 0 is by definition the empty label */
|
||||
|
||||
/* A list of labels */
|
||||
|
||||
typedef struct _labellist {
|
||||
int ll_nlabels;
|
||||
label *ll_label;
|
||||
} labellist;
|
||||
|
||||
/* An arc from one state to another */
|
||||
|
||||
typedef struct _arc {
|
||||
short a_lbl; /* Label of this arc */
|
||||
short a_arrow; /* State where this arc goes to */
|
||||
} arc;
|
||||
|
||||
/* A state in a DFA */
|
||||
|
||||
typedef struct _state {
|
||||
int s_narcs;
|
||||
arc *s_arc; /* Array of arcs */
|
||||
|
||||
/* Optional accelerators */
|
||||
int s_lower; /* Lowest label index */
|
||||
int s_upper; /* Highest label index */
|
||||
int *s_accel; /* Accelerator */
|
||||
int s_accept; /* Nonzero for accepting state */
|
||||
} state;
|
||||
|
||||
/* A DFA */
|
||||
|
||||
typedef struct _dfa {
|
||||
int d_type; /* Non-terminal this represents */
|
||||
char *d_name; /* For printing */
|
||||
int d_initial; /* Initial state */
|
||||
int d_nstates;
|
||||
state *d_state; /* Array of states */
|
||||
bitset d_first;
|
||||
} dfa;
|
||||
|
||||
/* A grammar */
|
||||
|
||||
typedef struct _grammar {
|
||||
int g_ndfas;
|
||||
dfa *g_dfa; /* Array of DFAs */
|
||||
labellist g_ll;
|
||||
int g_start; /* Start symbol of the grammar */
|
||||
int g_accel; /* Set if accelerators present */
|
||||
} grammar;
|
||||
|
||||
/* FUNCTIONS */
|
||||
|
||||
grammar *newgrammar PROTO((int start));
|
||||
dfa *adddfa PROTO((grammar *g, int type, char *name));
|
||||
int addstate PROTO((dfa *d));
|
||||
void addarc PROTO((dfa *d, int from, int to, int lbl));
|
||||
dfa *finddfa PROTO((grammar *g, int type));
|
||||
char *typename PROTO((grammar *g, int lbl));
|
||||
|
||||
int addlabel PROTO((labellist *ll, int type, char *str));
|
||||
int findlabel PROTO((labellist *ll, int type, char *str));
|
||||
char *labelrepr PROTO((label *lb));
|
||||
void translatelabels PROTO((grammar *g));
|
||||
|
||||
void addfirstsets PROTO((grammar *g));
|
||||
|
||||
void addaccellerators PROTO((grammar *g));
|
7
Include/import.h
Normal file
7
Include/import.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
/* Module definition and import interface */
|
||||
|
||||
void init_modules PROTO(());
|
||||
void close_modules PROTO(());
|
||||
object *new_module PROTO((char *name));
|
||||
void define_module PROTO((struct _context *ctx, char *name));
|
||||
object *import_module PROTO((struct _context *ctx, char *name));
|
48
Include/intobject.h
Normal file
48
Include/intobject.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
/* Integer object interface */
|
||||
|
||||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
intobject represents a (long) integer. This is an immutable object;
|
||||
an integer cannot change its value after creation.
|
||||
|
||||
There are functions to create new integer objects, to test an object
|
||||
for integer-ness, and to get the integer value. The latter functions
|
||||
returns -1 and sets errno to EBADF if the object is not an intobject.
|
||||
None of the functions should be applied to nil objects.
|
||||
|
||||
The type intobject is (unfortunately) exposed bere so we can declare
|
||||
TrueObject and FalseObject below; don't use this.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
OB_HEAD
|
||||
long ob_ival;
|
||||
} intobject;
|
||||
|
||||
extern typeobject Inttype;
|
||||
|
||||
#define is_intobject(op) ((op)->ob_type == &Inttype)
|
||||
|
||||
extern object *newintobject PROTO((long));
|
||||
extern long getintvalue PROTO((object *));
|
||||
|
||||
|
||||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
False and True are special intobjects used by Boolean expressions.
|
||||
All values of type Boolean must point to either of these; but in
|
||||
contexts where integers are required they are integers (valued 0 and 1).
|
||||
Hope these macros don't conflict with other people's.
|
||||
|
||||
Don't forget to apply INCREF() when returning True or False!!!
|
||||
*/
|
||||
|
||||
extern intobject FalseObject, TrueObject; /* Don't use these directly */
|
||||
|
||||
#define False ((object *) &FalseObject)
|
||||
#define True ((object *) &TrueObject)
|
||||
|
||||
/* Macro, trading safety for speed */
|
||||
#define GETINTVALUE(op) ((op)->ob_ival)
|
26
Include/listobject.h
Normal file
26
Include/listobject.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
/* List object interface */
|
||||
|
||||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
Another generally useful object type is an list of object pointers.
|
||||
This is a mutable type: the list items can be changed, and items can be
|
||||
added or removed. Out-of-range indices or non-list objects are ignored.
|
||||
|
||||
*** WARNING *** setlistitem does not increment the new item's reference
|
||||
count, but does decrement the reference count of the item it replaces,
|
||||
if not nil. It does *decrement* the reference count if it is *not*
|
||||
inserted in the list. Similarly, getlistitem does not increment the
|
||||
returned item's reference count.
|
||||
*/
|
||||
|
||||
extern typeobject Listtype;
|
||||
|
||||
#define is_listobject(op) ((op)->ob_type == &Listtype)
|
||||
|
||||
extern object *newlistobject PROTO((int size));
|
||||
extern int getlistsize PROTO((object *));
|
||||
extern object *getlistitem PROTO((object *, int));
|
||||
extern int setlistitem PROTO((object *, int, object *));
|
||||
extern int inslistitem PROTO((object *, int, object *));
|
||||
extern int addlistitem PROTO((object *, object *));
|
6
Include/metagrammar.h
Normal file
6
Include/metagrammar.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#define MSTART 256
|
||||
#define RULE 257
|
||||
#define RHS 258
|
||||
#define ALT 259
|
||||
#define ITEM 260
|
||||
#define ATOM 261
|
11
Include/methodobject.h
Normal file
11
Include/methodobject.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
/* Method object interface */
|
||||
|
||||
extern typeobject Methodtype;
|
||||
|
||||
#define is_methodobject(op) ((op)->ob_type == &Methodtype)
|
||||
|
||||
typedef object *(*method) FPROTO((object *, object *));
|
||||
|
||||
extern object *newmethodobject PROTO((char *, method, object *));
|
||||
extern method getmethod PROTO((object *));
|
||||
extern object *getself PROTO((object *));
|
11
Include/modsupport.h
Normal file
11
Include/modsupport.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
/* Module support interface */
|
||||
|
||||
struct methodlist {
|
||||
char *ml_name;
|
||||
method ml_meth;
|
||||
};
|
||||
|
||||
extern object *findmethod PROTO((struct methodlist *, object *, char *));
|
||||
extern object *initmodule PROTO((char *, struct methodlist *));
|
||||
extern int err_badargs PROTO((void));
|
||||
extern object *err_nomem PROTO((void));
|
9
Include/moduleobject.h
Normal file
9
Include/moduleobject.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
/* Module object interface */
|
||||
|
||||
extern typeobject Moduletype;
|
||||
|
||||
#define is_moduleobject(op) ((op)->ob_type == &Moduletype)
|
||||
|
||||
extern object *newmoduleobject PROTO((char *));
|
||||
extern object *getmoduledict PROTO((object *));
|
||||
extern int setmoduledict PROTO((object *, object *));
|
29
Include/node.h
Normal file
29
Include/node.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
/* Parse tree node interface */
|
||||
|
||||
typedef struct _node {
|
||||
int n_type;
|
||||
char *n_str;
|
||||
int n_nchildren;
|
||||
struct _node *n_child;
|
||||
} node;
|
||||
|
||||
extern node *newnode PROTO((int type));
|
||||
extern node *addchild PROTO((node *n, int type, char *str));
|
||||
|
||||
/* Node access functions */
|
||||
#define NCH(n) ((n)->n_nchildren)
|
||||
#define CHILD(n, i) (&(n)->n_child[i])
|
||||
#define TYPE(n) ((n)->n_type)
|
||||
#define STR(n) ((n)->n_str)
|
||||
|
||||
/* Assert that the type of a node is what we expect */
|
||||
#ifndef DEBUG
|
||||
#define REQ(n, type) { /*pass*/ ; }
|
||||
#else
|
||||
#define REQ(n, type) \
|
||||
{ if (TYPE(n) != (type)) { \
|
||||
fprintf(stderr, "FATAL: node type %d, required %d\n", \
|
||||
TYPE(n), type); \
|
||||
abort(); \
|
||||
} }
|
||||
#endif
|
310
Include/object.h
Normal file
310
Include/object.h
Normal file
|
@ -0,0 +1,310 @@
|
|||
/* Object and type object interface */
|
||||
|
||||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
Objects are structures allocated on the heap. Special rules apply to
|
||||
the use of objects to ensure they are properly garbage-collected.
|
||||
Objects are never allocated statically or on the stack; they must be
|
||||
accessed through special macros and functions only. (Type objects are
|
||||
exceptions to the first rule; the standard types are represented by
|
||||
statically initialized type objects.)
|
||||
|
||||
An object has a 'reference count' that is increased or decreased when a
|
||||
pointer to the object is copied or deleted; when the reference count
|
||||
reaches zero there are no references to the object left and it can be
|
||||
removed from the heap.
|
||||
|
||||
An object has a 'type' that determines what it represents and what kind
|
||||
of data it contains. An object's type is fixed when it is created.
|
||||
Types themselves are represented as objects; an object contains a
|
||||
pointer to the corresponding type object. The type itself has a type
|
||||
pointer pointing to the object representing the type 'type', which
|
||||
contains a pointer to itself!).
|
||||
|
||||
Objects do not float around in memory; once allocated an object keeps
|
||||
the same size and address. Objects that must hold variable-size data
|
||||
can contain pointers to variable-size parts of the object. Not all
|
||||
objects of the same type have the same size; but the size cannot change
|
||||
after allocation. (These restrictions are made so a reference to an
|
||||
object can be simply a pointer -- moving an object would require
|
||||
updating all the pointers, and changing an object's size would require
|
||||
moving it if there was another object right next to it.)
|
||||
|
||||
Objects are always accessed through pointers of the type 'object *'.
|
||||
The type 'object' is a structure that only contains the reference count
|
||||
and the type pointer. The actual memory allocated for an object
|
||||
contains other data that can only be accessed after casting the pointer
|
||||
to a pointer to a longer structure type. This longer type must start
|
||||
with the reference count and type fields; the macro OB_HEAD should be
|
||||
used for this (to accomodate for future changes). The implementation
|
||||
of a particular object type can cast the object pointer to the proper
|
||||
type and back.
|
||||
|
||||
A standard interface exists for objects that contain an array of items
|
||||
whose size is determined when the object is allocated.
|
||||
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
*/
|
||||
|
||||
#ifdef THINK_C
|
||||
/* Debugging options for THINK_C (which has no -D compiler option): */
|
||||
/*#define TRACE_REFS*/
|
||||
/*#define REF_DEBUG*/
|
||||
#endif
|
||||
|
||||
#ifdef TRACE_REFS
|
||||
#define OB_HEAD \
|
||||
struct _object *_ob_next, *_ob_prev; \
|
||||
unsigned int ob_refcnt; \
|
||||
struct _typeobject *ob_type;
|
||||
#define OB_HEAD_INIT(type) 0, 0, 1, type,
|
||||
#else
|
||||
#define OB_HEAD \
|
||||
unsigned int ob_refcnt; \
|
||||
struct _typeobject *ob_type;
|
||||
#define OB_HEAD_INIT(type) 1, type,
|
||||
#endif
|
||||
|
||||
#define OB_VARHEAD \
|
||||
OB_HEAD \
|
||||
unsigned int ob_size; /* Number of items in variable part */
|
||||
|
||||
typedef struct _object {
|
||||
OB_HEAD
|
||||
} object;
|
||||
|
||||
typedef struct {
|
||||
OB_VARHEAD
|
||||
} varobject;
|
||||
|
||||
|
||||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
Type objects contain a string containing the type name (to help somewhat
|
||||
in debugging), the allocation parameters (see newobj() and newvarobj()),
|
||||
and methods for accessing objects of the type. Methods are optional,a
|
||||
nil pointer meaning that particular kind of access is not available for
|
||||
this type. The DECREF() macro uses the tp_dealloc method without
|
||||
checking for a nil pointer; it should always be implemented except if
|
||||
the implementation can guarantee that the reference count will never
|
||||
reach zero (e.g., for type objects).
|
||||
|
||||
NB: the methods for certain type groups are now contained in separate
|
||||
method blocks.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
object *(*nb_add) FPROTO((object *, object *));
|
||||
object *(*nb_subtract) FPROTO((object *, object *));
|
||||
object *(*nb_multiply) FPROTO((object *, object *));
|
||||
object *(*nb_divide) FPROTO((object *, object *));
|
||||
object *(*nb_remainder) FPROTO((object *, object *));
|
||||
object *(*nb_power) FPROTO((object *, object *));
|
||||
object *(*nb_negative) FPROTO((object *));
|
||||
object *(*nb_positive) FPROTO((object *));
|
||||
} number_methods;
|
||||
|
||||
typedef struct {
|
||||
int (*sq_length) FPROTO((object *));
|
||||
object *(*sq_concat) FPROTO((object *, object *));
|
||||
object *(*sq_repeat) FPROTO((object *, int));
|
||||
object *(*sq_item) FPROTO((object *, int));
|
||||
object *(*sq_slice) FPROTO((object *, int, int));
|
||||
int (*sq_ass_item) FPROTO((object *, int, object *));
|
||||
int (*sq_ass_slice) FPROTO((object *, int, int, object *));
|
||||
} sequence_methods;
|
||||
|
||||
typedef struct {
|
||||
int (*mp_length) FPROTO((object *));
|
||||
object *(*mp_subscript) FPROTO((object *, object *));
|
||||
int (*mp_ass_subscript) FPROTO((object *, object *, object *));
|
||||
} mapping_methods;
|
||||
|
||||
typedef struct _typeobject {
|
||||
OB_VARHEAD
|
||||
char *tp_name; /* For printing */
|
||||
unsigned int tp_basicsize, tp_itemsize; /* For allocation */
|
||||
|
||||
/* Methods to implement standard operations */
|
||||
|
||||
void (*tp_dealloc) FPROTO((object *));
|
||||
void (*tp_print) FPROTO((object *, FILE *, int));
|
||||
object *(*tp_getattr) FPROTO((object *, char *));
|
||||
int (*tp_setattr) FPROTO((object *, char *, object *));
|
||||
int (*tp_compare) FPROTO((object *, object *));
|
||||
object *(*tp_repr) FPROTO((object *));
|
||||
|
||||
/* Method suites for standard classes */
|
||||
|
||||
number_methods *tp_as_number;
|
||||
sequence_methods *tp_as_sequence;
|
||||
mapping_methods *tp_as_mapping;
|
||||
} typeobject;
|
||||
|
||||
extern typeobject Typetype; /* The type of type objects */
|
||||
|
||||
#define is_typeobject(op) ((op)->ob_type == &Typetype)
|
||||
|
||||
extern void printobject PROTO((object *, FILE *, int));
|
||||
extern object * reprobject PROTO((object *));
|
||||
extern int cmpobject PROTO((object *, object *));
|
||||
|
||||
/* Flag bits for printing: */
|
||||
#define PRINT_RAW 1 /* No string quotes etc. */
|
||||
|
||||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
The macros INCREF(op) and DECREF(op) are used to increment or decrement
|
||||
reference counts. DECREF calls the object's deallocator function; for
|
||||
objects that don't contain references to other objects or heap memory
|
||||
this can be the standard function free(). Both macros can be used
|
||||
whereever a void expression is allowed. The argument shouldn't be a
|
||||
NIL pointer. The macro NEWREF(op) is used only to initialize reference
|
||||
counts to 1; it is defined here for convenience.
|
||||
|
||||
We assume that the reference count field can never overflow; this can
|
||||
be proven when the size of the field is the same as the pointer size
|
||||
but even with a 16-bit reference count field it is pretty unlikely so
|
||||
we ignore the possibility. (If you are paranoid, make it a long.)
|
||||
|
||||
Type objects should never be deallocated; the type pointer in an object
|
||||
is not considered to be a reference to the type object, to save
|
||||
complications in the deallocation function. (This is actually a
|
||||
decision that's up to the implementer of each new type so if you want,
|
||||
you can count such references to the type object.)
|
||||
|
||||
*** WARNING*** The DECREF macro must have a side-effect-free argument
|
||||
since it may evaluate its argument multiple times. (The alternative
|
||||
would be to mace it a proper function or assign it to a global temporary
|
||||
variable first, both of which are slower; and in a multi-threaded
|
||||
environment the global variable trick is not safe.)
|
||||
*/
|
||||
|
||||
#ifdef TRACE_REFS
|
||||
#ifndef REF_DEBUG
|
||||
#define REF_DEBUG
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef TRACE_REFS
|
||||
#define DELREF(op) (*(op)->ob_type->tp_dealloc)((object *)(op))
|
||||
#endif
|
||||
|
||||
#ifdef REF_DEBUG
|
||||
extern long ref_total;
|
||||
#ifndef TRACE_REFS
|
||||
#define NEWREF(op) (ref_total++, (op)->ob_refcnt = 1)
|
||||
#endif
|
||||
#define INCREF(op) (ref_total++, (op)->ob_refcnt++)
|
||||
#define DECREF(op) \
|
||||
if (--ref_total, --(op)->ob_refcnt != 0) \
|
||||
; \
|
||||
else \
|
||||
DELREF(op)
|
||||
#else
|
||||
#define NEWREF(op) ((op)->ob_refcnt = 1)
|
||||
#define INCREF(op) ((op)->ob_refcnt++)
|
||||
#define DECREF(op) \
|
||||
if (--(op)->ob_refcnt != 0) \
|
||||
; \
|
||||
else \
|
||||
DELREF(op)
|
||||
#endif
|
||||
|
||||
|
||||
/* Definition of NULL, so you don't have to include <stdio.h> */
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
NoObject is an object of undefined type which can be used in contexts
|
||||
where NULL (nil) is not suitable (since NULL often means 'error').
|
||||
|
||||
Don't forget to apply INCREF() when returning this value!!!
|
||||
*/
|
||||
|
||||
extern object NoObject; /* Don't use this directly */
|
||||
|
||||
#define None (&NoObject)
|
||||
|
||||
|
||||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
More conventions
|
||||
================
|
||||
|
||||
Argument Checking
|
||||
-----------------
|
||||
|
||||
Functions that take objects as arguments normally don't check for nil
|
||||
arguments, but they do check the type of the argument, and return an
|
||||
error if the function doesn't apply to the type.
|
||||
|
||||
Failure Modes
|
||||
-------------
|
||||
|
||||
Functions may fail for a variety of reasons, including running out of
|
||||
memory. This is communicated to the caller in two ways: 'errno' is set
|
||||
to indicate the error, and the function result differs: functions that
|
||||
normally return a pointer return nil for failure, functions returning
|
||||
an integer return -1 (which can be a legal return value too!), and
|
||||
other functions return 0 for success and the error number for failure.
|
||||
Callers should always check for errors before using the result. The
|
||||
following error codes are used:
|
||||
|
||||
EBADF bad object type (first argument only)
|
||||
EINVAL bad argument type (second and further arguments)
|
||||
ENOMEM no memory (malloc failed)
|
||||
ENOENT key not found in dictionary
|
||||
EDOM index out of range or division by zero
|
||||
ERANGE result not representable
|
||||
|
||||
XXX any others?
|
||||
|
||||
Reference Counts
|
||||
----------------
|
||||
|
||||
It takes a while to get used to the proper usage of reference counts.
|
||||
|
||||
Functions that create an object set the reference count to 1; such new
|
||||
objects must be stored somewhere or destroyed again with DECREF().
|
||||
Functions that 'store' objects such as settupleitem() and dictinsert()
|
||||
don't increment the reference count of the object, since the most
|
||||
frequent use is to store a fresh object. Functions that 'retrieve'
|
||||
objects such as gettupleitem() and dictlookup() also don't increment
|
||||
the reference count, since most frequently the object is only looked at
|
||||
quickly. Thus, to retrieve an object and store it again, the caller
|
||||
must call INCREF() explicitly.
|
||||
|
||||
NOTE: functions that 'consume' a reference count like dictinsert() even
|
||||
consume the reference if the object wasn't stored, to simplify error
|
||||
handling.
|
||||
|
||||
It seems attractive to make other functions that take an object as
|
||||
argument consume a reference count; however this may quickly get
|
||||
confusing (even the current practice is already confusing). Consider
|
||||
it carefully, it may safe lots of calls to INCREF() and DECREF() at
|
||||
times.
|
||||
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
*/
|
||||
|
||||
/* Error number interface */
|
||||
#include <errno.h>
|
||||
|
||||
#ifndef errno
|
||||
extern int errno;
|
||||
#endif
|
||||
|
||||
#ifdef THINK_C
|
||||
/* Lightspeed C doesn't define these in <errno.h> */
|
||||
#define EDOM 33
|
||||
#define ERANGE 34
|
||||
#endif
|
31
Include/objimpl.h
Normal file
31
Include/objimpl.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
Additional macros for modules that implement new object types.
|
||||
You must first include "object.h".
|
||||
|
||||
NEWOBJ(type, typeobj) allocates memory for a new object of the given
|
||||
type; here 'type' must be the C structure type used to represent the
|
||||
object and 'typeobj' the address of the corresponding type object.
|
||||
Reference count and type pointer are filled in; the rest of the bytes of
|
||||
the object are *undefined*! The resulting expression type is 'type *'.
|
||||
The size of the object is actually determined by the tp_basicsize field
|
||||
of the type object.
|
||||
|
||||
NEWVAROBJ(type, typeobj, n) is similar but allocates a variable-size
|
||||
object with n extra items. The size is computer as tp_basicsize plus
|
||||
n * tp_itemsize. This fills in the ob_size field as well.
|
||||
*/
|
||||
|
||||
extern object *newobject PROTO((typeobject *));
|
||||
extern varobject *newvarobject PROTO((typeobject *, unsigned int));
|
||||
|
||||
#define NEWOBJ(type, typeobj) ((type *) newobject(typeobj))
|
||||
#define NEWVAROBJ(type, typeobj, n) ((type *) newvarobject(typeobj, n))
|
||||
|
||||
extern int StopPrint; /* Set when printing is interrupted */
|
||||
|
||||
/* Malloc interface */
|
||||
#include "malloc.h"
|
||||
|
||||
extern char *strdup PROTO((char *));
|
9
Include/parsetok.h
Normal file
9
Include/parsetok.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
/* Parser-tokenizer link interface */
|
||||
|
||||
#if 0
|
||||
extern int parsetok PROTO((struct tok_state *, grammar *, int start,
|
||||
node **n_ret));
|
||||
#endif
|
||||
extern int parsestring PROTO((char *, grammar *, int start, node **n_ret));
|
||||
extern int parsefile PROTO((FILE *, grammar *, int start,
|
||||
char *ps1, char *ps2, node **n_ret));
|
17
Include/pyerrors.h
Normal file
17
Include/pyerrors.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
/* Error handling definitions */
|
||||
|
||||
void err_set PROTO((object *));
|
||||
void err_setval PROTO((object *, object *));
|
||||
void err_setstr PROTO((object *, char *));
|
||||
int err_occurred PROTO((void));
|
||||
void err_get PROTO((object **, object **));
|
||||
void err_clear PROTO((void));
|
||||
|
||||
/* Predefined exceptions (in run.c) */
|
||||
object *RuntimeError; /* Raised by error() */
|
||||
object *EOFError; /* Raised by eof_error() */
|
||||
object *TypeError; /* Rased by type_error() */
|
||||
object *MemoryError; /* Raised by mem_error() */
|
||||
object *NameError; /* Raised by name_error() */
|
||||
object *SystemError; /* Raised by sys_error() */
|
||||
object *KeyboardInterrupt; /* Raised by intr_error() */
|
39
Include/stringobject.h
Normal file
39
Include/stringobject.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
/* String object interface */
|
||||
|
||||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
Type stringobject represents a character string. An extra zero byte is
|
||||
reserved at the end to ensure it is zero-terminated, but a size is
|
||||
present so strings with null bytes in them can be represented. This
|
||||
is an immutable object type.
|
||||
|
||||
There are functions to create new string objects, to test
|
||||
an object for string-ness, and to get the
|
||||
string value. The latter function returns a null pointer
|
||||
if the object is not of the proper type.
|
||||
There is a variant that takes an explicit size as well as a
|
||||
variant that assumes a zero-terminated string. Note that none of the
|
||||
functions should be applied to nil objects.
|
||||
*/
|
||||
|
||||
/* NB The type is revealed here only because it is used in dictobject.c */
|
||||
|
||||
typedef struct {
|
||||
OB_VARHEAD
|
||||
char ob_sval[1];
|
||||
} stringobject;
|
||||
|
||||
extern typeobject Stringtype;
|
||||
|
||||
#define is_stringobject(op) ((op)->ob_type == &Stringtype)
|
||||
|
||||
extern object *newsizedstringobject PROTO((char *, int));
|
||||
extern object *newstringobject PROTO((char *));
|
||||
extern unsigned int getstringsize PROTO((object *));
|
||||
extern char *getstringvalue PROTO((object *));
|
||||
extern void joinstring PROTO((object **, object *));
|
||||
extern int resizestring PROTO((object **, int));
|
||||
|
||||
/* Macro, trading safety for speed */
|
||||
#define GETSTRINGVALUE(op) ((op)->ob_sval)
|
6
Include/sysmodule.h
Normal file
6
Include/sysmodule.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
/* System module interface */
|
||||
|
||||
object *sysget PROTO((char *));
|
||||
int sysset PROTO((char *, object *));
|
||||
FILE *sysgetfile PROTO((char *, FILE *));
|
||||
void initsys PROTO((int, char **));
|
45
Include/token.h
Normal file
45
Include/token.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
/* Token types */
|
||||
|
||||
#define ENDMARKER 0
|
||||
#define NAME 1
|
||||
#define NUMBER 2
|
||||
#define STRING 3
|
||||
#define NEWLINE 4
|
||||
#define INDENT 5
|
||||
#define DEDENT 6
|
||||
#define LPAR 7
|
||||
#define RPAR 8
|
||||
#define LSQB 9
|
||||
#define RSQB 10
|
||||
#define COLON 11
|
||||
#define COMMA 12
|
||||
#define SEMI 13
|
||||
#define PLUS 14
|
||||
#define MINUS 15
|
||||
#define STAR 16
|
||||
#define SLASH 17
|
||||
#define VBAR 18
|
||||
#define AMPER 19
|
||||
#define LESS 20
|
||||
#define GREATER 21
|
||||
#define EQUAL 22
|
||||
#define DOT 23
|
||||
#define PERCENT 24
|
||||
#define BACKQUOTE 25
|
||||
#define LBRACE 26
|
||||
#define RBRACE 27
|
||||
#define OP 28
|
||||
#define ERRORTOKEN 29
|
||||
#define N_TOKENS 30
|
||||
|
||||
/* Special definitions for cooperation with parser */
|
||||
|
||||
#define NT_OFFSET 256
|
||||
|
||||
#define ISTERMINAL(x) ((x) < NT_OFFSET)
|
||||
#define ISNONTERMINAL(x) ((x) >= NT_OFFSET)
|
||||
#define ISEOF(x) ((x) == ENDMARKER)
|
||||
|
||||
|
||||
extern char *tok_name[]; /* Token names */
|
||||
extern int tok_1char PROTO((int));
|
24
Include/tupleobject.h
Normal file
24
Include/tupleobject.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
/* Tuple object interface */
|
||||
|
||||
/*
|
||||
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
|
||||
|
||||
Another generally useful object type is an tuple of object pointers.
|
||||
This is a mutable type: the tuple items can be changed (but not their
|
||||
number). Out-of-range indices or non-tuple objects are ignored.
|
||||
|
||||
*** WARNING *** settupleitem does not increment the new item's reference
|
||||
count, but does decrement the reference count of the item it replaces,
|
||||
if not nil. It does *decrement* the reference count if it is *not*
|
||||
inserted in the tuple. Similarly, gettupleitem does not increment the
|
||||
returned item's reference count.
|
||||
*/
|
||||
|
||||
extern typeobject Tupletype;
|
||||
|
||||
#define is_tupleobject(op) ((op)->ob_type == &Tupletype)
|
||||
|
||||
extern object *newtupleobject PROTO((int size));
|
||||
extern int gettuplesize PROTO((object *));
|
||||
extern object *gettupleitem PROTO((object *, int));
|
||||
extern int settupleitem PROTO((object *, int, object *));
|
Loading…
Add table
Add a link
Reference in a new issue