Array literal can be used in array and slice

This commit is contained in:
Sasha Koshka 2023-10-29 15:13:25 -04:00
parent 45574d3cd5
commit b839517e9a

View File

@ -54,21 +54,29 @@ func (this *Tree) analyzeLiteralArray (
entity.Expression, entity.Expression,
error, error,
) { ) {
base, ok := this.reduceToBase(into).(*entity.TypeArray) into = this.reduceToBase(into)
if !ok { var elementType entity.Type
switch into.(type) {
case *entity.TypeArray:
into := into.(*entity.TypeArray)
if into.Length != len(literal.Elements) {
return nil, participle.Errorf (
literal.Pos, "expected %v elements",
into.Length)
}
elementType = into.Element
case *entity.TypeSlice:
into := into.(*entity.TypeSlice)
elementType = into.Element
default:
return nil, participle.Errorf ( return nil, participle.Errorf (
literal.Pos, "cannot use array literal as %v", literal.Pos, "cannot use array literal as %v",
into) into)
} }
if base.Length != len(literal.Elements) {
return nil, participle.Errorf (
literal.Pos, "expected %v elements",
base.Length)
}
for index, element := range literal.Elements { for index, element := range literal.Elements {
element, err := this.analyzeExpression(base.Element, strict, element) element, err := this.analyzeExpression(elementType, strict, element)
if err != nil { return nil, err } if err != nil { return nil, err }
literal.Elements[index] = element literal.Elements[index] = element
} }