mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Fix associative op const-value calculation
This commit is contained in:
1 parent
8d29cb15ce
commit
fb9a0964a2
4 files changed
+84
-9
No files matched your search
@@ -340,7 +340,7 @@ var errFailed = errors.New("failed")
|
||||
func proveReasonRequirement(q *checker, op t.ID, lhs *a.Expr, rhs *a.Expr) error {
|
||||
if !op.IsXBinaryOp() {
|
||||
return fmt.Errorf(
|
||||
"check: internal error: proveReasonRequirement token (0x%02X) is not an XBinaryOp", op)
|
||||
"check: internal error: proveReasonRequirement token (0x%X) is not an XBinaryOp", op)
|
||||
}
|
||||
if err := q.proveBinaryOp(op, lhs, rhs); err != nil {
|
||||
n := a.NewExpr(0, op, 0, 0, lhs.AsNode(), nil, rhs.AsNode(), nil)
|
||||
|
||||
@@ -495,7 +495,7 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
|
||||
|
||||
func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) (bounds, error) {
|
||||
if lhs == nil && op != t.IDEq {
|
||||
return bounds{}, fmt.Errorf("check: internal error: missing LHS for op key 0x%02X", op)
|
||||
return bounds{}, fmt.Errorf("check: internal error: missing LHS for op key 0x%X", op)
|
||||
}
|
||||
|
||||
lb, err := bounds{}, (error)(nil)
|
||||
@@ -795,6 +795,9 @@ func bcheckExprConstValue(n *a.Expr) bounds {
|
||||
if o := n.RHS(); o != nil && n.Operator() != t.IDXBinaryAs {
|
||||
bcheckExprConstValue(o.AsExpr())
|
||||
}
|
||||
for _, o := range n.Args() {
|
||||
bcheckExprConstValue(o.AsExpr())
|
||||
}
|
||||
cv := n.ConstValue()
|
||||
b := bounds{cv, cv}
|
||||
n.SetMBounds(b)
|
||||
|
||||
+7
-3
@@ -749,10 +749,14 @@ func nodeDebugString(tm *t.Map, n *a.Node) string {
|
||||
|
||||
func allTypeChecked(tm *t.Map, n *a.Node) error {
|
||||
return n.Walk(func(o *a.Node) error {
|
||||
b := o.MBounds()
|
||||
if b := o.MBounds(); (b[0] == nil) || (b[1] == nil) {
|
||||
return fmt.Errorf("check: internal error: unchecked %s (missing bounds)",
|
||||
nodeDebugString(tm, o))
|
||||
}
|
||||
typ := o.MType()
|
||||
if b[0] == nil || b[1] == nil || typ == nil {
|
||||
return fmt.Errorf("check: internal error: unchecked %s", nodeDebugString(tm, o))
|
||||
if typ == nil {
|
||||
return fmt.Errorf("check: internal error: unchecked %s (missing type)",
|
||||
nodeDebugString(tm, o))
|
||||
}
|
||||
|
||||
typOK := false
|
||||
|
||||
+72
-4
@@ -858,7 +858,7 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big.
|
||||
|
||||
return nil, fmt.Errorf("check: cannot apply tilde-operators to ideal numbers")
|
||||
}
|
||||
return nil, fmt.Errorf("check: unrecognized token (0x%02X) for evalConstValueBinaryOp", n.Operator())
|
||||
return nil, fmt.Errorf("check: unrecognized token (0x%X) for evalConstValueBinaryOp", n.Operator())
|
||||
}
|
||||
|
||||
func (q *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error {
|
||||
@@ -896,7 +896,6 @@ func (q *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error {
|
||||
typ = typeExprIdeal
|
||||
}
|
||||
n.SetMType(typ)
|
||||
return nil
|
||||
|
||||
case t.IDXAssociativeAnd, t.IDXAssociativeOr:
|
||||
for _, o := range n.Args() {
|
||||
@@ -910,10 +909,79 @@ func (q *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
}
|
||||
n.SetMType(typeExprBool)
|
||||
return nil
|
||||
|
||||
default:
|
||||
return fmt.Errorf("check: unrecognized token (0x%X) for tcheckExprAssociativeOp", n.Operator())
|
||||
}
|
||||
|
||||
return fmt.Errorf("check: unrecognized token (0x%X) for tcheckExprAssociativeOp", n.Operator())
|
||||
ncv, err := evalConstValueAssociativeOp(q.tm, n)
|
||||
n.SetConstValue(ncv)
|
||||
return err
|
||||
}
|
||||
|
||||
func evalConstValueAssociativeOp(tm *t.Map, n *a.Expr) (*big.Int, error) {
|
||||
args := n.Args()
|
||||
if len(args) == 0 {
|
||||
return nil, fmt.Errorf("check: no operands for associative operator")
|
||||
}
|
||||
cv0 := args[0].AsExpr().ConstValue()
|
||||
if cv0 == nil {
|
||||
return nil, nil
|
||||
}
|
||||
args = args[1:]
|
||||
ncv := big.NewInt(0).Set(cv0)
|
||||
|
||||
switch n.Operator() {
|
||||
case t.IDXAssociativePlus:
|
||||
for _, o := range args {
|
||||
if cv := o.AsExpr().ConstValue(); cv == nil {
|
||||
return nil, nil
|
||||
} else {
|
||||
ncv.Add(ncv, cv)
|
||||
}
|
||||
}
|
||||
|
||||
case t.IDXAssociativeStar:
|
||||
for _, o := range args {
|
||||
if cv := o.AsExpr().ConstValue(); cv == nil {
|
||||
return nil, nil
|
||||
} else {
|
||||
ncv.Mul(ncv, cv)
|
||||
}
|
||||
}
|
||||
|
||||
case t.IDXAssociativeAmp, t.IDXAssociativeAnd:
|
||||
for _, o := range args {
|
||||
if cv := o.AsExpr().ConstValue(); cv == nil {
|
||||
return nil, nil
|
||||
} else {
|
||||
ncv.And(ncv, cv)
|
||||
}
|
||||
}
|
||||
|
||||
case t.IDXAssociativePipe, t.IDXAssociativeOr:
|
||||
for _, o := range args {
|
||||
if cv := o.AsExpr().ConstValue(); cv == nil {
|
||||
return nil, nil
|
||||
} else {
|
||||
ncv.Or(ncv, cv)
|
||||
}
|
||||
}
|
||||
|
||||
case t.IDXAssociativeHat:
|
||||
for _, o := range args {
|
||||
if cv := o.AsExpr().ConstValue(); cv == nil {
|
||||
return nil, nil
|
||||
} else {
|
||||
ncv.Xor(ncv, cv)
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("check: unrecognized token (0x%X) for evalConstValueAssociativeOp", n.Operator())
|
||||
}
|
||||
|
||||
return ncv, nil
|
||||
}
|
||||
|
||||
func (q *checker) tcheckTypeExpr(typ *a.TypeExpr, depth uint32) error {
|
||||
|
||||
Reference in new issue
Block a user