2017-03-23 | Paul Boddie | file changeset files shortlog | Represent integers as __attr values with bit 0 set. Since pointers must be aligned (to four-byte boundaries on many modern systems, maybe two-byte boundaries on older or embedded systems, eight-byte boundaries on some 64-bit systems), any value with bit 0 set will not be referencing an object. This avoids allocating objects for integers and instead just allows them to be passed around. Bit 0 needs to be tested when attributes are accessed, and for integers, a common instance is employed to provide any instance attributes, an instance table reference and a reference to the integer class. | integers-as-tagged-attrs |
paul@141 | 1 | def f(a, b, c): |
paul@141 | 2 | return a and b and c |
paul@141 | 3 | |
paul@141 | 4 | def g(a, b, c): |
paul@141 | 5 | return a or b or c |
paul@141 | 6 | |
paul@141 | 7 | def h(a, b, c): |
paul@141 | 8 | return a and b or c |
paul@141 | 9 | |
paul@141 | 10 | def i(a, b, c): |
paul@141 | 11 | return a or b and c |
paul@141 | 12 | |
paul@141 | 13 | def j(a, b, c): |
paul@141 | 14 | return f(a, b, c) and g(a, b, c) or c |
paul@202 | 15 | |
paul@372 | 16 | print f(0, 0, 0) # 0 |
paul@372 | 17 | print f(1, 0, 1) # 0 |
paul@372 | 18 | print f(1, 1, 1) # 1 |
paul@372 | 19 | |
paul@372 | 20 | print g(0, 0, 0) # 0 |
paul@372 | 21 | print g(1, 0, 0) # 1 |
paul@372 | 22 | print g(0, 0, 1) # 1 |
paul@372 | 23 | |
paul@372 | 24 | print h(0, 0, 0) # 0 |
paul@372 | 25 | print h(0, 0, 1) # 1 |
paul@372 | 26 | print h(1, 0, 0) # 0 |
paul@372 | 27 | |
paul@372 | 28 | print i(0, 0, 0) # 0 |
paul@372 | 29 | print i(0, 0, 1) # 0 |
paul@372 | 30 | print i(1, 0, 0) # 1 |
paul@372 | 31 | |
paul@372 | 32 | print j(0, 0, 0) # 0 |
paul@372 | 33 | print j(0, 0, 1) # 1 |
paul@372 | 34 | print j(1, 0, 0) # 0 |
paul@372 | 35 | |
paul@372 | 36 | # Test any and all functions. |
paul@372 | 37 | |
paul@372 | 38 | l = [0, 0, 1, 0, 0] |
paul@372 | 39 | print any(l) # True |
paul@372 | 40 | print all(l) # False |
paul@372 | 41 | |
paul@372 | 42 | l = [1, 1, "one", 1] |
paul@372 | 43 | print any(l) # True |
paul@372 | 44 | print all(l) # True |
paul@372 | 45 | |
paul@372 | 46 | l = [1, 1, "one", ""] |
paul@372 | 47 | print any(l) # True |
paul@372 | 48 | print all(l) # False |