diff --git a/lang/check/string.go b/lang/ast/string.go similarity index 51% rename from lang/check/string.go rename to lang/ast/string.go index 38ae3d50..2da9d283 100644 --- a/lang/check/string.go +++ b/lang/ast/string.go @@ -1,42 +1,41 @@ // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. -package check +package ast import ( - a "github.com/google/puffs/lang/ast" t "github.com/google/puffs/lang/token" ) -// ExprString returns a string form of the Expr n. -func ExprString(m *t.IDMap, n *a.Expr) string { +// String returns a string form of the Expr n. +func (n *Expr) String(m *t.IDMap) string { if n == nil { return "" } if n.ID0() == 0 { return m.ByID(n.ID1()) } - return "ExprString.TODO" + return "Expr.String.TODO" } -// TypeExprString returns a string form of the TypeExpr n. -func TypeExprString(m *t.IDMap, n *a.TypeExpr) string { +// String returns a string form of the TypeExpr n. +func (n *TypeExpr) String(m *t.IDMap) string { if n != nil { s := "" switch n.PackageOrDecorator() { case 0: s = m.ByID(n.Name()) case t.IDPtr: - return "ptr " + TypeExprString(m, n.Inner()) + return "ptr " + n.Inner().String(m) case t.IDOpenBracket: - return "[" + ExprString(m, n.ArrayLength()) + "] " + TypeExprString(m, n.Inner()) + return "[" + n.ArrayLength().String(m) + "] " + n.Inner().String(m) default: s = m.ByID(n.PackageOrDecorator()) + "." + m.ByID(n.Name()) } if n.InclMin() == nil && n.ExclMax() == nil { return s } - return s + "[" + ExprString(m, n.InclMin()) + ":" + ExprString(m, n.ExclMax()) + "]" + return s + "[" + n.InclMin().String(m) + ":" + n.ExclMax().String(m) + "]" } return "!invalid_type!" } diff --git a/lang/check/check.go b/lang/check/check.go index df3979d3..becaba93 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -355,7 +355,7 @@ func (c *typeChecker) checkTypeExpr(n *a.TypeExpr) error { return err } if bound.ConstValue() == nil { - return fmt.Errorf("check: %q is not constant", ExprString(c.idMap, bound)) + return fmt.Errorf("check: %q is not constant", bound.String(c.idMap)) } } return nil @@ -375,7 +375,7 @@ func (c *typeChecker) checkTypeExpr(n *a.TypeExpr) error { return err } if aLen.ConstValue() == nil { - return fmt.Errorf("check: %q is not constant", ExprString(c.idMap, aLen)) + return fmt.Errorf("check: %q is not constant", aLen.String(c.idMap)) } default: @@ -388,7 +388,7 @@ func (c *typeChecker) checkTypeExpr(n *a.TypeExpr) error { func (c *typeChecker) typeConvertible(e *a.Expr, typ *a.TypeExpr) error { eTyp := e.MType() if eTyp == nil { - return fmt.Errorf("check: expression %q has no inferred type", ExprString(c.idMap, e)) + return fmt.Errorf("check: expression %q has no inferred type", e.String(c.idMap)) } if typ.PackageOrDecorator() == 0 { @@ -396,8 +396,7 @@ func (c *typeChecker) typeConvertible(e *a.Expr, typ *a.TypeExpr) error { if eTyp == DummyTypeIdealNumber { minMax := numTypeRanges[0xFF&(name>>t.KeyShift)] if minMax[0] == nil { - return fmt.Errorf("check: unknown range for built-in numeric type %q", - TypeExprString(c.idMap, typ)) + return fmt.Errorf("check: unknown range for built-in numeric type %q", typ.String(c.idMap)) } // TODO: update minMax for typ.Bounds(). // @@ -412,10 +411,7 @@ func (c *typeChecker) typeConvertible(e *a.Expr, typ *a.TypeExpr) error { } return fmt.Errorf("check: cannot convert expression %q of type %q to type %q", - ExprString(c.idMap, e), - TypeExprString(c.idMap, eTyp), - TypeExprString(c.idMap, typ), - ) + e.String(c.idMap), eTyp.String(c.idMap), typ.String(c.idMap)) } var numTypeRanges = [256][2]*big.Int{ diff --git a/lang/check/check_test.go b/lang/check/check_test.go index d29e3eb7..6ff24302 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -60,7 +60,7 @@ func TestCheck(t *testing.T) { for id, typ := range bar.LocalVars { got = append(got, [2]string{ idMap.ByID(id), - TypeExprString(idMap, typ), + typ.String(idMap), }) } sort.Slice(got, func(i, j int) bool {