Lichen

Annotated internal_tests/branches.py

720:d760a72fbd2e
2017-03-13 Paul Boddie Define invocation and assignment properties for single-attribute usage lookups.
paul@0 1
#!/usr/bin/env python
paul@0 2
paul@0 3
import branching
paul@0 4
paul@610 5
def simple_usage(assignment):
paul@610 6
    d = {}
paul@610 7
    for name, usages in assignment.get_usage().items():
paul@610 8
        l = []
paul@610 9
        for usage in usages:
paul@610 10
            if not usage:
paul@610 11
                l.append(usage)
paul@610 12
            else:
paul@610 13
                l2 = []
paul@610 14
                for t in usage:
paul@610 15
                    attrname, invocation, assignment = t
paul@610 16
                    l2.append(attrname)
paul@610 17
                l.append(tuple(l2))
paul@610 18
        d[name] = set(l)
paul@610 19
    return d
paul@610 20
paul@0 21
names = []
paul@0 22
paul@0 23
# Equivalent to...
paul@0 24
#
paul@610 25
# y = ...
paul@610 26
# while cond0:
paul@610 27
#   if cond1:
paul@610 28
#     y.a1
paul@610 29
#   elif cond2:
paul@610 30
#     y = ...
paul@610 31
#     y.a2
paul@610 32
#   else:
paul@610 33
#     y.a3
paul@610 34
paul@610 35
bt = branching.BranchTracker()
paul@610 36
y1 = bt.assign_names(["y"])
paul@610 37
bt.new_branchpoint(True)            # begin
paul@610 38
bt.new_branch(True)                 # while ...
paul@610 39
bt.new_branchpoint()                # begin
paul@610 40
bt.new_branch()                     # if ...
paul@610 41
ya1 = bt.use_attribute("y", "a1")
paul@610 42
bt.shelve_branch()
paul@610 43
bt.new_branch()                     # elif ...
paul@610 44
y2 = bt.assign_names(["y"])
paul@610 45
ya2 = bt.use_attribute("y", "a2")
paul@610 46
bt.shelve_branch()
paul@610 47
bt.new_branch()                     # else
paul@610 48
ya1 = bt.use_attribute("y", "a3")
paul@610 49
bt.shelve_branch()
paul@610 50
bt.merge_branches()                 # end
paul@610 51
bt.resume_continuing_branches()
paul@610 52
bt.shelve_branch(True)
paul@610 53
bt.new_branch()                     # (null)
paul@610 54
bt.shelve_branch()
paul@610 55
bt.merge_branches()                 # end
paul@610 56
bt.resume_broken_branches()
paul@610 57
paul@610 58
print simple_usage(y1) == \
paul@610 59
    {'y' : set([(), ('a1',), ('a3',)])}, \
paul@610 60
    simple_usage(y1)
paul@610 61
print simple_usage(y2) == \
paul@610 62
    {'y' : set([('a2',)])}, \
paul@610 63
    simple_usage(y2)
paul@610 64
print bt.get_assignment_positions_for_branches("y", ya1) == [0, 1], \
paul@610 65
    bt.get_assignment_positions_for_branches("y", ya1)
paul@610 66
print bt.get_assignment_positions_for_branches("y", ya2) == [1], \
paul@610 67
    bt.get_assignment_positions_for_branches("y", ya2)
paul@610 68
names.append(bt.assignments["y"])
paul@610 69
paul@610 70
# Equivalent to...
paul@610 71
#
paul@0 72
# a = ...
paul@0 73
# a.p
paul@0 74
# if ...:
paul@0 75
#   a = ...
paul@0 76
#   a.x
paul@0 77
# else:
paul@0 78
#   ...
paul@0 79
# a.q
paul@0 80
paul@0 81
bt = branching.BranchTracker()
paul@0 82
a1 = bt.assign_names(["a"])
paul@0 83
bt.use_attribute("a", "p")
paul@0 84
bt.new_branchpoint()                # begin
paul@0 85
bt.new_branch()                     # if ...
paul@0 86
a2 = bt.assign_names(["a"])         # a = ...
paul@0 87
ax = bt.use_attribute("a", "x")
paul@0 88
bt.shelve_branch()
paul@0 89
bt.new_branch()                     # else
paul@0 90
bt.shelve_branch()
paul@0 91
bt.merge_branches()                 # end
paul@0 92
aq = bt.use_attribute("a", "q")
paul@0 93
paul@610 94
print simple_usage(a1) == \
paul@0 95
    {'a' : set([('p',), ('p', 'q')])}, \
