Ticket #29: binOctLit.patch
File binOctLit.patch, 16.7 KB (added by hopscc, 16 years ago) |
---|
-
Source/CobraTokenizer.cobra
90 90 r'OPEN_IF if\(', 91 91 r'OPEN_CALL [A-Za-z_][A-Za-z0-9_]*\(', 92 92 93 r'HEX_LIT_UNSIGN 0x[\dA-Fa-f][\dA-Fa-f]*(u|_u)(8|16|32|64)?', 94 r'HEX_LIT_EXPLICIT 0x[\dA-Fa-f][\dA-Fa-f]*_(8|16|32|64)?', 95 r'HEX_LIT 0x[\dA-Fa-f][\dA-Fa-f]*', 93 r'BIN_LIT_EXPLICIT 0b[01][01_]*(u|i)(8|16|32|64)?', 94 r'BIN_LIT 0b[01][01_]*', 96 95 96 r'OCT_LIT_EXPLICIT 0o[0-7][0-7_]*(u|i)(8|16|32|64)?', 97 r'OCT_LIT 0o[0-7][0-7_]*', 98 99 r'HEX_LIT_EXPLICIT 0x[\dA-Fa-f][\dA-Fa-f_]*(u|i)(8|16|32|64)?', 100 r'HEX_LIT 0x[\dA-Fa-f][\dA-Fa-f_]*', 101 97 102 r'FLOAT_LIT_1 \d[\d_]*\.\d+_?f(32|64)?', 98 103 r'FLOAT_LIT_2 \d[\d_]*(f|_f)(32|64)?', 99 104 r'DECIMAL_LIT \d[\d_]*\.\d+(d|_d)', … … 574 579 ensure 575 580 tok.which == 'INTEGER_LIT' 576 581 (tok.info to int) in [-8, 8, -16, 16, -32, 32, -64, 64] # CC: axe cast 582 (signed and (tok.info to int) <0) or (not signed and (tok.info to int) >0) 577 583 body 578 k = if(signed, 'i', 'u')584 size = .determineLitSize(tok) 579 585 s = tok.text.replace('_', '') 580 if s.endsWith(k)581 size = 32582 s = s[:-1]583 else if s.endsWith('32')584 size = 32585 s = s[:-3]586 else if s.endsWith('64')587 size = 64588 s = s[:-3]589 else if s.endsWith('16')590 size = 16591 s = s[:-3]592 else if s.endsWith('8')593 size = 8594 s = s[:-2]595 else596 # cannot have other size specs given regex597 size = 32598 586 try 599 587 # TODO: The use of int.parse, as opposed to say int64.parse, uint32.parse, etc. probably 600 588 # means that legit int lits outside the signed 32 bit range will not work in Cobra … … 604 592 catch OverflowException 605 593 assert false, 'TODO' 606 594 tok.which = 'INTEGER_LIT' 607 tok.info = if(signed, -1, +1) *size595 tok.info = size 608 596 609 def on HEX_LIT_UNSIGN(tok as IToken) as IToken?597 def onBIN_LIT_EXPLICIT(tok as IToken) as IToken? 610 598 require 611 'u' in tok.text 599 'u' in tok.text or 'i' in tok.text 612 600 body 613 return .onHEX_LIT_EXPLICIT(tok) 614 601 return .anyBASE_LIT_EXPLICIT(tok, 2) 602 603 def onBIN_LIT(tok as IToken) as IToken? 604 return .anyBASE_LIT(tok, 2) 605 606 def onOCT_LIT_EXPLICIT(tok as IToken) as IToken? 607 require 608 'u' in tok.text or 'i' in tok.text 609 body 610 return .anyBASE_LIT_EXPLICIT(tok, 8) 611 612 def onOCT_LIT(tok as IToken) as IToken? 613 return .anyBASE_LIT(tok, 8) 614 615 615 def onHEX_LIT_EXPLICIT(tok as IToken) as IToken? 616 require 617 'u' in tok.text or 'i' in tok.text 618 body 619 return .anyBASE_LIT_EXPLICIT(tok, 16) 620 621 def onHEX_LIT(tok as IToken) as IToken? 622 return .anyBASE_LIT(tok, 16) 623 624 def anyBASE_LIT_EXPLICIT(tok as IToken, nbase as int) as IToken 616 625 ensure 617 626 tok.which == 'INTEGER_LIT' 618 (tok.info to int) in [8, 16, 32, 64 ] # CC: axe cast627 (tok.info to int) in [8, 16, 32, 64, -8, -16, -32, -64] 619 628 body 620 size = 32621 629 h = tok.text 622 s = tok.text 623 if s.endsWith('32') 624 size = 32 625 s = s[:-2] 626 else if s.endsWith('64') 627 size = 64 628 s = s[:-2] 629 else if s.endsWith('16') 630 size = 16 631 s = s[:-2] 632 else if s.endsWith('8') 633 size = 8 634 s = s[:-1] 635 if s.endsWith('u') 636 s = s[:-1] 637 s = s.replace('_', '') 638 tok.text = s 639 tok = .onHEX_LIT(tok) to ! 640 tok.info = size # unsigned 630 size = .determineLitSize(tok) 631 tok = .anyBASE_LIT(tok, nbase) to ! 632 tok.info = size 641 633 tok.text = h 642 634 return tok 643 644 def onHEX_LIT(tok as IToken) as IToken? 635 636 def determineLitSize(tok as IToken) as int 637 size as int = 32 638 s = tok.text 639 if s.endsWith('32') 640 size = 32 641 s = s[:-2] 642 else if s.endsWith('64') 643 size = 64 644 s = s[:-2] 645 else if s.endsWith('16') 646 size = 16 647 s = s[:-2] 648 else if s.endsWith('8') 649 size = 8 650 s = s[:-1] 651 if s.endsWith('u') 652 s = s[:-1] 653 else if s.endsWith('i') 654 s = s[:-1] 655 size = size * -1 656 tok.text = s 657 return size 658 659 def anyBASE_LIT(tok as IToken, nbase as int) as IToken? 645 660 ensure 646 661 tok.which == 'INTEGER_LIT' 647 662 tok.info == 32 648 663 body 664 s = tok.text.replace('_', '') 649 665 try 650 tok.value = int.parse(tok.text[2:], System.Globalization.NumberStyles.HexNumber)666 tok.value = .parseBase(s[2:], nbase) 651 667 catch FormatException 652 assert false, ' not expecting to get here given regex'668 assert false, 'Format Exception - not expecting to get here given regex' 653 669 catch OverflowException 654 assert false, 'TODO: HEX_LIT Overflow (int)'670 assert false, 'TODO: anyBase_LIT Overflow (int)' 655 671 tok.which = 'INTEGER_LIT' 656 672 tok.info = 32 # unsigned 657 673 return tok 674 675 def parseBase(v as String, nbase as int) as int 676 assert nbase in [2,8,16] 677 if nbase == 16 678 return int.parse(v, System.Globalization.NumberStyles.HexNumber) 679 680 zero = c'0' to int 681 len = v.length 682 r = 0 683 for i in len 684 d = v[i] 685 n = d to int - zero 686 r = r*nbase + n 687 return r 658 688 659 689 def onINT_SIZE(tok as IToken) as IToken? 660 690 size = int.parse(tok.text[3:]) to int -
Tests/100-basics/922-bin-lit.cobra
1 namespace Test 2 class BinLit 3 """ 4 r'BIN_LIT_UNSIGN 0b[01][01]*(u|_u)(8|16|32|64)?' 5 r'BIN_LIT_EXPLICIT 0b[01][01]*_(8|16|32|64)?' 6 r'BIN_LIT 0b[01][01]*' 7 """ 8 9 def lit is shared 10 """ 11 r'BIN_LIT 0b[01][01]*', 12 """ 13 14 h = 0b1 15 assert h == 1u 16 assert h.getType is UInt32 17 18 h1 = 0b1010 19 assert h1 == 10u 20 assert h1.getType is UInt32 21 22 h2 = 0b1111 23 assert h2 == 15u 24 assert h2.getType is UInt32 25 26 h3 = 0b10 27 assert h3 == 2u 28 assert h3.getType is UInt32 29 30 h4 = 0b00001010 31 assert h4 == 10u 32 assert h4.getType is UInt32 33 34 h5 = 0b00001111 35 assert h5 == 15u 36 assert h5.getType is UInt32 37 38 h6 = 0b00000011 39 assert h6 == 3u 40 assert h6.getType is UInt32 41 42 h7 = 0b011111110 43 assert h7 == 254u 44 assert h.getType is UInt32 45 46 h8 = 0b011111111 47 assert h8 == 255u 48 assert h8.getType is UInt32 49 50 h9 = 0b0111111111111 51 assert h9 == 4095u 52 assert h9.getType is UInt32 53 54 h10 = 0b1000001 55 assert h10 == 65u 56 assert h10.getType is UInt32 57 58 h11 = 0b100_0000_0001 59 assert h11 == 1025u 60 assert h11.getType is UInt32 61 62 def unsign is shared 63 """ 64 r'BIN_LIT_UNSIGN 0b[01][01]*(u|_u)(8|16|32|64)?' 65 """ 66 67 h2a = 0b1111u 68 assert h2a == 15u 69 assert h2a.getType is UInt32 70 71 h2b = 0b1111_u 72 assert h2b == 15u 73 assert h2b.getType is UInt32 74 75 h1 = 0b11111111u 76 assert h1 == 255u 77 assert h1.getType is UInt32 78 79 h2 = 0b11111111_u 80 assert h2 == 255u 81 assert h2.getType is UInt32 82 83 h3 = 0b11110000u16 84 assert h3 == 240u 85 assert h3.getType is UInt16 86 87 h4 = 0b11110000_u16 88 assert h4 == 240u 89 assert h4.getType is UInt16 90 91 h5 = 0b11111111_u64 92 assert h5 == 255u 93 assert h5.getType is UInt64 94 95 h6 = 0b111111110001_u64 96 assert h6 == 4081u 97 assert h6.getType is UInt64 98 99 h7 = 0b11111111_u8 100 assert h7 == 255u 101 assert h7.getType is Byte 102 103 h8 = 0b11111111u8 104 assert h8 == 255u 105 assert h8.getType is Byte 106 107 h9 = 0b100_0000_0001_u16 108 assert h9 == 1025u 109 assert h9.getType is UInt16 110 111 def sign is shared 112 """ 113 r'BIN_LIT_SIGN 0b[01][01_]*i(8|16|32|64)? 114 """ 115 h1 = 0b1110_i8 116 assert h1 == 14i 117 assert h1.getType is SByte 118 119 h2 = 0b1010_i 120 assert h2 == 10i 121 assert h2.getType is Int32 122 123 h3 = 0b1110_i16 124 assert h3 == 14i 125 assert h3.getType is Int16 126 127 h4 = 0b11111_i32 128 assert h4 == 31i 129 assert h4.getType is Int32 130 131 h5 = 0b1111111111111111_i64 132 assert h5 == 65535i 133 assert h5.getType is Int64 134 135 h6 = 0b0111111011101101i16 136 assert h6 == 32493i 137 assert h6.getType is Int16 138 139 h7 = 0b0111_1110_1110_1101i16 140 assert h7 == 32493i 141 assert h7.getType is Int16 142 143 h8 = 0b111_1111_0111_01101i 144 assert h8 == 65261i 145 assert h8.getType is Int32 146 147 def random is shared 148 b = 255u8 149 b = 255u8 & 31u8 150 assert b == 31_u8 151 assert b.getType is Byte 152 assert b == 0b11111u8 153 154 b = 255u8 155 b &= 0b11111u8 156 assert b == 0b11111_u8 157 assert b.getType is Byte 158 159 # These dont compile for some reason - same with non hex vals 160 #b = 255u8 161 #b = b & 0b11111u8 162 ## OR 163 ##b = b & 31u8 164 #assert b == 0b11111u8 165 #b = 255u8 166 #b1 = 0b11111u8 167 #b = b & b1 168 #print b 169 170 c = 0b0_u8 171 c |= 0b11110_u8 172 assert c == 0b11110_u8 173 174 # Neither do these 175 #c = 0x0_u8 176 #c = c | 0x0_u8 177 #assert c == 0x1e_u8 178 # 179 #c = 0x0_u8 180 #c1 = 0x0_u8 181 #c = c | c1 182 #assert c == 0x1e_u8 183 184 def underscore is shared 185 a = 0b1101_1000u8 186 assert a == 216_u8 187 assert a.getType is Byte 188 189 a1 = 0b1101_1001_u8 190 assert a1 == 217_u8 191 assert a1.getType is Byte 192 193 b = 0b1101_1010_u16 194 assert b == 218_u16 195 assert b.getType is UInt16 196 197 c = 0b1101_1011_u16 198 assert c == 219_u16 199 assert c.getType is UInt16 200 201 d = 0b1101_1100_u64 202 assert d == 220_u64 203 assert d.getType is UInt64 204 205 def main is shared 206 .lit 207 .unsign 208 .sign 209 .random 210 .underscore 211 212 -
Tests/100-basics/920-hex-lit.cobra
53 53 54 54 def unsign is shared 55 55 """ 56 r'HEX_LIT_UNSIGN 0x[\dA-Fa-f][\dA-Fa-f ]*(u|_u)(8|16|32|64)?'56 r'HEX_LIT_UNSIGN 0x[\dA-Fa-f][\dA-Fa-f_]*(u|_u)(8|16|32|64)?' 57 57 """ 58 58 59 h2 = 0xa_ # default is unsigned 60 assert h2 == 10u 61 assert h2.getType is UInt32 62 59 63 h2a = 0xfu 60 64 assert h2a == 15u 61 65 assert h2a.getType is UInt32 … … 96 100 assert h8 == 255u 97 101 assert h8.getType is Byte 98 102 99 def explicitis shared103 def sign is shared 100 104 """ 101 r'HEX_LIT_ EXPLICIT 0x[\dA-Fa-f][\dA-Fa-f]*_(8|16|32|64)?105 r'HEX_LIT_SIGNED 0x[\dA-Fa-f][\dA-Fa-f]*i(8|16|32|64)? 102 106 """ 103 h1 = 0xe_ 8104 assert h1 == 14 u105 assert h1.getType is Byte107 h1 = 0xe_i8 108 assert h1 == 14i 109 assert h1.getType is SByte 106 110 107 h2 = 0xa_ 111 h2 = 0xa_ # default is unsigned 108 112 assert h2 == 10u 109 assert h2.getType is UInt32113 assert h2.getType is UInt32 110 114 111 h3 = 0xe_ 16112 assert h3 == 14 u113 assert h3.getType is UInt16115 h3 = 0xe_i16 116 assert h3 == 14i16 117 assert h3.getType is Int16 114 118 115 h4 = 0x1f_ 32116 assert h4 == 31 u117 assert h4.getType is UInt32119 h4 = 0x1f_i32 120 assert h4 == 31i 121 assert h4.getType is Int32 118 122 119 h5 = 0xFffF_ 64120 assert h5 == 65535 u121 assert h5.getType is UInt64123 h5 = 0xFffF_i64 124 assert h5 == 65535i 125 assert h5.getType is Int64 122 126 123 h6 = 0xFeed_16 124 assert h6 == 65261u 125 assert h6.getType is UInt16 126 127 h6 = 0x7eed_i16 128 assert h6 == 32493i 129 assert h6.getType is Int16 130 131 h7 = 0x7fff_i16 132 assert h7 == 32767i 133 assert h7.getType is Int16 134 135 h8 = 0xff_ff_i32 136 assert h8 == 65535i 137 assert h8.getType is Int32 138 139 h9 = 0xf_f_f_fi32 140 assert h9 == 65535i 141 assert h9.getType is Int32 142 143 h10 = 0x7__f_i8 144 assert h10 == 127i8 145 assert h10.getType is SByte 146 127 147 def random is shared 128 b = 255u8148 b = 255u8 129 149 b = 255u8 & 31u8 130 150 assert b == 31_u8 131 151 assert b.getType is Byte 132 152 assert b == 0x1fu8 133 153 134 154 b = 255u8 135 b &= 0x1fu8155 b &= 0x1fu8 136 156 assert b == 0x1F_u8 137 157 assert b.getType is Byte 138 158 … … 164 184 def main is shared 165 185 .lit 166 186 .unsign 167 . explicit187 .sign 168 188 .random 169 189 170 190 -
Tests/100-basics/924-oct-lit.cobra
1 namespace Test 2 class OctLit 3 """ 4 r'OCT_LIT_EXPLICIT 0o[0-7][0-7_]*(u|i)(8|16|32|64)?', 5 r'OCT_LIT 0o[0-7][0-7_]*', 6 """ 7 8 def lit is shared 9 """ 10 r'OCT_LIT 0o[0-7][0-7_]*', 11 """ 12 h = 0o1 13 assert h == 1u 14 assert h.getType is UInt32 15 16 h1 = 0o10_ 17 assert h1 == 8u 18 assert h1.getType is UInt32 19 20 h2 = 0o7 21 assert h2 == 7u 22 assert h2.getType is UInt32 23 24 h3 = 0o02 25 assert h3 == 2u 26 assert h3.getType is UInt32 27 28 h4 = 0o05 29 assert h4 == 5u 30 assert h4.getType is UInt32 31 32 h5 = 0o07 33 assert h5 == 7u 34 assert h5.getType is UInt32 35 36 h6 = 0o03 37 assert h6 == 3u 38 assert h6.getType is UInt32 39 40 h7 = 0o076 41 assert h7 == 62u 42 assert h.getType is UInt32 43 44 h8 = 0o77 45 assert h8 == 63u 46 assert h8.getType is UInt32 47 48 h9 = 0o0777 49 assert h9 == 511u 50 assert h9.getType is UInt32 51 52 def unsign is shared 53 """ 54 r'OCT_LIT_EXPLICIT 0o[0-7][0-7_]*(u|i)(8|16|32|64)?', 55 """ 56 57 h2 = 0o7_ # default is unsigned 58 assert h2 == 7u 59 assert h2.getType is UInt32 60 61 h2a = 0o17u 62 assert h2a == 15u 63 assert h2a.getType is UInt32 64 65 h2b = 0o17_u 66 assert h2b == 15u 67 assert h2b.getType is UInt32 68 69 h1 = 0o377u 70 assert h1 == 255u 71 assert h1.getType is UInt32 72 73 h2 = 0o377_u 74 assert h2 == 255u 75 assert h2.getType is UInt32 76 77 h3 = 0o360u16 78 assert h3 == 240u 79 assert h3.getType is UInt16 80 81 h4 = 0o360_u16 82 assert h4 == 240u 83 assert h4.getType is UInt16 84 85 h5 = 0o377_u64 86 assert h5 == 255u 87 assert h5.getType is UInt64 88 89 h6 = 0o7761_u64 90 assert h6 == 4081u 91 assert h6.getType is UInt64 92 93 h7 = 0o377_u8 94 assert h7 == 255u 95 assert h7.getType is Byte 96 97 h8 = 0o377u8 98 assert h8 == 255u 99 assert h8.getType is Byte 100 101 def sign is shared 102 """ 103 r'OCT_LIT_EXPLICIT 0o[0-7][0-7_]*(u|i)(8|16|32|64)?', 104 """ 105 h1 = 0o16_i8 106 assert h1 == 14i 107 assert h1.getType is SByte 108 109 h2 = 0o12_ # default is unsigned 110 assert h2 == 10u 111 assert h2.getType is UInt32 112 113 h3 = 0o16_i16 114 assert h3 == 14i16 115 assert h3.getType is Int16 116 117 h4 = 0o37_i32 118 assert h4 == 31i 119 assert h4.getType is Int32 120 121 h5 = 0o177777_i64 122 assert h5 == 65535i 123 assert h5.getType is Int64 124 125 h6 = 0o77355_i16 126 assert h6 == 32493i 127 assert h6.getType is Int16 128 129 h7 = 0o77777_i16 130 assert h7 == 32767i 131 assert h7.getType is Int16 132 133 h8 = 0o177_777_i32 134 assert h8 == 65535i 135 assert h8.getType is Int32 136 137 h9 = 0o17_7_7_7_7i32 138 assert h9 == 65535i 139 assert h9.getType is Int32 140 141 h10 = 0o0__7__7_i8 142 assert h10 == 63i8 143 assert h10.getType is SByte 144 145 def random is shared 146 b = 255u8 147 b = 255u8 & 31u8 148 assert b == 31_u8 149 assert b.getType is Byte 150 assert b == 0o37u8 151 152 b = 255u8 153 b &= 0o37u8 154 assert b == 0o37_u8 155 assert b.getType is Byte 156 157 # These dont compile for some reason - same with non hex vals 158 #b = 255u8 159 #b = b & 0o37u8 160 ## OR 161 ##b = b & 31u8 162 #assert b == 0o37u8 163 #b = 255u8 164 #b1 = 0o37u8 165 #b = b & b1 166 #print b 167 168 c = 0o0_u8 169 c |= 0o36_u8 170 assert c == 0o36_u8 171 172 # Neither do these 173 #c = 0o0_u8 174 #c = c | 0o17_u8 175 #assert c == 0o17_u8 176 # 177 #c = 0o0_u8 178 #c1 = 0o71_u8 179 #c = c | c1 180 #assert c == 0o71_u8 181 182 def main is shared 183 .lit 184 .unsign 185 .sign 186 .random 187 188 189 -
Developer/IntermediateReleaseNotes.text
1 1 Post 0.8 2 2 3 * Augment integer hex literal support to allow (unsigned and signed) binary, octal or hex literals and 4 allow embedded _ separators in such literals. (ticket#29) 5 3 6 * Added a new built-in doc tool accessible via "cobra -doc ...". The documentation is generated to local HTML files using your declarations, doc strings, contracts, etc. 4 7 5 8 * Add support for specifying unsigned integers as Hex literals