diff --git a/src/core/evaluateRight.go b/src/core/evaluateRight.go index 6137dbc..50a0df7 100644 --- a/src/core/evaluateRight.go +++ b/src/core/evaluateRight.go @@ -40,26 +40,8 @@ func (f *Function) evaluateRight(expr *expression.Expression) (ssa.Value, error) switch typ := memory.Typ.(type) { case *types.Struct: - fields := make([]ssa.Value, 0, len(typ.Fields)) - - for _, field := range typ.Fields { - fieldMemory := f.structField(memory, field) - - fieldValue := f.Append(&ssa.Load{ - Memory: fieldMemory, - Source: expr.Source(), - }) - - fields = append(fields, fieldValue) - } - - value := &ssa.Struct{ - Typ: typ, - Arguments: fields, - Source: expr.Source(), - } - - return value, nil + structure := f.loadFields(memory, typ, expr.Source()) + return structure, nil default: load := f.Append(&ssa.Load{ diff --git a/src/core/loadFields.go b/src/core/loadFields.go new file mode 100644 index 0000000..3e2af0e --- /dev/null +++ b/src/core/loadFields.go @@ -0,0 +1,24 @@ +package core + +import ( + "git.urbach.dev/cli/q/src/ssa" + "git.urbach.dev/cli/q/src/types" +) + +// loadFields loads all fields from memory into a struct. +func (f *Function) loadFields(memory *ssa.Memory, typ *types.Struct, source ssa.Source) *ssa.Struct { + fields := make([]ssa.Value, len(typ.Fields)) + + for i, field := range typ.Fields { + fields[i] = f.Append(&ssa.Load{ + Memory: f.structField(memory, field), + Source: source, + }) + } + + return &ssa.Struct{ + Typ: typ, + Arguments: fields, + Source: source, + } +} \ No newline at end of file diff --git a/src/core/store.go b/src/core/store.go index db59585..b0f8a31 100644 --- a/src/core/store.go +++ b/src/core/store.go @@ -27,15 +27,7 @@ func (f *Function) store(memory *ssa.Memory, value ssa.Value) error { composite, isStruct := value.(*ssa.Struct) if isStruct { - for i, field := range structure.Fields { - fieldValue := composite.Arguments[i] - - f.Append(&ssa.Store{ - Memory: f.structField(memory, field), - Value: fieldValue, - }) - } - + f.storeFields(memory, structure, composite.Arguments) return nil } @@ -52,12 +44,6 @@ func (f *Function) store(memory *ssa.Memory, value ssa.Value) error { fieldValues[i] = fieldValue } - for i, field := range structure.Fields { - f.Append(&ssa.Store{ - Memory: f.structField(memory, field), - Value: fieldValues[i], - }) - } - + f.storeFields(memory, structure, fieldValues) return nil } \ No newline at end of file diff --git a/src/core/storeFields.go b/src/core/storeFields.go new file mode 100644 index 0000000..cf53cf0 --- /dev/null +++ b/src/core/storeFields.go @@ -0,0 +1,16 @@ +package core + +import ( + "git.urbach.dev/cli/q/src/ssa" + "git.urbach.dev/cli/q/src/types" +) + +// storeFields stores all struct fields into memory. +func (f *Function) storeFields(memory *ssa.Memory, typ *types.Struct, fields []ssa.Value) { + for i, field := range typ.Fields { + f.Append(&ssa.Store{ + Memory: f.structField(memory, field), + Value: fields[i], + }) + } +} \ No newline at end of file