paul@610 96
    simple_usage(a1)
paul@610 97
print simple_usage(a2) == \
paul@0 98
    {'a' : set([('q', 'x')])}, \
paul@610 99
    simple_usage(a2)
paul@0 100
print bt.get_assignment_positions_for_branches("a", ax) == [1], \
paul@0 101
    bt.get_assignment_positions_for_branches("a", ax)
paul@0 102
print bt.get_assignment_positions_for_branches("a", aq) == [0, 1], \
paul@0 103
    bt.get_assignment_positions_for_branches("a", aq)
paul@0 104
names.append(bt.assignments["a"])
paul@0 105
paul@0 106
# Equivalent to...
paul@0 107
#
paul@0 108
# a = ...
paul@0 109
# a.p
paul@0 110
# if ...:
paul@0 111
#   a.x
paul@0 112
# elif ...:
paul@0 113
#   a.y; a.z
paul@0 114
# else:
paul@0 115
#   ...
paul@0 116
# a.q
paul@0 117
paul@0 118
bt = branching.BranchTracker()
paul@0 119
a = bt.assign_names(["a"])
paul@0 120
bt.use_attribute("a", "p")
paul@0 121
bt.new_branchpoint()                # begin
paul@0 122
bt.new_branch()                     # if ...
paul@0 123
ax = bt.use_attribute("a", "x")
paul@0 124
bt.shelve_branch()
paul@0 125
bt.new_branch()                     # elif ...
paul@0 126
ay = bt.use_attribute("a", "y")
paul@0 127
az = bt.use_attribute("a", "z")
paul@0 128
bt.shelve_branch()
paul@0 129
bt.new_branch()                     # else
paul@0 130
bt.shelve_branch()
paul@0 131
bt.merge_branches()                 # end
paul@0 132
bt.use_attribute("a", "q")
paul@0 133
paul@610 134
print simple_usage(a) == \
paul@0 135
    {'a' : set([('p', 'q'), ('p', 'q', 'x'), ('p', 'q', 'y', 'z')])}, \
paul@610 136
    simple_usage(a)
paul@0 137
print bt.get_assignment_positions_for_branches("a", ax) == [0], \
paul@0 138
    bt.get_assignment_positions_for_branches("a", ax)
paul@0 139
print bt.get_assignment_positions_for_branches("a", ay) == [0], \
paul@0 140
    bt.get_assignment_positions_for_branches("a", ay)
paul@0 141
print bt.get_assignment_positions_for_branches("a", az) == [0], \
paul@0 142
    bt.get_assignment_positions_for_branches("a", az)
paul@0 143
names.append(bt.assignments["a"])
paul@0 144
paul@0 145
# Equivalent to...
paul@0 146
#
paul@0 147
# a = ...
paul@0 148
# a.p
paul@0 149
# while ...:
paul@0 150
#   a.x
paul@0 151
# a.q
paul@0 152
paul@0 153
bt = branching.BranchTracker()
paul@0 154
a = bt.assign_names(["a"])
paul@0 155
bt.use_attribute("a", "p")
paul@0 156
bt.new_branchpoint(True)            # begin
paul@0 157
bt.new_branch(True)                 # while ...
paul@0 158
ax = bt.use_attribute("a", "x")
paul@0 159
bt.resume_continuing_branches()
paul@0 160
bt.shelve_branch(True)
paul@0 161
bt.new_branch()                     # (null)
paul@0 162
bt.shelve_branch()
paul@0 163
bt.merge_branches()                 # end
paul@0 164
bt.resume_broken_branches()
paul@0 165
bt.use_attribute("a", "q")
paul@0 166
paul@610 167
print simple_usage(a) == \
paul@610 168
    {'a' : set([('p', 'q'), ('p', 'q', 'x')])}, simple_usage(a)
paul@0 169
print bt.get_assignment_positions_for_branches("a", ax) == [0], \
paul@0 170
    bt.get_assignment_positions_for_branches("a", ax)
