Exemplo n.º 1
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)
}
Exemplo n.º 2
0
func execFunctionWithArguments(context *parser.FunctionCallContext, function *parser.Function, name string, popExcess bool, args ...*parser.Value) (*parser.Block, error) {
	switch function.Type {
	case parser.FUNCTION_DECLARED:
		if len(function.Args) != len(args) {
			return nil, util.NewInvalidArgumentError(name, fmt.Sprintf("given function needs %d argument(s), %d specified", len(args), len(function.Args)), context.Token.Line, context.Token.Column, context.Token.File)
		}

		for i := 0; i < len(args); i++ {
			function.D.Scope.SetSymbol(function.Args[i], parser.NewSymbol(args[i], true, false))
		}

		err := function.D.Exec()

		if popExcess {
			for function.D.Stack.Len() > 0 {
				context.Block.Stack.Push(function.D.Stack.Pop())
			}
		}

		return function.D, err
	case parser.FUNCTION_LAMBDA:
		for _, arg := range args {
			function.L.Stack.Push(arg)
		}

		err := function.L.Exec()

		if popExcess {
			for function.L.Stack.Len() > 0 {
				context.Block.Stack.Push(function.L.Stack.Pop())
			}
		}

		return function.L, err
	default:
		return nil, util.NewInvalidArgumentError(name, "cannot handle "+function.Type.String()+" function", context.Token.Line, context.Token.Column, context.Token.File)
	}
}