2022-08-11 00:57:04 -06:00
|
|
|
package lexer
|
|
|
|
|
2022-08-17 22:31:27 -06:00
|
|
|
import "strconv"
|
2022-08-29 23:11:10 -06:00
|
|
|
import "git.tebibyte.media/arf/arf/infoerr"
|
2022-08-17 22:31:27 -06:00
|
|
|
|
2022-08-11 00:57:04 -06:00
|
|
|
// tokenizeSymbolBeginning lexes a token that starts with a number.
|
|
|
|
func (lexer *LexingOperation) tokenizeNumberBeginning (negative bool) (err error) {
|
2022-08-17 22:31:27 -06:00
|
|
|
var intNumber uint64
|
|
|
|
var floatNumber float64
|
|
|
|
var isFloat bool
|
2022-08-18 09:32:50 -06:00
|
|
|
var amountRead int
|
|
|
|
var totalRead int
|
2022-08-11 00:57:04 -06:00
|
|
|
|
2022-08-12 13:34:07 -06:00
|
|
|
token := lexer.newToken()
|
|
|
|
|
2022-08-11 00:57:04 -06:00
|
|
|
if lexer.char == '0' {
|
|
|
|
lexer.nextRune()
|
2022-08-18 09:32:50 -06:00
|
|
|
totalRead ++
|
2022-08-11 00:57:04 -06:00
|
|
|
|
|
|
|
if lexer.char == 'x' {
|
|
|
|
lexer.nextRune()
|
2022-08-18 09:32:50 -06:00
|
|
|
totalRead ++
|
|
|
|
intNumber, floatNumber,
|
|
|
|
isFloat, amountRead,
|
|
|
|
err = lexer.tokenizeNumber(16)
|
|
|
|
|
2022-08-11 00:57:04 -06:00
|
|
|
} else if lexer.char == 'b' {
|
|
|
|
lexer.nextRune()
|
2022-08-18 09:32:50 -06:00
|
|
|
totalRead ++
|
|
|
|
intNumber, floatNumber,
|
|
|
|
isFloat, amountRead,
|
|
|
|
err = lexer.tokenizeNumber(2)
|
|
|
|
|
2022-08-11 00:57:04 -06:00
|
|
|
} else if lexer.char == '.' {
|
2022-08-18 09:32:50 -06:00
|
|
|
intNumber, floatNumber,
|
|
|
|
isFloat, amountRead,
|
|
|
|
err = lexer.tokenizeNumber(10)
|
|
|
|
|
2022-09-01 23:40:33 -06:00
|
|
|
} else if lexer.char >= '0' && lexer.char <= '7' {
|
2022-08-18 09:32:50 -06:00
|
|
|
intNumber, floatNumber,
|
|
|
|
isFloat, amountRead,
|
|
|
|
err = lexer.tokenizeNumber(8)
|
2022-09-01 23:37:33 -06:00
|
|
|
} else if lexer.char != ' ' { // a space should correctly
|
|
|
|
err = infoerr.NewError ( // terminate this
|
|
|
|
lexer.file.Location(1),
|
|
|
|
"unexpected rune '" + string(lexer.char) +
|
|
|
|
"' in integer literal",
|
|
|
|
infoerr.ErrorKindError)
|
|
|
|
return
|
2022-08-11 00:57:04 -06:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-18 09:32:50 -06:00
|
|
|
intNumber, floatNumber,
|
|
|
|
isFloat, amountRead,
|
|
|
|
err = lexer.tokenizeNumber(10)
|
|
|
|
}
|
|
|
|
|
|
|
|
totalRead += amountRead
|
|
|
|
if negative {
|
|
|
|
totalRead += 1
|
2022-08-11 00:57:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-08-11 01:17:35 -06:00
|
|
|
if isFloat {
|
|
|
|
token.kind = TokenKindFloat
|
|
|
|
if negative {
|
|
|
|
token.value = floatNumber * -1
|
|
|
|
} else {
|
|
|
|
token.value = floatNumber
|
|
|
|
}
|
2022-08-11 00:57:04 -06:00
|
|
|
} else {
|
2022-08-11 01:17:35 -06:00
|
|
|
if negative {
|
|
|
|
token.kind = TokenKindInt
|
2022-08-17 22:31:27 -06:00
|
|
|
token.value = int64(intNumber) * -1
|
2022-08-11 01:17:35 -06:00
|
|
|
} else {
|
|
|
|
token.kind = TokenKindUInt
|
2022-08-17 22:31:27 -06:00
|
|
|
token.value = uint64(intNumber)
|
2022-08-11 01:17:35 -06:00
|
|
|
}
|
2022-08-11 00:57:04 -06:00
|
|
|
}
|
2022-08-18 09:32:50 -06:00
|
|
|
|
|
|
|
token.location.SetWidth(totalRead)
|
2022-08-11 00:57:04 -06:00
|
|
|
lexer.addToken(token)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-17 22:31:27 -06:00
|
|
|
// runeIsDigit checks to see if the rune is a valid digit within the given
|
|
|
|
// radix, up to 16. A '.' rune will also be treated as valid.
|
|
|
|
func runeIsDigit (char rune, radix uint64) (isDigit bool) {
|
|
|
|
isDigit = true
|
2022-08-11 00:57:04 -06:00
|
|
|
|
2022-08-17 22:31:27 -06:00
|
|
|
var digit uint64
|
2022-08-11 00:57:04 -06:00
|
|
|
if char >= '0' && char <= '9' {
|
|
|
|
digit = uint64(char - '0')
|
|
|
|
} else if char >= 'A' && char <= 'F' {
|
2022-08-11 03:25:56 -06:00
|
|
|
digit = uint64(char - 'A' + 10)
|
2022-08-11 00:57:04 -06:00
|
|
|
} else if char >= 'a' && char <= 'f' {
|
2022-08-11 03:25:56 -06:00
|
|
|
digit = uint64(char - 'a' + 10)
|
2022-08-17 22:31:27 -06:00
|
|
|
} else if char != '.' {
|
|
|
|
isDigit = false
|
2022-08-11 00:57:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if digit >= radix {
|
2022-08-17 22:31:27 -06:00
|
|
|
isDigit = false
|
2022-08-11 00:57:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 01:03:54 -06:00
|
|
|
// tokenizeNumber reads and tokenizes a number with the specified radix.
|
2022-08-11 01:17:35 -06:00
|
|
|
func (lexer *LexingOperation) tokenizeNumber (
|
|
|
|
radix uint64,
|
|
|
|
) (
|
2022-08-17 22:31:27 -06:00
|
|
|
intNumber uint64,
|
|
|
|
floatNumber float64,
|
|
|
|
isFloat bool,
|
2022-08-18 09:32:50 -06:00
|
|
|
amountRead int,
|
2022-08-17 22:31:27 -06:00
|
|
|
err error,
|
2022-08-11 01:17:35 -06:00
|
|
|
) {
|
2022-08-17 22:31:27 -06:00
|
|
|
got := ""
|
2022-08-11 00:57:04 -06:00
|
|
|
for {
|
2022-08-17 22:31:27 -06:00
|
|
|
if !runeIsDigit(lexer.char, radix) { break }
|
|
|
|
if lexer.char == '.' {
|
|
|
|
if radix != 10 {
|
2022-08-17 22:58:40 -06:00
|
|
|
err = infoerr.NewError (
|
2022-08-17 22:31:27 -06:00
|
|
|
lexer.file.Location(1),
|
|
|
|
"floats must have radix of 10",
|
2022-08-17 22:58:40 -06:00
|
|
|
infoerr.ErrorKindError)
|
2022-08-17 22:31:27 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
isFloat = true
|
|
|
|
}
|
2022-08-11 00:57:04 -06:00
|
|
|
|
2022-08-17 22:31:27 -06:00
|
|
|
got += string(lexer.char)
|
2022-08-11 00:57:04 -06:00
|
|
|
err = lexer.nextRune()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
2022-08-11 11:12:41 -06:00
|
|
|
|
2022-08-18 09:32:50 -06:00
|
|
|
amountRead = len(got)
|
|
|
|
|
2022-08-17 22:31:27 -06:00
|
|
|
if isFloat {
|
|
|
|
floatNumber, err = strconv.ParseFloat(got, 64)
|
|
|
|
} else {
|
|
|
|
intNumber, err = strconv.ParseUint(got, int(radix), 64)
|
2022-08-11 01:17:35 -06:00
|
|
|
}
|
|
|
|
|
2022-08-17 22:31:27 -06:00
|
|
|
if err != nil {
|
2022-08-17 22:58:40 -06:00
|
|
|
err = infoerr.NewError (
|
2022-08-17 22:31:27 -06:00
|
|
|
lexer.file.Location(1),
|
|
|
|
"could not parse number: " + err.Error(),
|
2022-08-17 22:58:40 -06:00
|
|
|
infoerr.ErrorKindError)
|
2022-08-17 22:31:27 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 00:57:04 -06:00
|
|
|
return
|
|
|
|
}
|