mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 16:52:18 +01:00
Reject "x = y" if their types don't match.
This commit is contained in:
1 parent
a651946900
commit
ede8786f4b
2 files changed
+74
-5
No files matched your search
+64
-2
@@ -7,7 +7,7 @@ import (
|
|||||||
t "github.com/google/puffs/lang/token"
|
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 {
|
func (n *Expr) String(m *t.IDMap) string {
|
||||||
if n == nil {
|
if n == nil {
|
||||||
return ""
|
return ""
|
||||||
@@ -18,7 +18,50 @@ func (n *Expr) String(m *t.IDMap) string {
|
|||||||
return "Expr.String.TODO"
|
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 {
|
func (n *TypeExpr) String(m *t.IDMap) string {
|
||||||
if n != nil {
|
if n != nil {
|
||||||
s := ""
|
s := ""
|
||||||
@@ -39,3 +82,22 @@ func (n *TypeExpr) String(m *t.IDMap) string {
|
|||||||
}
|
}
|
||||||
return "!invalid_type!"
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
-3
@@ -56,14 +56,21 @@ func (c *typeChecker) checkStatement(n *a.Node) error {
|
|||||||
|
|
||||||
case a.KAssign:
|
case a.KAssign:
|
||||||
o := n.Assign()
|
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
|
return err
|
||||||
}
|
}
|
||||||
if err := c.checkExpr(o.RHS()); err != nil {
|
if err := c.checkExpr(r); err != nil {
|
||||||
return err
|
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().
|
// 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
|
return nil
|
||||||
|
|
||||||
case a.KVar:
|
case a.KVar:
|
||||||
|
|||||||
Reference in new issue
Block a user