Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 85a5fbb

Browse files
committed
Initial revision
1 parent c636014 commit 85a5fbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+13589
-0
lines changed

Grammar/Grammar

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Grammar for Python, version 3
2+
3+
# Changes compared to version 2:
4+
# The syntax of Boolean operations is changed to use more
5+
# conventional priorities: or < and < not.
6+
7+
# Changes compared to version 1:
8+
# modules and scripts are unified;
9+
# 'quit' is gone (use ^D);
10+
# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
11+
# 'import' and 'def' aren't special any more;
12+
# added 'from' NAME option on import clause, and '*' to import all;
13+
# added class definition.
14+
# TO DO:
15+
# replace 'dir' by something more general?
16+
17+
# Start symbols for the grammar:
18+
# single_input is a single interactive statement;
19+
# file_input is a module or sequence of commands read from an input file;
20+
# expr_input is the input for the input() function;
21+
# eval_input is the input for the eval() function.
22+
23+
# NB: compound_stmt in single_input is followed by extra NEWLINE!
24+
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
25+
file_input: (NEWLINE | stmt)* ENDMARKER
26+
expr_input: testlist NEWLINE
27+
eval_input: testlist ENDMARKER
28+
29+
funcdef: 'def' NAME parameters ':' suite
30+
parameters: '(' [fplist] ')'
31+
fplist: fpdef (',' fpdef)*
32+
fpdef: NAME | '(' fplist ')'
33+
34+
stmt: simple_stmt | compound_stmt
35+
simple_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | dir_stmt | flow_stmt | import_stmt
36+
expr_stmt: (exprlist '=')* exprlist NEWLINE
37+
# For assignments, additional restrictions enforced by the interpreter
38+
print_stmt: 'print' (test ',')* [test] NEWLINE
39+
del_stmt: 'del' exprlist NEWLINE
40+
dir_stmt: 'dir' [expr] NEWLINE
41+
pass_stmt: 'pass' NEWLINE
42+
flow_stmt: break_stmt | return_stmt | raise_stmt
43+
break_stmt: 'break' NEWLINE
44+
return_stmt: 'return' [testlist] NEWLINE
45+
raise_stmt: 'raise' expr [',' expr] NEWLINE
46+
import_stmt: 'import' NAME (',' NAME)* NEWLINE | 'from' NAME 'import' ('*' | NAME (',' NAME)*) NEWLINE
47+
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
48+
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
49+
while_stmt: 'while' test ':' suite ['else' ':' suite]
50+
for_stmt: 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
51+
try_stmt: 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite]
52+
except_clause: 'except' [expr [',' expr]]
53+
suite: simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
54+
55+
test: and_test ('or' and_test)*
56+
and_test: not_test ('and' not_test)*
57+
not_test: 'not' not_test | comparison
58+
comparison: expr (comp_op expr)*
59+
comp_op: '<'|'>'|'='|'>' '='|'<' '='|'<' '>'|'in'|'not' 'in'|'is'|'is' 'not'
60+
expr: term (('+'|'-') term)*
61+
term: factor (('*'|'/'|'%') factor)*
62+
factor: ('+'|'-') factor | atom trailer*
63+
atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' '}' | '`' testlist '`' | NAME | NUMBER | STRING
64+
trailer: '(' [exprlist] ')' | '[' subscript ']' | '.' NAME
65+
subscript: expr | [expr] ':' [expr]
66+
exprlist: expr (',' expr)* [',']
67+
testlist: test (',' test)* [',']
68+
69+
classdef: 'class' NAME parameters ['=' baselist] ':' suite
70+
baselist: atom arguments (',' atom arguments)*
71+
arguments: '(' [testlist] ')'

Include/assert.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define assert(e) { if (!(e)) { printf("Assertion failed\n"); abort(); } }

Include/bitset.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Bitset interface */
2+
3+
#define BYTE char
4+
5+
typedef BYTE *bitset;
6+
7+
bitset newbitset PROTO((int nbits));
8+
void delbitset PROTO((bitset bs));
9+
/* int testbit PROTO((bitset bs, int ibit)); /* Now a macro, see below */
10+
int addbit PROTO((bitset bs, int ibit)); /* Returns 0 if already set */
11+
int samebitset PROTO((bitset bs1, bitset bs2, int nbits));
12+
void mergebitset PROTO((bitset bs1, bitset bs2, int nbits));
13+
14+
#define BITSPERBYTE (8*sizeof(BYTE))
15+
#define NBYTES(nbits) (((nbits) + BITSPERBYTE - 1) / BITSPERBYTE)
16+
17+
#define BIT2BYTE(ibit) ((ibit) / BITSPERBYTE)
18+
#define BIT2SHIFT(ibit) ((ibit) % BITSPERBYTE)
19+
#define BIT2MASK(ibit) (1 << BIT2SHIFT(ibit))
20+
#define BYTE2BIT(ibyte) ((ibyte) * BITSPERBYTE)
21+
22+
#define testbit(ss, ibit) (((ss)[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0)