paul@0 171
names.append(bt.assignments["a"])
paul@0 172
paul@0 173
# Equivalent to...
paul@0 174
#
paul@0 175
# a = ...
paul@0 176
# a.p
paul@0 177
# while ...:
paul@0 178
#   if ...:
paul@0 179
#     a.x
paul@0 180
#   else ...:
paul@0 181
#     a.y
paul@0 182
# a.q
paul@0 183
paul@0 184
bt = branching.BranchTracker()
paul@0 185
a = bt.assign_names(["a"])
paul@0 186
bt.use_attribute("a", "p")
paul@0 187
bt.new_branchpoint(True)            # begin
paul@0 188
bt.new_branch(True)                 # while ...
paul@0 189
bt.new_branchpoint()                # begin
paul@0 190
bt.new_branch()                     # if ...
paul@0 191
ax = bt.use_attribute("a", "x")
paul@0 192
bt.shelve_branch()
paul@0 193
bt.new_branch()
paul@0 194
ay = bt.use_attribute("a", "y")
paul@0 195
bt.shelve_branch()
paul@0 196
bt.merge_branches()                 # end
paul@0 197
bt.resume_continuing_branches()
paul@0 198
bt.shelve_branch(True)
paul@0 199
bt.new_branch()                     # (null)
paul@0 200
bt.shelve_branch()
paul@0 201
bt.merge_branches()                 # end
paul@0 202
bt.resume_broken_branches()
paul@0 203
bt.use_attribute("a", "q")
paul@0 204
paul@610 205
print simple_usage(a) == \
paul@0 206
    {'a' : set([('p', 'q'), ('p', 'q', 'x'), ('p', 'q', 'y')])}, \
paul@610 207
    simple_usage(a)
paul@0 208
print bt.get_assignment_positions_for_branches("a", ax) == [0], \
paul@0 209
    bt.get_assignment_positions_for_branches("a", ax)
paul@0 210
print bt.get_assignment_positions_for_branches("a", ay) == [0], \
paul@0 211
    bt.get_assignment_positions_for_branches("a", ay)
paul@0 212
names.append(bt.assignments["a"])
paul@0 213
paul@0 214
# Equivalent to...
paul@0 215
#
paul@0 216
# a = ...
paul@0 217
# a.p
paul@0 218
# while ...:
paul@0 219
#   if ...:
paul@0 220
#     a = ...
paul@0 221
#     a.x
paul@0 222
#   else ...:
paul@0 223
#     a.y
paul@0 224
# a.q
paul@0 225
paul@0 226
bt = branching.BranchTracker()
paul@0 227
a1 = bt.assign_names(["a"])
paul@0 228
bt.use_attribute("a", "p")
paul@0 229
bt.new_branchpoint(True)            # begin
paul@0 230
bt.new_branch(True)                 # while ...
paul@0 231
bt.new_branchpoint()                # begin
paul@0 232
bt.new_branch()                     # if ...
paul@0 233
a2 = bt.assign_names(["a"])         # a = ...
paul@0 234
ax = bt.use_attribute("a", "x")
paul@0 235
bt.shelve_branch()
paul@0 236
bt.new_branch()
paul@0 237
ay = bt.use_attribute("a", "y")
paul@0 238
bt.shelve_branch()
paul@0 239
bt.merge_branches()                 # end
paul@0 240
bt.resume_continuing_branches()
paul@0 241
bt.shelve_branch(True)
paul@0 242
bt.new_branch()                     # (null)
paul@0 243
bt.shelve_branch()
paul@0 244
bt.merge_branches()                 # end
paul@0 245
bt.resume_broken_branches()
paul@0 246
bt.use_attribute("a", "q")
paul@0 247
paul@610 248
print simple_usage(a1) == \
paul@610 249
    {'a' : set([('p', 'q'), ('p', 'q', 'y'), ('p',)])}, simple_usage(a1)
paul@610 250
print simple_usage(a2) == \
paul@610 251
    {'a' : set([('q', 'x')])}, simple_usage(a2)
paul@0 252
print bt.get_assignment_positions_for_branches("a", ax) == [1], \
paul@0 253
    bt.get_assignment_positions_for_branches("a", ax)
paul@0 254
print bt.get_assignment_positions_for_branches("a", ay) == [0, 1], \
paul@0 255
    bt.get_assignment_positions_for_branches("a", ay)
