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 // Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file. // in the LICENSE file.
package check package ast
import ( import (
a "github.com/google/puffs/lang/ast"
t "github.com/google/puffs/lang/token" t "github.com/google/puffs/lang/token"
) )
// ExprString returns a string form of the Expr n. // String returns a string form of the Expr n.
func ExprString(m *t.IDMap, n *a.Expr) string { func (n *Expr) String(m *t.IDMap) string {
if n == nil { if n == nil {
return "" return ""
} }
if n.ID0() == 0 { if n.ID0() == 0 {
return m.ByID(n.ID1()) return m.ByID(n.ID1())
} }
return "ExprString.TODO" return "Expr.String.TODO"
} }
// TypeExprString returns a string form of the TypeExpr n. // String returns a string form of the TypeExpr n.
func TypeExprString(m *t.IDMap, n *a.TypeExpr) string { func (n *TypeExpr) String(m *t.IDMap) string {
if n != nil { if n != nil {
s := "" s := ""
switch n.PackageOrDecorator() { switch n.PackageOrDecorator() {
case 0: case 0:
s = m.ByID(n.Name()) s = m.ByID(n.Name())
case t.IDPtr: case t.IDPtr:
return "ptr " + TypeExprString(m, n.Inner()) return "ptr " + n.Inner().String(m)
case t.IDOpenBracket: case t.IDOpenBracket:
return "[" + ExprString(m, n.ArrayLength()) + "] " + TypeExprString(m, n.Inner()) return "[" + n.ArrayLength().String(m) + "] " + n.Inner().String(m)
default: default:
s = m.ByID(n.PackageOrDecorator()) + "." + m.ByID(n.Name()) s = m.ByID(n.PackageOrDecorator()) + "." + m.ByID(n.Name())
} }
if n.InclMin() == nil && n.ExclMax() == nil { if n.InclMin() == nil && n.ExclMax() == nil {
return s 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!" return "!invalid_type!"
} }
+5 -9
View File
@@ -355,7 +355,7 @@ func (c *typeChecker) checkTypeExpr(n *a.TypeExpr) error {
return err return err
} }
if bound.ConstValue() == nil { 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 return nil
@@ -375,7 +375,7 @@ func (c *typeChecker) checkTypeExpr(n *a.TypeExpr) error {
return err return err
} }
if aLen.ConstValue() == nil { 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: default:
@@ -388,7 +388,7 @@ func (c *typeChecker) checkTypeExpr(n *a.TypeExpr) error {
func (c *typeChecker) typeConvertible(e *a.Expr, typ *a.TypeExpr) error { func (c *typeChecker) typeConvertible(e *a.Expr, typ *a.TypeExpr) error {
eTyp := e.MType() eTyp := e.MType()
if eTyp == nil { 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 { if typ.PackageOrDecorator() == 0 {
@@ -396,8 +396,7 @@ func (c *typeChecker) typeConvertible(e *a.Expr, typ *a.TypeExpr) error {
if eTyp == DummyTypeIdealNumber { if eTyp == DummyTypeIdealNumber {
minMax := numTypeRanges[0xFF&(name>>t.KeyShift)] minMax := numTypeRanges[0xFF&(name>>t.KeyShift)]
if minMax[0] == nil { if minMax[0] == nil {
return fmt.Errorf("check: unknown range for built-in numeric type %q", return fmt.Errorf("check: unknown range for built-in numeric type %q", typ.String(c.idMap))
TypeExprString(c.idMap, typ))
} }
// TODO: update minMax for typ.Bounds(). // 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", return fmt.Errorf("check: cannot convert expression %q of type %q to type %q",
ExprString(c.idMap, e), e.String(c.idMap), eTyp.String(c.idMap), typ.String(c.idMap))
TypeExprString(c.idMap, eTyp),
TypeExprString(c.idMap, typ),
)
} }
var numTypeRanges = [256][2]*big.Int{ 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 { for id, typ := range bar.LocalVars {
got = append(got, [2]string{ got = append(got, [2]string{
idMap.ByID(id), idMap.ByID(id),
TypeExprString(idMap, typ), typ.String(idMap),
}) })
} }
sort.Slice(got, func(i, j int) bool { sort.Slice(got, func(i, j int) bool {