Skip to content

Commit e3db547

Browse files
committed
fix number literal parsing
1 parent 7c2dab0 commit e3db547

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

compiler/parser/parser.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,32 +122,37 @@ func (p *parser) parseOneWithOptions(withAheadParse, withArithAhead, withIdentif
122122
// HEX always returns a ConstantNode
123123
// Convert string hex representation to int64
124124
case lexer.HEX:
125-
val, err := strconv.ParseInt(current.Val, 16, 64)
126-
if err != nil {
127-
panic(err)
128-
}
129-
res = &ConstantNode{
130-
Type: NUMBER,
131-
TargetType: p.parseNumberSuffix(),
132-
Value: val,
133-
}
134-
if withAheadParse {
135-
res = p.aheadParse(res)
136-
}
137-
return
138-
125+
fallthrough
139126
// NUMBER always returns a ConstantNode
140127
// Convert string representation to int64
141128
case lexer.NUMBER:
142-
val, err := strconv.ParseInt(current.Val, 10, 64)
143-
if err != nil {
144-
panic(err)
129+
base := 10
130+
if current.Type == lexer.HEX {
131+
base = 16
132+
}
133+
targetTy := p.parseNumberSuffix()
134+
var val int64
135+
valStr := ""
136+
if targetTy != nil && (targetTy.GetName() == "uint128" || targetTy.GetName() == "uint256") {
137+
valStr = current.Val
138+
// try to parse integer as 64-bit integer, set ValStr only if failed
139+
tmpVal, err := strconv.ParseInt(current.Val, base, 64)
140+
if err == nil {
141+
val = tmpVal
142+
}
143+
} else {
144+
tmpVal, err := strconv.ParseInt(current.Val, base, 64)
145+
if err != nil {
146+
panic(err)
147+
}
148+
val = tmpVal
145149
}
146150

147151
res = &ConstantNode{
148152
Type: NUMBER,
149-
TargetType: p.parseNumberSuffix(),
153+
TargetType: targetTy,
150154
Value: val,
155+
ValueStr: valStr,
151156
}
152157
if withAheadParse {
153158
res = p.aheadParse(res)

0 commit comments

Comments
 (0)