Compare commits

...

3 Commits

  1. 53
      examples/examples_test.go
  2. 27
      vm/function.go
  3. 20
      vm/scope.go
  4. 2
      vm/vm.go
  5. 6
      vm/vm_test.go

@ -0,0 +1,53 @@
package examples_test
import (
"embed"
"fmt"
"testing"
"code.betamike.com/mediocregopher/ginger/gg"
"code.betamike.com/mediocregopher/ginger/vm"
"github.com/stretchr/testify/assert"
)
//go:embed *.gg
var examplesFS embed.FS
func TestAllExamples(t *testing.T) {
tests := []struct {
path string
in vm.Value
exp vm.Value
}{
{
path: "fib.gg",
in: vm.Value{Value: gg.Number(5)},
exp: vm.Value{Value: gg.Number(5)},
},
{
path: "fib.gg",
in: vm.Value{Value: gg.Number(10)},
exp: vm.Value{Value: gg.Number(55)},
},
{
path: "fib.gg",
in: vm.Value{Value: gg.Number(69)},
exp: vm.Value{Value: gg.Number(117669030460994)},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s_%v", test.path, test.in), func(t *testing.T) {
f, err := examplesFS.Open(test.path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
got, err := vm.EvaluateSource(f, test.in, vm.GlobalScope)
assert.NoError(t, err)
assert.True(t, test.exp.Equal(got), "%v != %v", test.exp, got)
})
}
}

@ -1,6 +1,7 @@
package vm
import (
"errors"
"fmt"
"code.betamike.com/mediocregopher/ginger/gg"
@ -79,8 +80,6 @@ func FunctionFromGraph(g *gg.Graph, scope Scope) (Function, error) {
return edgeFn(Identity(val)), nil
}
name := *val.Name
if val.Equal(valNameIn) {
return edgeFn(FunctionFunc(func(inArg Value) Value {
return inArg
@ -89,15 +88,17 @@ func FunctionFromGraph(g *gg.Graph, scope Scope) (Function, error) {
edgesIn := g.ValueIns(val.Value)
if l := len(edgesIn); l == 0 {
val, err := scope.Resolve(name)
name := *val.Name
if err != nil {
if l := len(edgesIn); l == 0 {
resolvedVal, err := scope.Resolve(name)
if errors.Is(err, ErrNameNotDefined) {
return edgeFn(Identity(val)), nil
} else if err != nil {
return nil, fmt.Errorf("resolving name %q from the outer scope: %w", name, err)
}
return edgeFn(Identity(val)), nil
return edgeFn(Identity(resolvedVal)), nil
} else if l != 1 {
return nil, fmt.Errorf("resolved name %q to %d input edges, rather than one", name, l)
@ -123,7 +124,6 @@ func FunctionFromGraph(g *gg.Graph, scope Scope) (Function, error) {
return valToEdgeFn(Value{Value: ggVal})
},
func(ggEdgeVal gg.OptionalValue, inEdgeFns []edgeFn) (edgeFn, error) {
if ggEdgeVal.Equal(valNameIf.Value) {
if len(inEdgeFns) != 3 {
@ -177,11 +177,7 @@ func FunctionFromGraph(g *gg.Graph, scope Scope) (Function, error) {
if edgeVal.Graph != nil {
opFromGraph, err := FunctionFromGraph(
edgeVal.Graph,
scope.NewScope(),
)
opFromGraph, err := FunctionFromGraph(edgeVal.Graph, scope)
if err != nil {
return nil, fmt.Errorf("compiling graph to operation: %w", err)
}
@ -198,7 +194,7 @@ func FunctionFromGraph(g *gg.Graph, scope Scope) (Function, error) {
})), nil
}
// the edgeVal is not an Function at compile time, and so
// the edgeVal is not a Function at compile time, and so
// it must become one at runtime. We must resolve edgeVal to an
// edgeFn as well (edgeEdgeFn), and then at runtime that is
// given the inArg and (hopefully) the resultant Function is
@ -217,8 +213,7 @@ func FunctionFromGraph(g *gg.Graph, scope Scope) (Function, error) {
if runtimeEdgeVal.Graph != nil {
runtimeFn, err := FunctionFromGraph(
runtimeEdgeVal.Graph,
scope.NewScope(),
runtimeEdgeVal.Graph, scope,
)
if err != nil {

@ -1,18 +1,19 @@
package vm
import (
"fmt"
"errors"
)
// ErrNameNotDefined is returned from Scope.Resolve when a name could not be
// resolved within a Scope.
var ErrNameNotDefined = errors.New("not defined")
// Scope encapsulates a set of name->Value mappings.
type Scope interface {
// Resolve accepts a name and returns an Value.
// Resolve accepts a name and returns an Value, or returns
// ErrNameNotDefined.
Resolve(string) (Value, error)
// NewScope returns a new Scope which sub-operations within this Scope
// should use for themselves.
NewScope() Scope
}
// ScopeMap implements the Scope interface.
@ -27,17 +28,12 @@ func (m ScopeMap) Resolve(name string) (Value, error) {
v, ok := m[name]
if !ok {
return Value{}, fmt.Errorf("%q not defined", name)
return Value{}, ErrNameNotDefined
}
return v, nil
}
// NewScope returns the ScopeMap as-is.
func (m ScopeMap) NewScope() Scope {
return m
}
type scopeWith struct {
Scope // parent
name string

@ -114,7 +114,7 @@ func EvaluateSource(opSrc io.Reader, input Value, scope Scope) (Value, error) {
return Value{}, errors.New("value must be a graph")
}
fn, err := FunctionFromGraph(v.Value.Graph, scope.NewScope())
fn, err := FunctionFromGraph(v.Value.Graph, scope)
if err != nil {
return Value{}, err
}

@ -32,10 +32,16 @@ func TestVM(t *testing.T) {
in: Value{Value: gg.Number(1)},
expErr: "name !foo cannot start with a '!'",
},
{
src: `{foo = bar; !out = foo;}`,
in: Value{},
exp: Value{Value: gg.Name("bar")},
},
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Log(test.src)
val, err := EvaluateSource(
bytes.NewBufferString(test.src), test.in, GlobalScope,
)

Loading…
Cancel
Save