diff --git a/src/core/compileStoreField.go b/src/core/compileStoreField.go index 6ecb5a0..95b94d0 100644 --- a/src/core/compileStoreField.go +++ b/src/core/compileStoreField.go @@ -1,6 +1,8 @@ package core import ( + "fmt" + "git.urbach.dev/cli/q/src/ast" "git.urbach.dev/cli/q/src/ssa" "git.urbach.dev/cli/q/src/token" @@ -56,6 +58,7 @@ func (f *Function) compileStoreField(node *ast.Assign) error { case *types.Struct: field := pointer.FieldByName(fieldName) addressValue.(*ssa.Struct).Arguments[field.Index] = rightValue + f.Block().Identify(fmt.Sprintf("%s.%s", address.String(f.File.Bytes), fieldName), rightValue) } return nil diff --git a/src/core/define.go b/src/core/define.go index 84dbbd4..666a03d 100644 --- a/src/core/define.go +++ b/src/core/define.go @@ -1,6 +1,8 @@ package core import ( + "fmt" + "git.urbach.dev/cli/q/src/errors" "git.urbach.dev/cli/q/src/expression" "git.urbach.dev/cli/q/src/ssa" @@ -28,6 +30,14 @@ func (f *Function) define(identifier *expression.Expression, value ssa.Value) er _, isCall := value.(*ssa.Call) if !isCall { + structure, isStructType := value.(*ssa.Struct) + + if isStructType { + for i, field := range structure.Typ.Fields { + f.Block().Identify(fmt.Sprintf("%s.%s", name, field.Name), structure.Arguments[i]) + } + } + f.Block().Identify(name, value) return nil } @@ -45,16 +55,17 @@ func (f *Function) define(identifier *expression.Expression, value ssa.Value) er Source: identifier.Source(), } - for i := range structure.Fields { - field := &ssa.FromTuple{ + for i, field := range structure.Fields { + fieldValue := &ssa.FromTuple{ Tuple: value, Index: i, Structure: composite, Source: identifier.Source(), } - f.Block().Append(field) - composite.Arguments = append(composite.Arguments, field) + f.Block().Append(fieldValue) + f.Block().Identify(fmt.Sprintf("%s.%s", name, field.Name), fieldValue) + composite.Arguments = append(composite.Arguments, fieldValue) } f.Block().Identify(name, composite) diff --git a/tests/loop-modify-struct.q b/tests/loop-modify-struct.q new file mode 100644 index 0000000..507993f --- /dev/null +++ b/tests/loop-modify-struct.q @@ -0,0 +1,15 @@ +import io + +Point { + x int + y int +} + +main() { + p := Point{x: 1} + + loop 0..5 { + io.write(p.x) + p.x += 1 + } +} \ No newline at end of file diff --git a/tests/tests_test.go b/tests/tests_test.go index 8418e38..0aa7a23 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -56,6 +56,7 @@ var tests = []run{ {"loop-keepalive", "", "", 0}, {"loop-copy-from", "", "", 0}, {"loop-copy-until", "", "", 0}, + {"loop-modify-struct", "", "12345", 0}, {"factorial", "", "", 0}, {"fibonacci", "", "", 0}, {"memory", "", "Hello\n", 0},