Skip to content

Commit

Permalink
increment and decrement
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybaloney committed Apr 17, 2017
1 parent 584f24b commit fd7c20c
Show file tree
Hide file tree
Showing 15 changed files with 1,243 additions and 1,014 deletions.
5 changes: 4 additions & 1 deletion Grammar/Grammar
Expand Up @@ -39,13 +39,15 @@ simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
('=' (yield_expr|testlist_star_expr))*)
('=' (yield_expr|testlist_star_expr))* | incr_stmt | decr_stmt)
annassign: ':' test ['=' test]
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
'<<=' | '>>=' | '**=' | '//=')
# For normal and annotated assignments, additional restrictions enforced by the interpreter
del_stmt: 'del' exprlist
incr_stmt: '++'
decr_stmt: '--'
pass_stmt: 'pass'
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
break_stmt: 'break'
Expand Down Expand Up @@ -147,3 +149,4 @@ encoding_decl: NAME

yield_expr: 'yield' [yield_arg]
yield_arg: 'from' test | testlist

26 changes: 20 additions & 6 deletions Include/Python-ast.h
Expand Up @@ -66,12 +66,12 @@ struct _mod {

enum _stmt_kind {FunctionDef_kind=1, AsyncFunctionDef_kind=2, ClassDef_kind=3,
Return_kind=4, Delete_kind=5, Assign_kind=6,
AugAssign_kind=7, AnnAssign_kind=8, For_kind=9,
AsyncFor_kind=10, While_kind=11, If_kind=12, With_kind=13,
AsyncWith_kind=14, Raise_kind=15, Try_kind=16,
Assert_kind=17, Import_kind=18, ImportFrom_kind=19,
Global_kind=20, Nonlocal_kind=21, Expr_kind=22, Pass_kind=23,
Break_kind=24, Continue_kind=25};
AugAssign_kind=7, Increment_kind=8, Decrement_kind=9,
AnnAssign_kind=10, For_kind=11, AsyncFor_kind=12,
While_kind=13, If_kind=14, With_kind=15, AsyncWith_kind=16,
Raise_kind=17, Try_kind=18, Assert_kind=19, Import_kind=20,
ImportFrom_kind=21, Global_kind=22, Nonlocal_kind=23,
Expr_kind=24, Pass_kind=25, Break_kind=26, Continue_kind=27};
struct _stmt {
enum _stmt_kind kind;
union {
Expand Down Expand Up @@ -121,6 +121,14 @@ struct _stmt {
expr_ty value;
} AugAssign;

struct {
expr_ty target;
} Increment;

struct {
expr_ty target;
} Decrement;

struct {
expr_ty target;
expr_ty annotation;
Expand Down Expand Up @@ -475,6 +483,12 @@ stmt_ty _Py_Assign(asdl_seq * targets, expr_ty value, int lineno, int
#define AugAssign(a0, a1, a2, a3, a4, a5) _Py_AugAssign(a0, a1, a2, a3, a4, a5)
stmt_ty _Py_AugAssign(expr_ty target, operator_ty op, expr_ty value, int
lineno, int col_offset, PyArena *arena);
#define Increment(a0, a1, a2, a3) _Py_Increment(a0, a1, a2, a3)
stmt_ty _Py_Increment(expr_ty target, int lineno, int col_offset, PyArena
*arena);
#define Decrement(a0, a1, a2, a3) _Py_Decrement(a0, a1, a2, a3)
stmt_ty _Py_Decrement(expr_ty target, int lineno, int col_offset, PyArena
*arena);
#define AnnAssign(a0, a1, a2, a3, a4, a5, a6) _Py_AnnAssign(a0, a1, a2, a3, a4, a5, a6)
stmt_ty _Py_AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int
simple, int lineno, int col_offset, PyArena *arena);
Expand Down
132 changes: 67 additions & 65 deletions Include/graminit.h
Expand Up @@ -21,68 +21,70 @@
#define testlist_star_expr 274
#define augassign 275
#define del_stmt 276
#define pass_stmt 277
#define flow_stmt 278
#define break_stmt 279
#define continue_stmt 280
#define return_stmt 281
#define yield_stmt 282
#define raise_stmt 283
#define import_stmt 284
#define import_name 285
#define import_from 286
#define import_as_name 287
#define dotted_as_name 288
#define import_as_names 289
#define dotted_as_names 290
#define dotted_name 291
#define global_stmt 292
#define nonlocal_stmt 293
#define assert_stmt 294
#define compound_stmt 295
#define async_stmt 296
#define if_stmt 297
#define while_stmt 298
#define for_stmt 299
#define try_stmt 300
#define with_stmt 301
#define with_item 302
#define except_clause 303
#define suite 304
#define test 305
#define test_nocond 306
#define lambdef 307
#define lambdef_nocond 308
#define or_test 309
#define and_test 310
#define not_test 311
#define comparison 312
#define comp_op 313
#define star_expr 314
#define expr 315
#define xor_expr 316
#define and_expr 317
#define shift_expr 318
#define arith_expr 319
#define term 320
#define factor 321
#define power 322
#define atom_expr 323
#define atom 324
#define testlist_comp 325
#define trailer 326
#define subscriptlist 327
#define subscript 328
#define sliceop 329
#define exprlist 330
#define testlist 331
#define dictorsetmaker 332
#define classdef 333
#define arglist 334
#define argument 335
#define comp_iter 336
#define comp_for 337
#define comp_if 338
#define encoding_decl 339
#define yield_expr 340
#define yield_arg 341
#define incr_stmt 277
#define decr_stmt 278
#define pass_stmt 279
#define flow_stmt 280
#define break_stmt 281
#define continue_stmt 282
#define return_stmt 283
#define yield_stmt 284
#define raise_stmt 285
#define import_stmt 286
#define import_name 287
#define import_from 288
#define import_as_name 289
#define dotted_as_name 290
#define import_as_names 291
#define dotted_as_names 292
#define dotted_name 293
#define global_stmt 294
#define nonlocal_stmt 295
#define assert_stmt 296
#define compound_stmt 297
#define async_stmt 298
#define if_stmt 299
#define while_stmt 300
#define for_stmt 301
#define try_stmt 302
#define with_stmt 303
#define with_item 304
#define except_clause 305
#define suite 306
#define test 307
#define test_nocond 308
#define lambdef 309
#define lambdef_nocond 310
#define or_test 311
#define and_test 312
#define not_test 313
#define comparison 314
#define comp_op 315
#define star_expr 316
#define expr 317
#define xor_expr 318
#define and_expr 319
#define shift_expr 320
#define arith_expr 321
#define term 322
#define factor 323
#define power 324
#define atom_expr 325
#define atom 326
#define testlist_comp 327
#define trailer 328
#define subscriptlist 329
#define subscript 330
#define sliceop 331
#define exprlist 332
#define testlist 333
#define dictorsetmaker 334
#define classdef 335
#define arglist 336
#define argument 337
#define comp_iter 338
#define comp_for 339
#define comp_if 340
#define encoding_decl 341
#define yield_expr 342
#define yield_arg 343
3 changes: 3 additions & 0 deletions Include/token.h
Expand Up @@ -69,6 +69,9 @@ extern "C" {
#define ERRORTOKEN 56
#define N_TOKENS 57

#define INCREMENT 58
#define DECREMENT 59

/* Special definitions for cooperation with parser */

#define NT_OFFSET 256
Expand Down
2 changes: 1 addition & 1 deletion Lib/importlib/_bootstrap_external.py
Expand Up @@ -21,7 +21,7 @@
# anything specified at the class level.

# Bootstrap-related code ######################################################
_CASE_INSENSITIVE_PLATFORMS_STR_KEY = 'win',
_CASE_INSENSITIVE_PLATFORMS_STR_KEY = 'win'
_CASE_INSENSITIVE_PLATFORMS_BYTES_KEY = 'cygwin', 'darwin'
_CASE_INSENSITIVE_PLATFORMS = (_CASE_INSENSITIVE_PLATFORMS_BYTES_KEY
+ _CASE_INSENSITIVE_PLATFORMS_STR_KEY)
Expand Down
2 changes: 2 additions & 0 deletions Lib/lib2to3/pgen2/grammar.py
Expand Up @@ -198,6 +198,8 @@ def _make_deterministic(top):
// DOUBLESLASH
//= DOUBLESLASHEQUAL
-> RARROW
++ INCREMENT
-- DECREMENT
"""

opmap = {}
Expand Down
2 changes: 2 additions & 0 deletions Lib/token.py
Expand Up @@ -68,6 +68,8 @@
ASYNC = 55
ERRORTOKEN = 56
N_TOKENS = 57
INCREMENT = 58
DECREMENT = 59
NT_OFFSET = 256
#--end constants--

Expand Down
2 changes: 2 additions & 0 deletions Lib/tokenize.py
Expand Up @@ -96,6 +96,8 @@
'->': RARROW,
'@': AT,
'@=': ATEQUAL,
'++': INCREMENT,
'--': DECREMENT
}

class TokenInfo(collections.namedtuple('TokenInfo', 'type string start end line')):
Expand Down
2 changes: 2 additions & 0 deletions Parser/Python.asdl
Expand Up @@ -31,6 +31,8 @@ module Python
| Delete(expr* targets)
| Assign(expr* targets, expr value)
| AugAssign(expr target, operator op, expr value)
| Increment(expr target)
| Decrement(expr target)
-- 'simple' indicates that we annotate simple name without parens
| AnnAssign(expr target, expr annotation, expr? value, int simple)

Expand Down
6 changes: 5 additions & 1 deletion Parser/tokenizer.c
Expand Up @@ -106,7 +106,9 @@ const char *_PyParser_TokenNames[] = {
"AWAIT",
"ASYNC",
"<ERRORTOKEN>",
"<N_TOKENS>"
"<N_TOKENS>",
"INCREMENT",
"DECREMENT"
};


Expand Down Expand Up @@ -1175,11 +1177,13 @@ PyToken_TwoChars(int c1, int c2)
break;
case '+':
switch (c2) {
case '+': return INCREMENT;
case '=': return PLUSEQUAL;
}
break;
case '-':
switch (c2) {
case '-': return DECREMENT;
case '=': return MINEQUAL;
case '>': return RARROW;
}
Expand Down

0 comments on commit fd7c20c

Please sign in to comment.