diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 5b051f35..9cd74752 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -110,6 +110,27 @@ func (n *Node) TypeExpr() *TypeExpr { return (*TypeExpr)(n) } func (n *Node) Use() *Use { return (*Use)(n) } func (n *Node) Var() *Var { return (*Var)(n) } +func (n *Node) Walk(f func(*Node) error) error { + if n != nil { + if err := f(n); err != nil { + return err + } + for _, m := range n.Raw().SubNodes() { + if err := m.Walk(f); err != nil { + return err + } + } + for _, l := range n.Raw().SubLists() { + for _, m := range l { + if err := m.Walk(f); err != nil { + return err + } + } + } + } + return nil +} + type Raw Node func (n *Raw) Node() *Node { return (*Node)(n) } diff --git a/lang/check/check.go b/lang/check/check.go index fb50fe90..72f48c44 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -137,6 +137,21 @@ func (c *Checker) checkFuncBody(n *a.Node) error { return fmt.Errorf("%v at %s:%d", err, m.Raw().Filename(), m.Raw().Line()) } } + if err := f.Node().Walk(func(n *a.Node) error { + if n.Kind() == a.KExpr { + e := n.Expr() + if typ := e.MType(); typ == nil { + return fmt.Errorf("check: internal error: expression node %q has no (implicit) type", + e.String(tc.idMap)) + } else if typ == TypeExprIdealNumber && e.ConstValue() == nil { + return fmt.Errorf("check: internal error: expression node %q has ideal number type "+ + "but no const value", e.String(tc.idMap)) + } + } + return nil + }); err != nil { + return err + } // TODO: bounds and assertion checking. diff --git a/lang/check/check_test.go b/lang/check/check_test.go index c3e07db8..cb9abaca 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -118,41 +118,6 @@ func TestCheck(t *testing.T) { if !reflect.DeepEqual(got, want) { t.Fatalf("\ngot %v\nwant %v", got, want) } - - walk(file.Node(), func(n *ast.Node) error { - if n.Kind() == ast.KExpr { - e := n.Expr() - if typ := e.MType(); typ == nil { - t.Errorf("expression node %q has no (implicit) type", n.Expr().String(idMap)) - } else if typ == TypeExprIdealNumber && e.ConstValue() == nil { - t.Errorf("expression node %q has ideal number type but no const value", n.Expr().String(idMap)) - } - } - return nil - }) -} - -func walk(n *ast.Node, f func(*ast.Node) error) error { - if err := f(n); err != nil { - return err - } - for _, m := range n.Raw().SubNodes() { - if m != nil { - if err := walk(m, f); err != nil { - return err - } - } - } - for _, l := range n.Raw().SubLists() { - for _, m := range l { - if m != nil { - if err := walk(m, f); err != nil { - return err - } - } - } - } - return nil } func TestConstValues(t *testing.T) {