Esempio n. 1
0
        static void NormalizeValue(ParserFunction function)
        {
            GetVarFunction gvf = function as GetVarFunction;

            if (gvf != null)
            {
                gvf.Value.CurrentAssign = "";
            }
        }
Esempio n. 2
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);
            }

            GetVarFunction varFunc = pf as GetVarFunction;

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

            varFunc.PropertyName = prop;
            return(varFunc);
        }
Esempio n. 3
0
        static string CreateVariableEntry(ParserFunction variable, bool isLocal = false)
        {
            if (!(variable is GetVarFunction) || string.IsNullOrWhiteSpace(variable.Name))
            {
                return(null);
            }
            GetVarFunction gvf = variable as GetVarFunction;

            return(CreateVariableEntry(gvf.Value, variable.Name, isLocal));
        }
Esempio n. 4
0
        public void RegisterArguments(List <Variable> args)
        {
            StackLevel stackLevel = new StackLevel(m_name);

            for (int i = 0; i < m_args.Length; i++)
            {
                var arg = new GetVarFunction(args[i]);
                arg.Name = m_args[i];
                stackLevel.Variables[m_args[i]] = arg;
            }

            ParserFunction.AddLocalVariables(stackLevel);
        }
Esempio n. 5
0
        static string CreateVariableEntry(ParserFunction variable, bool isLocal = false)
        {
            if (!(variable is GetVarFunction) || string.IsNullOrWhiteSpace(variable.Name))
            {
                return(null);
            }
            GetVarFunction gvf         = variable as GetVarFunction;
            string         value       = gvf.Value.AsString(true, true, 16);
            string         localGlobal = isLocal ? "0" : "1";
            string         varData     = variable.Name + ":" + localGlobal + ":" +
                                         Constants.TypeToString(gvf.Value.Type).ToLower() + ":" + value;

            return(varData.Trim());
        }
Esempio n. 6
0
        public static bool TryAddToNamespace(string name, string nameSpace, Variable varValue)
        {
            StackLevel level;

            if (string.IsNullOrWhiteSpace(nameSpace) ||
                !s_namespaces.TryGetValue(nameSpace, out level))
            {
                return(false);
            }

            var vars = level.Variables;

            vars[name] = new GetVarFunction(varValue);

            return(true);
        }
Esempio n. 7
0
        static ParserFunction GetArrayFunction(string name, ParsingScript script, string action)
        {
            int arrayStart = name.IndexOf(Constants.START_ARRAY);

            if (arrayStart < 0)
            {
                return(null);
            }

            if (arrayStart == 0)
            {
                Variable arr = Utils.ProcessArrayMap(new ParsingScript(name));
                return(new GetVarFunction(arr));
            }

            string arrayName = name;

            int             delta        = 0;
            List <Variable> arrayIndices = Utils.GetArrayIndices(script, arrayName, delta, (string arr, int del) => { arrayName = arr; delta = del; });

            if (arrayIndices.Count == 0)
            {
                return(null);
            }

            ParserFunction pf      = ParserFunction.GetVariable(arrayName, script);
            GetVarFunction varFunc = pf as GetVarFunction;

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

            // we temporarily backtrack for the processing
            script.Backward(name.Length - arrayStart - 1);
            script.Backward(action != null ? action.Length : 0);
            // delta shows us how manxy chars we need to advance forward in GetVarFunction()
            delta -= arrayName.Length;
            delta += action != null ? action.Length : 0;

            varFunc.Indices = arrayIndices;
            varFunc.Delta   = delta;
            return(varFunc);
        }
Esempio n. 8
0
        public static void AddGlobalOrLocalVariable(string name, GetVarFunction function)
        {
            name = Constants.ConvertName(name);

            Dictionary <string, ParserFunction> lastLevel = GetLastLevel();

            if (lastLevel != null && s_locals.Peek().IsNamespace&& !string.IsNullOrWhiteSpace(s_namespace))
            {
                name = s_namespacePrefix + name;
            }

            function.Name = Constants.GetRealName(name);
            if (s_locals.Count > StackLevelDelta && (LocalNameExists(name) || !GlobalNameExists(name)))
            {
                AddLocalVariable(function);
            }
            else
            {
                AddGlobal(name, function, false /* not native */);
            }
        }
