[a b;c d] desugars to [[a b] [c d]]

This commit is contained in:
Qi Xiao 2016-03-08 02:01:58 +01:00
parent 254d63d55b
commit 1638370417
2 changed files with 29 additions and 4 deletions

View File

@ -269,10 +269,7 @@ func (cp *compiler) primary(n *parse.Primary) ValuesOpFunc {
case parse.OutputCapture: case parse.OutputCapture:
return cp.outputCapture(n) return cp.outputCapture(n)
case parse.List: case parse.List:
op := cp.arrayOp(n.List) return cp.list(n.List)
return func(ec *EvalCtx) []Value {
return []Value{NewList(op.Exec(ec)...)}
}
case parse.Lambda: case parse.Lambda:
return cp.lambda(n) return cp.lambda(n)
case parse.Map: case parse.Map:
@ -285,6 +282,33 @@ func (cp *compiler) primary(n *parse.Primary) ValuesOpFunc {
} }
} }
func (cp *compiler) list(n *parse.Array) ValuesOpFunc {
if len(n.Semicolons) == 0 {
op := cp.arrayOp(n)
return func(ec *EvalCtx) []Value {
return []Value{NewList(op.Exec(ec)...)}
}
} else {
ns := len(n.Semicolons)
rowOps := make([]ValuesOpFunc, ns+1)
f := func(k, i, j int) {
rowOps[k] = catValuesOps(cp.compoundOps(n.Compounds[i:j]))
}
f(0, 0, n.Semicolons[0])
for i := 1; i < ns; i++ {
f(i, n.Semicolons[i-1], n.Semicolons[i])
}
f(ns, n.Semicolons[ns-1], len(n.Compounds))
return func(ec *EvalCtx) []Value {
rows := make([]Value, ns+1)
for i := 0; i <= ns; i++ {
rows[i] = NewList(rowOps[i](ec)...)
}
return []Value{List{&rows}}
}
}
}
func (cp *compiler) errorCapture(n *parse.Chunk) ValuesOpFunc { func (cp *compiler) errorCapture(n *parse.Chunk) ValuesOpFunc {
op := cp.chunkOp(n) op := cp.chunkOp(n)
return func(ec *EvalCtx) []Value { return func(ec *EvalCtx) []Value {

View File

@ -98,6 +98,7 @@ var evalTests = []struct {
{"println [a b c] [&key=value] | from-lines", {"println [a b c] [&key=value] | from-lines",
strs("[a b c] [&key=value]"), nomore}, strs("[a b c] [&key=value]"), nomore},
{"put [a b c][2]", strs("c"), nomore}, {"put [a b c][2]", strs("c"), nomore},
{"put [;a;b c][2][0]", strs("b"), nomore},
{"put [&key=value][key]", strs("value"), nomore}, {"put [&key=value][key]", strs("value"), nomore},
// String literal // String literal