示例#1
0
func listMap(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "list-map", parser.VALUE_FUNCTION, parser.VALUE_LIST)

	if err != nil {
		return err
	}

	list := parser.NewListValue()

	for _, item := range values[1].L {
		block, err := execFunctionWithArguments(context, values[0].F, "list-map", false, item)

		if err != nil {
			return err
		}

		for block.Stack.Len() > 0 {
			list.L = append(list.L, block.Stack.Pop().(*parser.Value))
		}
	}

	context.Block.Stack.Push(list)

	return nil
}
示例#2
0
func listSeq(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "list-seq", parser.VALUE_NUMBER, parser.VALUE_NUMBER)

	if err != nil {
		return err
	}

	newList := parser.NewListValue()

	min, _ := values[1].N.Float64()
	max, _ := values[0].N.Float64()

	if int(min) > int(max) {
		for i := int(min); i >= int(max); i-- {
			newList.L = append(newList.L, parser.NewValueFromInt64(int64(i)))
		}
	} else {
		for i := int(min); i <= int(max); i++ {
			newList.L = append(newList.L, parser.NewValueFromInt64(int64(i)))
		}
	}

	context.Block.Stack.Push(newList)

	return nil
}
示例#3
0
func listFilter(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "list-filter", parser.VALUE_FUNCTION, parser.VALUE_LIST)

	if err != nil {
		return err
	}

	list := parser.NewListValue()

	for _, item := range values[1].L {
		block, err := execFunctionWithArguments(context, values[0].F, "list-filter", false, item)

		if err != nil {
			return err
		}

		if block.Stack.Len() == 1 {
			b := block.Stack.Pop().(*parser.Value)

			if b.Type != parser.VALUE_BOOL {
				return util.NewInvalidArgumentError("list-filter", "callback function should have a boolean value on the stack, found "+b.Type.String(), context.Token.Line, context.Token.Column, context.Token.File)
			}

			if b.B {
				list.L = append(list.L, item)
			}
		} else {
			return util.NewInvalidArgumentError("list-filter", fmt.Sprintf("callback function needs to exactly have 1 boolean value on top of the stack, found %d", block.Stack.Len()), context.Token.Line, context.Token.Column, context.Token.File)
		}
	}

	context.Block.Stack.Push(list)

	return nil
}
示例#4
0
func listRange(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "list-range", parser.VALUE_NUMBER, parser.VALUE_NUMBER, parser.VALUE_LIST)

	if err != nil {
		return err
	}

	newList := parser.NewListValue()
	list := values[2].L

	start, _ := values[1].N.Float64()
	end, _ := values[0].N.Float64()

	if int(start) > int(end) || int(end) >= len(list) || int(start) < 0 || int(end) < 0 {
		return util.NewInvalidArgumentError("list-range", "invalid bounds", context.Token.Line, context.Token.Column, context.Token.File)
	}

	for i := int(start); i <= int(end); i++ {
		newList.L = append(newList.L, list[i])
	}

	context.Block.Stack.Push(newList)

	return nil
}
示例#5
0
func list(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "list", parser.VALUE_FUNCTION)

	if err != nil {
		return err
	}

	if values[0].F.Type != parser.FUNCTION_LAMBDA {
		return util.NewInvalidArgumentError("list", "expected a lambda", context.Token.Line, context.Token.Column, context.Token.File)
	}

	err = values[0].F.L.Exec()

	if err != nil {
		return err
	}

	var data []*parser.Value

	list := parser.NewListValue()

	for values[0].F.L.Stack.Len() > 0 {
		data = append(data, values[0].F.L.Stack.Pop().(*parser.Value))
	}

	for i := len(data) - 1; i >= 0; i-- {
		list.L = append(list.L, data[i])
	}

	context.Block.Stack.Push(list)

	return nil
}
示例#6
0
文件: os.go 项目: raoulvdberge/stak
func args(context *parser.FunctionCallContext) error {
	list := parser.NewListValue()

	for _, item := range flag.Args() {
		list.L = append(list.L, parser.NewValueFromString(item))
	}

	context.Block.Stack.Push(list)

	return nil
}
示例#7
0
func listReverse(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "list-reverse", parser.VALUE_LIST)

	if err != nil {
		return err
	}

	list := parser.NewListValue()

	for i := len(values[0].L) - 1; i >= 0; i-- {
		list.L = append(list.L, values[0].L[i])
	}

	context.Block.Stack.Push(list)

	return nil
}
示例#8
0
文件: maps.go 项目: raoulvdberge/stak
func mapValues(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "map-values", parser.VALUE_MAP)

	if err != nil {
		return err
	}

	newList := parser.NewListValue()

	for _, value := range values[0].M {
		newList.L = append(newList.L, value)
	}

	context.Block.Stack.Push(newList)

	return nil
}
示例#9
0
func stringSplit(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-split", parser.VALUE_STRING, parser.VALUE_STRING)

	if err != nil {
		return err
	}

	sep := values[0].S
	s := values[1].S

	list := parser.NewListValue()

	for _, item := range strings.Split(s, sep) {
		list.L = append(list.L, parser.NewValueFromString(item))
	}

	context.Block.Stack.Push(list)

	return nil
}
示例#10
0
func fileGlob(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "file-glob", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	matches, err := filepath.Glob(values[0].S)

	if err != nil {
		return err
	}

	list := parser.NewListValue()

	for _, match := range matches {
		list.L = append(list.L, parser.NewValueFromString(match))
	}

	context.Block.Stack.Push(list)

	return nil
}