Implemented runes

This commit is contained in:
Eduard Urbach 2024-07-22 15:32:16 +02:00
parent eaeeba495e
commit f7645104fb
Signed by: eduard
GPG key ID: 49226B848C78F6C8
11 changed files with 88 additions and 27 deletions

36
src/build/core/Number.go Normal file
View file

@ -0,0 +1,36 @@
package core
import (
"strconv"
"unicode/utf8"
"git.akyoto.dev/cli/q/src/build/errors"
"git.akyoto.dev/cli/q/src/build/token"
)
// Number tries to convert the token into a numeric value.
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))
return number, 8, err
case token.Rune:
r := t.Bytes(f.File.Bytes)
r = r[1 : len(r)-1]
if len(r) == 0 {
return 0, 0, errors.New(errors.InvalidRune, f.File, t.Position+1)
}
number, size := utf8.DecodeRune(r)
if len(r) > size {
return 0, 0, errors.New(errors.InvalidRune, f.File, t.Position+1)
}
return int(number), byte(size), nil
}
return 0, 0, errors.New(errors.InvalidNumber, f.File, t.Position)
}