2021-12-28 20:18:01 +00:00
|
|
|
package vm
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
import (
|
|
|
|
"github.com/mediocregopher/ginger/gg"
|
|
|
|
)
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
outVal = nameVal("out")
|
|
|
|
)
|
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
// Thunk is returned from the performance of an Operation. When called it will
|
|
|
|
// return the result of that Operation having been called with the particular
|
|
|
|
// arguments which were passed in.
|
|
|
|
type Thunk func() (Value, error)
|
|
|
|
|
|
|
|
func valThunk(val Value) Thunk {
|
|
|
|
return func() (Value, error) { return val, nil }
|
2021-12-28 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
// evalThunks is used to coalesce the results of multiple Thunks into a single
|
|
|
|
// Thunk which will return a tuple Value. As a special case, if only one Thunk
|
|
|
|
// is given then it is returned directly (1-tuple is equivalent to its only
|
|
|
|
// element).
|
|
|
|
func evalThunks(args []Thunk) Thunk {
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
if len(args) == 1 {
|
|
|
|
return args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() (Value, error) {
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
tupVals = make([]Value, len(args))
|
|
|
|
)
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
for i := range args {
|
|
|
|
if tupVals[i], err = args[i](); err != nil {
|
|
|
|
return ZeroValue, err
|
|
|
|
}
|
2021-12-28 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
return Value{Tuple: tupVals}, nil
|
|
|
|
}
|
2021-12-28 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
// Operation is an entity which can accept one or more arguments (each not
|
2021-12-30 22:10:25 +00:00
|
|
|
// having been evaluated yet) and return a Thunk which will perform some
|
|
|
|
// internal processing on those arguments and return a resultant Value.
|
|
|
|
//
|
|
|
|
// The Operation passed into Perform is the Operation which is calling the
|
|
|
|
// Perform. It may be nil.
|
2021-12-30 21:00:04 +00:00
|
|
|
type Operation interface {
|
2021-12-30 22:10:25 +00:00
|
|
|
Perform([]Thunk, Operation) (Thunk, error)
|
2021-12-30 21:00:04 +00:00
|
|
|
}
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
func preEvalValOp(fn func(Value) (Value, error)) Operation {
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 22:10:25 +00:00
|
|
|
return OperationFunc(func(args []Thunk, _ Operation) (Thunk, error) {
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
return func() (Value, error) {
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
val, err := evalThunks(args)()
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return ZeroValue, err
|
|
|
|
}
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
return fn(val)
|
2021-12-28 20:18:01 +00:00
|
|
|
|
2021-12-30 21:00:04 +00:00
|
|
|
}, nil
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type graphOp struct {
|
2021-12-30 16:56:20 +00:00
|
|
|
*gg.Graph
|
2021-12-28 20:18:01 +00:00
|
|
|
scope Scope
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperationFromGraph wraps the given Graph such that it can be used as an
|
|
|
|
// operation.
|
|
|
|
//
|
2021-12-30 21:00:04 +00:00
|
|
|
// The Thunk returned by Perform will evaluate the passed in Thunks, and set
|
|
|
|
// them to the "in" name value of the given Graph. The "out" name value is
|
|
|
|
// Evaluated using the given Scope to obtain a resultant Value.
|
2021-12-30 16:56:20 +00:00
|
|
|
func OperationFromGraph(g *gg.Graph, scope Scope) Operation {
|
2021-12-28 20:18:01 +00:00
|
|
|
return &graphOp{
|
|
|
|
Graph: g,
|
|
|
|
scope: scope,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 22:10:25 +00:00
|
|
|
func (g *graphOp) Perform(args []Thunk, _ Operation) (Thunk, error) {
|
2021-12-30 21:00:04 +00:00
|
|
|
return ScopeFromGraph(
|
|
|
|
g.Graph,
|
|
|
|
evalThunks(args),
|
|
|
|
g.scope,
|
2021-12-30 22:10:25 +00:00
|
|
|
g,
|
2021-12-30 21:00:04 +00:00
|
|
|
).Evaluate(outVal)
|
2021-12-28 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OperationFunc is a function which implements the Operation interface.
|
2021-12-30 22:10:25 +00:00
|
|
|
type OperationFunc func([]Thunk, Operation) (Thunk, error)
|
2021-12-28 20:18:01 +00:00
|
|
|
|
|
|
|
// Perform calls the underlying OperationFunc directly.
|
2021-12-30 22:10:25 +00:00
|
|
|
func (f OperationFunc) Perform(args []Thunk, op Operation) (Thunk, error) {
|
|
|
|
return f(args, op)
|
2021-12-28 20:18:01 +00:00
|
|
|
}
|