Пример #1
0
        static ParserFunction GetObjectFunction(string name, ParsingScript script)
        {
            if (script.CurrentClass != null && script.CurrentClass.Name == name)
            {
                script.Backward(name.Length + 1);
                return(new FunctionCreator());
            }
            if (script.ClassInstance != null &&
                (script.ClassInstance.PropertyExists(name) || script.ClassInstance.FunctionExists(name)))
            {
                name = script.ClassInstance.InstanceName + "." + name;
            }
            //int ind = name.LastIndexOf('.');
            int ind = name.IndexOf('.');

            if (ind <= 0)
            {
                return(null);
            }
            string baseName = name.Substring(0, ind);

            if (s_namespaces.ContainsKey(baseName))
            {
                int ind2 = name.IndexOf('.', ind + 1);
                if (ind2 > 0)
                {
                    ind      = ind2;
                    baseName = name.Substring(0, ind);
                }
            }

            string prop = name.Substring(ind + 1);

            ParserFunction pf = ParserFunction.GetFromNamespace(prop, baseName, script);

            if (pf != null)
            {
                return(pf);
            }

            pf = ParserFunction.GetVariable(baseName, script, true);
            if (pf == null || !(pf is GetVarFunction))
            {
                pf = ParserFunction.GetFunction(baseName, script);
                if (pf == null)
                {
                    pf = Utils.ExtractArrayElement(baseName);
                }
            }

            GetVarFunction varFunc = pf as GetVarFunction;

            if (varFunc == null)
            {
                return(null);
            }

            varFunc.PropertyName = prop;
            return(varFunc);
        }
Пример #2
0
        static string TranslateScript(string script, string fromLang, string toLang, ParsingScript parentScript)
        {
            StringBuilder result = new StringBuilder();
            StringBuilder item   = new StringBuilder();

            Dictionary <string, string> keywordsDict = KeywordsDictionary(fromLang, toLang);
            Dictionary <string, string> transDict    = TranslationDictionary(fromLang, toLang);
            bool inQuotes = false;

            for (int i = 0; i < script.Length; i++)
            {
                char ch = script[i];
                inQuotes = ch == Constants.QUOTE ? !inQuotes : inQuotes;

                if (inQuotes)
                {
                    result.Append(ch);
                    continue;
                }
                if (!Constants.TOKEN_SEPARATION.Contains(ch))
                {
                    item.Append(ch);
                    continue;
                }
                if (item.Length > 0)
                {
                    string token       = item.ToString();
                    string translation = string.Empty;
                    if (toLang == Constants.ENGLISH)
                    {
                        ParserFunction func = ParserFunction.GetFunction(token, parentScript);
                        if (func != null)
                        {
                            translation = func.Name;
                        }
                    }
                    if (string.IsNullOrEmpty(translation) &&
                        !keywordsDict.TryGetValue(token, out translation) &&
                        !transDict.TryGetValue(token, out translation))
                    {
                        translation = token;//TryTranslit (fromLang, toLang, token);
                    }
                    result.Append(translation);
                    item.Clear();
                }
                result.Append(ch);
            }

            return(result.ToString());
        }
Пример #3
0
        public static bool ExtractParameterNames(List <Variable> args, string functionName, ParsingScript script)
        {
            CustomFunction custFunc = ParserFunction.GetFunction(functionName, script) as CustomFunction;

            if (custFunc == null)
            {
                return(false);
            }

            var realArgs = custFunc.RealArgs;

            for (int i = 0; i < args.Count && i < realArgs.Length; i++)
            {
                string name = args[i].CurrentAssign;
                args[i].ParamName = string.IsNullOrWhiteSpace(name) ? realArgs[i] : name;
            }
            return(true);
        }
