Example #1
0
func env(context *parser.FunctionCallContext) error {
	envs := parser.NewMapValue()

	for _, item := range os.Environ() {
		data := strings.Split(item, "=")

		envs.M[parser.NewValueFromString(data[0])] = parser.NewValueFromString(data[1])
	}

	context.Block.Stack.Push(envs)

	return nil
}
Example #2
0
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
}
Example #3
0
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
}
Example #4
0
func setDefaults(scope *parser.Scope) {
	scope.SetSymbol("true", parser.NewSymbol(parser.NewValueFromBool(true), true, false))
	scope.SetSymbol("false", parser.NewSymbol(parser.NewValueFromBool(false), true, false))

	scope.Modules["std"] = parser.NewModule()
	scope.Modules["std"].AddBuiltinFunctions(modules.StdFunctions)

	scope.Modules["os"] = parser.NewModule()
	scope.Modules["os"].AddBuiltinFunctions(modules.OsFunctions)

	scope.Modules["math"] = parser.NewModule()
	scope.Modules["math"].Data["pi"] = parser.NewValueFromFloat64(math.Pi)
	scope.Modules["math"].Data["e"] = parser.NewValueFromFloat64(math.E)
	scope.Modules["math"].AddBuiltinFunctions(modules.MathFunctions)

	scope.Modules["lists"] = parser.NewModule()
	scope.Modules["lists"].AddBuiltinFunctions(modules.ListFunctions)

	scope.Modules["maps"] = parser.NewModule()
	scope.Modules["maps"].AddBuiltinFunctions(modules.MapsFunctions)

	scope.Modules["files"] = parser.NewModule()
	scope.Modules["files"].AddBuiltinFunctions(modules.FilesFunctions)
	scope.Modules["files"].Data["file-separator"] = parser.NewValueFromString(string(os.PathSeparator))

	scope.Modules["strings"] = parser.NewModule()
	scope.Modules["strings"].AddBuiltinFunctions(modules.StringsFunctions)

	scope.Modules["errors"] = parser.NewModule()
	scope.Modules["errors"].AddBuiltinFunctions(modules.ErrorsFunctions)
}
Example #5
0
func stringUpper(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-upper", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromString(strings.ToUpper(values[0].S)))

	return nil
}
Example #6
0
func fileBase(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "file-base", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromString(filepath.Base(values[0].S)))

	return nil
}
Example #7
0
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
}
Example #8
0
func fileWorkingDir(context *parser.FunctionCallContext) error {
	dir, err := os.Getwd()

	if err != nil {
		return err
	}

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

	return nil
}
Example #9
0
func stringCat(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-cat", parser.VALUE_ANY, parser.VALUE_ANY)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromString(values[1].String() + values[0].String()))

	return nil
}
Example #10
0
func stringTrim(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-trim", parser.VALUE_STRING, parser.VALUE_STRING)

	if err != nil {
		return err
	}

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

	context.Block.Stack.Push(parser.NewValueFromString(strings.Trim(s, cutset)))

	return nil
}
Example #11
0
func stringJoin(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-join", parser.VALUE_STRING, parser.VALUE_STRING, parser.VALUE_STRING)

	if err != nil {
		return err
	}

	sep := values[0].S
	s2 := values[1].S
	s1 := values[2].S

	context.Block.Stack.Push(parser.NewValueFromString(s1 + sep + s2))

	return nil
}
Example #12
0
func stringReplace(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-replace", parser.VALUE_STRING, parser.VALUE_STRING, parser.VALUE_STRING)

	if err != nil {
		return err
	}

	replace := values[0].S
	search := values[1].S
	s := values[2].S

	context.Block.Stack.Push(parser.NewValueFromString(strings.Replace(s, search, replace, -1)))

	return nil
}
Example #13
0
func fileReadall(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "file-readall", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	file, err := util.NewFile(values[0].S)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromString(string(file.Contents)))

	return nil
}
Example #14
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
}
Example #15
0
func stringReverse(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-reverse", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	s := values[0].S

	n := len(s)
	runes := make([]rune, n)

	for _, rune := range s {
		n--
		runes[n] = rune
	}

	context.Block.Stack.Push(parser.NewValueFromString(string(runes[n:])))

	return nil
}
Example #16
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
}
Example #17
0
func arch(context *parser.FunctionCallContext) error {
	context.Block.Stack.Push(parser.NewValueFromString(runtime.GOARCH))

	return nil
}