elvish/eval/compile_value.go

574 lines
14 KiB
Go
Raw Normal View History

2016-02-20 00:21:51 +08:00
package eval
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path"
"strings"
"sync"
2016-02-20 00:21:51 +08:00
"github.com/elves/elvish/eval/types"
2016-02-20 00:21:51 +08:00
"github.com/elves/elvish/glob"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/util"
"github.com/xiaq/persistent/hashmap"
2016-02-20 00:21:51 +08:00
)
var outputCaptureBufferSize = 16
2018-01-21 06:34:38 +08:00
// ValuesOp is an operation on an Frame that produce Value's.
2016-02-20 07:48:13 +08:00
type ValuesOp struct {
Func ValuesOpFunc
Begin, End int
}
2017-06-11 07:04:53 +08:00
// ValuesOpFunc is the body of ValuesOp.
type ValuesOpFunc func(*Frame) ([]types.Value, error)
2017-06-11 07:04:53 +08:00
// Exec executes a ValuesOp and produces Value's.
func (op ValuesOp) Exec(ec *Frame) ([]types.Value, error) {
2016-02-20 07:48:13 +08:00
ec.begin, ec.end = op.Begin, op.End
return op.Func(ec)
}
func (cp *compiler) compound(n *parse.Compound) ValuesOpFunc {
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 {
return func(ec *Frame) ([]types.Value, error) {
home, err := util.GetHome("")
if err != nil {
return nil, err
}
return []types.Value{types.String(home)}, nil
2016-02-20 00:21:51 +08:00
}
}
tilde = true
indexings = indexings[1:]
}
2016-02-20 07:48:13 +08:00
ops := cp.indexingOps(indexings)
2016-02-20 00:21:51 +08:00
return func(ec *Frame) ([]types.Value, error) {
2016-02-20 00:21:51 +08:00
// Accumulator.
vs, err := ops[0].Exec(ec)
if err != nil {
return nil, err
}
2016-02-20 00:21:51 +08:00
// Logger.Printf("concatenating %v with %d more", vs, len(ops)-1)
for _, op := range ops[1:] {
us, err := op.Exec(ec)
if err != nil {
return nil, err
}
vs, err = outerProduct(vs, us, cat)
if err != nil {
return nil, err
}
2016-02-20 00:21:51 +08:00
}
if tilde {
newvs := make([]types.Value, len(vs))
2016-02-20 00:21:51 +08:00
for i, v := range vs {
newvs[i] = doTilde(v)
}
vs = newvs
}
hasGlob := false
for _, v := range vs {
if _, ok := v.(GlobPattern); ok {
hasGlob = true
break
}
}
if hasGlob {
newvs := make([]types.Value, 0, len(vs))
2016-02-20 00:21:51 +08:00
for _, v := range vs {
if gp, ok := v.(GlobPattern); ok {
// Logger.Printf("globbing %v", gp)
newvs = append(newvs, doGlob(gp, ec.Interrupts())...)
2016-02-20 00:21:51 +08:00
} else {
newvs = append(newvs, v)
}
}
vs = newvs
}
return vs, nil
2016-02-20 00:21:51 +08:00
}
}
func cat(lhs, rhs types.Value) (types.Value, error) {
2016-02-20 00:21:51 +08:00
switch lhs := lhs.(type) {
2018-01-02 09:34:09 +08:00
case types.String:
2016-02-20 00:21:51 +08:00
switch rhs := rhs.(type) {
2018-01-02 09:34:09 +08:00
case types.String:
return lhs + rhs, nil
2016-02-20 00:21:51 +08:00
case GlobPattern:
segs := stringToSegments(string(lhs))
// We know rhs contains exactly one segment.
segs = append(segs, rhs.Segments[0])
return GlobPattern{glob.Pattern{segs, ""}, rhs.Flags, rhs.Buts}, nil
2016-02-20 00:21:51 +08:00
}
case GlobPattern:
// NOTE Modifies lhs in place.
switch rhs := rhs.(type) {
2018-01-02 09:34:09 +08:00
case types.String:
2016-02-20 00:21:51 +08:00
lhs.append(stringToSegments(string(rhs))...)
return lhs, nil
2016-02-20 00:21:51 +08:00
case GlobPattern:
// We know rhs contains exactly one segment.
lhs.append(rhs.Segments[0])
lhs.Flags |= rhs.Flags
lhs.Buts = append(lhs.Buts, rhs.Buts...)
return lhs, nil
2016-02-20 00:21:51 +08:00
}
}
return nil, fmt.Errorf("unsupported concat: %s and %s", lhs.Kind(), rhs.Kind())
2016-02-20 00:21:51 +08:00
}
func outerProduct(vs []types.Value, us []types.Value, f func(types.Value, types.Value) (types.Value, error)) ([]types.Value, error) {
ws := make([]types.Value, 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 {
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
}
}
return ws, nil
2016-02-20 00:21:51 +08:00
}
2017-06-11 07:04:53 +08:00
// Errors thrown when globbing.
var (
ErrBadGlobPattern = errors.New("bad GlobPattern; elvish bug")
ErrCannotDetermineUsername = errors.New("cannot determine user name from glob pattern")
)
func doTilde(v types.Value) types.Value {
2016-02-20 00:21:51 +08:00
switch v := v.(type) {
2018-01-02 09:34:09 +08:00
case types.String:
2016-02-20 00:21:51 +08:00
s := string(v)
i := strings.Index(s, "/")
var uname, rest string
if i == -1 {
uname = s
} else {
uname = s[:i]
rest = s[i+1:]
}
dir := mustGetHome(uname)
2018-01-02 09:34:09 +08:00
return types.String(path.Join(dir, rest))
2016-02-20 00:21:51 +08:00
case GlobPattern:
if len(v.Segments) == 0 {
throw(ErrBadGlobPattern)
2016-02-20 00:21:51 +08:00
}
2017-02-03 03:15:42 +08:00
switch seg := v.Segments[0].(type) {
case glob.Literal:
2017-02-03 03:15:42 +08:00
s := seg.Data
// Find / in the first segment to determine the username.
i := strings.Index(s, "/")
if i == -1 {
throw(ErrCannotDetermineUsername)
}
uname := s[:i]
dir := mustGetHome(uname)
// 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:]}
case glob.Slash:
v.DirOverride = mustGetHome("")
default:
throw(ErrCannotDetermineUsername)
2016-02-20 00:21:51 +08:00
}
return v
default:
throw(fmt.Errorf("tilde doesn't work on value of type %s", v.Kind()))
panic("unreachable")
}
}
2016-02-20 07:48:13 +08:00
func (cp *compiler) array(n *parse.Array) ValuesOpFunc {
return newSeqValuesOp(cp.compoundOps(n.Compounds))
2016-02-20 00:21:51 +08:00
}
2016-02-20 07:48:13 +08:00
func (cp *compiler) indexing(n *parse.Indexing) ValuesOpFunc {
2016-02-20 00:21:51 +08:00
if len(n.Indicies) == 0 {
return cp.primary(n.Head)
}
2016-02-20 07:48:13 +08:00
headOp := cp.primaryOp(n.Head)
indexOps := cp.arrayOps(n.Indicies)
2016-02-20 00:21:51 +08:00
return func(ec *Frame) ([]types.Value, error) {
vs, err := headOp.Exec(ec)
if err != nil {
return nil, err
}
2016-02-20 00:21:51 +08:00
for _, indexOp := range indexOps {
indicies, err := indexOp.Exec(ec)
if err != nil {
return nil, err
}
newvs := make([]types.Value, 0, len(vs)*len(indicies))
2016-02-20 00:21:51 +08:00
for _, v := range vs {
indexer, ok := v.(types.Indexer)
if !ok {
return nil, fmt.Errorf("a %s not indexable", v.Kind())
}
for _, index := range indicies {
result, err := indexer.Index(index)
if err != nil {
return nil, err
}
newvs = append(newvs, result)
}
2016-02-20 00:21:51 +08:00
}
vs = newvs
}
return vs, nil
2016-02-20 00:21:51 +08:00
}
}
func literalValues(v ...types.Value) ValuesOpFunc {
return func(e *Frame) ([]types.Value, error) {
return v, nil
2016-02-20 00:21:51 +08:00
}
}
2016-02-20 07:48:13 +08:00
func literalStr(text string) ValuesOpFunc {
2018-01-02 09:34:09 +08:00
return literalValues(types.String(text))
2016-02-20 00:21:51 +08:00
}
func variable(qname string) ValuesOpFunc {
2017-12-23 22:28:30 +08:00
explode, ns, name := ParseVariable(qname)
return func(ec *Frame) ([]types.Value, error) {
2016-02-20 00:21:51 +08:00
variable := ec.ResolveVar(ns, name)
if variable == nil {
return nil, fmt.Errorf("variable $%s not found", qname)
2016-02-20 00:21:51 +08:00
}
value := variable.Get()
if explode {
iterator, ok := value.(types.Iterator)
2016-02-20 00:21:51 +08:00
if !ok {
// Use qname[1:] to skip the leading "@"
return nil, fmt.Errorf("variable $%s (kind %s) cannot be exploded", qname[1:], value.Kind())
2016-02-20 00:21:51 +08:00
}
return types.CollectFromIterator(iterator), nil
2016-02-20 00:21:51 +08:00
}
return []types.Value{value}, nil
2016-02-20 00:21:51 +08:00
}
}
2016-02-20 07:48:13 +08:00
func (cp *compiler) primary(n *parse.Primary) ValuesOpFunc {
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:
qname := n.Value
if !cp.registerVariableGetQname(qname) {
2016-02-20 04:16:23 +08:00
cp.errorf("variable $%s not found", n.Value)
2016-02-20 00:21:51 +08:00
}
return variable(qname)
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)
}
vs := []types.Value{
2017-06-27 01:47:10 +08:00
GlobPattern{glob.Pattern{[]glob.Segment{seg}, ""}, 0, nil}}
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:
return cp.exceptionCapture(n.Chunk)
2016-02-20 00:21:51 +08:00
case parse.OutputCapture:
return cp.outputCapture(n)
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())
}
}
2017-08-06 03:49:50 +08:00
func (cp *compiler) list(n *parse.Primary) ValuesOpFunc {
// TODO(xiaq): Use Vector.Cons to build the list, instead of building a
// slice and converting to Vector.
op := newSeqValuesOp(cp.compoundOps(n.Elements))
return func(ec *Frame) ([]types.Value, error) {
values, err := op(ec)
if err != nil {
return nil, err
}
return []types.Value{types.MakeList(values...)}, nil
2016-03-08 09:01:58 +08:00
}
}
2017-02-18 12:43:27 +08:00
func (cp *compiler) exceptionCapture(n *parse.Chunk) ValuesOpFunc {
2016-02-20 07:48:13 +08:00
op := cp.chunkOp(n)
return func(ec *Frame) ([]types.Value, error) {
2017-02-16 12:37:13 +08:00
err := ec.PEval(op)
if err == nil {
return []types.Value{OK}, nil
2017-02-16 12:37:13 +08:00
}
return []types.Value{err.(*Exception)}, nil
2016-02-20 00:21:51 +08:00
}
}
2016-02-20 07:48:13 +08:00
func (cp *compiler) outputCapture(n *parse.Primary) ValuesOpFunc {
op := cp.chunkOp(n.Chunk)
return func(ec *Frame) ([]types.Value, error) {
return captureOutput(ec, op), nil
2016-02-20 00:21:51 +08:00
}
}
func captureOutput(ec *Frame, op Op) []types.Value {
vs, err := pcaptureOutput(ec, op)
maybeThrow(err)
return vs
}
func pcaptureOutput(ec *Frame, op Op) ([]types.Value, error) {
vs := []types.Value{}
var m sync.Mutex
valueCb := func(ch <-chan types.Value) {
2016-02-20 00:21:51 +08:00
for v := range ch {
m.Lock()
2016-02-20 00:21:51 +08:00
vs = append(vs, v)
m.Unlock()
2016-02-20 00:21:51 +08:00
}
}
bytesCb := func(r *os.File) {
buffered := bufio.NewReader(r)
2016-02-20 00:21:51 +08:00
for {
line, err := buffered.ReadString('\n')
if line != "" {
2018-01-02 09:34:09 +08:00
v := types.String(strings.TrimSuffix(line, "\n"))
m.Lock()
vs = append(vs, v)
m.Unlock()
}
if err != nil {
if err != io.EOF {
logger.Println("error on reading:", err)
}
2016-02-20 00:21:51 +08:00
break
}
}
}
err := pcaptureOutputInner(ec, op, valueCb, bytesCb)
return vs, err
}
func pcaptureOutputInner(ec *Frame, op Op, valuesCb func(<-chan types.Value), bytesCb func(*os.File)) error {
2017-09-23 05:06:27 +08:00
newEc := ec.fork("[output capture]")
ch := make(chan types.Value, outputCaptureBufferSize)
pipeRead, pipeWrite, err := os.Pipe()
if err != nil {
return fmt.Errorf("failed to create pipe: %v", err)
}
newEc.ports[1] = &Port{
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
}()
err = newEc.PEval(op)
2016-02-20 00:21:51 +08:00
ClosePorts(newEc.ports)
2016-02-20 00:21:51 +08:00
<-bytesCollected
<-chCollected
return err
2016-02-20 00:21:51 +08:00
}
2016-02-20 07:48:13 +08:00
func (cp *compiler) lambda(n *parse.Primary) ValuesOpFunc {
// Parse signature.
var (
argNames []string
restArgName string
optNames []string
optDefaultOps []ValuesOp
)
2017-08-06 03:49:50 +08:00
if len(n.Elements) > 0 {
// Argument list.
2017-08-06 03:49:50 +08:00
argNames = make([]string, len(n.Elements))
for i, arg := range n.Elements {
qname := mustString(cp, arg, "argument name must be literal string")
2017-12-23 22:28:30 +08:00
explode, ns, name := ParseVariable(qname)
if ns != "" {
cp.errorpf(arg.Begin(), arg.End(), "argument name must be unqualified")
}
if name == "" {
cp.errorpf(arg.Begin(), arg.End(), "argument name must not be empty")
}
if explode {
2017-08-06 03:49:50 +08:00
if i != len(n.Elements)-1 {
cp.errorpf(arg.Begin(), arg.End(), "only the last argument may have @")
}
restArgName = name
argNames = argNames[:i]
} else {
argNames[i] = name
}
2016-02-20 00:21:51 +08:00
}
}
if len(n.MapPairs) > 0 {
optNames = make([]string, len(n.MapPairs))
optDefaultOps = make([]ValuesOp, len(n.MapPairs))
for i, opt := range n.MapPairs {
qname := mustString(cp, opt.Key, "option name must be literal string")
2017-12-23 22:28:30 +08:00
_, ns, name := ParseVariable(qname)
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 {
thisScope.set(argName)
2016-02-20 00:21:51 +08:00
}
if restArgName != "" {
thisScope.set(restArgName)
}
for _, optName := range optNames {
thisScope.set(optName)
}
thisScope.set("opts")
2016-02-20 07:48:13 +08:00
op := cp.chunkOp(n.Chunk)
// XXX The fiddlings with cp.capture is error-prone.
2016-02-20 00:21:51 +08:00
capture := cp.capture
cp.capture = make(staticNs)
2016-02-20 00:21:51 +08:00
cp.popScope()
for name := range capture {
cp.registerVariableGetQname(name)
2017-09-21 07:10:21 +08:00
}
2016-02-20 00:21:51 +08:00
srcMeta := cp.srcMeta
return func(ec *Frame) ([]types.Value, error) {
evCapture := make(Ns)
for name := range capture {
evCapture[name] = ec.ResolveVar("", name)
2017-09-21 07:10:21 +08:00
}
optDefaults := make([]types.Value, len(optDefaultOps))
for i, op := range optDefaultOps {
2017-09-05 07:15:06 +08:00
defaultValue := ec.ExecAndUnwrap("option default value", op).One().Any()
optDefaults[i] = defaultValue
}
// XXX(xiaq): Capture uses.
return []types.Value{&Closure{argNames, restArgName, optNames, optDefaults, op, evCapture, srcMeta}}, nil
2016-02-20 00:21:51 +08:00
}
}
2016-02-20 07:48:13 +08:00
func (cp *compiler) map_(n *parse.Primary) ValuesOpFunc {
return cp.mapPairs(n.MapPairs)
}
func (cp *compiler) mapPairs(pairs []*parse.MapPair) ValuesOpFunc {
npairs := len(pairs)
2016-02-20 00:21:51 +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)
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-01-01 23:21:15 +08:00
valuesOps[i] = ValuesOp{literalValues(types.Bool(true)), p, p}
2016-02-20 00:21:51 +08:00
} else {
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
}
return func(ec *Frame) ([]types.Value, error) {
m := hashmap.Empty
2016-02-20 00:21:51 +08:00
for i := 0; i < npairs; i++ {
keys, err := keysOps[i].Exec(ec)
if err != nil {
return nil, err
}
values, err := valuesOps[i].Exec(ec)
if err != nil {
return nil, err
}
2016-02-20 00:21:51 +08:00
if len(keys) != len(values) {
2016-02-20 07:48:13 +08:00
ec.errorpf(begins[i], ends[i],
"%d keys but %d values", len(keys), len(values))
2016-02-20 00:21:51 +08:00
}
for j, key := range keys {
m = m.Assoc(key, values[j])
2016-02-20 00:21:51 +08:00
}
}
return []types.Value{types.NewMap(m)}, nil
2016-02-20 00:21:51 +08:00
}
}
2016-02-20 07:48:13 +08:00
func (cp *compiler) braced(n *parse.Primary) ValuesOpFunc {
ops := cp.compoundOps(n.Braced)
2016-02-20 00:21:51 +08:00
// TODO: n.IsRange
// isRange := n.IsRange
return newSeqValuesOp(ops)
}
func newSeqValuesOp(ops []ValuesOp) ValuesOpFunc {
return func(ec *Frame) ([]types.Value, error) {
var values []types.Value
for _, op := range ops {
moreValues, err := op.Exec(ec)
if err != nil {
return nil, err
}
values = append(values, moreValues...)
}
return values, nil
}
2016-02-20 00:21:51 +08:00
}