lang: ast: Add better logging about scope issues
This may help out programmers who aren't sure what's going on.
This commit is contained in:
@@ -8128,6 +8128,9 @@ func (obj *ExprCall) SetScope(scope *interfaces.Scope, sctx map[string]interface
|
|||||||
} else {
|
} else {
|
||||||
f, exists := obj.scope.Variables[obj.Name]
|
f, exists := obj.scope.Variables[obj.Name]
|
||||||
if !exists {
|
if !exists {
|
||||||
|
if obj.data.Debug || true { // TODO: leave this on permanently?
|
||||||
|
lambdaScopeFeedback(obj.scope, obj.data.Logf)
|
||||||
|
}
|
||||||
return fmt.Errorf("func `%s` does not exist in this scope", prefixedName)
|
return fmt.Errorf("func `%s` does not exist in this scope", prefixedName)
|
||||||
}
|
}
|
||||||
target = f
|
target = f
|
||||||
@@ -8137,6 +8140,9 @@ func (obj *ExprCall) SetScope(scope *interfaces.Scope, sctx map[string]interface
|
|||||||
prefixedName = obj.Name
|
prefixedName = obj.Name
|
||||||
f, exists := obj.scope.Functions[obj.Name]
|
f, exists := obj.scope.Functions[obj.Name]
|
||||||
if !exists {
|
if !exists {
|
||||||
|
if obj.data.Debug || true { // TODO: leave this on permanently?
|
||||||
|
functionScopeFeedback(obj.scope, obj.data.Logf)
|
||||||
|
}
|
||||||
return fmt.Errorf("func `%s` does not exist in this scope", prefixedName)
|
return fmt.Errorf("func `%s` does not exist in this scope", prefixedName)
|
||||||
}
|
}
|
||||||
target = f
|
target = f
|
||||||
@@ -8727,6 +8733,9 @@ func (obj *ExprVar) SetScope(scope *interfaces.Scope, sctx map[string]interfaces
|
|||||||
|
|
||||||
target, exists := obj.scope.Variables[obj.Name]
|
target, exists := obj.scope.Variables[obj.Name]
|
||||||
if !exists {
|
if !exists {
|
||||||
|
if obj.data.Debug || true { // TODO: leave this on permanently?
|
||||||
|
variableScopeFeedback(obj.scope, obj.data.Logf)
|
||||||
|
}
|
||||||
return fmt.Errorf("variable %s not in scope", obj.Name)
|
return fmt.Errorf("variable %s not in scope", obj.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ package ast
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -340,3 +341,55 @@ func trueCallee(apparentCallee interfaces.Expr) interfaces.Expr {
|
|||||||
return apparentCallee
|
return apparentCallee
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// variableScopeFeedback logs some messages about what is actually in scope so
|
||||||
|
// that the user gets a hint about what's going on. This is useful for catching
|
||||||
|
// bugs in our programming or in user code!
|
||||||
|
func variableScopeFeedback(scope *interfaces.Scope, logf func(format string, v ...interface{})) {
|
||||||
|
logf("variables in scope:")
|
||||||
|
names := []string{}
|
||||||
|
for name := range scope.Variables {
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
for _, name := range names {
|
||||||
|
logf("$%s", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// functionScopeFeedback logs some messages about what is actually in scope so
|
||||||
|
// that the user gets a hint about what's going on. This is useful for catching
|
||||||
|
// bugs in our programming or in user code!
|
||||||
|
func functionScopeFeedback(scope *interfaces.Scope, logf func(format string, v ...interface{})) {
|
||||||
|
logf("functions in scope:")
|
||||||
|
names := []string{}
|
||||||
|
for name := range scope.Functions {
|
||||||
|
if strings.HasPrefix(name, "_") { // hidden function
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
for _, name := range names {
|
||||||
|
logf("%s(...)", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lambdaScopeFeedback logs some messages about what is actually in scope so
|
||||||
|
// that the user gets a hint about what's going on. This is useful for catching
|
||||||
|
// bugs in our programming or in user code!
|
||||||
|
func lambdaScopeFeedback(scope *interfaces.Scope, logf func(format string, v ...interface{})) {
|
||||||
|
logf("lambdas in scope:")
|
||||||
|
names := []string{}
|
||||||
|
for name, val := range scope.Variables {
|
||||||
|
// XXX: Is this a valid way to filter?
|
||||||
|
if _, ok := trueCallee(val).(*ExprFunc); !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
for _, name := range names {
|
||||||
|
logf("$%s(...)", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user