Fix goofy bugs

This commit is contained in:
Sasha Koshka 2023-10-31 21:52:47 -04:00
parent af52bbb070
commit a2e22d1154
7 changed files with 31 additions and 13 deletions

View File

@ -133,6 +133,8 @@ func (this *Tree) canAssignInterface (
method))
}
fmt.Println(signature, behavior)
// check equivalence
if !signature.Equals(behavior) {
return participle.Errorf (

View File

@ -181,14 +181,27 @@ testString (test,
func TestAssignmentInterfaceErrBadSignature (test *testing.T) {
testStringErr (test,
"BlueJay has wrong signature for method fly", 7, 2,
"BlueJay has wrong signature for method fly", 7, 11,
`
Bird: ([fly distance:F64] [land])
BlueJay: Int
BlueJay.[fly] = { }
BlueJay.[land] = { }
[main] = {
b:Bird = [@a:BlueJay]
b:Bird = a:BlueJay
}
`)
}
func TestAssignmentInterfaceErrMissingMethod (test *testing.T) {
testStringErr (test,
"no method named fly defined on this type", 6, 11,
`
Bird: ([fly distance:F64] [land])
BlueJay: Int
BlueJay.[land] = { }
[main] = {
b:Bird = a:BlueJay
}
`)
}
@ -201,8 +214,7 @@ BlueJay: Int
BlueJay.[fly distance:F64] = { }
BlueJay.[land] = { }
[main] = {
a:BlueJay
b:Bird = [@a]
b:Bird = a:BlueJay
}
`)
}

View File

@ -11,7 +11,6 @@ func (this *Tree) analyzeExpression (
entity.Expression,
error,
) {
fmt.Println(into)
switch expression.(type) {
case *entity.Variable:
return this.analyzeVariable(into, mode, expression.(*entity.Variable))

View File

@ -246,8 +246,6 @@ func (this *Tree) analyzeReference (
entity.Expression,
error,
) {
// TODO this does not work when assigning to an interface
referenced, ok := into.(*entity.TypePointer)
if !ok {
return nil, participle.Errorf(reference.Pos, "expected %v", into)

View File

@ -20,7 +20,6 @@ func (this *Tree) analyzeAssignment (
if err != nil { return nil, err }
// analyze value
println("analyzing value")
value, err := this.analyzeExpression(location.Type(), strict, assignment.Value)
if err != nil { return nil, err }
assignment.Value = value

View File

@ -34,7 +34,7 @@ func (this *Signature) Equals (ty Type) bool {
real, ok := ty.(*Signature)
if !ok ||
len(real.Arguments) != len(this.Arguments) ||
!real.Return.Equals(this.Return) ||
!TypesEqual(real.Return, this.Return) ||
real.Name != this.Name { return false }
for index, argument := range this.Arguments {

View File

@ -35,7 +35,7 @@ func (this *TypePointer) String () string {
}
func (this *TypePointer) Equals (ty Type) bool {
real, ok := ty.(*TypePointer)
return ok && real.Referenced.Equals(this.Referenced)
return ok && TypesEqual(real.Referenced, this.Referenced)
}
// TypeSlice is a pointer to several values of a given type stored next to
@ -51,7 +51,7 @@ func (this *TypeSlice) String () string {
}
func (this *TypeSlice) Equals (ty Type) bool {
real, ok := ty.(*TypeSlice)
return ok && real.Element.Equals(this.Element)
return ok && TypesEqual(real.Element, this.Element)
}
// TypeArray is a group of values of a given type stored next to eachother. The
@ -70,7 +70,7 @@ func (this *TypeArray) Equals (ty Type) bool {
real, ok := ty.(*TypeArray)
return ok &&
real.Length == this.Length &&
real.Element.Equals(this.Element)
TypesEqual(real.Element, this.Element)
}
// TypeStruct is a composite type that stores keyed values. The positions of the
@ -99,7 +99,7 @@ func (this *TypeStruct) Equals (ty Type) bool {
if !ok || len(real.Members) != len(this.Members) { return false }
for index, member := range this.Members {
if !member.Type().Equals(real.Members[index].Type()) {
if !TypesEqual(member.Type(), real.Members[index].Type()) {
return false
}
}
@ -200,3 +200,11 @@ func (this *TypeBool) Equals (ty Type) bool {
_, ok := ty.(*TypeBool)
return ok
}
// TypesEqual checks if two types are equal to eachother, even if one or both
// are nil.
func TypesEqual (left, right Type) bool {
if (left == nil) != (right == nil) { return false }
if left == nil { return true }
return left.Equals(right)
}