Add an "iterate" keyword.

This commit is contained in:
Nigel Tao committed 2017-10-20 15:33:31 +11:00
1 parent 7b18295688
commit 51bd52239b
12 files changed
+253 -53

No files matched your search

+32 -12
View File
@@ -265,22 +265,24 @@ func (q *checker) bcheckStatement(n *a.Node) error {
// TODO.
case a.KVar:
n := n.Var()
if innTyp := n.XType().Innermost(); innTyp.IsRefined() {
if _, _, err := typeBounds(q.tm, innTyp); err != nil {
return q.bcheckVar(n.Var())
case a.KIterate:
n := n.Iterate()
for _, o := range n.Variables() {
if err := q.bcheckVar(o.Var()); err != nil {
return err
}
}
lhs := a.NewExpr(a.FlagsTypeChecked, 0, n.Name(), nil, nil, nil, nil)
lhs.SetMType(n.XType())
rhs := n.Value()
if rhs == nil {
// "var x T" has an implicit "= 0".
//
// TODO: check that T is an integer type.
rhs = zeroExpr
// TODO: this isn't right, as the body is a loop, not an
// execute-exactly-once block. We should have pre / inv / post
// conditions, a la bcheckWhile.
for _, o := range n.Body() {
if err := q.bcheckStatement(o); err != nil {
return err
}
}
return q.bcheckAssignment(lhs, t.IDEq, rhs)
return nil
case a.KWhile:
return q.bcheckWhile(n.While())
@@ -636,6 +638,24 @@ func (q *checker) bcheckWhile(n *a.While) error {
return nil
}
func (q *checker) bcheckVar(n *a.Var) error {
if innTyp := n.XType().Innermost(); innTyp.IsRefined() {
if _, _, err := typeBounds(q.tm, innTyp); err != nil {
return err
}
}
lhs := a.NewExpr(a.FlagsTypeChecked, 0, n.Name(), nil, nil, nil, nil)
lhs.SetMType(n.XType())
rhs := n.Value()
if rhs == nil {
// "var x T" has an implicit "= 0".
//
// TODO: check that T is an integer type.
rhs = zeroExpr
}
return q.bcheckAssignment(lhs, t.IDEq, rhs)
}
func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
if depth > a.MaxExprDepth {
return nil, nil, fmt.Errorf("check: expression recursion depth too large")
+38 -1
View File
@@ -39,6 +39,14 @@ func (q *checker) tcheckVars(block []*a.Node) error {
}
}
case a.KIterate:
if err := q.tcheckVars(o.Iterate().Variables()); err != nil {
return err
}
if err := q.tcheckVars(o.Iterate().Body()); err != nil {
return err
}
case a.KVar:
o := o.Var()
name := o.Name()
@@ -167,8 +175,24 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
lTyp := n.XType()
rTyp := value.MType()
if (rTyp.IsIdeal() && lTyp.IsNumType()) || lTyp.EqIgnoringRefinements(rTyp) {
if n.IterateVariable() {
if lTyp.Decorator().Key() != t.KeyPtr {
return fmt.Errorf("check: iterate variable %q, of type %q, does not have a pointer type",
n.Name().String(q.tm), lTyp.String(q.tm))
}
if rTyp.Decorator().Key() != t.KeyColon {
return fmt.Errorf("check: iterate range %q, of type %q, does not have a slice type",
value.String(q.tm), rTyp.String(q.tm))
}
if !lTyp.Inner().Eq(rTyp.Inner()) {
return fmt.Errorf("check: cannot iterate %q of type %q over %q of type %q "+
"as their inner types don't match",
n.Name().String(q.tm), lTyp.String(q.tm), value.String(q.tm), rTyp.String(q.tm))
}
} else if (rTyp.IsIdeal() && lTyp.IsNumType()) || lTyp.EqIgnoringRefinements(rTyp) {
// No-op.
} else {
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
value.String(q.tm), rTyp.String(q.tm), n.Name().String(q.tm), lTyp.String(q.tm))
@@ -177,6 +201,19 @@ func (q *checker) tcheckStatement(n *a.Node) error {
// TODO: check that the default zero value is assignable to n.XType().
}
case a.KIterate:
n := n.Iterate()
for _, o := range n.Variables() {
if err := q.tcheckStatement(o); err != nil {
return err
}
}
for _, o := range n.Body() {
if err := q.tcheckStatement(o); err != nil {
return err
}
}
case a.KWhile:
n := n.While()
cond := n.Condition()