lang: Add a new function interface to accept data

Sometimes certain internal functions might want to get some data from
the AST or from something relating to the state of the language. This
adds a method to pass in that data. For now it's a very simple method,
but we could generalize it in the future if it becomes more useful.
This commit is contained in:
James Shubin
2019-07-22 06:46:04 -04:00
parent f2f9c043bf
commit 2980523a5b
2 changed files with 39 additions and 0 deletions

View File

@@ -6676,6 +6676,13 @@ func (obj *ExprFunc) Init(data *interfaces.Data) error {
return fmt.Errorf("func is being re-built")
}
obj.function = obj.Function() // build it
// pass in some data to the function
// TODO: do we want to pass in the full obj.data instead ?
if dataFunc, ok := obj.function.(interfaces.DataFunc); ok {
dataFunc.SetData(&interfaces.FuncData{
Base: obj.data.Base,
})
}
}
if len(obj.Values) > 0 {
@@ -6755,6 +6762,13 @@ func (obj *ExprFunc) Copy() (interfaces.Expr, error) {
var function interfaces.Func
if obj.Function != nil {
function = obj.Function() // force re-build a new pointer here!
// pass in some data to the function
// TODO: do we want to pass in the full obj.data instead ?
if dataFunc, ok := function.(interfaces.DataFunc); ok {
dataFunc.SetData(&interfaces.FuncData{
Base: obj.data.Base,
})
}
copied = true
}