From ede8786f4b2ff0e37996c98d0af89eb45703562c Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 12 May 2017 11:53:27 +1000 Subject: [PATCH] Reject "x = y" if their types don't match. --- lang/ast/string.go | 66 ++++++++++++++++++++++++++++++++++++++++++++-- lang/check/type.go | 13 ++++++--- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/lang/ast/string.go b/lang/ast/string.go index 2da9d283..f3f2943c 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -7,7 +7,7 @@ import ( t "github.com/google/puffs/lang/token" ) -// String returns a string form of the Expr n. +// String returns a string form of n. func (n *Expr) String(m *t.IDMap) string { if n == nil { return "" @@ -18,7 +18,50 @@ func (n *Expr) String(m *t.IDMap) string { return "Expr.String.TODO" } -// String returns a string form of the TypeExpr n. +// Eq returns whether n and o are equal. +// +// It may return false negatives. In general, it will not report that "x + y" +// equals "y + x". However, if both are constant expressions (i.e. each Expr +// node, including the sum nodes, has a ConstValue), both sums will have the +// same value and will compare equal. +func (n *Expr) Eq(o *Expr) bool { + if n == nil || o == nil { + return n == nil && o == nil + } + if n.constValue != nil && o.constValue != nil { + return n.constValue.Cmp(o.constValue) == 0 + } + + if n.flags != o.flags || n.id0 != o.id0 || n.id1 != o.id1 { + return false + } + if !n.lhs.Expr().Eq(o.lhs.Expr()) { + return false + } + if !n.mhs.Expr().Eq(o.mhs.Expr()) { + return false + } + + if n.id0 == t.IDXBinaryAs { + if !n.rhs.TypeExpr().Eq(o.rhs.TypeExpr()) { + return false + } + } else if !n.rhs.Expr().Eq(o.rhs.Expr()) { + return false + } + + if len(n.list0) != len(o.list0) { + return false + } + for i, x := range n.list0 { + if !x.Expr().Eq(o.list0[i].Expr()) { + return false + } + } + return true +} + +// String returns a string form of n. func (n *TypeExpr) String(m *t.IDMap) string { if n != nil { s := "" @@ -39,3 +82,22 @@ func (n *TypeExpr) String(m *t.IDMap) string { } return "!invalid_type!" } + +// Eq returns whether n and o are equal. +func (n *TypeExpr) Eq(o *TypeExpr) bool { + for { + if n == nil || o == nil { + return n == nil && o == nil + } + if n.id0 != o.id0 || n.id1 != o.id1 || + !n.lhs.Expr().Eq(o.lhs.Expr()) || + !n.mhs.Expr().Eq(o.mhs.Expr()) { + return false + } + if n.rhs == nil && o.rhs == nil { + return true + } + n = n.rhs.TypeExpr() + o = o.rhs.TypeExpr() + } +} diff --git a/lang/check/type.go b/lang/check/type.go index 1c5afce1..6ae7c850 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -56,14 +56,21 @@ func (c *typeChecker) checkStatement(n *a.Node) error { case a.KAssign: o := n.Assign() - if err := c.checkExpr(o.LHS()); err != nil { + l := o.LHS() + r := o.RHS() + if err := c.checkExpr(l); err != nil { return err } - if err := c.checkExpr(o.RHS()); err != nil { + if err := c.checkExpr(r); err != nil { return err } - // TODO: check that o.RHS().Type is assignable to o.LHS().Type. + lTyp := l.MType() + rTyp := r.MType() // TODO, look at o.Operator(). + if !lTyp.Eq(rTyp) { + return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q", + r.String(c.idMap), rTyp.String(c.idMap), l.String(c.idMap), lTyp.String(c.idMap)) + } return nil case a.KVar: