Pass all literal assignment tests

This commit is contained in:
Sasha Koshka 2023-10-29 15:18:44 -04:00
parent b839517e9a
commit 1a09e9ba2a
2 changed files with 12 additions and 14 deletions

View File

@ -113,7 +113,7 @@ testStringErr (test,
func TestAssignmentLiteralErrInterfaceInt (test *testing.T) {
testStringErr (test,
"cannot use float literal as Bird", 6, 2,
"cannot use integer literal as Bird", 4, 11,
`
Bird: ([fly distance:F64] [land])
[main] = {
@ -124,7 +124,7 @@ Bird: ([fly distance:F64] [land])
func TestAssignmentLiteralErrInterfaceFloat (test *testing.T) {
testStringErr (test,
"cannot use integer literal as Bird", 6, 2,
"cannot use float literal as Bird", 4, 11,
`
Bird: ([fly distance:F64] [land])
[main] = {
@ -135,7 +135,7 @@ Bird: ([fly distance:F64] [land])
func TestAssignmentLiteralErrInterfaceArray (test *testing.T) {
testStringErr (test,
"cannot use array literal as Bird", 6, 2,
"cannot use array literal as Bird", 4, 11,
`
Bird: ([fly distance:F64] [land])
[main] = {
@ -146,11 +146,9 @@ Bird: ([fly distance:F64] [land])
func TestAssignmentLiteralErrInterfaceStruct (test *testing.T) {
testStringErr (test,
"cannot assign literal to interface", 6, 2,
"cannot use struct literal as Bird", 4, 11,
`
Bird: ([fly distance:F64] [land])
BlueJay: Int
BlueJay::[land] = { }
[main] = {
b:Bird = (x: 5 y: 6)
}

View File

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