mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 08:47:28 +01:00
Add a "limit" keyword.
This commit is contained in:
1 parent
1d39b1e173
commit
7a3ddbc025
14 files changed
+204
-29
No files matched your search
+18
-1
@@ -339,7 +339,10 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rMin.Cmp(lMin) < 0 || rMax.Cmp(lMax) > 0 {
|
||||
|
||||
if (rMin != nil && lMin != nil && rMin.Cmp(lMin) < 0) ||
|
||||
(rMax != nil && lMax != nil && rMax.Cmp(lMax) > 0) {
|
||||
|
||||
if op == t.IDEq {
|
||||
return fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]",
|
||||
rhs.String(q.tm), rMin, rMax, lMin, lMax)
|
||||
@@ -663,6 +666,17 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
}
|
||||
return nil, nil, nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.decode(etc)".
|
||||
if isDecode(q.tm, n) {
|
||||
for _, aNode := range n.Args() {
|
||||
a := aNode.Arg().Value()
|
||||
_, _, err := q.bcheckExpr(a, depth)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
return nil, nil, nil
|
||||
}
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
|
||||
|
||||
case t.KeyOpenBracket:
|
||||
@@ -708,6 +722,9 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
case t.KeyLimit:
|
||||
return nil, nil, nil
|
||||
|
||||
default:
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ func trimQuotes(s string) string {
|
||||
var (
|
||||
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
|
||||
typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil)
|
||||
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
|
||||
|
||||
// TODO: delete this.
|
||||
typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
|
||||
|
||||
+55
-2
@@ -384,6 +384,22 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.decode(etc)".
|
||||
if isDecode(q.tm, n) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
n.LHS().SetTypeChecked()
|
||||
n.LHS().Expr().SetMType(typeExprPlaceholder) // HACK.
|
||||
for _, o := range n.Args() {
|
||||
if err := q.tcheckArg(o.Arg(), depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
return nil
|
||||
}
|
||||
|
||||
case t.KeyOpenBracket:
|
||||
// n is an index.
|
||||
@@ -397,12 +413,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
lTyp := lhs.MType()
|
||||
if lTyp.Decorator().Key() != t.KeyOpenBracket {
|
||||
return fmt.Errorf("%s is an array-index expression but %s has type %s, not an array type",
|
||||
return fmt.Errorf("check: %s is an array-index expression but %s has type %s, not an array type",
|
||||
n.String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
|
||||
}
|
||||
rTyp := rhs.MType()
|
||||
if !rTyp.IsNumTypeOrIdeal() {
|
||||
return fmt.Errorf("%s is an array-index expression but %s has type %s, not a numeric type",
|
||||
return fmt.Errorf("check: %s is an array-index expression but %s has type %s, not a numeric type",
|
||||
n.String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
|
||||
}
|
||||
n.SetMType(lTyp.Inner())
|
||||
@@ -414,6 +430,34 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
|
||||
case t.KeyDot:
|
||||
return q.tcheckDot(n, depth)
|
||||
|
||||
case t.KeyLimit:
|
||||
lhs := n.LHS().Expr()
|
||||
if err := q.tcheckExpr(lhs, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
if lhs.Impure() {
|
||||
return fmt.Errorf(`check: limit count %q is not pure`, lhs.String(q.tm))
|
||||
}
|
||||
lTyp := lhs.MType()
|
||||
if !lTyp.IsIdeal() && !lTyp.EqIgnoringRefinements(typeExprU64) {
|
||||
return fmt.Errorf("check: limit count %q type %q not compatible with u64",
|
||||
lhs.String(q.tm), lTyp.String(q.tm))
|
||||
}
|
||||
rhs := n.RHS().Expr()
|
||||
if err := q.tcheckExpr(rhs, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
if rhs.Impure() {
|
||||
return fmt.Errorf("check: limit arg %q is not pure", rhs.String(q.tm))
|
||||
}
|
||||
rTyp := rhs.MType()
|
||||
if rTyp.Decorator() != 0 || (rTyp.Name().Key() != t.KeyReader1 && rTyp.Name().Key() != t.KeyWriter1) {
|
||||
return fmt.Errorf(`check: limit arg %q type %q not "reader1" or "writer1"`,
|
||||
rhs.String(q.tm), rTyp.String(q.tm))
|
||||
}
|
||||
n.SetMType(rTyp)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprOther", n.ID0().Key())
|
||||
}
|
||||
@@ -482,6 +526,15 @@ func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool {
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("set_literal_width")
|
||||
}
|
||||
|
||||
func isDecode(tm *t.Map, n *a.Expr) bool {
|
||||
// TODO: check that n.Args() is "(dst:bar, src:baz)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 2 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("decode")
|
||||
}
|
||||
|
||||
func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
|
||||
lhs := n.LHS().Expr()
|
||||
if err := q.tcheckExpr(lhs, depth); err != nil {
|
||||
|
||||
Reference in new issue
Block a user