package core import ( "math" "git.akyoto.dev/cli/q/src/asm" "git.akyoto.dev/cli/q/src/errors" "git.akyoto.dev/cli/q/src/expression" "git.akyoto.dev/cli/q/src/types" ) var _len = Function{OutputTypes: []types.Type{types.Int}} // CompileLen returns the length of a slice. func (f *Function) CompileLen(root *expression.Expression) error { typ, register, isTemporary, err := f.Evaluate(root.Children[1]) if err != nil { return err } if !types.Is(typ, types.AnyArray) { return errors.New(&errors.TypeMismatch{Encountered: typ.Name(), Expected: types.AnyArray.Name(), ParameterName: "array"}, f.File, root.Children[1].Token.Position) } f.SaveRegister(f.CPU.Output[0]) f.MemoryRegister(asm.LOAD, asm.Memory{Base: register, Offset: -8, OffsetRegister: math.MaxUint8, Length: 8}, f.CPU.Output[0]) if isTemporary { f.FreeRegister(register) } return nil }