MCPcopy Create free account
hub / github.com/akyoto/q / evaluateArray

Method evaluateArray

src/core/evaluateArray.go:12–63  ·  view source on GitHub ↗

evaluateArray converts a array indexing expression to an SSA value.

(expr *expression.Expression)

Source from the content-addressed store, hash-verified

10
11// evaluateArray converts a array indexing expression to an SSA value.
12func (f *Function) evaluateArray(expr *expression.Expression) (ssa.Value, error) {
13 address := expr.Children[0]
14 addressValue, err := f.evaluateRight(address)
15
16 if err != nil {
17 return nil, err
18 }
19
20 addressValue, addressType, length, err := f.decomposeSlice(addressValue)
21
22 if err != nil {
23 return nil, err
24 }
25
26 pointer, isPointer := types.Unwrap(addressType).(*types.Pointer)
27
28 if !isPointer {
29 return nil, errors.New(&TypeNotIndexable{TypeName: addressType.Name()}, f.File, address.Source())
30 }
31
32 var indexValue ssa.Value
33
34 if len(expr.Children) > 1 {
35 index := expr.Children[1]
36
37 if index.Token.Kind == token.Range {
38 return f.evaluateSlice(expr, index, addressValue, length)
39 } else {
40 indexValue, err = f.evaluateRight(index)
41
42 if err != nil {
43 return nil, err
44 }
45
46 if !types.Is(indexValue.Type(), types.AnyInt) {
47 return nil, errors.New(&TypeMismatch{Encountered: indexValue.Type().Name(), Expected: types.AnyInt.Name()}, f.File, index.Source())
48 }
49 }
50 } else {
51 indexValue = f.Append(&ssa.Int{Int: 0})
52 }
53
54 memory := &ssa.Memory{
55 Address: addressValue,
56 Index: indexValue,
57 Scale: true,
58 Typ: pointer.To,
59 Source: expr.Source(),
60 }
61
62 return memory, nil
63}

Callers 2

compileStoreArrayMethod · 0.95
evaluateMethod · 0.95

Calls 10

evaluateRightMethod · 0.95
decomposeSliceMethod · 0.95
evaluateSliceMethod · 0.95
TypeMethod · 0.95
UnwrapFunction · 0.92
NewFunction · 0.92
IsFunction · 0.92
NameMethod · 0.65
SourceMethod · 0.45
AppendMethod · 0.45

Tested by

no test coverage detected