paul@0 256
names.append(bt.assignments["a"])
paul@0 257
paul@0 258
# Equivalent to...
paul@0 259
#
paul@0 260
# a = ...
paul@0 261
# a.p
paul@0 262
# while ...:
paul@0 263
#   if ...:
paul@0 264
#     a.y
paul@0 265
#   else ...:
paul@0 266
#     a = ...
paul@0 267
#     a.x
paul@0 268
# a.q
paul@0 269
paul@0 270
bt = branching.BranchTracker()
paul@0 271
a1 = bt.assign_names(["a"])
paul@0 272
bt.use_attribute("a", "p")
paul@0 273
bt.new_branchpoint(True)            # begin
paul@0 274
bt.new_branch(True)                 # while ...
paul@0 275
bt.new_branchpoint()                # begin
paul@0 276
bt.new_branch()                     # if ...
paul@0 277
ay = bt.use_attribute("a", "y")
paul@0 278
bt.shelve_branch()
paul@0 279
bt.new_branch()
paul@0 280
a2 = bt.assign_names(["a"])         # a = ...
paul@0 281
ax = bt.use_attribute("a", "x")
paul@0 282
bt.shelve_branch()
paul@0 283
bt.merge_branches()                 # end
paul@0 284
bt.resume_continuing_branches()
paul@0 285
bt.shelve_branch(True)
paul@0 286
bt.new_branch()                     # (null)
paul@0 287
bt.shelve_branch()
paul@0 288
bt.merge_branches()                 # end
paul@0 289
bt.resume_broken_branches()
paul@0 290
bt.use_attribute("a", "q")
paul@0 291
paul@610 292
print simple_usage(a1) == \
paul@610 293
    {'a' : set([('p', 'q'), ('p', 'q', 'y'), ('p',)])}, simple_usage(a1)
paul@610 294
print simple_usage(a2) == \
paul@610 295
    {'a' : set([('q', 'x')])}, simple_usage(a2)
paul@0 296
print bt.get_assignment_positions_for_branches("a", ax) == [1], \
paul@0 297
    bt.get_assignment_positions_for_branches("a", ax)
paul@0 298
print bt.get_assignment_positions_for_branches("a", ay) == [0, 1], \
paul@0 299
    bt.get_assignment_positions_for_branches("a", ay)
paul@0 300
names.append(bt.assignments["a"])
paul@0 301
paul@0 302
# Equivalent to...
paul@0 303
#
paul@0 304
# a = ...
paul@0 305
# a.p
paul@0 306
# while ...:
paul@0 307
#   a = ...
paul@0 308
#   a.x
paul@0 309
# a.q
paul@0 310
paul@0 311
bt = branching.BranchTracker()
paul@0 312
a1 = bt.assign_names(["a"])
paul@0 313
ap = bt.use_attribute("a", "p")
paul@0 314
bt.new_branchpoint(True)            # begin
paul@0 315
bt.new_branch(True)                 # while ...
paul@0 316
a2 = bt.assign_names(["a"])         # a = ...
paul@0 317
ax = bt.use_attribute("a", "x")
paul@0 318
bt.resume_continuing_branches()
paul@0 319
bt.shelve_branch(True)
paul@0 320
bt.new_branch()                     # (null)
paul@0 321
bt.shelve_branch()
paul@0 322
bt.merge_branches()                 # end
paul@0 323
bt.resume_broken_branches()
paul@0 324
aq = bt.use_attribute("a", "q")
paul@0 325
paul@610 326
print simple_usage(a1) == \
paul@610 327
    {'a' : set([('p', 'q'), ('p',)])}, simple_usage(a1)
paul@610 328
print simple_usage(a2) == \
paul@610 329
    {'a' : set([('q', 'x')])}, simple_usage(a2)
paul@0 330
print bt.get_assignment_positions_for_branches("a", ax) == [1], \
paul@0 331
    bt.get_assignment_positions_for_branches("a", ax)
paul@0 332
print bt.get_assignment_positions_for_branches("a", ap) == [0], \
paul@0 333
    bt.get_assignment_positions_for_branches("a", ap)
paul@0 334
print bt.get_assignment_positions_for_branches("a", aq) == [0, 1], \
paul@0 335
    bt.get_assignment_positions_for_branches("a", aq)
