From 120976a0f3fab4ec9663223ef6e1bf69fdad7b2a Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 18 Aug 2022 11:32:50 -0400 Subject: [PATCH] Numbers now tokenize with the correct width --- lexer/numbers.go | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/lexer/numbers.go b/lexer/numbers.go index d4f7225..af1d2ab 100644 --- a/lexer/numbers.go +++ b/lexer/numbers.go @@ -8,25 +8,48 @@ func (lexer *LexingOperation) tokenizeNumberBeginning (negative bool) (err error var intNumber uint64 var floatNumber float64 var isFloat bool + var amountRead int + var totalRead int token := lexer.newToken() if lexer.char == '0' { lexer.nextRune() + totalRead ++ if lexer.char == 'x' { lexer.nextRune() - intNumber, floatNumber, isFloat, err = lexer.tokenizeNumber(16) + totalRead ++ + intNumber, floatNumber, + isFloat, amountRead, + err = lexer.tokenizeNumber(16) + } else if lexer.char == 'b' { lexer.nextRune() - intNumber, floatNumber, isFloat, err = lexer.tokenizeNumber(2) + totalRead ++ + intNumber, floatNumber, + isFloat, amountRead, + err = lexer.tokenizeNumber(2) + } else if lexer.char == '.' { - intNumber, floatNumber, isFloat, err = lexer.tokenizeNumber(10) + intNumber, floatNumber, + isFloat, amountRead, + err = lexer.tokenizeNumber(10) + } else if lexer.char >= '0' && lexer.char <= '9' { - intNumber, floatNumber, isFloat, err = lexer.tokenizeNumber(8) + intNumber, floatNumber, + isFloat, amountRead, + err = lexer.tokenizeNumber(8) } } else { - intNumber, floatNumber, isFloat, err = lexer.tokenizeNumber(10) + intNumber, floatNumber, + isFloat, amountRead, + err = lexer.tokenizeNumber(10) + } + + totalRead += amountRead + if negative { + totalRead += 1 } if err != nil { return } @@ -47,7 +70,8 @@ func (lexer *LexingOperation) tokenizeNumberBeginning (negative bool) (err error token.value = uint64(intNumber) } } - + + token.location.SetWidth(totalRead) lexer.addToken(token) return } @@ -82,6 +106,7 @@ func (lexer *LexingOperation) tokenizeNumber ( intNumber uint64, floatNumber float64, isFloat bool, + amountRead int, err error, ) { got := "" @@ -103,6 +128,8 @@ func (lexer *LexingOperation) tokenizeNumber ( if err != nil { return } } + amountRead = len(got) + if isFloat { floatNumber, err = strconv.ParseFloat(got, 64) } else {