Skip to content

Commit

Permalink
Merge pull request #309 from xushiwei/refactor
Browse files Browse the repository at this point in the history
NodeInterpreter: rm Caller
  • Loading branch information
xushiwei authored Nov 3, 2023
2 parents 424983d + 7fc6550 commit 19b9702
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
22 changes: 9 additions & 13 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,19 +951,11 @@ func toRetType(t *types.Tuple, it *instantiated) types.Type {
return it.normalizeTuple(t)
}

func getCaller(pkg *Package, fn *internal.Elem) (caller string, pos token.Pos) {
if fn != nil {
_, pos = pkg.cb.loadExpr(fn.Src)
caller = types.ExprString(fn.Val)
}
return
}

func matchFuncType(
pkg *Package, args []*internal.Elem, flags InstrFlags, sig *types.Signature, fn *internal.Elem) error {
if (flags & InstrFlagTwoValue) != 0 {
if n := sig.Results().Len(); n != 2 {
caller, pos := getCaller(pkg, fn)
caller, pos := getFunExpr(fn)
return pkg.cb.newCodeErrorf(pos, "assignment mismatch: 2 variables but %v returns %v values", caller, n)
}
}
Expand Down Expand Up @@ -996,7 +988,7 @@ func matchFuncType(
if (flags & InstrFlagEllipsis) == 0 {
n1 := getParamLen(sig) - 1
if n < n1 {
caller, pos := getCaller(pkg, fn)
caller, pos := getFunExpr(fn)
return pkg.cb.newCodeErrorf(pos, "not enough arguments in call to %v\n\thave (%v)\n\twant %v",
caller, getTypes(args), sig.Params())
}
Expand All @@ -1010,15 +1002,15 @@ func matchFuncType(
return matchElemType(pkg, args[n1:], tyVariadic.Elem(), at)
}
} else if (flags & InstrFlagEllipsis) != 0 {
caller, pos := getCaller(pkg, fn)
caller, pos := getFunExpr(fn)
return pkg.cb.newCodeErrorf(pos, "cannot use ... in call to non-variadic %v", caller)
}
if nreq := getParamLen(sig); nreq != n {
fewOrMany := "not enough"
if n > nreq {
fewOrMany = "too many"
}
caller, pos := getCaller(pkg, fn)
caller, pos := getFunExpr(fn)
return pkg.cb.newCodeErrorf(pos,
"%s arguments in call to %s\n\thave (%v)\n\twant %v", fewOrMany, caller, getTypes(args), sig.Params())
}
Expand Down Expand Up @@ -1177,8 +1169,12 @@ func (p *MatchError) Message(fileLine string) string {
"%scannot use %s (type %v) as type %v in %s", fileLine, src, p.Arg, p.Param, strval(p.At))
}

func (p *MatchError) Pos() token.Pos {
return getSrcPos(p.Src)
}

func (p *MatchError) Error() string {
pos := p.Fset.Position(getSrcPos(p.Src))
pos := p.Fset.Position(p.Pos())
return p.Message(pos.String() + ": ")
}

Expand Down
11 changes: 5 additions & 6 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,14 @@ func TestCheckUdt(t *testing.T) {

func TestNodeInterp(t *testing.T) {
interp := nodeInterp{}
if interp.Caller(nil) != "the function call" {
t.Fatal("TestNodeInterp interp.Caller failed")
}
if src := interp.LoadExpr(nil); src != "" {
t.Fatal("TestNodeInterp interp.LoadExpr failed:", src)
}
var cb CodeBuilder
if cb.getCaller(nil) != "" {
t.Fatal("TestNodeInterp cb.getCaller failed")
if caller := getCaller(&internal.Elem{}); caller != "the function call" {
t.Fatal("TestNodeInterp getCaller failed:", caller)
}
if caller, pos := getFunExpr(nil); caller != "the closure call" || pos != token.NoPos {
t.Fatal("TestNodeInterp getGoExpr failed:", caller, pos)
}
}

Expand Down
23 changes: 14 additions & 9 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,24 @@ func defaultHandleErr(err error) {

type nodeInterp struct{}

func (p nodeInterp) Caller(expr ast.Node) string {
return "the function call"
}

func (p nodeInterp) LoadExpr(expr ast.Node) string {
return ""
}

func (p *CodeBuilder) getCaller(expr ast.Node) string {
if expr == nil {
return ""
func getFunExpr(fn *internal.Elem) (caller string, pos token.Pos) {
if fn == nil {
return "the closure call", token.NoPos
}
return p.interp.Caller(expr)
caller = types.ExprString(fn.Val)
pos = getSrcPos(fn.Src)
return
}

func getCaller(expr *internal.Elem) string {
if ce, ok := expr.Val.(*ast.CallExpr); ok {
return types.ExprString(ce.Fun)
}
return "the function call"
}

func (p *CodeBuilder) loadExpr(expr ast.Node) (string, token.Pos) {
Expand Down Expand Up @@ -1952,7 +1957,7 @@ func (p *CodeBuilder) doAssignWith(lhs, rhs int, src ast.Node) *CodeBuilder {
if rhsVals, ok := args[lhs].Type.(*types.Tuple); ok {
if lhs != rhsVals.Len() {
pos := getSrcPos(src)
caller := p.getCaller(args[lhs].Src)
caller := getCaller(args[lhs])
p.panicCodeErrorf(
pos, "assignment mismatch: %d variables but %v returns %d values",
lhs, caller, rhsVals.Len())
Expand Down
5 changes: 1 addition & 4 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ type dbgPositioner interface {
type NodeInterpreter interface {
// LoadExpr is called to load an expr code.
LoadExpr(expr ast.Node) string

// Caller returns the name of a function call.
Caller(expr ast.Node) string
}

// Config type
Expand Down Expand Up @@ -315,14 +312,14 @@ type ObjectDocs = map[types.Object]*ast.CommentGroup
type Package struct {
PkgRef
Docs ObjectDocs
Fset *token.FileSet

cb CodeBuilder
imp types.Importer
files map[string]*File
file *File
conf *Config
ctx *Context
Fset *token.FileSet
builtin *types.Package
utBigInt *types.Named
utBigRat *types.Named
Expand Down
2 changes: 1 addition & 1 deletion type_var_and_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (p *ValueDecl) endInit(cb *CodeBuilder, arity int) *ValueDecl {
}()
if arity == 1 && checkTuple(&t, rets[0].Type) {
if n != t.Len() {
caller := cb.getCaller(rets[0].Src)
caller := getCaller(rets[0])
cb.panicCodeErrorf(
p.pos, "assignment mismatch: %d variables but %s returns %d values", n, caller, t.Len())
}
Expand Down

0 comments on commit 19b9702

Please sign in to comment.