paul@0 336
names.append(bt.assignments["a"])
paul@0 337
paul@0 338
# Equivalent to...
paul@0 339
#
paul@0 340
# a = ...
paul@0 341
# a.p
paul@0 342
# while ...:
paul@0 343
#   if ...:
paul@0 344
#     break
paul@0 345
#   a.q
paul@0 346
# a.r
paul@0 347
paul@0 348
bt = branching.BranchTracker()
paul@0 349
a1 = bt.assign_names(["a"])
paul@0 350
bt.use_attribute("a", "p")
paul@0 351
bt.new_branchpoint(True)            # begin
paul@0 352
bt.new_branch(True)                 # while ...
paul@0 353
bt.new_branchpoint()                # begin
paul@0 354
bt.new_branch()                     # if ...
paul@0 355
bt.suspend_broken_branch()          # break
paul@0 356
bt.shelve_branch()
paul@0 357
bt.new_branch()                     # (null)
paul@0 358
bt.shelve_branch()
paul@0 359
bt.merge_branches()                 # end
paul@0 360
bt.use_attribute("a", "q")
paul@0 361
bt.resume_continuing_branches()
paul@0 362
bt.shelve_branch(True)
paul@0 363
bt.merge_branches()                 # end
paul@0 364
bt.resume_broken_branches()
paul@0 365
bt.use_attribute("a", "r")
paul@0 366
paul@610 367
print simple_usage(a1) == \
paul@610 368
    {'a' : set([('p', 'q', 'r'), ('p', 'r')])}, simple_usage(a1)
paul@0 369
names.append(bt.assignments["a"])
paul@0 370
paul@0 371
# Equivalent to...
paul@0 372
#
paul@0 373
# a = ...
paul@0 374
# a.p and a.q and a.r
paul@0 375
paul@0 376
bt = branching.BranchTracker()
paul@0 377
a1 = bt.assign_names(["a"])
paul@0 378
bt.new_branchpoint()                # begin
paul@0 379
bt.new_branch()
paul@0 380
bt.use_attribute("a", "p")
paul@0 381
bt.new_branchpoint()                # begin
paul@0 382
bt.new_branch()
paul@0 383
bt.use_attribute("a", "q")
paul@0 384
bt.new_branchpoint()                # begin
paul@0 385
bt.new_branch()
paul@0 386
bt.use_attribute("a", "r")
paul@0 387
bt.shelve_branch()
paul@0 388
bt.new_branch()                     # (null)
paul@0 389
bt.shelve_branch()
paul@0 390
bt.merge_branches()                 # end
paul@0 391
bt.shelve_branch()
paul@0 392
bt.new_branch()                     # (null)
paul@0 393
bt.shelve_branch()
paul@0 394
bt.merge_branches()                 # end
paul@0 395
bt.shelve_branch()
paul@0 396
bt.merge_branches()                 # end
paul@0 397
paul@610 398
print simple_usage(a1) == \
paul@610 399
    {'a' : set([('p', 'q', 'r'), ('p', 'q'), ('p',)])}, simple_usage(a1)
paul@0 400
names.append(bt.assignments["a"])
paul@0 401
paul@0 402
# Equivalent to...
paul@0 403
#
paul@0 404
# a = ...
paul@0 405
# if ...:
paul@0 406
#   a.p
paul@0 407
#   return
paul@0 408
# a.q
paul@0 409
paul@0 410
bt = branching.BranchTracker()
paul@0 411
a1 = bt.assign_names(["a"])
paul@0 412
bt.new_branchpoint()                # begin
paul@0 413
bt.new_branch()                     # if ...
paul@0 414
bt.use_attribute("a", "p")
paul@0 415
bt.abandon_returning_branch()
paul@0 416
bt.shelve_branch()
paul@0 417
bt.new_branch()                     # (null)
paul@0 418
bt.shelve_branch()
paul@0 419
bt.merge_branches()                 # end
paul@0 420
bt.use_attribute("a", "q")
paul@0 421
paul@610 422
print simple_usage(a1) == \
paul@610 423
    {'a' : set([('p',), ('q',)])}, simple_usage(a1)
