Skip to content

Commit

Permalink
fix slice array node
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Aug 28, 2024
1 parent 0d68a92 commit 9f9204e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion compiler/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,14 @@ func (c *Compiler) compileValue(node parser.Node) value.Value {

if ty, ok := src.Type.(*types.Slice); ok {
src.Type = ty.Type
return c.compileSliceArray(src, v, true)
}
// array type as pointer receriver
if ptr, ok := src.Type.(*types.Pointer); ok {
src.Type = ptr.Type
src.Value = c.contextBlock.NewLoad(pointer.ElemType(src.Value), src.Value)
}
return c.compileSliceArray(src, v)
return c.compileSliceArray(src, v, false)
case *parser.InitializeStructNode:
return c.compileInitStructWithValues(v)
case *parser.TypeCastInterfaceNode:
Expand Down
9 changes: 7 additions & 2 deletions compiler/compiler/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c *Compiler) compileSubstring(src value.Value, v *parser.SliceArrayNode) v
}
}

func (c *Compiler) compileSliceArray(src value.Value, v *parser.SliceArrayNode) value.Value {
func (c *Compiler) compileSliceArray(src value.Value, v *parser.SliceArrayNode, isSliceSlice bool) value.Value {
sliceType := internal.Slice(src.Type.LLVM())

alloc := c.contextBlock.NewAlloca(sliceType)
Expand All @@ -109,7 +109,12 @@ func (c *Compiler) compileSliceArray(src value.Value, v *parser.SliceArrayNode)
endIndexValue = c.compileValue(v.End)
} else {
srcVal := internal.LoadIfVariable(c.contextBlock, src)
endIndexValue.Value = c.contextBlock.NewExtractValue(srcVal, 1)
if isSliceSlice {
endIndexValue.Value = c.contextBlock.NewExtractValue(srcVal, 1)
} else {
arrTy := src.Type.LLVM().(*llvmTypes.ArrayType)
endIndexValue.Value = constant.NewInt(llvmTypes.I32, int64(arrTy.Len))
}
}

endIndex := internal.LoadIfVariable(c.contextBlock, endIndexValue)
Expand Down

0 comments on commit 9f9204e

Please sign in to comment.