Make {Type,}ExprString methods, not functions.

This commit is contained in:
Nigel Tao committed 2017-05-12 10:59:10 +10:00
1 parent e10ddeb248
commit d8d17b66c0
3 files changed
+15 -20

No files matched your search

+9 -10
View File
@@ -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!"
}
+5 -9
View File
@@ -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{
+1 -1
View File
@@ -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 {