Allowed foreach to pull from the pipe

This commit is contained in:
Leon Mika 2024-04-24 20:12:39 +10:00
parent bfd2785b1c
commit 4c72c733bc
6 changed files with 79 additions and 13 deletions

View file

@ -8,6 +8,7 @@ import (
type astLiteral struct {
Str *string `parser:"@String"`
Int *int `parser:"| @Int"`
}
type astHashKey struct {
@ -69,6 +70,7 @@ var scanner = lexer.MustStateful(lexer.Rules{
"Root": {
{"Whitespace", `[ \t]+`, nil},
{"String", `"(\\"|[^"])*"`, nil},
{"Int", `[-]?[0-9][0-9]*`, nil},
{"DOLLAR", `\$`, nil},
{"COLON", `\:`, nil},
{"LP", `\(`, nil},
@ -79,7 +81,7 @@ var scanner = lexer.MustStateful(lexer.Rules{
{"RC", `\}`, nil},
{"NL", `[;\n][; \n\t]*`, nil},
{"PIPE", `\|`, nil},
{"Ident", `\w+`, nil},
{"Ident", `[-]*[a-zA-Z_][\w-]*`, nil},
},
})
var parser = participle.MustBuild[astScript](participle.Lexer(scanner),

View file

@ -238,28 +238,44 @@ func ifBuiltin(ctx context.Context, args macroArgs) (object, error) {
}
func foreachBuiltin(ctx context.Context, args macroArgs) (object, error) {
var (
items object
blockIdx int
err error
)
if !args.hasPipe {
if args.nargs() < 2 {
return nil, errors.New("need at least 2 arguments")
}
items, err := args.evalArg(ctx, 0)
items, err = args.evalArg(ctx, 0)
if err != nil {
return nil, err
}
blockIdx = 1
} else {
if args.nargs() < 1 {
return nil, errors.New("need at least 1 argument")
}
items = args.pipeArg
blockIdx = 0
}
var last object
switch t := items.(type) {
case listObject:
for _, v := range t {
last, err = args.evalBlock(ctx, 1, []object{v}, true) // TO INCLUDE: the index
case listable:
l := t.Len()
for i := 0; i < l; i++ {
v := t.Index(i)
last, err = args.evalBlock(ctx, blockIdx, []object{v}, true) // TO INCLUDE: the index
if err != nil {
return nil, err
}
}
case hashObject:
for k, v := range t {
last, err = args.evalBlock(ctx, 1, []object{strObject(k), v}, true)
last, err = args.evalBlock(ctx, blockIdx, []object{strObject(k), v}, true)
if err != nil {
return nil, err
}

View file

@ -74,7 +74,7 @@ func (e evaluator) evalCmd(ctx context.Context, ec *evalCtx, currentPipe object,
if cmd := ec.lookupInvokable(name); cmd != nil {
return e.evalInvokable(ctx, ec, currentPipe, ast, cmd)
} else if macro := ec.lookupMacro(name); macro != nil {
return e.evalMacro(ctx, ec, currentPipe, ast, macro)
return e.evalMacro(ctx, ec, currentPipe != nil, currentPipe, ast, macro)
} else {
return nil, errors.New("unknown command: " + name)
}
@ -132,10 +132,11 @@ func (e evaluator) evalInvokable(ctx context.Context, ec *evalCtx, currentPipe o
return cmd.invoke(ctx, invArgs)
}
func (e evaluator) evalMacro(ctx context.Context, ec *evalCtx, pipeArg object, ast *astCmd, cmd macroable) (object, error) {
func (e evaluator) evalMacro(ctx context.Context, ec *evalCtx, hasPipe bool, pipeArg object, ast *astCmd, cmd macroable) (object, error) {
return cmd.invokeMacro(ctx, macroArgs{
eval: e,
ec: ec,
hasPipe: hasPipe,
pipeArg: pipeArg,
ast: ast,
})
@ -217,6 +218,8 @@ func (e evaluator) evalLiteral(ctx context.Context, ec *evalCtx, n *astLiteral)
return nil, err
}
return strObject(uq), nil
case n.Int != nil:
return intObject(*n.Int), nil
}
return nil, errors.New("unhandled literal type")
}

View file

@ -15,6 +15,9 @@ func TestInst_Eval(t *testing.T) {
want any
}{
{desc: "simple string", expr: `firstarg "hello"`, want: "hello"},
{desc: "simple int 1", expr: `firstarg 123`, want: 123},
{desc: "simple int 2", expr: `firstarg -234`, want: -234},
{desc: "simple ident", expr: `firstarg a-test`, want: "a-test"},
// Sub-expressions
{desc: "sub expression 1", expr: `firstarg (sjoin "hello")`, want: "hello"},

View file

@ -69,6 +69,16 @@ func (s strObject) Truthy() bool {
return string(s) != ""
}
type intObject int
func (i intObject) String() string {
return strconv.Itoa(int(i))
}
func (i intObject) Truthy() bool {
return i != 0
}
type boolObject bool
func (b boolObject) String() string {
@ -88,6 +98,8 @@ func toGoValue(obj object) (interface{}, bool) {
return nil, true
case strObject:
return string(v), true
case intObject:
return int(v), true
case listObject:
xs := make([]interface{}, 0, len(v))
for _, va := range v {
@ -127,6 +139,7 @@ func fromGoValue(v any) (object, error) {
type macroArgs struct {
eval evaluator
ec *evalCtx
hasPipe bool
pipeArg object
ast *astCmd
argShift int

View file

@ -132,6 +132,35 @@ func TestInst_SetBuiltin(t *testing.T) {
})
}
})
t.Run("slices returned by commands treated as lists", func(t *testing.T) {
tests := []struct {
descr string
expr string
want any
wantOut string
}{
{descr: "return as is", expr: `countTo3`, want: []string{"1", "2", "3"}},
{descr: "iterate over", expr: `foreach (countTo3) { |x| echo $x }`, wantOut: "1\n2\n3\n"},
{descr: "iterate via foreach", expr: `["2" "4" "6"] | foreach { |x| echo $x }`, wantOut: "2\n4\n6\n"},
}
for _, tt := range tests {
t.Run(tt.descr, func(t *testing.T) {
outW := bytes.NewBuffer(nil)
inst := cmdlang.New(cmdlang.WithOut(outW))
inst.SetBuiltin("countTo3", func(ctx context.Context, args cmdlang.CallArgs) (any, error) {
return []string{"1", "2", "3"}, nil
})
res, err := inst.Eval(context.Background(), tt.expr)
assert.NoError(t, err)
assert.Equal(t, tt.want, res)
assert.Equal(t, tt.wantOut, outW.String())
})
}
})
}
func TestCallArgs_Bind(t *testing.T) {