Implemented numbers with different bases

This commit is contained in:
Eduard Urbach 2024-07-29 00:30:26 +02:00
parent 0e999cae92
commit 6d77a8a120
Signed by: eduard
GPG key ID: 49226B848C78F6C8
11 changed files with 165 additions and 12 deletions

View file

@ -2,6 +2,7 @@ package core
import (
"strconv"
"strings"
"unicode/utf8"
"git.akyoto.dev/cli/q/src/build/errors"
@ -12,7 +13,24 @@ import (
func (f *Function) Number(t token.Token) (int, byte, error) {
switch t.Kind {
case token.Number:
number, err := strconv.Atoi(t.Text(f.File.Bytes))
digits := t.Text(f.File.Bytes)
if strings.HasPrefix(digits, "0x") {
number, err := strconv.ParseInt(digits[2:], 16, 64)
return int(number), 8, err
}
if strings.HasPrefix(digits, "0o") {
number, err := strconv.ParseInt(digits[2:], 8, 64)
return int(number), 8, err
}
if strings.HasPrefix(digits, "0b") {
number, err := strconv.ParseInt(digits[2:], 2, 64)
return int(number), 8, err
}
number, err := strconv.Atoi(digits)
return number, 8, err
case token.Rune: