mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 16: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
@@ -199,6 +199,8 @@ const MaxExprDepth = 255
|
||||
// For slices, like "LHS[MHS:RHS]", ID0 is IDColon.
|
||||
//
|
||||
// For selectors, like "LHS.ID1", ID0 is IDDot.
|
||||
//
|
||||
// For limits, like "limit (LHS) RHS", ID0 is IDLimit.
|
||||
type Expr Node
|
||||
|
||||
func (n *Expr) Node() *Node { return (*Node)(n) }
|
||||
|
||||
+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 {
|
||||
|
||||
+37
-3
@@ -565,9 +565,16 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
||||
value := (*a.Expr)(nil)
|
||||
if p.peek1().Key() == t.KeyEq {
|
||||
p.src = p.src[1:]
|
||||
value, err = p.parseExpr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if p.peek1().Key() == t.KeyLimit {
|
||||
value, err = p.parseLimitExpr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
value, err = p.parseExpr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return a.NewVar(id, typ, value).Node(), nil
|
||||
@@ -639,6 +646,33 @@ func (p *parser) parseArgNode() (*a.Node, error) {
|
||||
return a.NewArg(name, value).Node(), nil
|
||||
}
|
||||
|
||||
func (p *parser) parseLimitExpr() (*a.Expr, error) {
|
||||
if x := p.peek1().Key(); x != t.KeyLimit {
|
||||
got := p.tm.ByKey(x)
|
||||
return nil, fmt.Errorf(`parse: expected "limit", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
if x := p.peek1().Key(); x != t.KeyOpenParen {
|
||||
got := p.tm.ByKey(x)
|
||||
return nil, fmt.Errorf(`parse: expected "(", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
lhs, err := p.parseExpr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if x := p.peek1().Key(); x != t.KeyCloseParen {
|
||||
got := p.tm.ByKey(x)
|
||||
return nil, fmt.Errorf(`parse: expected ")", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
rhs, err := p.parseExpr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a.NewExpr(0, t.IDLimit, 0, lhs.Node(), nil, rhs.Node(), nil), nil
|
||||
}
|
||||
|
||||
func (p *parser) parseExpr() (*a.Expr, error) {
|
||||
lhs, err := p.parseOperand()
|
||||
if err != nil {
|
||||
|
||||
@@ -209,6 +209,7 @@ const (
|
||||
KeyPri = Key(IDPri >> KeyShift)
|
||||
KeyError = Key(IDError >> KeyShift)
|
||||
KeyStatus = Key(IDStatus >> KeyShift)
|
||||
KeyLimit = Key(IDLimit >> KeyShift)
|
||||
|
||||
KeyFalse = Key(IDFalse >> KeyShift)
|
||||
KeyTrue = Key(IDTrue >> KeyShift)
|
||||
@@ -366,6 +367,7 @@ const (
|
||||
IDPri = ID(0x72<<KeyShift | FlagsOther)
|
||||
IDError = ID(0x73<<KeyShift | FlagsOther)
|
||||
IDStatus = ID(0x74<<KeyShift | FlagsOther)
|
||||
IDLimit = ID(0x75<<KeyShift | FlagsOther)
|
||||
|
||||
IDFalse = ID(0x80<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||
IDTrue = ID(0x81<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||
@@ -543,6 +545,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
||||
KeyPri: {"pri", IDPri},
|
||||
KeyError: {"error", IDError},
|
||||
KeyStatus: {"status", IDStatus},
|
||||
KeyLimit: {"limit", IDLimit},
|
||||
|
||||
KeyFalse: {"false", IDFalse},
|
||||
KeyTrue: {"true", IDTrue},
|
||||
|
||||
Reference in new issue
Block a user