2021-12-28 20:18:01 +00:00
|
|
|
package vm
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
import (
|
|
|
|
"github.com/mediocregopher/ginger/gg"
|
|
|
|
"github.com/mediocregopher/ginger/graph"
|
|
|
|
)
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
inVal = nameVal("in")
|
|
|
|
outVal = nameVal("out")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Operation is an entity which can accept a single argument (the OpenEdge),
|
|
|
|
// perform some internal processing on that argument, and return a resultant
|
|
|
|
// Value.
|
|
|
|
//
|
|
|
|
// The Scope passed into Perform can be used to Evaluate the OpenEdge, as
|
|
|
|
// needed.
|
|
|
|
type Operation interface {
|
2021-12-29 19:32:53 +00:00
|
|
|
Perform(graph.OpenEdge[gg.Value], Scope) (Value, error)
|
2021-12-28 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func preEvalValOp(fn func(Value) (Value, error)) Operation {
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
return OperationFunc(func(edge graph.OpenEdge[gg.Value], scope Scope) (Value, error) {
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
edgeVal, err := EvaluateEdge(edge, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return Value{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(edgeVal)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE this is a giant hack to get around the fact that we're not yet
|
|
|
|
// using a genericized Graph implementation, so when we do AddValueIn
|
2021-12-29 19:32:53 +00:00
|
|
|
// on a graph.Graph[gg.Value] we can't use a Tuple value (because gg has no Tuple
|
2021-12-28 20:18:01 +00:00
|
|
|
// value), we have to use a Tuple vertex instead.
|
|
|
|
//
|
|
|
|
// This also doesn't yet support passing an operation as a value to another
|
|
|
|
// operation.
|
2021-12-29 19:32:53 +00:00
|
|
|
func preEvalEdgeOp(fn func(graph.OpenEdge[gg.Value]) (Value, error)) Operation {
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
return preEvalValOp(func(val Value) (Value, error) {
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
var edge graph.OpenEdge[gg.Value]
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
if len(val.Tuple) > 0 {
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
tupEdges := make([]graph.OpenEdge[gg.Value], len(val.Tuple))
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
for i := range val.Tuple {
|
2021-12-29 19:32:53 +00:00
|
|
|
tupEdges[i] = graph.ValueOut[gg.Value](val.Tuple[i].Value, gg.ZeroValue)
|
2021-12-28 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
edge = graph.TupleOut[gg.Value](tupEdges, gg.ZeroValue)
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
edge = graph.ValueOut[gg.Value](val.Value, gg.ZeroValue)
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(edge)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type graphOp struct {
|
2021-12-29 19:32:53 +00:00
|
|
|
*graph.Graph[gg.Value]
|
2021-12-28 20:18:01 +00:00
|
|
|
scope Scope
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperationFromGraph wraps the given Graph such that it can be used as an
|
|
|
|
// operation.
|
|
|
|
//
|
|
|
|
// When Perform is called the passed in OpenEdge is set to the "in" name value
|
|
|
|
// of the given Graph, then that resultant graph and the given parent Scope are
|
|
|
|
// used to construct a new Scope. The "out" name value is Evaluated on that
|
|
|
|
// Scope to obtain a resultant Value.
|
2021-12-29 19:32:53 +00:00
|
|
|
func OperationFromGraph(g *graph.Graph[gg.Value], scope Scope) Operation {
|
2021-12-28 20:18:01 +00:00
|
|
|
return &graphOp{
|
|
|
|
Graph: g,
|
|
|
|
scope: scope,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
func (g *graphOp) Perform(edge graph.OpenEdge[gg.Value], scope Scope) (Value, error) {
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
return preEvalEdgeOp(func(edge graph.OpenEdge[gg.Value]) (Value, error) {
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
scope = ScopeFromGraph(
|
|
|
|
g.Graph.AddValueIn(edge, inVal.Value),
|
|
|
|
g.scope,
|
|
|
|
)
|
|
|
|
|
|
|
|
return scope.Evaluate(outVal)
|
|
|
|
|
|
|
|
}).Perform(edge, scope)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperationFunc is a function which implements the Operation interface.
|
2021-12-29 19:32:53 +00:00
|
|
|
type OperationFunc func(graph.OpenEdge[gg.Value], Scope) (Value, error)
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
// Perform calls the underlying OperationFunc directly.
|
2021-12-29 19:32:53 +00:00
|
|
|
func (f OperationFunc) Perform(edge graph.OpenEdge[gg.Value], scope Scope) (Value, error) {
|
2021-12-28 20:18:01 +00:00
|
|
|
return f(edge, scope)
|
|
|
|
}
|