Include/cgensupport.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Definitions used by cgen output */
2+
3+
typedef char *string;
4+
5+
#define mknewlongobject(x) newintobject(x)
6+
#define mknewshortobject(x) newintobject((long)x)
7+
#define mknewfloatobject(x) newfloatobject(x)
8+
9+
extern object *mknewcharobject PROTO((int c));
10+
11+
extern int getiobjectarg PROTO((object *args, int nargs, int i, object **p_a));
12+
extern int getilongarg PROTO((object *args, int nargs, int i, long *p_a));
13+
extern int getishortarg PROTO((object *args, int nargs, int i, short *p_a));
14+
extern int getifloatarg PROTO((object *args, int nargs, int i, float *p_a));
15+
extern int getistringarg PROTO((object *args, int nargs, int i, string *p_a));

Include/classobject.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Class object interface */
2+
3+
/*
4+
Classes are really hacked in at the last moment.
5+
It should be possible to use other object types as base classes,
6+
but currently it isn't. We'll see if we can fix that later, sigh...
7+
*/
8+
9+
extern typeobject Classtype, Classmembertype, Classmethodtype;
10+
11+
#define is_classobject(op) ((op)->ob_type == &Classtype)
12+
#define is_classmemberobject(op) ((op)->ob_type == &Classmembertype)
13+
#define is_classmethodobject(op) ((op)->ob_type == &Classmethodtype)
14+
15+
extern object *newclassobject PROTO((node *, object *, object *));
16+
extern object *newclassmemberobject PROTO((object *));
17+
extern object *newclassmethodobject PROTO((object *, object *));
18+
19+
extern object *classmethodgetfunc PROTO((object *));
20+
extern object *classmethodgetself PROTO((object *));

Include/dictobject.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Dictionary object type -- mapping from char * to object.
3+
NB: the key is given as a char *, not as a stringobject.
4+
These functions set errno for errors. Functions dictremove() and
5+
dictinsert() return nonzero for errors, getdictsize() returns -1,
6+
the others NULL. A successful call to dictinsert() calls INCREF()
7+
for the inserted item.
8+
*/
9+
10+
extern typeobject Dicttype;
11+
12+
#define is_dictobject(op) ((op)->ob_type == &Dicttype)
13+
14+
extern object *newdictobject PROTO((void));
15+
extern object *dictlookup PROTO((object *dp, char *key));
16+
extern int dictinsert PROTO((object *dp, char *key, object *item));
17+
extern int dictremove PROTO((object *dp, char *key));
18+
extern int getdictsize PROTO((object *dp));
19+
extern char *getdictkey PROTO((object *dp, int i));
20+
21+
/* New interface with (string)object * instead of char * arguments */
22+
extern object *dict2lookup PROTO((object *dp, object *key));
23+
extern int dict2insert PROTO((object *dp, object *key, object *item));
24+
extern int dict2remove PROTO((object *dp, object *key));
25+
extern object *getdict2key PROTO((object *dp, int i));

Include/errcode.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Error codes passed around between file input, tokenizer, parser and
2+
interpreter. This was necessary so we can turn them into Python
3+
exceptions at a higher level. */
4+
5+
#define E_OK 10 /* No error */
6+
#define E_EOF 11 /* (Unexpected) EOF read */
7+
#define E_INTR 12 /* Interrupted */
8+
#define E_TOKEN 13 /* Bad token */
9+
#define E_SYNTAX 14 /* Syntax error */
10+
#define E_NOMEM 15 /* Ran out of memory */
11+
#define E_DONE 16 /* Parsing complete */
12+
#define E_ERROR 17 /* Execution error */

Include/errors.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Error handling definitions */
2+
3+
void err_set PROTO((object *));
4+
void err_setval PROTO((object *, object *));
5+
void err_setstr PROTO((object *, char *));
6+
int err_occurred PROTO((void));
7+
void err_get PROTO((object **, object **));
8+
void err_clear PROTO((void));
9+
10+
/* Predefined exceptions (in run.c) */
11+
object *RuntimeError; /* Raised by error() */
12+
object *EOFError; /* Raised by eof_error() */
13+
object *TypeError; /* Rased by type_error() */
14+
object *MemoryError; /* Raised by mem_error() */
15+
object *NameError; /* Raised by name_error() */
16+
object *SystemError; /* Raised by sys_error() */
17+
object *KeyboardInterrupt; /* Raised by intr_error() */

Include/fileobject.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* File object interface */
2+
3+
extern typeobject Filetype;
4+
5+
#define is_fileobject(op) ((op)->ob_type == &Filetype)
6+
7+
extern object *newfileobject PROTO((char *, char *));
8+
extern object *newopenfileobject PROTO((FILE *, char *, char *));
9+
extern FILE *getfilefile PROTO((object *));

