mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Refactor the checkAssign code.
This commit is contained in:
1 parent
33af5598cc
commit
da9612667e
1 file changed
+45
-42
+45
-42
@@ -59,48 +59,7 @@ func (c *typeChecker) checkStatement(n *a.Node) error {
|
||||
return nil
|
||||
|
||||
case a.KAssign:
|
||||
o := n.Assign()
|
||||
lhs := o.LHS()
|
||||
rhs := o.RHS()
|
||||
if err := c.checkExpr(lhs); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.checkExpr(rhs); err != nil {
|
||||
return err
|
||||
}
|
||||
lTyp := lhs.MType()
|
||||
rTyp := rhs.MType()
|
||||
|
||||
if o.Operator() == t.IDEq {
|
||||
if (rTyp == TypeExprIdealNumber && lTyp.IsNumType()) || lTyp.EqIgnoringRefinements(rTyp) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
|
||||
rhs.String(c.idMap), rTyp.String(c.idMap), lhs.String(c.idMap), lTyp.String(c.idMap))
|
||||
}
|
||||
|
||||
if !lTyp.IsNumType() {
|
||||
return fmt.Errorf("check: assignment %q: assignee %q, of type %q, does not have numeric type",
|
||||
c.idMap.ByID(o.Operator()), lhs.String(c.idMap), lTyp.String(c.idMap))
|
||||
}
|
||||
|
||||
switch o.Operator() {
|
||||
case t.IDShiftLEq, t.IDShiftREq:
|
||||
if rTyp.IsNumType() {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type",
|
||||
c.idMap.ByID(o.Operator()), rhs.String(c.idMap), rTyp.String(c.idMap))
|
||||
}
|
||||
|
||||
if rTyp == TypeExprIdealNumber || lTyp.EqIgnoringRefinements(rTyp) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: assignment %q: %q and %q, of types %q and %q, do not have compatible types",
|
||||
c.idMap.ByID(o.Operator()),
|
||||
lhs.String(c.idMap), rhs.String(c.idMap),
|
||||
lTyp.String(c.idMap), rTyp.String(c.idMap),
|
||||
)
|
||||
return c.checkAssign(n.Assign())
|
||||
|
||||
case a.KVar:
|
||||
o := n.Var()
|
||||
@@ -172,6 +131,50 @@ func (c *typeChecker) checkStatement(n *a.Node) error {
|
||||
return fmt.Errorf("check: unrecognized ast.Kind (%d) for checkStatement", n.Kind())
|
||||
}
|
||||
|
||||
func (c *typeChecker) checkAssign(n *a.Assign) error {
|
||||
lhs := n.LHS()
|
||||
rhs := n.RHS()
|
||||
if err := c.checkExpr(lhs); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.checkExpr(rhs); err != nil {
|
||||
return err
|
||||
}
|
||||
lTyp := lhs.MType()
|
||||
rTyp := rhs.MType()
|
||||
|
||||
if n.Operator() == t.IDEq {
|
||||
if (rTyp == TypeExprIdealNumber && lTyp.IsNumType()) || lTyp.EqIgnoringRefinements(rTyp) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
|
||||
rhs.String(c.idMap), rTyp.String(c.idMap), lhs.String(c.idMap), lTyp.String(c.idMap))
|
||||
}
|
||||
|
||||
if !lTyp.IsNumType() {
|
||||
return fmt.Errorf("check: assignment %q: assignee %q, of type %q, does not have numeric type",
|
||||
c.idMap.ByID(n.Operator()), lhs.String(c.idMap), lTyp.String(c.idMap))
|
||||
}
|
||||
|
||||
switch n.Operator() {
|
||||
case t.IDShiftLEq, t.IDShiftREq:
|
||||
if rTyp.IsNumType() {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type",
|
||||
c.idMap.ByID(n.Operator()), rhs.String(c.idMap), rTyp.String(c.idMap))
|
||||
}
|
||||
|
||||
if rTyp == TypeExprIdealNumber || lTyp.EqIgnoringRefinements(rTyp) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: assignment %q: %q and %q, of types %q and %q, do not have compatible types",
|
||||
c.idMap.ByID(n.Operator()),
|
||||
lhs.String(c.idMap), rhs.String(c.idMap),
|
||||
lTyp.String(c.idMap), rTyp.String(c.idMap),
|
||||
)
|
||||
}
|
||||
|
||||
func (c *typeChecker) checkExpr(n *a.Expr) error {
|
||||
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
||||
case 0:
|
||||
|
||||
Reference in new issue
Block a user