2016-02-20 00:21:51 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-03-29 04:14:14 +08:00
|
|
|
"github.com/elves/elvish/pkg/diag"
|
2021-01-05 12:07:35 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/errs"
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
2020-12-25 01:39:51 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vars"
|
2020-09-03 12:27:18 +08:00
|
|
|
"github.com/elves/elvish/pkg/fsutil"
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/glob"
|
|
|
|
"github.com/elves/elvish/pkg/parse"
|
2016-02-20 00:21:51 +08:00
|
|
|
)
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
// An operation that produces values.
|
|
|
|
type valuesOp interface {
|
|
|
|
diag.Ranger
|
|
|
|
exec(*Frame) ([]interface{}, error)
|
|
|
|
}
|
|
|
|
|
2016-02-22 02:25:49 +08:00
|
|
|
var outputCaptureBufferSize = 16
|
|
|
|
|
2020-03-29 05:31:47 +08:00
|
|
|
func (cp *compiler) compoundOp(n *parse.Compound) valuesOp {
|
2016-02-20 00:21:51 +08:00
|
|
|
if len(n.Indexings) == 0 {
|
2020-08-22 05:28:54 +08:00
|
|
|
return literalValues(n, "")
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tilde := false
|
|
|
|
indexings := n.Indexings
|
|
|
|
|
|
|
|
if n.Indexings[0].Head.Type == parse.Tilde {
|
|
|
|
// A lone ~.
|
|
|
|
if len(n.Indexings) == 1 {
|
2020-08-22 05:28:54 +08:00
|
|
|
return loneTildeOp{n.Range()}
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
tilde = true
|
|
|
|
indexings = indexings[1:]
|
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
return compoundOp{n.Range(), tilde, cp.indexingOps(indexings)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type loneTildeOp struct{ diag.Ranging }
|
|
|
|
|
|
|
|
func (loneTildeOp) exec(fm *Frame) ([]interface{}, error) {
|
2020-09-03 12:27:18 +08:00
|
|
|
home, err := fsutil.GetHome("")
|
2020-08-22 05:28:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []interface{}{home}, nil
|
2020-03-29 05:31:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *compiler) compoundOps(ns []*parse.Compound) []valuesOp {
|
|
|
|
ops := make([]valuesOp, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
ops[i] = cp.compoundOp(n)
|
|
|
|
}
|
|
|
|
return ops
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type compoundOp struct {
|
2020-08-22 05:28:54 +08:00
|
|
|
diag.Ranging
|
2018-01-21 21:20:06 +08:00
|
|
|
tilde bool
|
2018-09-29 03:45:11 +08:00
|
|
|
subops []valuesOp
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op compoundOp) exec(fm *Frame) ([]interface{}, error) {
|
2018-01-21 21:20:06 +08:00
|
|
|
// Accumulator.
|
2018-09-29 03:45:11 +08:00
|
|
|
vs, err := op.subops[0].exec(fm)
|
2018-01-21 21:20:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-20 00:21:51 +08:00
|
|
|
|
2018-01-21 21:20:06 +08:00
|
|
|
for _, subop := range op.subops[1:] {
|
2018-09-29 03:45:11 +08:00
|
|
|
us, err := subop.exec(fm)
|
2018-01-21 09:14:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-16 05:15:09 +08:00
|
|
|
vs, err = outerProduct(vs, us, vals.Concat)
|
2018-01-21 21:20:06 +08:00
|
|
|
if err != nil {
|
2020-08-22 05:28:54 +08:00
|
|
|
return nil, fm.errorp(op, err)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
if op.tilde {
|
2018-01-30 01:39:41 +08:00
|
|
|
newvs := make([]interface{}, len(vs))
|
2018-01-21 21:20:06 +08:00
|
|
|
for i, v := range vs {
|
2018-09-27 22:32:41 +08:00
|
|
|
tilded, err := doTilde(v)
|
|
|
|
if err != nil {
|
2020-08-22 05:28:54 +08:00
|
|
|
return nil, fm.errorp(op, err)
|
2018-09-27 22:32:41 +08:00
|
|
|
}
|
|
|
|
newvs[i] = tilded
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
vs = newvs
|
|
|
|
}
|
|
|
|
hasGlob := false
|
|
|
|
for _, v := range vs {
|
2021-01-05 12:07:35 +08:00
|
|
|
if _, ok := v.(globPattern); ok {
|
2018-01-21 21:20:06 +08:00
|
|
|
hasGlob = true
|
|
|
|
break
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
if hasGlob {
|
2018-01-30 01:39:41 +08:00
|
|
|
newvs := make([]interface{}, 0, len(vs))
|
2018-01-21 21:20:06 +08:00
|
|
|
for _, v := range vs {
|
2021-01-05 12:07:35 +08:00
|
|
|
if gp, ok := v.(globPattern); ok {
|
2018-09-27 08:48:44 +08:00
|
|
|
results, err := doGlob(gp, fm.Interrupts())
|
|
|
|
if err != nil {
|
2020-08-22 05:28:54 +08:00
|
|
|
return nil, fm.errorp(op, err)
|
2018-09-27 08:48:44 +08:00
|
|
|
}
|
|
|
|
newvs = append(newvs, results...)
|
2018-01-21 21:20:06 +08:00
|
|
|
} else {
|
|
|
|
newvs = append(newvs, v)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
vs = newvs
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
return vs, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 01:39:41 +08:00
|
|
|
func outerProduct(vs []interface{}, us []interface{}, f func(interface{}, interface{}) (interface{}, error)) ([]interface{}, error) {
|
|
|
|
ws := make([]interface{}, len(vs)*len(us))
|
2016-02-20 00:21:51 +08:00
|
|
|
nu := len(us)
|
|
|
|
for i, v := range vs {
|
|
|
|
for j, u := range us {
|
2018-01-21 19:17:17 +08:00
|
|
|
var err error
|
|
|
|
ws[i*nu+j], err = f(v, u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-21 19:17:17 +08:00
|
|
|
return ws, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2017-06-11 07:04:53 +08:00
|
|
|
// Errors thrown when globbing.
|
2016-02-22 23:05:14 +08:00
|
|
|
var (
|
2021-01-05 12:07:35 +08:00
|
|
|
ErrBadglobPattern = errors.New("bad globPattern; elvish bug")
|
2016-02-22 23:05:14 +08:00
|
|
|
ErrCannotDetermineUsername = errors.New("cannot determine user name from glob pattern")
|
|
|
|
)
|
|
|
|
|
2018-09-27 22:32:41 +08:00
|
|
|
func doTilde(v interface{}) (interface{}, error) {
|
2016-02-20 00:21:51 +08:00
|
|
|
switch v := v.(type) {
|
2018-01-25 09:40:15 +08:00
|
|
|
case string:
|
|
|
|
s := v
|
2016-02-20 00:21:51 +08:00
|
|
|
i := strings.Index(s, "/")
|
|
|
|
var uname, rest string
|
|
|
|
if i == -1 {
|
|
|
|
uname = s
|
|
|
|
} else {
|
|
|
|
uname = s[:i]
|
|
|
|
rest = s[i+1:]
|
|
|
|
}
|
2020-09-03 12:27:18 +08:00
|
|
|
dir, err := fsutil.GetHome(uname)
|
2018-09-28 05:19:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-20 17:40:02 +08:00
|
|
|
// We do not use path.Join, as it removes trailing slashes.
|
|
|
|
//
|
|
|
|
// TODO(xiaq): Make this correct on Windows.
|
|
|
|
return dir + "/" + rest, nil
|
2021-01-05 12:07:35 +08:00
|
|
|
case globPattern:
|
2016-02-22 23:05:14 +08:00
|
|
|
if len(v.Segments) == 0 {
|
2021-01-05 12:07:35 +08:00
|
|
|
return nil, ErrBadglobPattern
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2017-02-03 03:15:42 +08:00
|
|
|
switch seg := v.Segments[0].(type) {
|
2016-02-22 23:05:14 +08:00
|
|
|
case glob.Literal:
|
2020-01-14 21:03:41 +08:00
|
|
|
if len(v.Segments) == 1 {
|
2021-01-05 12:07:35 +08:00
|
|
|
return nil, ErrBadglobPattern
|
2016-02-22 23:05:14 +08:00
|
|
|
}
|
2020-01-14 21:03:41 +08:00
|
|
|
_, isSlash := v.Segments[1].(glob.Slash)
|
|
|
|
if isSlash {
|
|
|
|
// ~username or ~username/xxx. Replace the first segment with
|
|
|
|
// the home directory of the specified user.
|
2020-09-03 12:27:18 +08:00
|
|
|
dir, err := fsutil.GetHome(seg.Data)
|
2020-01-14 21:03:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
v.Segments[0] = glob.Literal{Data: dir}
|
|
|
|
return v, nil
|
2018-09-28 05:19:47 +08:00
|
|
|
}
|
2016-02-22 23:05:14 +08:00
|
|
|
case glob.Slash:
|
2020-09-03 12:27:18 +08:00
|
|
|
dir, err := fsutil.GetHome("")
|
2018-09-28 05:19:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
v.DirOverride = dir
|
2020-01-14 21:03:41 +08:00
|
|
|
return v, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2020-01-14 21:03:41 +08:00
|
|
|
return nil, ErrCannotDetermineUsername
|
2016-02-20 00:21:51 +08:00
|
|
|
default:
|
2018-09-27 22:32:41 +08:00
|
|
|
return nil, fmt.Errorf("tilde doesn't work on value of type %s", vals.Kind(v))
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 05:31:47 +08:00
|
|
|
func (cp *compiler) arrayOp(n *parse.Array) valuesOp {
|
2020-08-22 05:28:54 +08:00
|
|
|
return seqValuesOp{n.Range(), cp.compoundOps(n.Compounds)}
|
2020-03-29 05:31:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *compiler) arrayOps(ns []*parse.Array) []valuesOp {
|
|
|
|
ops := make([]valuesOp, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
ops[i] = cp.arrayOp(n)
|
|
|
|
}
|
|
|
|
return ops
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 05:31:47 +08:00
|
|
|
func (cp *compiler) indexingOp(n *parse.Indexing) valuesOp {
|
2016-02-20 00:21:51 +08:00
|
|
|
if len(n.Indicies) == 0 {
|
2020-03-29 05:31:47 +08:00
|
|
|
return cp.primaryOp(n.Head)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2020-08-22 05:28:54 +08:00
|
|
|
return &indexingOp{n.Range(), cp.primaryOp(n.Head), cp.arrayOps(n.Indicies)}
|
2020-03-29 05:31:47 +08:00
|
|
|
}
|
2016-02-20 00:21:51 +08:00
|
|
|
|
2020-03-29 05:31:47 +08:00
|
|
|
func (cp *compiler) indexingOps(ns []*parse.Indexing) []valuesOp {
|
|
|
|
ops := make([]valuesOp, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
ops[i] = cp.indexingOp(n)
|
|
|
|
}
|
|
|
|
return ops
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-01-21 21:20:06 +08:00
|
|
|
type indexingOp struct {
|
2020-08-22 05:28:54 +08:00
|
|
|
diag.Ranging
|
2018-09-29 03:45:11 +08:00
|
|
|
headOp valuesOp
|
|
|
|
indexOps []valuesOp
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op *indexingOp) exec(fm *Frame) ([]interface{}, error) {
|
2018-09-29 03:45:11 +08:00
|
|
|
vs, err := op.headOp.exec(fm)
|
2018-01-21 21:20:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, indexOp := range op.indexOps {
|
2020-10-06 02:35:52 +08:00
|
|
|
indices, err := indexOp.exec(fm)
|
2018-01-21 21:20:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2020-10-06 02:35:52 +08:00
|
|
|
newvs := make([]interface{}, 0, len(vs)*len(indices))
|
2018-01-21 21:20:06 +08:00
|
|
|
for _, v := range vs {
|
2020-10-06 02:35:52 +08:00
|
|
|
for _, index := range indices {
|
2018-02-15 17:14:05 +08:00
|
|
|
result, err := vals.Index(v, index)
|
2018-01-21 21:20:06 +08:00
|
|
|
if err != nil {
|
2020-08-22 05:28:54 +08:00
|
|
|
return nil, fm.errorp(op, err)
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
2020-08-16 05:52:50 +08:00
|
|
|
deprecation := vals.CheckDeprecatedIndex(v, index)
|
|
|
|
if deprecation != "" {
|
|
|
|
fm.Deprecate(deprecation, diag.NewContext(fm.srcMeta.Name, fm.srcMeta.Code, indexOp))
|
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
newvs = append(newvs, result)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
vs = newvs
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
return vs, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 05:31:47 +08:00
|
|
|
func (cp *compiler) primaryOp(n *parse.Primary) valuesOp {
|
2016-02-20 00:21:51 +08:00
|
|
|
switch n.Type {
|
|
|
|
case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted:
|
2020-08-22 05:28:54 +08:00
|
|
|
return literalValues(n, n.Value)
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.Variable:
|
2020-12-26 06:31:52 +08:00
|
|
|
sigil, qname := SplitSigil(n.Value)
|
2020-12-25 01:39:51 +08:00
|
|
|
ref := resolveVarRef(cp, qname, n)
|
|
|
|
if ref == nil {
|
2020-03-29 04:50:41 +08:00
|
|
|
cp.errorpf(n, "variable $%s not found", qname)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2020-12-25 01:39:51 +08:00
|
|
|
return &variableOp{n.Range(), sigil != "", qname, ref}
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.Wildcard:
|
2020-04-26 01:55:44 +08:00
|
|
|
seg, err := wildcardToSegment(parse.SourceText(n))
|
2017-06-27 01:47:10 +08:00
|
|
|
if err != nil {
|
2020-03-29 04:50:41 +08:00
|
|
|
cp.errorpf(n, "%s", err)
|
2017-06-27 01:47:10 +08:00
|
|
|
}
|
2018-01-30 01:39:41 +08:00
|
|
|
vs := []interface{}{
|
2021-01-05 12:07:35 +08:00
|
|
|
globPattern{Pattern: glob.Pattern{Segments: []glob.Segment{seg}, DirOverride: ""},
|
2020-08-28 13:10:51 +08:00
|
|
|
Flags: 0, Buts: nil, TypeCb: nil}}
|
2020-08-22 05:28:54 +08:00
|
|
|
return literalValues(n, vs...)
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.Tilde:
|
2020-03-29 04:50:41 +08:00
|
|
|
cp.errorpf(n, "compiler bug: Tilde not handled in .compound")
|
2020-08-22 05:28:54 +08:00
|
|
|
return literalValues(n, "~")
|
2017-02-18 12:43:27 +08:00
|
|
|
case parse.ExceptionCapture:
|
2020-08-22 05:28:54 +08:00
|
|
|
return exceptionCaptureOp{n.Range(), cp.chunkOp(n.Chunk)}
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.OutputCapture:
|
2020-08-22 05:28:54 +08:00
|
|
|
return outputCaptureOp{n.Range(), cp.chunkOp(n.Chunk)}
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.List:
|
2020-08-22 05:28:54 +08:00
|
|
|
return listOp{n.Range(), cp.compoundOps(n.Elements)}
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.Lambda:
|
2020-08-22 05:28:54 +08:00
|
|
|
return cp.lambda(n)
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.Map:
|
2020-08-22 05:28:54 +08:00
|
|
|
return mapOp{n.Range(), cp.mapPairs(n.MapPairs)}
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.Braced:
|
2020-08-22 05:28:54 +08:00
|
|
|
return seqValuesOp{n.Range(), cp.compoundOps(n.Braced)}
|
2016-02-20 00:21:51 +08:00
|
|
|
default:
|
2020-03-29 04:50:41 +08:00
|
|
|
cp.errorpf(n, "bad PrimaryType; parser bug")
|
2020-08-22 05:28:54 +08:00
|
|
|
return literalValues(n, parse.SourceText(n))
|
2020-03-29 05:31:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *compiler) primaryOps(ns []*parse.Primary) []valuesOp {
|
|
|
|
ops := make([]valuesOp, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
ops[i] = cp.primaryOp(n)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2020-03-29 05:31:47 +08:00
|
|
|
return ops
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-01-21 21:20:06 +08:00
|
|
|
type variableOp struct {
|
2020-08-22 05:28:54 +08:00
|
|
|
diag.Ranging
|
2018-01-21 21:20:06 +08:00
|
|
|
explode bool
|
2019-10-23 05:49:32 +08:00
|
|
|
qname string
|
2020-12-25 01:39:51 +08:00
|
|
|
ref *varRef
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op variableOp) exec(fm *Frame) ([]interface{}, error) {
|
2020-12-25 01:39:51 +08:00
|
|
|
variable := deref(fm, op.ref)
|
2018-01-21 21:20:06 +08:00
|
|
|
if variable == nil {
|
2020-08-22 05:28:54 +08:00
|
|
|
return nil, fm.errorpf(op, "variable $%s not found", op.qname)
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
value := variable.Get()
|
|
|
|
if op.explode {
|
2020-08-22 05:28:54 +08:00
|
|
|
vs, err := vals.Collect(value)
|
|
|
|
return vs, fm.errorp(op, err)
|
2016-03-08 09:01:58 +08:00
|
|
|
}
|
2018-01-30 01:39:41 +08:00
|
|
|
return []interface{}{value}, nil
|
2016-03-08 09:01:58 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
type listOp struct {
|
|
|
|
diag.Ranging
|
|
|
|
subops []valuesOp
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op listOp) exec(fm *Frame) ([]interface{}, error) {
|
2018-02-15 17:14:05 +08:00
|
|
|
list := vals.EmptyList
|
2018-01-21 21:24:00 +08:00
|
|
|
for _, subop := range op.subops {
|
2018-09-29 03:45:11 +08:00
|
|
|
moreValues, err := subop.exec(fm)
|
2018-01-21 21:24:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, moreValue := range moreValues {
|
2018-01-28 01:26:22 +08:00
|
|
|
list = list.Cons(moreValue)
|
2018-01-21 21:24:00 +08:00
|
|
|
}
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-01-30 01:39:41 +08:00
|
|
|
return []interface{}{list}, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
type exceptionCaptureOp struct {
|
|
|
|
diag.Ranging
|
|
|
|
subop effectOp
|
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op exceptionCaptureOp) exec(fm *Frame) ([]interface{}, error) {
|
2020-04-10 04:01:14 +08:00
|
|
|
err := op.subop.exec(fm)
|
2018-01-21 21:20:06 +08:00
|
|
|
if err == nil {
|
2018-01-30 01:39:41 +08:00
|
|
|
return []interface{}{OK}, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2021-01-08 08:56:45 +08:00
|
|
|
return []interface{}{err.(*exception)}, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
type outputCaptureOp struct {
|
|
|
|
diag.Ranging
|
|
|
|
subop effectOp
|
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op outputCaptureOp) exec(fm *Frame) ([]interface{}, error) {
|
2021-01-02 09:40:04 +08:00
|
|
|
return fm.CaptureOutput(op.subop.exec)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (cp *compiler) lambda(n *parse.Primary) valuesOp {
|
2017-08-06 04:27:44 +08:00
|
|
|
// Parse signature.
|
|
|
|
var (
|
|
|
|
argNames []string
|
2020-08-20 20:15:00 +08:00
|
|
|
restArg int = -1
|
2017-08-06 04:27:44 +08:00
|
|
|
optNames []string
|
2018-09-29 03:45:11 +08:00
|
|
|
optDefaultOps []valuesOp
|
2017-08-06 04:27:44 +08:00
|
|
|
)
|
2017-08-06 03:49:50 +08:00
|
|
|
if len(n.Elements) > 0 {
|
2017-08-06 04:27:44 +08:00
|
|
|
// Argument list.
|
2017-08-06 03:49:50 +08:00
|
|
|
argNames = make([]string, len(n.Elements))
|
|
|
|
for i, arg := range n.Elements {
|
2019-10-23 05:49:32 +08:00
|
|
|
ref := mustString(cp, arg, "argument name must be literal string")
|
2020-12-26 06:31:52 +08:00
|
|
|
sigil, qname := SplitSigil(ref)
|
|
|
|
name, rest := SplitQName(qname)
|
|
|
|
if rest != "" {
|
2020-03-29 04:34:52 +08:00
|
|
|
cp.errorpf(arg, "argument name must be unqualified")
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
|
|
|
if name == "" {
|
2020-03-29 04:34:52 +08:00
|
|
|
cp.errorpf(arg, "argument name must not be empty")
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
2020-08-20 20:15:00 +08:00
|
|
|
if sigil == "@" {
|
|
|
|
if restArg != -1 {
|
|
|
|
cp.errorpf(arg, "only one argument may have @")
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
2020-08-20 20:15:00 +08:00
|
|
|
restArg = i
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
2020-08-20 20:15:00 +08:00
|
|
|
argNames[i] = name
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2017-08-06 04:27:44 +08:00
|
|
|
if len(n.MapPairs) > 0 {
|
|
|
|
optNames = make([]string, len(n.MapPairs))
|
2018-09-29 03:45:11 +08:00
|
|
|
optDefaultOps = make([]valuesOp, len(n.MapPairs))
|
2017-08-06 04:27:44 +08:00
|
|
|
for i, opt := range n.MapPairs {
|
|
|
|
qname := mustString(cp, opt.Key, "option name must be literal string")
|
2020-12-26 06:31:52 +08:00
|
|
|
name, rest := SplitQName(qname)
|
|
|
|
if rest != "" {
|
2020-03-29 04:34:52 +08:00
|
|
|
cp.errorpf(opt.Key, "option name must be unqualified")
|
2017-08-06 04:27:44 +08:00
|
|
|
}
|
|
|
|
if name == "" {
|
2020-03-29 04:34:52 +08:00
|
|
|
cp.errorpf(opt.Key, "option name must not be empty")
|
2017-08-06 04:27:44 +08:00
|
|
|
}
|
|
|
|
optNames[i] = name
|
|
|
|
if opt.Value == nil {
|
2020-03-29 04:34:52 +08:00
|
|
|
cp.errorpf(opt.Key, "option must have default value")
|
2017-08-06 04:27:44 +08:00
|
|
|
} else {
|
|
|
|
optDefaultOps[i] = cp.compoundOp(opt.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-20 00:21:51 +08:00
|
|
|
|
2021-01-04 21:41:10 +08:00
|
|
|
local, capture := cp.pushScope()
|
2016-02-20 00:21:51 +08:00
|
|
|
for _, argName := range argNames {
|
2021-01-04 21:41:10 +08:00
|
|
|
local.add(argName)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2017-08-06 04:27:44 +08:00
|
|
|
for _, optName := range optNames {
|
2021-01-04 21:41:10 +08:00
|
|
|
local.add(optName)
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
2021-01-04 21:41:10 +08:00
|
|
|
scopeSizeInit := len(local.names)
|
2020-04-27 02:04:49 +08:00
|
|
|
chunkOp := cp.chunkOp(n.Chunk)
|
2021-01-04 21:41:10 +08:00
|
|
|
newLocal := local.names[scopeSizeInit:]
|
2016-02-20 00:21:51 +08:00
|
|
|
cp.popScope()
|
|
|
|
|
2021-01-04 21:41:10 +08:00
|
|
|
return &lambdaOp{n.Range(), argNames, restArg, optNames, optDefaultOps, newLocal, capture, chunkOp, cp.srcMeta}
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
2016-10-11 21:37:27 +08:00
|
|
|
|
2018-01-21 21:20:06 +08:00
|
|
|
type lambdaOp struct {
|
2020-08-22 05:28:54 +08:00
|
|
|
diag.Ranging
|
2018-01-21 21:20:06 +08:00
|
|
|
argNames []string
|
2020-08-20 20:15:00 +08:00
|
|
|
restArg int
|
2018-01-21 21:20:06 +08:00
|
|
|
optNames []string
|
2018-09-29 03:45:11 +08:00
|
|
|
optDefaultOps []valuesOp
|
2021-01-04 21:41:10 +08:00
|
|
|
newLocal []string
|
2020-12-25 01:39:51 +08:00
|
|
|
capture *staticUpNs
|
2018-09-29 03:45:11 +08:00
|
|
|
subop effectOp
|
2020-04-26 02:22:38 +08:00
|
|
|
srcMeta parse.Source
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op *lambdaOp) exec(fm *Frame) ([]interface{}, error) {
|
2020-12-25 01:39:51 +08:00
|
|
|
capture := &Ns{
|
|
|
|
names: op.capture.names, slots: make([]vars.Var, len(op.capture.names))}
|
|
|
|
for i := range op.capture.names {
|
|
|
|
if op.capture.local[i] {
|
|
|
|
capture.slots[i] = fm.local.slots[op.capture.index[i]]
|
|
|
|
} else {
|
|
|
|
capture.slots[i] = fm.up.slots[op.capture.index[i]]
|
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
2018-01-30 01:39:41 +08:00
|
|
|
optDefaults := make([]interface{}, len(op.optDefaultOps))
|
2018-01-21 21:20:06 +08:00
|
|
|
for i, op := range op.optDefaultOps {
|
2020-04-13 20:24:49 +08:00
|
|
|
defaultValue, err := evalForValue(fm, op, "option default value")
|
2018-09-28 03:44:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
optDefaults[i] = defaultValue
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2021-01-04 21:41:10 +08:00
|
|
|
return []interface{}{&closure{op.argNames, op.restArg, op.optNames, optDefaults, op.subop, op.newLocal, capture, op.srcMeta, op.Range()}}, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
type mapOp struct {
|
|
|
|
diag.Ranging
|
|
|
|
pairsOp *mapPairsOp
|
2016-09-15 05:34:10 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op mapOp) exec(fm *Frame) ([]interface{}, error) {
|
|
|
|
m := vals.EmptyMap
|
|
|
|
err := op.pairsOp.exec(fm, func(k, v interface{}) error {
|
|
|
|
m = m.Assoc(k, v)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []interface{}{m}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *compiler) mapPairs(pairs []*parse.MapPair) *mapPairsOp {
|
2016-09-15 05:34:10 +08:00
|
|
|
npairs := len(pairs)
|
2018-09-29 03:45:11 +08:00
|
|
|
keysOps := make([]valuesOp, npairs)
|
|
|
|
valuesOps := make([]valuesOp, npairs)
|
2016-02-20 07:48:13 +08:00
|
|
|
begins, ends := make([]int, npairs), make([]int, npairs)
|
2016-09-15 05:34:10 +08:00
|
|
|
for i, pair := range pairs {
|
2016-02-20 07:48:13 +08:00
|
|
|
keysOps[i] = cp.compoundOp(pair.Key)
|
2016-02-20 00:21:51 +08:00
|
|
|
if pair.Value == nil {
|
2018-10-13 21:07:54 +08:00
|
|
|
p := pair.Range().To
|
2020-08-22 05:28:54 +08:00
|
|
|
valuesOps[i] = literalValues(diag.PointRanging(p), true)
|
2016-02-20 00:21:51 +08:00
|
|
|
} else {
|
2016-09-15 05:34:10 +08:00
|
|
|
valuesOps[i] = cp.compoundOp(pairs[i].Value)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-10-13 21:07:54 +08:00
|
|
|
begins[i], ends[i] = pair.Range().From, pair.Range().To
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
return &mapPairsOp{keysOps, valuesOps, begins, ends}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mapPairsOp struct {
|
2018-09-29 03:45:11 +08:00
|
|
|
keysOps []valuesOp
|
|
|
|
valuesOps []valuesOp
|
2018-01-21 21:20:06 +08:00
|
|
|
begins []int
|
|
|
|
ends []int
|
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op *mapPairsOp) exec(fm *Frame, f func(k, v interface{}) error) error {
|
2018-01-21 21:20:06 +08:00
|
|
|
for i := range op.keysOps {
|
2018-09-29 03:45:11 +08:00
|
|
|
keys, err := op.keysOps[i].exec(fm)
|
2018-01-21 21:20:06 +08:00
|
|
|
if err != nil {
|
2020-08-22 05:28:54 +08:00
|
|
|
return err
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
2018-09-29 03:45:11 +08:00
|
|
|
values, err := op.valuesOps[i].exec(fm)
|
2018-01-21 21:20:06 +08:00
|
|
|
if err != nil {
|
2020-08-22 05:28:54 +08:00
|
|
|
return err
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
if len(keys) != len(values) {
|
2020-08-22 05:28:54 +08:00
|
|
|
return fm.errorpf(diag.Ranging{From: op.begins[i], To: op.ends[i]},
|
2018-01-21 21:20:06 +08:00
|
|
|
"%d keys but %d values", len(keys), len(values))
|
|
|
|
}
|
|
|
|
for j, key := range keys {
|
2020-08-22 05:28:54 +08:00
|
|
|
err := f(key, values[j])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-22 05:28:54 +08:00
|
|
|
return nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
type literalValuesOp struct {
|
|
|
|
diag.Ranging
|
|
|
|
values []interface{}
|
2018-01-21 09:14:39 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op literalValuesOp) exec(*Frame) ([]interface{}, error) {
|
2018-01-21 21:20:06 +08:00
|
|
|
return op.values, nil
|
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func literalValues(r diag.Ranger, vs ...interface{}) valuesOp {
|
|
|
|
return literalValuesOp{r.Range(), vs}
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
type seqValuesOp struct {
|
|
|
|
diag.Ranging
|
|
|
|
subops []valuesOp
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
func (op seqValuesOp) exec(fm *Frame) ([]interface{}, error) {
|
2018-01-30 01:39:41 +08:00
|
|
|
var values []interface{}
|
2018-01-21 21:20:06 +08:00
|
|
|
for _, subop := range op.subops {
|
2018-09-29 03:45:11 +08:00
|
|
|
moreValues, err := subop.exec(fm)
|
2018-01-21 21:20:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-01-21 09:14:39 +08:00
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
values = append(values, moreValues...)
|
2018-01-21 09:14:39 +08:00
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
return values, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2021-01-05 12:07:35 +08:00
|
|
|
|
|
|
|
func evalForValue(fm *Frame, op valuesOp, what string) (interface{}, error) {
|
|
|
|
values, err := op.exec(fm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(values) != 1 {
|
|
|
|
return nil, fm.errorp(op, errs.ArityMismatch{
|
|
|
|
What: what, ValidLow: 1, ValidHigh: 1, Actual: len(values)})
|
|
|
|
}
|
|
|
|
return values[0], nil
|
|
|
|
}
|