paul@0 424
names.append(bt.assignments["a"])
paul@0 425
paul@0 426
# Equivalent to...
paul@0 427
#
paul@0 428
# a = ...
paul@0 429
# try:
paul@0 430
#   if ...:
paul@0 431
#     a.p
paul@0 432
#     return
paul@0 433
#   a.q
paul@0 434
# except:
paul@0 435
#   a.r
paul@0 436
paul@0 437
bt = branching.BranchTracker()
paul@0 438
a1 = bt.assign_names(["a"])
paul@0 439
bt.new_branchpoint()                # begin (try)
paul@0 440
bt.new_branchpoint()                # begin
paul@0 441
bt.new_branch()                     # if ...
paul@0 442
bt.use_attribute("a", "p")
paul@0 443
bt.abandon_returning_branch()
paul@0 444
bt.shelve_branch()                  # ... if
paul@0 445
bt.new_branch()                     # (null)
paul@0 446
bt.shelve_branch()
paul@0 447
bt.merge_branches()                 # end
paul@0 448
bt.use_attribute("a", "q")
paul@0 449
bt.resume_abandoned_branches()      # except
paul@0 450
bt.use_attribute("a", "r")
paul@0 451
bt.shelve_branch()
paul@0 452
bt.merge_branches()                 # end
paul@0 453
paul@610 454
print simple_usage(a1) == \
paul@610 455
    {'a' : set([('p',), ('q', 'r')])}, simple_usage(a1)
paul@0 456
names.append(bt.assignments["a"])
paul@0 457
paul@0 458
# Equivalent to...
paul@0 459
#
paul@0 460
# a = ...
paul@0 461
# if ...:
paul@0 462
#   a.p
paul@0 463
# a = ...
paul@0 464
# if ...:
paul@0 465
#   a.q
paul@0 466
paul@0 467
bt = branching.BranchTracker()
paul@0 468
a1 = bt.assign_names(["a"])
paul@0 469
bt.new_branchpoint()                # begin
paul@0 470
bt.new_branch()                     # if ...
paul@0 471
ap = bt.use_attribute("a", "p")
paul@0 472
bt.abandon_branch()
paul@0 473
bt.shelve_branch()                  # ... if
paul@0 474
bt.new_branch()                     # (null)
paul@0 475
bt.shelve_branch()
paul@0 476
bt.merge_branches()                 # end
paul@0 477
a2 = bt.assign_names(["a"])
paul@0 478
bt.new_branchpoint()                # begin
paul@0 479
bt.new_branch()                     # if ...
paul@0 480
aq = bt.use_attribute("a", "q")
paul@0 481
bt.abandon_branch()
paul@0 482
bt.shelve_branch()                  # ... if
paul@0 483
bt.new_branch()                     # (null)
paul@0 484
bt.shelve_branch()
paul@0 485
bt.merge_branches()                 # end
paul@0 486
paul@610 487
print simple_usage(a1) == \
paul@610 488
    {'a' : set([('p',), ()])}, simple_usage(a1)
paul@610 489
print simple_usage(a2) == \
paul@610 490
    {'a' : set([('q',), ()])}, simple_usage(a2)
paul@0 491
print bt.get_assignment_positions_for_branches("a", ap) == [0], \
paul@0 492
    bt.get_assignment_positions_for_branches("a", ap)
paul@0 493
print bt.get_assignment_positions_for_branches("a", aq) == [1], \
paul@0 494
    bt.get_assignment_positions_for_branches("a", aq)
paul@0 495
names.append(bt.assignments["a"])
paul@0 496
paul@0 497
# Equivalent to...
paul@0 498
#
paul@0 499
# a = {}
paul@0 500
# a.p
paul@0 501
# if ...:
paul@0 502
#   a = ...
paul@0 503
#   a.x
paul@0 504
# else:
paul@0 505
#   ...
paul@0 506
# a.q
paul@0 507
paul@0 508
bt = branching.BranchTracker()
paul@0 509
a1 = bt.assign_names(["a"], ["<instance>:__builtins__.dict.dict"])
paul@0 510
ap = bt.use_attribute("a", "p")
paul@0 511
bt.new_branchpoint()                # begin
paul@0 512
bt.new_branch()                     # if ...
paul@0 513
a2 = bt.assign_names(["a"])         # a = ...
paul@0 514
ax = bt.use_attribute("a", "x")
paul@0 515
bt.shelve_branch()
paul@0 516
bt.new_branch()                     # else
paul@0 517
bt.shelve_branch()
paul@0 518
bt.merge_branches()                 # end
paul@0 519
aq = bt.use_attribute("a", "q")
paul@0 520
paul@610 521
print simple_usage(a1) == \
paul@0 522
    {'a' : set([('p',), ('p', 'q')])}, \
