package core import ( "fmt" "strconv" "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/errors" "git.akyoto.dev/cli/q/src/build/token" ) // TokenToRegister moves a token into a register. // It only works with identifiers, numbers and strings. func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error { switch t.Kind { case token.Identifier: name := t.Text() variable := f.Variable(name) if variable == nil { return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position) } f.useVariable(variable) f.RegisterRegister(asm.MOVE, register, variable.Register) return nil case token.Number: value := t.Text() n, err := strconv.Atoi(value) if err != nil { return err } f.RegisterNumber(asm.MOVE, register, n) return nil case token.String: f.count.data++ label := fmt.Sprintf("%s_data_%d", f.Name, f.count.data) value := t.Bytes[1 : len(t.Bytes)-1] f.Assembler.SetData(label, value) f.RegisterLabel(asm.MOVE, register, label) return nil default: return errors.New(errors.InvalidExpression, f.File, t.Position) } }