From e3db547967067f855a0e77a1bdff1ca5bf632dda Mon Sep 17 00:00:00 2001 From: Cedric <14017092+douyixuan@users.noreply.github.com> Date: Fri, 13 Sep 2024 22:32:58 +0800 Subject: [PATCH] fix number literal parsing --- compiler/parser/parser.go | 41 ++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/compiler/parser/parser.go b/compiler/parser/parser.go index cfaf010..289dcab 100644 --- a/compiler/parser/parser.go +++ b/compiler/parser/parser.go @@ -122,32 +122,37 @@ func (p *parser) parseOneWithOptions(withAheadParse, withArithAhead, withIdentif // HEX always returns a ConstantNode // Convert string hex representation to int64 case lexer.HEX: - val, err := strconv.ParseInt(current.Val, 16, 64) - if err != nil { - panic(err) - } - res = &ConstantNode{ - Type: NUMBER, - TargetType: p.parseNumberSuffix(), - Value: val, - } - if withAheadParse { - res = p.aheadParse(res) - } - return - + fallthrough // NUMBER always returns a ConstantNode // Convert string representation to int64 case lexer.NUMBER: - val, err := strconv.ParseInt(current.Val, 10, 64) - if err != nil { - panic(err) + base := 10 + if current.Type == lexer.HEX { + base = 16 + } + targetTy := p.parseNumberSuffix() + var val int64 + valStr := "" + if targetTy != nil && (targetTy.GetName() == "uint128" || targetTy.GetName() == "uint256") { + valStr = current.Val + // try to parse integer as 64-bit integer, set ValStr only if failed + tmpVal, err := strconv.ParseInt(current.Val, base, 64) + if err == nil { + val = tmpVal + } + } else { + tmpVal, err := strconv.ParseInt(current.Val, base, 64) + if err != nil { + panic(err) + } + val = tmpVal } res = &ConstantNode{ Type: NUMBER, - TargetType: p.parseNumberSuffix(), + TargetType: targetTy, Value: val, + ValueStr: valStr, } if withAheadParse { res = p.aheadParse(res)