Пример #4
0
        public static Variable RunCompiled(string functionName, string argsString)
        {
            string          adjArgs   = PrepareArgs(argsString, true);
            ParsingScript   argScript = new ParsingScript(adjArgs);
            List <Variable> args      = argScript.GetFunctionArgs();

            ParserFunction function = ParserFunction.GetFunction(functionName, null);

            if (function is CustomCompiledFunction)
            {
                CustomCompiledFunction customFunction = function as CustomCompiledFunction;
                Variable result = customFunction.Run(args);
                if (result != null)
                {
                    return(result);
                }
            }
            return(Calculate(functionName, argsString));
        }
Пример #5
0
        public static void PrintScriptColor(string script, ParsingScript parentSript)
        {
            StringBuilder item = new StringBuilder();

            bool inQuotes = false;

            for (int i = 0; i < script.Length; i++)
            {
                char ch = script[i];
                inQuotes = ch == Constants.QUOTE ? !inQuotes : inQuotes;

                if (inQuotes)
                {
                    Interpreter.Instance.AppendOutput(ch.ToString());
                    continue;
                }
                if (!Constants.TOKEN_SEPARATION.Contains(ch))
                {
                    item.Append(ch);
                    continue;
                }
                if (item.Length > 0)
                {
                    string         token    = item.ToString();
                    ParserFunction func     = ParserFunction.GetFunction(token, parentSript);
                    bool           isNative = Translation.IsNativeWord(token);
                    if (func != null || isNative)
                    {
                        ConsoleColor col = isNative || func.isNative ? ConsoleColor.Green :
                                           func.isGlobal ? ConsoleColor.Magenta :
                                           ConsoleColor.Gray;
                        Utils.PrintColor(token, col);
                    }
                    else
                    {
                        Interpreter.Instance.AppendOutput(token);
                    }
                    item.Clear();
                }
                Interpreter.Instance.AppendOutput(ch.ToString());
            }
        }
Пример #6
0
        public static void Add(NameValueCollection langDictionary, string origName,
                               Dictionary <string, string> translations1,
                               Dictionary <string, string> translations2)
        {
            AddNativeKeyword(origName);

            string translation = langDictionary[origName];

            if (string.IsNullOrWhiteSpace(translation))
            {
                // The translation is not provided for this function.
                translations1[origName] = origName;
                translations2[origName] = origName;
                return;
            }

            AddNativeKeyword(translation);

            translations1[origName]    = translation;
            translations2[translation] = origName;

            if (translation.IndexOfAny((" \t\r\n").ToCharArray()) >= 0)
            {
                throw new ArgumentException("Translation of [" + translation + "] contains white spaces");
            }

            ParserFunction origFunction = ParserFunction.GetFunction(origName, null);

            Utils.CheckNotNull(origName, origFunction, null);
            ParserFunction.RegisterFunction(translation, origFunction);

            // Also add the translation to the list of functions after which there
            // can be a space (besides a parenthesis).
            if (Constants.FUNCT_WITH_SPACE.Contains(origName))
            {
                Constants.FUNCT_WITH_SPACE.Add(translation);
            }
            if (Constants.FUNCT_WITH_SPACE_ONCE.Contains(origName))
            {
                Constants.FUNCT_WITH_SPACE_ONCE.Add(translation);
            }
        }
Пример #7
0
        protected override Variable Evaluate(ParsingScript script)
        {
            List <Variable> args = script.GetFunctionArgs();

            Utils.CheckArgs(args.Count, 2, m_name, true);

            string language = args[0].AsString();
            string funcName = args[1].AsString();

            ParserFunction function = ParserFunction.GetFunction(funcName, script);
            CustomFunction custFunc = function as CustomFunction;

            Utils.CheckNotNull(funcName, custFunc, script);

            string body       = Utils.BeautifyScript(custFunc.Body, custFunc.Header);
            string translated = Translation.TranslateScript(body, language, script);

            Utils.PrintScript(translated, script);

            return(new Variable(translated));
        }
Пример #8
0
        public static bool IsNumericFunction(string paramName, ParsingScript script = null)
        {
            ParserFunction function = ParserFunction.GetFunction(paramName, script);

            return(function is INumericFunction);
        }