paul@610 523
    simple_usage(a1)
paul@610 524
print simple_usage(a2) == \
paul@0 525
    {'a' : set([('q', 'x')])}, \
paul@610 526
    simple_usage(a2)
paul@0 527
print bt.get_assignment_positions_for_branches("a", ap) == [0], \
paul@0 528
    bt.get_assignment_positions_for_branches("a", ap)
paul@0 529
print bt.get_assignment_positions_for_branches("a", ax) == [1], \
paul@0 530
    bt.get_assignment_positions_for_branches("a", ax)
paul@0 531
print bt.get_assignment_positions_for_branches("a", aq) == [0, 1], \
paul@0 532
    bt.get_assignment_positions_for_branches("a", aq)
paul@0 533
names.append(bt.assignments["a"])
paul@0 534
paul@0 535
# Equivalent to...
paul@0 536
#
paul@0 537
# if ...:
paul@0 538
#   a = ...
paul@0 539
#   a.x
paul@0 540
# else:
paul@0 541
#   ...
paul@0 542
# a.q
paul@0 543
paul@0 544
bt = branching.BranchTracker()
paul@0 545
bt.new_branchpoint()                # begin
paul@0 546
bt.new_branch()                     # if ...
paul@0 547
a1 = bt.assign_names(["a"])         # a = ...
paul@0 548
ax = bt.use_attribute("a", "x")
paul@0 549
bt.shelve_branch()
paul@0 550
bt.new_branch()                     # else
paul@0 551
bt.shelve_branch()
paul@0 552
bt.merge_branches()                 # end
paul@0 553
aq = bt.use_attribute("a", "q")
paul@0 554
paul@610 555
print simple_usage(a1) == \
paul@0 556
    {'a' : set([('q', 'x')])}, \
paul@610 557
    simple_usage(a1)
paul@0 558
print bt.get_assignment_positions_for_branches("a", aq) == [None, 0], \
paul@0 559
    bt.get_assignment_positions_for_branches("a", aq)
paul@0 560
names.append(bt.assignments["a"])
paul@0 561
paul@0 562
# Equivalent to...
paul@0 563
#
paul@0 564
# if ...:
paul@0 565
#   a = ...
paul@0 566
#   return
paul@0 567
# a.q
paul@0 568
paul@0 569
bt = branching.BranchTracker()
paul@0 570
bt.new_branchpoint()                # begin
paul@0 571
bt.new_branch()                     # if ...
paul@0 572
a1 = bt.assign_names(["a"])
paul@0 573
bt.abandon_returning_branch()
paul@0 574
bt.shelve_branch()
paul@0 575
bt.new_branch()                     # (null)
paul@0 576
bt.shelve_branch()
paul@0 577
bt.merge_branches()                 # end
paul@0 578
aq = bt.use_attribute("a", "q")
paul@0 579
paul@610 580
print simple_usage(a1) == \
paul@610 581
    {'a' : set([()])}, simple_usage(a1)
paul@0 582
print bt.get_assignment_positions_for_branches("a", aq) == [None], \
paul@0 583
    bt.get_assignment_positions_for_branches("a", aq)
paul@0 584
names.append(bt.assignments["a"])
paul@0 585
paul@0 586
# Equivalent to...
paul@0 587
#
paul@0 588
# a = ...
paul@0 589
# try:
paul@0 590
#   if ...:
paul@0 591
#     a.p
paul@0 592
#     return
paul@0 593
#   a.q
paul@0 594
# finally:
paul@0 595
#   a.r
paul@0 596
paul@0 597
bt = branching.BranchTracker()
paul@0 598
a1 = bt.assign_names(["a"])
paul@0 599
bt.new_branchpoint()                # begin
paul@0 600
bt.new_branch()                     # if ...
paul@0 601
bt.use_attribute("a", "p")
paul@0 602
bt.abandon_returning_branch()
paul@0 603
bt.shelve_branch()                  # ... if
paul@0 604
bt.new_branch()                     # (null)
paul@0 605
bt.shelve_branch()
paul@0 606
bt.merge_branches()                 # end
paul@0 607
bt.use_attribute("a", "q")
paul@0 608
branches = bt.resume_all_abandoned_branches()
paul@0 609
bt.use_attribute("a", "r")
paul@0 610
bt.restore_active_branches(branches)
paul@0 611
paul@610 612
print simple_usage(a1) == \
paul@610 613
    {'a' : set([('p', 'r'), ('q', 'r')])}, simple_usage(a1)
