当前位置: 首页>>代码示例>>Golang>>正文


Golang parser.ValidateArguments函数代码示例

本文整理汇总了Golang中github.com/raoulvdberge/stak/parser.ValidateArguments函数的典型用法代码示例。如果您正苦于以下问题:Golang ValidateArguments函数的具体用法?Golang ValidateArguments怎么用?Golang ValidateArguments使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ValidateArguments函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: list

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
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:33,代码来源:lists.go

示例2: listFilter

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
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:35,代码来源:lists.go

示例3: listSeq

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
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:26,代码来源:lists.go

示例4: listMap

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
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:25,代码来源:lists.go

示例5: keep

func keep(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "keep", parser.VALUE_NUMBER)

	if err != nil {
		return err
	}

	n, _ := values[0].N.Float64()

	if int(n) < 0 || int(n) > context.Block.Stack.Len() {
		return util.NewInvalidArgumentError("keep", fmt.Sprintf("cannot keep %d values while stack height is %d", int(n), context.Block.Stack.Len()), context.Token.Line, context.Token.Column, context.Token.File)
	}

	var kept []*parser.Value

	for i := 0; i < int(n); i++ {
		kept = append(kept, context.Block.Stack.Pop().(*parser.Value))
	}

	for context.Block.Stack.Len() > 0 {
		context.Block.Stack.Pop()
	}

	for i := len(kept) - 1; i >= 0; i-- {
		context.Block.Stack.Push(kept[i])
	}

	return nil
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:29,代码来源:std.go

示例6: stringFormat

func stringFormat(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-format", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	format := values[0].S
	count := strings.Count(format, "{}")

	if count > context.Block.Stack.Len() {
		return util.NewArgumentCountError("string-format", count, context.Block.Stack.Len(), context.Token.Line, context.Token.Column, context.Token.File)
	}

	var items []*parser.Value

	for i := 0; i < count; i++ {
		items = append(items, context.Block.Stack.Pop().(*parser.Value))
	}

	for i := count - 1; i >= 0; i-- {
		format = strings.Replace(format, "{}", items[i].String(), 1)
	}

	context.Block.Stack.Push(parser.NewValueFromString(format))

	return nil
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:28,代码来源:strings.go

示例7: stringRange

func stringRange(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-range", parser.VALUE_NUMBER, parser.VALUE_NUMBER, parser.VALUE_STRING)

	if err != nil {
		return err
	}

	beginf, _ := values[1].N.Float64()
	endf, _ := values[0].N.Float64()

	begin := int(beginf)
	end := int(endf)

	s := values[2].S

	if begin < 0 || begin > len(s)-1 {
		return util.NewInvalidArgumentError("string-range", fmt.Sprintf("%d out of range (string length is: %d)", begin, len(s)), context.Token.Line, context.Token.Column, context.Token.File)
	}

	if end < begin {
		return util.NewInvalidArgumentError("string-range", fmt.Sprintf("%d must be bigger then %d", end, begin), context.Token.Line, context.Token.Column, context.Token.File)
	}

	if end < 0 || end > len(s)-1 {
		return util.NewInvalidArgumentError("string-range", fmt.Sprintf("%d out of range (string length is: %d)", end, len(s)), context.Token.Line, context.Token.Column, context.Token.File)
	}

	context.Block.Stack.Push(parser.NewValueFromString(s[begin : end+1]))

	return nil
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:31,代码来源:strings.go

示例8: while_

func while_(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "while", parser.VALUE_FUNCTION, parser.VALUE_FUNCTION)

	if err != nil {
		return err
	}

	check := values[0].F

recheck:
	fakeBlock := parser.NewBlock(nil, nil, nil)

	checkErr := check.Exec(fakeBlock, context.Token)

	if checkErr != nil {
		return checkErr
	}

	if fakeBlock.Stack.Len() != 1 || fakeBlock.Stack.Peek(0).(*parser.Value).Type != parser.VALUE_BOOL {
		return util.NewInvalidArgumentError("while", "the callback function provided to while must return 1 boolean value", context.Token.Line, context.Token.Column, context.Token.File)
	}

	if fakeBlock.Stack.Peek(0).(*parser.Value).B {
		runErr := values[1].F.Exec(context.Block, context.Token)

		if runErr != nil {
			return runErr
		}

		goto recheck
	}

	return nil
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:34,代码来源:std.go

示例9: orN

func orN(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "or-n", parser.VALUE_FUNCTION)

	if err != nil {
		return err
	}

	fakeBlock := parser.NewBlock(nil, nil, nil)

	runErr := values[0].F.Exec(fakeBlock, context.Token)

	if runErr != nil {
		return runErr
	}

	result := false

	if fakeBlock.Stack.Len() >= 1 {
		for fakeBlock.Stack.Len() > 0 {
			value := fakeBlock.Stack.Pop().(*parser.Value)

			if value.Type != parser.VALUE_BOOL {
				return util.NewInvalidArgumentError("or-n", "expected a boolean in the function stack", context.Token.Line, context.Token.Column, context.Token.File)
			}

			result = result || value.B
		}
	} else {
		return util.NewInvalidArgumentError("or-n", "expected at least 2 values to compare in function stack", context.Token.Line, context.Token.Column, context.Token.File)
	}

	context.Block.Stack.Push(parser.NewValueFromBool(result))

	return nil
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:35,代码来源:std.go

示例10: listRange

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
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:25,代码来源:lists.go

示例11: fileRemove

func fileRemove(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "file-remove", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	return os.Remove(values[0].S)
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:9,代码来源:files.go

示例12: call

func call(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "call", parser.VALUE_FUNCTION)

	if err != nil {
		return err
	}

	return values[0].F.Exec(context.Block, context.Token)
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:9,代码来源:std.go

示例13: drop

func drop(context *parser.FunctionCallContext) error {
	_, err := parser.ValidateArguments(context, "drop", parser.VALUE_ANY)

	if err != nil {
		return err
	}

	return nil
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:9,代码来源:std.go

示例14: print_

func print_(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "print", parser.VALUE_ANY)

	if err != nil {
		return err
	}

	fmt.Print(values[0])

	return nil
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:11,代码来源:std.go

示例15: stringAt

func stringAt(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-at", parser.VALUE_STRING, parser.VALUE_STRING)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromInt64(int64(strings.Index(values[1].S, values[0].S))))

	return nil
}
开发者ID:raoulvdberge,项目名称:stak,代码行数:11,代码来源:strings.go


注:本文中的github.com/raoulvdberge/stak/parser.ValidateArguments函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。