eval: Use location information in {,Values}Op.

This also fixes a NullPointerException.
This commit is contained in:
Qi Xiao 2016-02-20 12:25:51 +01:00
parent 91fc4a3419
commit f407db7283
2 changed files with 5 additions and 16 deletions

View File

@ -190,13 +190,12 @@ func (cp *compiler) form(n *parse.Form) OpFunc {
// TODO: n.ErrorRedir
begin, end := n.Begin(), n.End()
headBegin, headEnd := n.Head.Begin(), n.Head.End()
// ec here is always a subevaler created in compiler.pipeline, so it can
// be safely modified.
return func(ec *EvalCtx) {
// head
headValues := headOp.Exec(ec)
ec.must(headValues, "head of command", headBegin, headEnd).mustLen(1)
ec.must(headValues, "head of command", headOp.Begin, headOp.End).mustLen(1)
headCaller := mustCaller(headValues[0])
// args
@ -310,9 +309,6 @@ func (cp *compiler) redir(n *parse.Redir) OpFunc {
mode := n.Mode
flag := makeFlag(mode)
dstBegin, dstEnd := n.Dest.Begin(), n.Dest.End()
srcBegin, srcEnd := n.Source.Begin(), n.Source.End()
return func(ec *EvalCtx) {
var dst int
if dstOp.Func == nil {
@ -328,14 +324,14 @@ func (cp *compiler) redir(n *parse.Redir) OpFunc {
}
} else {
// dst must be a valid fd
dst = ec.must(dstOp.Exec(ec), "FD", dstBegin, dstEnd).mustOneNonNegativeInt()
dst = ec.must(dstOp.Exec(ec), "FD", dstOp.Begin, dstOp.End).mustOneNonNegativeInt()
}
ec.growPorts(dst + 1)
// Logger.Printf("closing old port %d of %s", dst, ec.context)
ec.ports[dst].Close()
srcMust := ec.must(srcOp.Exec(ec), "redirection source", srcBegin, srcEnd)
srcMust := ec.must(srcOp.Exec(ec), "redirection source", srcOp.Begin, srcOp.End)
src := string(srcMust.mustOneStr())
if sourceIsFd {
if src == "-" {

View File

@ -187,11 +187,6 @@ func (cp *compiler) indexing(n *parse.Indexing) ValuesOpFunc {
headOp := cp.primaryOp(n.Head)
indexOps := cp.arrayOps(n.Indicies)
// p := n.Begin()
indexPoses := make([]int, len(n.Indicies))
for i, index := range n.Indicies {
indexPoses[i] = index.Begin()
}
return func(ec *EvalCtx) []Value {
vs := headOp.Exec(ec)
@ -217,7 +212,7 @@ func literalStr(text string) ValuesOpFunc {
return literalValues(String(text))
}
func variable(qname string, p int) ValuesOpFunc {
func variable(qname string) ValuesOpFunc {
splice, ns, name := parseVariable(qname)
return func(ec *EvalCtx) []Value {
variable := ec.ResolveVar(ns, name)
@ -237,7 +232,6 @@ func variable(qname string, p int) ValuesOpFunc {
}
func (cp *compiler) primary(n *parse.Primary) ValuesOpFunc {
cp.compiling(n)
switch n.Type {
case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted:
return literalStr(n.Value)
@ -246,7 +240,7 @@ func (cp *compiler) primary(n *parse.Primary) ValuesOpFunc {
if !cp.registerVariableGet(qname) {
cp.errorf("variable $%s not found", n.Value)
}
return variable(qname, n.Begin())
return variable(qname)
case parse.Wildcard:
vs := []Value{GlobPattern{[]glob.Segment{
wildcardToSegment(n.SourceText())}}}
@ -286,7 +280,6 @@ func (cp *compiler) errorCapture(n *parse.Chunk) ValuesOpFunc {
func (cp *compiler) outputCapture(n *parse.Primary) ValuesOpFunc {
op := cp.chunkOp(n.Chunk)
// p := n.Chunk.Begin()
return func(ec *EvalCtx) []Value {
return captureOutput(ec, op)
}