paul@0 614
names.append(bt.assignments["a"])
paul@0 615
paul@0 616
# Equivalent to...
paul@0 617
#
paul@0 618
# a = ...
paul@0 619
# try:
paul@0 620
#   if ...:
paul@0 621
#     a = ...
paul@0 622
#     a.p
paul@0 623
#     return
paul@0 624
#   a.q
paul@0 625
# finally:
paul@0 626
#   a.r
paul@0 627
paul@0 628
bt = branching.BranchTracker()
paul@0 629
a1 = bt.assign_names(["a"])
paul@0 630
bt.new_branchpoint()                # begin
paul@0 631
bt.new_branch()                     # if ...
paul@0 632
a2 = bt.assign_names(["a"])
paul@0 633
bt.use_attribute("a", "p")
paul@0 634
bt.abandon_returning_branch()
paul@0 635
bt.shelve_branch()                  # ... if
paul@0 636
bt.new_branch()                     # (null)
paul@0 637
bt.shelve_branch()
paul@0 638
bt.merge_branches()                 # end
paul@0 639
aq = bt.use_attribute("a", "q")
paul@0 640
branches = bt.resume_all_abandoned_branches()
paul@0 641
ar = bt.use_attribute("a", "r")
paul@0 642
bt.restore_active_branches(branches)
paul@0 643
paul@610 644
print simple_usage(a1) == \
paul@610 645
    {'a' : set([(), ('q', 'r')])}, simple_usage(a1)
paul@610 646
print simple_usage(a2) == \
paul@610 647
    {'a' : set([('p', 'r')])}, simple_usage(a2)
paul@0 648
print bt.get_assignment_positions_for_branches("a", ar) == [0, 1], \
paul@0 649
    bt.get_assignment_positions_for_branches("a", ar)
paul@0 650
names.append(bt.assignments["a"])
paul@0 651
paul@0 652
# Equivalent to...
paul@0 653
#
paul@0 654
# a = ...
paul@0 655
# try:
paul@0 656
#   if ...:
paul@0 657
#     a = ...
paul@0 658
#     a.p
paul@0 659
#     return
paul@0 660
#   a.q
paul@0 661
# except:
paul@0 662
#   a.r
paul@0 663
paul@0 664
bt = branching.BranchTracker()
paul@0 665
a1 = bt.assign_names(["a"])
paul@0 666
bt.new_branchpoint()                # begin (try)
paul@0 667
bt.new_branchpoint()                # begin
paul@0 668
bt.new_branch()                     # if ...
paul@0 669
a2 = bt.assign_names(["a"])
paul@0 670
bt.use_attribute("a", "p")
paul@0 671
bt.abandon_returning_branch()
paul@0 672
bt.shelve_branch()                  # ... if
paul@0 673
bt.new_branch()                     # (null)
paul@0 674
bt.shelve_branch()
paul@0 675
bt.merge_branches()                 # end
paul@0 676
bt.use_attribute("a", "q")
paul@0 677
bt.resume_abandoned_branches()      # except
paul@0 678
ar = bt.use_attribute("a", "r")
paul@0 679
bt.shelve_branch()
paul@0 680
bt.merge_branches()                 # end
paul@0 681
paul@610 682
print simple_usage(a1) == \
paul@610 683
    {'a' : set([(), ('q', 'r')])}, simple_usage(a1)
paul@610 684
print simple_usage(a2) == \
paul@610 685
    {'a' : set([('p',)])}, simple_usage(a2)
paul@0 686
print bt.get_assignment_positions_for_branches("a", ar) == [0, 1], \
paul@0 687
    bt.get_assignment_positions_for_branches("a", ar)
paul@0 688
names.append(bt.assignments["a"])
paul@0 689
paul@0 690
# vim: tabstop=4 expandtab shiftwidth=4