ginger/vm/scope_global.go
Brian Picciano 6040abc836 Implementation of a super basic vm
The vm does what it needs to do (evaluate the result of passing an input
to an operatio, where the input and the operation themselves may have
sub-inputs/operations to evaluate), with many caveats/misgivings.
2021-12-29 10:43:08 -07:00

34 lines
679 B
Go

package vm
import (
"fmt"
"github.com/mediocregopher/ginger/gg"
)
// GlobalScope contains operations and values which are available from within
// any operation in a ginger program.
var GlobalScope = ScopeMap{
"add": Value{Operation: preEvalValOp(func(val Value) (Value, error) {
if len(val.Tuple) == 0 {
return Value{}, fmt.Errorf("add requires a non-zero tuple of numbers as an argument")
}
var sum int64
for _, tupVal := range val.Tuple {
if tupVal.Number == nil {
return Value{}, fmt.Errorf("add requires a non-zero tuple of numbers as an argument")
}
sum += *tupVal.Number
}
return Value{Value: gg.Value{Number: &sum}}, nil
})},
}