Include/floatobject.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Float object interface */
2+
3+
/*
4+
floatobject represents a (double precision) floating point number.
5+
*/
6+
7+
typedef struct {
8+
OB_HEAD
9+
double ob_fval;
10+
} floatobject;
11+
12+
extern typeobject Floattype;
13+
14+
#define is_floatobject(op) ((op)->ob_type == &Floattype)
15+
16+
extern object *newfloatobject PROTO((double));
17+
extern double getfloatvalue PROTO((object *));
18+
19+
/* Macro, trading safety for speed */
20+
#define GETFLOATVALUE(op) ((op)->ob_fval)

Include/funcobject.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* Function object interface */
2+
3+
extern typeobject Functype;
4+
5+
#define is_funcobject(op) ((op)->ob_type == &Functype)
6+
7+
extern object *newfuncobject PROTO((node *, object *));
8+
extern node *getfuncnode PROTO((object *));
9+
extern object *getfuncglobals PROTO((object *));

Include/graminit.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#define single_input 256
2+
#define file_input 257
3+
#define expr_input 258
4+
#define eval_input 259
5+
#define funcdef 260
6+
#define parameters 261
7+
#define fplist 262
8+
#define fpdef 263
9+
#define stmt 264
10+
#define simple_stmt 265
11+
#define expr_stmt 266
12+
#define print_stmt 267
13+
#define del_stmt 268
14+
#define dir_stmt 269
15+
#define pass_stmt 270
16+
#define flow_stmt 271
17+
#define break_stmt 272
18+
#define return_stmt 273
19+
#define raise_stmt 274
20+
#define import_stmt 275
21+
#define compound_stmt 276
22+
#define if_stmt 277
23+
#define while_stmt 278
24+
#define for_stmt 279
25+
#define try_stmt 280
26+
#define except_clause 281
27+
#define suite 282
28+
#define test 283
29+
#define and_test 284
30+
#define not_test 285
31+
#define comparison 286
32+
#define comp_op 287
33+
#define expr 288
34+
#define term 289
35+
#define factor 290
36+
#define atom 291
37+
#define trailer 292
38+
#define subscript 293
39+
#define exprlist 294
40+
#define testlist 295
41+
#define classdef 296
42+
#define baselist 297
43+
#define arguments 298

Include/grammar.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Grammar interface */
2+
3+
#include "bitset.h" /* Sigh... */
4+
5+
/* A label of an arc */
6+
7+
typedef struct _label {
8+
int lb_type;
9+
char *lb_str;
10+
} label;
11+
12+
#define EMPTY 0 /* Label number 0 is by definition the empty label */
13+
14+
/* A list of labels */
15+
16+
typedef struct _labellist {
17+
int ll_nlabels;
18+
label *ll_label;
19+
} labellist;
20+
21+
/* An arc from one state to another */
22+
23+
typedef struct _arc {
24+
short a_lbl; /* Label of this arc */
25+
short a_arrow; /* State where this arc goes to */
26+
} arc;
27+
28+
/* A state in a DFA */
29+
30+
typedef struct _state {
31+
int s_narcs;
32+
arc *s_arc; /* Array of arcs */
33+
34+
/* Optional accelerators */
35+
int s_lower; /* Lowest label index */
36+
int s_upper; /* Highest label index */
37+
int *s_accel; /* Accelerator */
38+
int s_accept; /* Nonzero for accepting state */
39+
} state;
40+
41+
/* A DFA */
42+
43+
typedef struct _dfa {
44+
int d_type; /* Non-terminal this represents */
45+
char *d_name; /* For printing */
46+
int d_initial; /* Initial state */
47+
int d_nstates;
48+
state *d_state; /* Array of states */
49+
bitset d_first;
50+
} dfa;
51+
52+
/* A grammar */
53+
54+
typedef struct _grammar {
55+
int g_ndfas;
56+
dfa *g_dfa; /* Array of DFAs */
57+
labellist g_ll;
58+
int g_start; /* Start symbol of the grammar */
59+
int g_accel; /* Set if accelerators present */
60+
} grammar;
61+
62+
/* FUNCTIONS */
63+
64+
grammar *newgrammar PROTO((int start));
65+
dfa *adddfa PROTO((grammar *g, int type, char *name));
66+
int addstate PROTO((dfa *d));
67+
void addarc PROTO((dfa *d, int from, int to, int lbl));
68+
dfa *finddfa PROTO((grammar *g, int type));
69+
char *typename PROTO((grammar *g, int lbl));
70+
71+
int addlabel PROTO((labellist *ll, int type, char *str));
72+
int findlabel PROTO((labellist *ll, int type, char *str));
73+
char *labelrepr PROTO((label *lb));
74+
void translatelabels PROTO((grammar *g));
75+
76+
void addfirstsets PROTO((grammar *g));
77+
78+
void addaccellerators PROTO((grammar *g));

Include/import.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* Module definition and import interface */
2+
3+
void init_modules PROTO(());
4+
void close_modules PROTO(());
5+
object *new_module PROTO((char *name));
6+
void define_module PROTO((struct _context *ctx, char *name));
7+
object *import_module PROTO((struct _context *ctx, char *name));

0 commit comments

Comments
 (0)