2016-02-20 00:21:51 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2017-08-13 06:03:28 +08:00
|
|
|
"sync"
|
2016-02-20 00:21:51 +08:00
|
|
|
|
2018-02-15 17:14:05 +08:00
|
|
|
"github.com/elves/elvish/eval/vals"
|
2016-02-20 00:21:51 +08:00
|
|
|
"github.com/elves/elvish/glob"
|
|
|
|
"github.com/elves/elvish/parse"
|
2018-01-21 09:14:39 +08:00
|
|
|
"github.com/elves/elvish/util"
|
2016-02-20 00:21:51 +08:00
|
|
|
)
|
|
|
|
|
2016-02-22 02:25:49 +08:00
|
|
|
var outputCaptureBufferSize = 16
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (cp *compiler) compound(n *parse.Compound) valuesOpBody {
|
2016-02-20 00:21:51 +08:00
|
|
|
if len(n.Indexings) == 0 {
|
|
|
|
return literalStr("")
|
|
|
|
}
|
|
|
|
|
|
|
|
tilde := false
|
|
|
|
indexings := n.Indexings
|
|
|
|
|
|
|
|
if n.Indexings[0].Head.Type == parse.Tilde {
|
|
|
|
// A lone ~.
|
|
|
|
if len(n.Indexings) == 1 {
|
2018-03-01 10:17:56 +08:00
|
|
|
return funcValuesOp(func(fm *Frame) ([]interface{}, error) {
|
2018-01-21 09:14:39 +08:00
|
|
|
home, err := util.GetHome("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-30 01:39:41 +08:00
|
|
|
return []interface{}{home}, nil
|
2018-01-21 21:20:06 +08:00
|
|
|
})
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
tilde = true
|
|
|
|
indexings = indexings[1:]
|
|
|
|
}
|
|
|
|
|
2018-01-21 21:20:06 +08:00
|
|
|
return compoundOp{tilde, cp.indexingOps(indexings)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type compoundOp struct {
|
|
|
|
tilde bool
|
2018-09-29 03:45:11 +08:00
|
|
|
subops []valuesOp
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op compoundOp) invoke(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 {
|
|
|
|
return nil, 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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
if _, ok := v.(GlobPattern); ok {
|
|
|
|
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 {
|
|
|
|
if gp, ok := v.(GlobPattern); ok {
|
2018-09-27 08:48:44 +08:00
|
|
|
results, err := doGlob(gp, fm.Interrupts())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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 (
|
|
|
|
ErrBadGlobPattern = errors.New("bad GlobPattern; elvish bug")
|
|
|
|
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:]
|
|
|
|
}
|
2018-09-28 05:19:47 +08:00
|
|
|
dir, err := util.GetHome(uname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-27 22:32:41 +08:00
|
|
|
return path.Join(dir, rest), nil
|
2016-02-20 00:21:51 +08:00
|
|
|
case GlobPattern:
|
2016-02-22 23:05:14 +08:00
|
|
|
if len(v.Segments) == 0 {
|
2018-09-27 22:32:41 +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:
|
2017-02-03 03:15:42 +08:00
|
|
|
s := seg.Data
|
2016-02-22 23:05:14 +08:00
|
|
|
// Find / in the first segment to determine the username.
|
|
|
|
i := strings.Index(s, "/")
|
|
|
|
if i == -1 {
|
2018-09-27 22:32:41 +08:00
|
|
|
return nil, ErrCannotDetermineUsername
|
2016-02-22 23:05:14 +08:00
|
|
|
}
|
|
|
|
uname := s[:i]
|
2018-09-28 05:19:47 +08:00
|
|
|
dir, err := util.GetHome(uname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-22 23:05:14 +08:00
|
|
|
// Replace ~uname in first segment with the found path.
|
2017-02-03 03:15:42 +08:00
|
|
|
v.Segments[0] = glob.Literal{dir + s[i:]}
|
2016-02-22 23:05:14 +08:00
|
|
|
case glob.Slash:
|
2018-09-28 05:19:47 +08:00
|
|
|
dir, err := util.GetHome("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
v.DirOverride = dir
|
2016-02-22 23:05:14 +08:00
|
|
|
default:
|
2018-09-27 22:32:41 +08:00
|
|
|
return nil, ErrCannotDetermineUsername
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-09-27 22:32:41 +08:00
|
|
|
return v, nil
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (cp *compiler) array(n *parse.Array) valuesOpBody {
|
2018-01-21 21:20:06 +08:00
|
|
|
return seqValuesOp{cp.compoundOps(n.Compounds)}
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (cp *compiler) indexing(n *parse.Indexing) valuesOpBody {
|
2016-02-20 00:21:51 +08:00
|
|
|
if len(n.Indicies) == 0 {
|
|
|
|
return cp.primary(n.Head)
|
|
|
|
}
|
|
|
|
|
2018-01-21 21:20:06 +08:00
|
|
|
return &indexingOp{cp.primaryOp(n.Head), cp.arrayOps(n.Indicies)}
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-01-21 21:20:06 +08:00
|
|
|
type indexingOp struct {
|
2018-09-29 03:45:11 +08:00
|
|
|
headOp valuesOp
|
|
|
|
indexOps []valuesOp
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op *indexingOp) invoke(fm *Frame) ([]interface{}, error) {
|
|
|
|
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 {
|
2018-09-29 03:45:11 +08:00
|
|
|
indicies, 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
|
|
|
}
|
2018-01-30 01:39:41 +08:00
|
|
|
newvs := make([]interface{}, 0, len(vs)*len(indicies))
|
2018-01-21 21:20:06 +08:00
|
|
|
for _, v := range vs {
|
|
|
|
for _, index := range indicies {
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (cp *compiler) primary(n *parse.Primary) valuesOpBody {
|
2016-02-20 00:21:51 +08:00
|
|
|
switch n.Type {
|
|
|
|
case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted:
|
|
|
|
return literalStr(n.Value)
|
|
|
|
case parse.Variable:
|
2018-01-30 01:29:38 +08:00
|
|
|
explode, ns, name := ParseVariableRef(n.Value)
|
2018-01-21 21:20:06 +08:00
|
|
|
if !cp.registerVariableGet(ns, name) {
|
2016-02-20 04:16:23 +08:00
|
|
|
cp.errorf("variable $%s not found", n.Value)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
return &variableOp{explode, ns, name}
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.Wildcard:
|
2017-06-27 01:47:10 +08:00
|
|
|
seg, err := wildcardToSegment(n.SourceText())
|
|
|
|
if err != nil {
|
|
|
|
cp.errorf("%s", err)
|
|
|
|
}
|
2018-01-30 01:39:41 +08:00
|
|
|
vs := []interface{}{
|
2017-06-27 01:47:10 +08:00
|
|
|
GlobPattern{glob.Pattern{[]glob.Segment{seg}, ""}, 0, nil}}
|
2018-01-21 09:14:39 +08:00
|
|
|
return literalValues(vs...)
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.Tilde:
|
2016-02-20 04:16:23 +08:00
|
|
|
cp.errorf("compiler bug: Tilde not handled in .compound")
|
2016-02-20 00:21:51 +08:00
|
|
|
return literalStr("~")
|
2017-02-18 12:43:27 +08:00
|
|
|
case parse.ExceptionCapture:
|
2018-01-21 21:20:06 +08:00
|
|
|
return exceptionCaptureOp{cp.chunkOp(n.Chunk)}
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.OutputCapture:
|
2018-01-21 21:20:06 +08:00
|
|
|
return outputCaptureOp{cp.chunkOp(n.Chunk)}
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.List:
|
2017-08-06 03:49:50 +08:00
|
|
|
return cp.list(n)
|
2016-02-20 00:21:51 +08:00
|
|
|
case parse.Lambda:
|
|
|
|
return cp.lambda(n)
|
|
|
|
case parse.Map:
|
|
|
|
return cp.map_(n)
|
|
|
|
case parse.Braced:
|
|
|
|
return cp.braced(n)
|
|
|
|
default:
|
2016-02-20 04:16:23 +08:00
|
|
|
cp.errorf("bad PrimaryType; parser bug")
|
2016-02-20 00:21:51 +08:00
|
|
|
return literalStr(n.SourceText())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 21:20:06 +08:00
|
|
|
type variableOp struct {
|
|
|
|
explode bool
|
|
|
|
ns string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op variableOp) invoke(fm *Frame) ([]interface{}, error) {
|
2018-03-01 10:17:56 +08:00
|
|
|
variable := fm.ResolveVar(op.ns, op.name)
|
2018-01-21 21:20:06 +08:00
|
|
|
if variable == nil {
|
|
|
|
return nil, fmt.Errorf("variable $%s:%s not found", op.ns, op.name)
|
|
|
|
}
|
|
|
|
value := variable.Get()
|
|
|
|
if op.explode {
|
2018-02-15 17:14:05 +08:00
|
|
|
return vals.Collect(value)
|
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
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (cp *compiler) list(n *parse.Primary) valuesOpBody {
|
2018-01-21 21:24:00 +08:00
|
|
|
return listOp{cp.compoundOps(n.Elements)}
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
type listOp struct{ subops []valuesOp }
|
2018-01-21 21:20:06 +08:00
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op listOp) invoke(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
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
type exceptionCaptureOp struct{ subop effectOp }
|
2018-01-21 21:20:06 +08:00
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op exceptionCaptureOp) invoke(fm *Frame) ([]interface{}, error) {
|
2018-10-12 19:57:41 +08:00
|
|
|
err := fm.eval(op.subop)
|
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
|
|
|
}
|
2018-01-30 01:39:41 +08:00
|
|
|
return []interface{}{err.(*Exception)}, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
type outputCaptureOp struct{ subop effectOp }
|
2018-01-21 21:20:06 +08:00
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op outputCaptureOp) invoke(fm *Frame) ([]interface{}, error) {
|
2018-01-21 21:20:06 +08:00
|
|
|
return pcaptureOutput(fm, op.subop)
|
2016-10-22 21:02:27 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func pcaptureOutput(fm *Frame, op effectOp) ([]interface{}, error) {
|
2018-01-30 01:39:41 +08:00
|
|
|
vs := []interface{}{}
|
2017-08-13 06:03:28 +08:00
|
|
|
var m sync.Mutex
|
2018-01-30 01:39:41 +08:00
|
|
|
valueCb := func(ch <-chan interface{}) {
|
2016-02-20 00:21:51 +08:00
|
|
|
for v := range ch {
|
2017-08-13 06:03:28 +08:00
|
|
|
m.Lock()
|
2016-02-20 00:21:51 +08:00
|
|
|
vs = append(vs, v)
|
2017-08-13 06:03:28 +08:00
|
|
|
m.Unlock()
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2017-08-13 06:03:28 +08:00
|
|
|
}
|
|
|
|
bytesCb := func(r *os.File) {
|
|
|
|
buffered := bufio.NewReader(r)
|
2016-02-20 00:21:51 +08:00
|
|
|
for {
|
2017-08-13 06:03:28 +08:00
|
|
|
line, err := buffered.ReadString('\n')
|
2017-07-11 01:00:32 +08:00
|
|
|
if line != "" {
|
2018-01-25 09:40:15 +08:00
|
|
|
v := strings.TrimSuffix(line, "\n")
|
2017-08-13 06:03:28 +08:00
|
|
|
m.Lock()
|
|
|
|
vs = append(vs, v)
|
|
|
|
m.Unlock()
|
2017-07-11 01:00:32 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
logger.Println("error on reading:", err)
|
|
|
|
}
|
2016-02-20 00:21:51 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 06:03:28 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 10:17:56 +08:00
|
|
|
err := pcaptureOutputInner(fm, op, valueCb, bytesCb)
|
2017-08-13 06:03:28 +08:00
|
|
|
return vs, err
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func pcaptureOutputInner(fm *Frame, op effectOp, valuesCb func(<-chan interface{}), bytesCb func(*os.File)) error {
|
2017-08-13 06:03:28 +08:00
|
|
|
|
2018-03-01 10:17:56 +08:00
|
|
|
newFm := fm.fork("[output capture]")
|
2017-08-13 06:03:28 +08:00
|
|
|
|
2018-01-30 01:39:41 +08:00
|
|
|
ch := make(chan interface{}, outputCaptureBufferSize)
|
2017-08-13 06:03:28 +08:00
|
|
|
pipeRead, pipeWrite, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create pipe: %v", err)
|
|
|
|
}
|
2018-03-01 10:17:56 +08:00
|
|
|
newFm.ports[1] = &Port{
|
2017-08-13 06:03:28 +08:00
|
|
|
Chan: ch, CloseChan: true,
|
|
|
|
File: pipeWrite, CloseFile: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesCollected := make(chan struct{})
|
|
|
|
chCollected := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
valuesCb(ch)
|
|
|
|
close(chCollected)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
bytesCb(pipeRead)
|
|
|
|
pipeRead.Close()
|
|
|
|
close(bytesCollected)
|
2016-02-20 00:21:51 +08:00
|
|
|
}()
|
|
|
|
|
2018-10-12 19:57:41 +08:00
|
|
|
err = newFm.eval(op)
|
2016-02-20 00:21:51 +08:00
|
|
|
|
2018-03-01 10:17:56 +08:00
|
|
|
newFm.Close()
|
2016-02-20 00:21:51 +08:00
|
|
|
<-bytesCollected
|
|
|
|
<-chCollected
|
|
|
|
|
2017-08-13 06:03:28 +08:00
|
|
|
return err
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (cp *compiler) lambda(n *parse.Primary) valuesOpBody {
|
2017-08-06 04:27:44 +08:00
|
|
|
// Parse signature.
|
|
|
|
var (
|
|
|
|
argNames []string
|
|
|
|
restArgName string
|
|
|
|
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 {
|
2017-08-06 04:27:44 +08:00
|
|
|
qname := mustString(cp, arg, "argument name must be literal string")
|
2018-01-30 01:29:38 +08:00
|
|
|
explode, ns, name := ParseVariableRef(qname)
|
2016-02-26 08:54:27 +08:00
|
|
|
if ns != "" {
|
2017-08-06 04:27:44 +08:00
|
|
|
cp.errorpf(arg.Begin(), arg.End(), "argument name must be unqualified")
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
cp.errorpf(arg.Begin(), arg.End(), "argument name must not be empty")
|
|
|
|
}
|
2016-10-13 14:10:10 +08:00
|
|
|
if explode {
|
2017-08-06 03:49:50 +08:00
|
|
|
if i != len(n.Elements)-1 {
|
2016-02-26 08:54:27 +08:00
|
|
|
cp.errorpf(arg.Begin(), arg.End(), "only the last argument may have @")
|
|
|
|
}
|
2017-08-06 04:27:44 +08:00
|
|
|
restArgName = name
|
2016-02-26 08:54:27 +08:00
|
|
|
argNames = argNames[:i]
|
|
|
|
} else {
|
|
|
|
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")
|
2018-01-30 01:29:38 +08:00
|
|
|
_, ns, name := ParseVariableRef(qname)
|
2017-08-06 04:27:44 +08:00
|
|
|
if ns != "" {
|
|
|
|
cp.errorpf(opt.Key.Begin(), opt.Key.End(), "option name must be unqualified")
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
cp.errorpf(opt.Key.Begin(), opt.Key.End(), "option name must not be empty")
|
|
|
|
}
|
|
|
|
optNames[i] = name
|
|
|
|
if opt.Value == nil {
|
|
|
|
cp.errorpf(opt.End(), opt.End(), "option must have default value")
|
|
|
|
} else {
|
|
|
|
optDefaultOps[i] = cp.compoundOp(opt.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-20 00:21:51 +08:00
|
|
|
|
|
|
|
thisScope := cp.pushScope()
|
|
|
|
for _, argName := range argNames {
|
2017-12-24 07:51:32 +08:00
|
|
|
thisScope.set(argName)
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
2017-08-06 04:27:44 +08:00
|
|
|
if restArgName != "" {
|
2017-12-24 07:51:32 +08:00
|
|
|
thisScope.set(restArgName)
|
2017-08-06 04:27:44 +08:00
|
|
|
}
|
|
|
|
for _, optName := range optNames {
|
2017-12-24 07:51:32 +08:00
|
|
|
thisScope.set(optName)
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
2017-08-06 04:27:44 +08:00
|
|
|
|
2018-01-21 21:20:06 +08:00
|
|
|
subop := cp.chunkOp(n.Chunk)
|
2017-08-06 04:27:44 +08:00
|
|
|
|
|
|
|
// XXX The fiddlings with cp.capture is error-prone.
|
2016-02-20 00:21:51 +08:00
|
|
|
capture := cp.capture
|
2017-12-24 07:51:32 +08:00
|
|
|
cp.capture = make(staticNs)
|
2016-02-20 00:21:51 +08:00
|
|
|
cp.popScope()
|
|
|
|
|
2017-12-24 07:51:32 +08:00
|
|
|
for name := range capture {
|
|
|
|
cp.registerVariableGetQname(name)
|
2017-09-21 07:10:21 +08:00
|
|
|
}
|
2016-02-20 00:21:51 +08:00
|
|
|
|
2018-03-22 06:54:47 +08:00
|
|
|
return &lambdaOp{argNames, restArgName, optNames, optDefaultOps, capture, subop, cp.srcMeta, n.Begin(), n.End()}
|
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 {
|
|
|
|
argNames []string
|
|
|
|
restArgName string
|
|
|
|
optNames []string
|
2018-09-29 03:45:11 +08:00
|
|
|
optDefaultOps []valuesOp
|
2018-01-21 21:20:06 +08:00
|
|
|
capture staticNs
|
2018-09-29 03:45:11 +08:00
|
|
|
subop effectOp
|
2018-01-21 21:20:06 +08:00
|
|
|
srcMeta *Source
|
2018-03-22 06:54:47 +08:00
|
|
|
defBegin int
|
|
|
|
defEnd int
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op *lambdaOp) invoke(fm *Frame) ([]interface{}, error) {
|
2018-01-21 21:20:06 +08:00
|
|
|
evCapture := make(Ns)
|
|
|
|
for name := range op.capture {
|
|
|
|
evCapture[name] = fm.ResolveVar("", name)
|
|
|
|
}
|
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 {
|
2018-09-28 03:44:46 +08:00
|
|
|
defaultValue, err := fm.ExecAndUnwrap("option default value", op).One().Any()
|
|
|
|
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
|
|
|
}
|
2018-03-22 06:54:47 +08:00
|
|
|
return []interface{}{&Closure{op.argNames, op.restArgName, op.optNames, optDefaults, op.subop, evCapture, op.srcMeta, op.defBegin, op.defEnd}}, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (cp *compiler) map_(n *parse.Primary) valuesOpBody {
|
2016-09-15 05:34:10 +08:00
|
|
|
return cp.mapPairs(n.MapPairs)
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (cp *compiler) mapPairs(pairs []*parse.MapPair) valuesOpBody {
|
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 {
|
2016-02-20 07:48:13 +08:00
|
|
|
p := pair.End()
|
2018-09-29 03:45:11 +08:00
|
|
|
valuesOps[i] = valuesOp{literalValues(true), p, p}
|
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
|
|
|
}
|
2016-02-20 07:48:13 +08:00
|
|
|
begins[i], ends[i] = pair.Begin(), pair.End()
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op *mapPairsOp) invoke(fm *Frame) ([]interface{}, error) {
|
2018-02-15 17:14:05 +08:00
|
|
|
m := vals.EmptyMap
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(keys) != len(values) {
|
2018-09-28 01:50:53 +08:00
|
|
|
return nil, fm.errorpf(op.begins[i], 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 {
|
|
|
|
m = m.Assoc(key, values[j])
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:39:41 +08:00
|
|
|
return []interface{}{m}, nil
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (cp *compiler) braced(n *parse.Primary) valuesOpBody {
|
2016-02-20 07:48:13 +08:00
|
|
|
ops := cp.compoundOps(n.Braced)
|
2016-02-20 00:21:51 +08:00
|
|
|
// TODO: n.IsRange
|
|
|
|
// isRange := n.IsRange
|
2018-01-21 21:20:06 +08:00
|
|
|
return seqValuesOp{ops}
|
2018-01-21 09:14:39 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 01:39:41 +08:00
|
|
|
type literalValuesOp struct{ values []interface{} }
|
2018-01-21 21:20:06 +08:00
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op literalValuesOp) invoke(*Frame) ([]interface{}, error) {
|
2018-01-21 21:20:06 +08:00
|
|
|
return op.values, nil
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func literalValues(v ...interface{}) valuesOpBody {
|
2018-01-21 21:20:06 +08:00
|
|
|
return literalValuesOp{v}
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func literalStr(text string) valuesOpBody {
|
2018-01-25 09:40:15 +08:00
|
|
|
return literalValues(text)
|
2018-01-21 21:20:06 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
type seqValuesOp struct{ subops []valuesOp }
|
2018-01-21 21:20:06 +08:00
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op seqValuesOp) invoke(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
|
|
|
}
|
2018-01-21 21:20:06 +08:00
|
|
|
|
2018-01-30 01:39:41 +08:00
|
|
|
type funcValuesOp func(*Frame) ([]interface{}, error)
|
2018-01-21 21:20:06 +08:00
|
|
|
|
2018-09-29 03:45:11 +08:00
|
|
|
func (op funcValuesOp) invoke(fm *Frame) ([]interface{}, error) { return op(fm) }
|