For loop elements are properly copied

This commit is contained in:
Sasha Koshka
2024-03-24 03:09:33 -04:00
parent a0496d3fab
commit 005baa2ab6
6 changed files with 43 additions and 4 deletions
+20
View File
@@ -122,3 +122,23 @@ testUnit (test,
"", "a\nb\nc\na\nb\nc\n",
0,
)}
func TestForStringArrayOnce (test *testing.T) {
dependencies := []string {
compileDependency(test, "io"),
}
testUnit (test,
"/test-data/data/for-string-array-once", dependencies,
"", "abc\n",
0,
)}
func TestForBreakBranch (test *testing.T) {
dependencies := []string {
compileDependency(test, "io"),
}
testUnit (test,
"/test-data/data/for-break-branch", dependencies,
"", "iter\niter\n",
0,
)}
@@ -0,0 +1,2 @@
'61b23750-b3c5-4cbd-85f7-71322a23f980'
+ 'io'
@@ -0,0 +1,8 @@
[main]:I32 'main' = {
arr:3:Int = (* 5 6 7)
for i:Index e:Int in arr {
if [>= i 2] then [break]
io::[println 'iter']
}
0
}
@@ -0,0 +1,3 @@
'c9024148-f4ec-4d69-84c1-c838f9c68073'
+ 'io'
+ 'cstdio'
@@ -0,0 +1,5 @@
[main]:I32 'main' = {
arr:5:String = (* 'abc' 'def' 'ghi')
for e:String in arr { io::[println e] [break] }
0
}
+5 -4
View File
@@ -250,7 +250,7 @@ func (this *generator) generateFor (loop *entity.For, mode resultMode) (llvm.Val
irOverType, err := this.generateType(loop.Over.Type())
if err != nil { return nil, false, err }
baseOverType := analyzer.ReduceToBase(loop.Over.Type())
array, isArray := baseOverType.(*entity.TypeArray)
array, isArray := baseOverType.(*entity.TypeArray)
previous := this.blockManager.Block
header := this.blockManager.newBlock()
@@ -288,10 +288,11 @@ func (this *generator) generateFor (loop *entity.For, mode resultMode) (llvm.Val
llvm.Pointer,
this.getSliceDataFieldLoc(irOverLoc, irOverType))
}
irElementLocInData := this.blockManager.NewGetElementPtr (
irElementType, irDataLoc,
this.blockManager.NewLoad(irIndexType, irIndexLoc))
this.blockManager.NewStore (
this.blockManager.NewGetElementPtr (
irElementType, irDataLoc,
this.blockManager.NewLoad(irIndexType, irIndexLoc)),
this.blockManager.NewLoad(irElementType, irElementLocInData),
irElementLoc)
// generate loop body