Implemented runes
This commit is contained in:
parent
eaeeba495e
commit
f7645104fb
11 changed files with 88 additions and 27 deletions
36
src/build/core/Number.go
Normal file
36
src/build/core/Number.go
Normal 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue