Wiki

Ticket #29: hunk2

File hunk2, 3.2 KB (added by hopscc, 10 years ago)
Line 
1    def _onINTEGER_LIT_EXPLICIT(tok as IToken, signed as bool)
2        ensure
3            tok.which == 'INTEGER_LIT'
4            (tok.info to int) in [-8, 8, -16, 16, -32, 32, -64, 64]  # CC: axe cast
5            (signed and (tok.info to int) <0) or (not signed and (tok.info to int) >0)
6        body
7            size = .determineLitSize(tok)
8            s = tok.text.replace('_', '')
9            try
10                # TODO: The use of int.parse, as opposed to say int64.parse, uint32.parse, etc. probably
11                # means that legit int lits outside the signed 32 bit range will not work in Cobra
12                tok.value = int.parse(s, Utils.cultureInfoForNumbers)
13            catch FormatException
14                assert false, 'not expecting to get here given regex'
15            catch OverflowException
16                assert false, 'TODO'
17            tok.which = 'INTEGER_LIT'
18>>>> above is context
19>>>> verbatim new code starts after this line - ~line 595 after patch applied
20            tok.info = size
21           
22    def onBIN_LIT_EXPLICIT(tok as IToken) as IToken?
23        require
24            'u' in tok.text or 'i' in tok.text
25        body
26            return .anyBASE_LIT_EXPLICIT(tok, 2)
27           
28    def onBIN_LIT(tok as IToken) as IToken?
29        return .anyBASE_LIT(tok, 2)
30
31    def onOCT_LIT_EXPLICIT(tok as IToken) as IToken?
32        require
33            'u' in tok.text or 'i' in tok.text
34        body
35            return .anyBASE_LIT_EXPLICIT(tok, 8)
36           
37    def onOCT_LIT(tok as IToken) as IToken?
38        return .anyBASE_LIT(tok, 8)
39       
40    def onHEX_LIT_EXPLICIT(tok as IToken) as IToken?
41        require
42            'u' in tok.text or 'i' in tok.text
43        body
44            return .anyBASE_LIT_EXPLICIT(tok, 16)
45
46    def onHEX_LIT(tok as IToken) as IToken?
47        return .anyBASE_LIT(tok, 16)
48
49    def anyBASE_LIT_EXPLICIT(tok as IToken, nbase as int) as IToken
50        ensure
51            tok.which == 'INTEGER_LIT'
52            (tok.info to int) in [8, 16, 32, 64, -8, -16, -32, -64]
53        body
54            h = tok.text
55            size = .determineLitSize(tok)
56            tok = .anyBASE_LIT(tok, nbase) to !
57            tok.info = size 
58            tok.text = h
59            return tok
60           
61    def determineLitSize(tok as IToken) as int
62        size as int = 32
63        s = tok.text
64        if s.endsWith('32')
65            size = 32
66            s = s[:-2]
67        else if s.endsWith('64')
68            size = 64
69            s = s[:-2]
70        else if s.endsWith('16')
71            size = 16
72            s = s[:-2]
73        else if s.endsWith('8')
74            size = 8
75            s = s[:-1]
76        if s.endsWith('u')
77            s = s[:-1]
78        else if s.endsWith('i')
79            s = s[:-1]
80            size = size * -1
81        tok.text = s
82        return size
83   
84    def anyBASE_LIT(tok as IToken, nbase as int) as IToken?
85        ensure
86            tok.which == 'INTEGER_LIT'
87            tok.info == 32
88        body
89            s = tok.text.replace('_', '')
90            try
91                tok.value = .parseBase(s[2:], nbase)
92            catch FormatException
93                assert false, 'Format Exception - not expecting to get here given regex'
94            catch OverflowException
95                assert false, 'TODO: anyBase_LIT Overflow (int)'
96            tok.which = 'INTEGER_LIT'
97            tok.info = 32  # unsigned
98            return tok
99
100    def parseBase(v as String, nbase as int) as int
101        assert nbase in [2,8,16]
102        if nbase == 16
103            return int.parse(v, System.Globalization.NumberStyles.HexNumber)
104
105        zero = c'0' to int
106        len = v.length
107        r = 0
108        for i in len
109            d = v[i]
110            n = d to int - zero
111            r = r*nbase + n
112        return r
113           
114<<<< patch ends here
115<<<< this method shld be next
116    def onINT_SIZE(tok as IToken) as IToken?
117        size = int.parse(tok.text[3:]) to int
118        tok.value = size
119        return tok
120