Exemplo n.º 1
0
func builtinNot(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.BooleanValue); err != nil {
		return nil, err
	}

	return runtime.BooleanValueFor(!context.Args[0].Boolean), nil
}
Exemplo n.º 2
0
func stringsCharacterCheck(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.StringValue); err != nil {
		return nil, err
	}

	s := context.Args[0].Str

	if len(s) != 1 {
		return nil, runtime.NewRuntimeError(context.Pos, "expected string that has 1 character, got %d character(s)", len(s))
	}

	var callback func(rune) bool

	switch context.Name {
	case "is-digit":
		callback = unicode.IsDigit
	case "is-letter":
		callback = unicode.IsLetter
	case "is-lower":
		callback = unicode.IsLower
	case "is-upper":
		callback = unicode.IsUpper
	}

	return runtime.BooleanValueFor(callback(rune(s[0]))), nil
}
Exemplo n.º 3
0
func stringsContains(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.StringValue, runtime.StringValue); err != nil {
		return nil, err
	}

	return runtime.BooleanValueFor(strings.Contains(context.Args[0].Str, context.Args[1].Str)), nil
}
Exemplo n.º 4
0
func builtinMathCmp(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.NumberValue, runtime.NumberValue); err != nil {
		return nil, err
	}

	n1 := context.Args[0].Number
	n2 := context.Args[1].Number

	ok := false

	switch context.Name {
	case ">":
		ok = n1.Cmp(n2) == 1
	case ">=":
		ok = n1.Cmp(n2) >= 0
	case "<":
		ok = n1.Cmp(n2) == -1
	case "<=":
		ok = n1.Cmp(n2) <= 0
	}

	return runtime.BooleanValueFor(ok), nil
}