예제 #1
0
        /// <summary>
        /// Call a function from the <see cref="functions_dict"/> or <see cref="functions_dict"/> dictionary.
        /// This can be a default function or a custom function
        /// </summary>
        /// <param name="name"> The function name (key)</param>
        /// <param name="args"> The arguments for you function</param>
        /// <returns> Returned value from the function</returns>
        /// <exception cref="AquilaExceptions.FunctionNameError"> Function does not exist</exception>
        public static Variable callFunctionByName(string name, params object[] args)
        {
            if (functions_dict.ContainsKey(name))
            {
                // no new context scope needed, because no function defined here would benefit from it. plus, wouldn't it break some functionalities ? idk

                Context.assertStatus(Context.StatusEnum.predefined_function_call);
                Debugging.print("invoking value function ", name, " dynamically with ", args.Length, " argument(s)");
                return(functions_dict[name].DynamicInvoke(args) as Variable);
            }
            if (user_functions.ContainsKey(name))
            {
                Debugging.print("calling user function: " + name);
                Dictionary <string, Variable> arg_dict = args2Dict(name, args);

                // user-functions: should not be frozen
                // bool unfreeze = Context.tryFreeze();
                Global.newMainContextScope();
                Variable result = user_functions[name].callFunction(arg_dict);
                Global.resetMainContextScope();
                // if (unfreeze) Context.unfreeze();

                return(result);
            }

            throw new AquilaExceptions.FunctionNameError($"Function \"{name}\" does not exist");
        }