Esempio n. 9
0
        public static void AddGlobalOrLocalVariable(string name, GetVarFunction function,
                                                    ParsingScript script = null, bool localIfPossible = false)
        {
            name = Constants.ConvertName(name);
            if (Constants.CheckReserved(name))
            {
                Utils.ThrowErrorMsg(name + " is a reserved name.", script, name);
            }

            bool globalOnly = !localIfPossible && !LocalNameExists(name);
            Dictionary <string, ParserFunction> lastLevel = GetLastLevel();

            if (!globalOnly && lastLevel != null && s_lastExecutionLevel.IsNamespace && !string.IsNullOrWhiteSpace(s_namespace))
            {
                name = s_namespacePrefix + name;
            }

            function.Name            = Constants.GetRealName(name);
            function.Value.ParamName = function.Name;

            if (!globalOnly && !localIfPossible && script != null && script.StackLevel != null && !GlobalNameExists(name))
            {
                script.StackLevel.Variables[name] = function;
                var handle = OnVariableChange;
                if (handle != null)
                {
                    handle.Invoke(function.Name, function.Value, false);
                }
            }

            if (!globalOnly && s_locals.Count > StackLevelDelta &&
                (localIfPossible || LocalNameExists(name) || !GlobalNameExists(name)))
            {
                AddLocalVariable(function);
            }
            else
            {
                AddGlobal(name, function, false /* not native */);
            }
        }
Esempio n. 10
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.IndexOf(".");

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

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

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

            pf = ParserFunction.GetFunction(baseName, script);
            GetVarFunction varFunc = pf as GetVarFunction;

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

            varFunc.PropertyName = prop;
            return(varFunc);
        }
Esempio n. 11
0
        internal async Task <Variable> ProcessTryAsync(ParsingScript script)
        {
            int       startTryCondition = script.Pointer - 1;
            int       currentStackLevel = ParserFunction.GetCurrentStackLevel();
            Exception exception         = null;

            Variable result = null;

            bool alreadyInTryBlock = script.InTryBlock;

            script.InTryBlock = true;
            try
            {
                result = await ProcessBlockAsync(script);
            }
            catch (Exception exc)
            {
                exception = exc;
            }
            finally
            {
                script.InTryBlock = alreadyInTryBlock;
            }

            if (exception != null || result.IsReturn ||
                result.Type == Variable.VarType.BREAK ||
                result.Type == Variable.VarType.CONTINUE)
            {
                // We are here from the middle of the try-block either because
                // an exception was thrown or because of a Break/Continue. Skip it.
                script.Pointer = startTryCondition;
                SkipBlock(script);
            }

            string catchToken = Utils.GetNextToken(script);

            script.Forward(); // skip opening parenthesis
                              // The next token after the try block must be a catch.
            if (Constants.CATCH != catchToken)
            {
                throw new ArgumentException("Expecting a 'catch()' but got [" +
                                            catchToken + "]");
            }

            string exceptionName = Utils.GetNextToken(script);

            script.Forward(); // skip closing parenthesis

            if (exception != null)
            {
                string excStack = CreateExceptionStack(exceptionName, currentStackLevel);
                ParserFunction.InvalidateStacksAfterLevel(currentStackLevel);

                GetVarFunction excMsgFunc = new GetVarFunction(new Variable(exception.Message));
                ParserFunction.AddGlobalOrLocalVariable(exceptionName, excMsgFunc);
                GetVarFunction excStackFunc = new GetVarFunction(new Variable(excStack));
                ParserFunction.AddGlobalOrLocalVariable(exceptionName + ".Stack", excStackFunc);

                result = await ProcessBlockAsync(script);

                ParserFunction.PopLocalVariable(exceptionName);
            }
            else
            {
                SkipBlock(script);
            }

            SkipRestBlocks(script);
            return(result);
        }