コード例 #1
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(data, ref from, Constants.END_ARG_ARRAY);

            if (from >= data.Length)
            {
                throw new ArgumentException("Couldn't get variable");
            }

            // 2. Get the current value of the variable.
            ParserFunction func = ParserFunction.GetFunction(varName);

            Parser.Result currentValue = func.GetValue(data, ref from);

            // 3. Take either the length of the underlying tuple or
            // string part if it is defined,
            // or the numerical part converted to a string otherwise.
            int size = currentValue.Tuple != null ?  currentValue.Tuple.Count :
                       currentValue.String != null ? currentValue.String.Length :
                       currentValue.Value.ToString().Length;

            Parser.Result newValue = new Parser.Result(size);
            return(newValue);
        }
コード例 #2
0
        internal bool EvalCondition(Parser.Result arg1, string comparison, Parser.Result arg2)
        {
            bool compare = arg1.String != null?CompareStrings(arg1.String, comparison, arg2.String) :
                               CompareNumbers(arg1.Value, comparison, arg2.Value);

            return(compare);
        }
コード例 #3
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(data, ref from, Constants.NEXT_ARG_ARRAY);

            if (from >= data.Length)
            {
                throw new ArgumentException("Couldn't append variable");
            }

            // 2. Get the current value of the variable.
            ParserFunction func = ParserFunction.GetFunction(varName);

            Parser.Result currentValue = func.GetValue(data, ref from);

            // 3. Get the value to be looked for.
            Parser.Result searchValue = Utils.GetItem(data, ref from);

            // 4. Take either the string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string basePart = currentValue.String != null ? currentValue.String :
                              currentValue.Value.ToString();
            string search = searchValue.String != null ? searchValue.String :
                            searchValue.Value.ToString();

            int result = basePart.IndexOf(search);

            return(new Parser.Result(result, null));
        }
コード例 #4
0
        public static int ExtractArrayElement(ref string varName)
        {
            int argStart = varName.IndexOf(Constants.START_ARG);

            if (argStart <= 0)
            {
                return(-1);
            }

            int argEnd = varName.IndexOf(Constants.END_ARG, argStart + 1);

            if (argEnd <= argStart + 1)
            {
                return(-1);
            }

            int getIndexFrom = argStart;

            Parser.Result existing = Parser.LoadAndCalculate(varName, ref getIndexFrom,
                                                             Constants.NEXT_OR_END_ARRAY);

            if (!Double.IsNaN(existing.Value) && existing.Value >= 0)
            {
                varName = varName.Substring(0, argStart);
                return((int)existing.Value);
            }

            return(-1);
        }
コード例 #5
0
        public static List <string> GetFunctionArgs(string data, ref int from)
        {
            List <string> args     = new List <string>();
            bool          moreArgs = true;

            while (moreArgs)
            {
                moreArgs = Utils.SeparatorExists(data, from);
                Parser.Result item = Utils.GetItem(data, ref from);

                // Separate treatment for an array.
                // Only 1-dimensional arrays are supported at the moment.
                if (item.Tuple != null && item.Tuple.Count > 0)
                {
                    for (int i = 0; i < item.Tuple.Count; i++)
                    {
                        Parser.Result arg = item.Tuple[i];
                        args.Add((arg.String != null ? arg.String : arg.Value.ToString()) + '\n');
                    }
                    continue;
                }

                if (item.String == null && Double.IsNaN(item.Value))
                {
                    break;
                }
                args.Add(item.String != null ? item.String : item.Value.ToString());
            }

            return(args);
        }
コード例 #6
0
        internal Parser.Result ProcessBlock()
        {
            int blockStart = m_currentChar;

            Parser.Result result = null;

            while (true)
            {
                int endGroupRead = Utils.GoToNextStatement(m_data, ref m_currentChar);
                if (endGroupRead > 0)
                {
                    return(result != null ? result : new Parser.Result());
                }

                if (m_currentChar >= m_data.Length)
                {
                    throw new ArgumentException("Couldn't process block [" +
                                                m_data.Substring(blockStart) + "]");
                }

                result = Parser.LoadAndCalculate(m_data, ref m_currentChar, Constants.END_PARSE_ARRAY);

                if (result is Continue || result is Break)
                {
                    return(result);
                }
            }
        }
コード例 #7
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(data, ref from, Constants.NEXT_ARG_ARRAY);

            if (from >= data.Length)
            {
                throw new ArgumentException("Couldn't append variable");
            }

            // 2. Get the current value of the variable.
            ParserFunction func = ParserFunction.GetFunction(varName);

            Parser.Result currentValue = func.GetValue(data, ref from);

            // 3. Get the value to be added or appended.
            Parser.Result newValue = Utils.GetItem(data, ref from);

            // 4. Take either the string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string arg1 = currentValue.String != null ? currentValue.String :
                          currentValue.Value.ToString();
            string arg2 = newValue.String != null ? newValue.String :
                          newValue.Value.ToString();

            // 5. The variable becomes a string after adding a string to it.
            newValue.Reset();
            newValue.String = arg1 + arg2;

            ParserFunction.AddFunction(varName, new GetVarFunction(newValue));

            return(newValue);
        }
コード例 #8
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            m_interpreter.CurrentChar = from;

            Parser.Result result = m_interpreter.ProcessIf();

            return(result);
        }
コード例 #9
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            Parser.Result arg1 = Parser.LoadAndCalculate(data, ref from, Constants.NEXT_ARG_ARRAY);
            from++; // eat separation
            Parser.Result arg2 = Parser.LoadAndCalculate(data, ref from, Constants.END_ARG_ARRAY);

            arg1.Value = Math.Pow(arg1.Value, arg2.Value);
            return(arg1);
        }
コード例 #10
0
        internal void ProcessWhile()
        {
            int startWhileCondition = m_currentChar;

            // A heuristic check against an infinite loop.
            int cycles = 0;
            int START_CHECK_INF_LOOP = CHECK_AFTER_LOOPS / 2;

            Parser.Result argCache1 = null;
            Parser.Result argCache2 = null;

            bool stillValid = true;

            while (stillValid)
            {
                m_currentChar = startWhileCondition;

                Parser.Result arg1       = GetNextIfToken();
                string        comparison = Utils.GetComparison(m_data, ref m_currentChar);
                Parser.Result arg2       = GetNextIfToken();

                stillValid = EvalCondition(arg1, comparison, arg2);
                int startSkipOnBreakChar = m_currentChar;

                if (!stillValid)
                {
                    break;
                }

                // Check for an infinite loop if we are comparing same values:
                if (++cycles % START_CHECK_INF_LOOP == 0)
                {
                    if (cycles >= MAX_LOOPS || (arg1.IsEqual(argCache1) && arg2.IsEqual(argCache2)))
                    {
                        throw new ArgumentException("Looks like an infinite loop after " +
                                                    cycles + " cycles.");
                    }
                    argCache1 = arg1;
                    argCache2 = arg2;
                }

                Parser.Result result = ProcessBlock();
                if (result is Break)
                {
                    m_currentChar = startSkipOnBreakChar;
                    break;
                }
            }

            // The while condition is not true anymore: must skip the whole while
            // block before continuing with next statements.
            SkipBlock();
        }
コード例 #11
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            string varName = Utils.GetToken(data, ref from, Constants.END_ARG_ARRAY);

            if (from >= data.Length)
            {
                throw new ArgumentException("Couldn't set variable before end of line");
            }
            NameVar nv = new NameVar(varName);

            Parser.Result varValue = new Parser.Result();
            ScDumper.SetValue(nv, varValue.Value.ToString());
            return(varValue);
        }
コード例 #12
0
ファイル: Functions.cs プロジェクト: zanxi/WPFCSS-Compilator
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            string varName = Utils.GetToken(data, ref from, Constants.NEXT_ARG_ARRAY);

            if (from >= data.Length)
            {
                throw new ArgumentException("Couldn't set env variable");
            }

            Parser.Result varValue = Utils.GetItem(data, ref from);
            string        strValue = varValue.String ?? varValue.Value.ToString();

            Environment.SetEnvironmentVariable(varName, strValue);

            return(new Parser.Result(Double.NaN, varName));
        }
コード例 #13
0
        internal Parser.Result ProcessIf()
        {
            int startIfCondition = m_currentChar;

            Parser.Result result = null;

            Parser.Result arg1       = GetNextIfToken();
            string        comparison = Utils.GetComparison(m_data, ref m_currentChar);

            Parser.Result arg2 = GetNextIfToken();

            bool isTrue = EvalCondition(arg1, comparison, arg2);

            if (isTrue)
            {
                result = ProcessBlock();

                if (result is Continue || result is Break)
                {
                    // Got here from the middle of the if-block. Skip it.
                    m_currentChar = startIfCondition;
                    SkipBlock();
                }
                SkipRestBlocks();

                return(result);
            }

            // We are in Else. Skip everything in the If statement.
            SkipBlock();

            int    endOfToken = m_currentChar;
            string nextToken  = Utils.GetNextToken(m_data, ref endOfToken);

            if (ELSE_IF_LIST.Contains(nextToken))
            {
                m_currentChar = endOfToken + 1;
                result        = ProcessIf();
            }
            else if (ELSE_LIST.Contains(nextToken))
            {
                m_currentChar = endOfToken + 1;
                result        = ProcessBlock();
            }

            return(new Parser.Result());
        }
コード例 #14
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            string varName = Utils.GetToken(data, ref from, Constants.END_ARG_ARRAY);

            if (from >= data.Length)
            {
                throw new ArgumentException("Couldn't set variable before end of line");
            }
            NameVar nv = new NameVar(varName);

            Parser.Result varValue = new Parser.Result();
            string        value    = ScDumper.GetValue(nv);

            varValue.Value = Double.Parse(value);
            // Check if the variable to be set has the form of x(0),
            // meaning that this is an array element.
            int arrayIndex = Utils.ExtractArrayElement(ref varName);

            if (arrayIndex >= 0)
            {
                bool          exists       = ParserFunction.FunctionExists(varName);
                Parser.Result currentValue = exists ?
                                             ParserFunction.GetFunction(varName).GetValue(data, ref from) :
                                             new Parser.Result();

                List <Parser.Result> tuple = currentValue.Tuple ?? new List <Parser.Result>();
                if (tuple.Count > arrayIndex)
                {
                    tuple[arrayIndex] = varValue;
                }
                else
                {
                    for (int i = tuple.Count; i < arrayIndex; i++)
                    {
                        tuple.Add(new Parser.Result(Double.NaN, string.Empty));
                    }
                    tuple.Add(varValue);
                }

                varValue = new Parser.Result(Double.NaN, null, tuple);
            }

            ParserFunction.AddFunction(varName, new GetVarFunction(varValue));

            return(new Parser.Result(Double.NaN, varName));
        }
コード例 #15
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            string substring;

            // 1. Get the name of the variable.
            string varName = Utils.GetToken(data, ref from, Constants.NEXT_ARG_ARRAY);

            if (from >= data.Length)
            {
                throw new ArgumentException("Couldn't get variable");
            }

            // 2. Get the current value of the variable.
            ParserFunction func = ParserFunction.GetFunction(varName);

            Parser.Result currentValue = func.GetValue(data, ref from);

            // 3. Take either the string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string arg = currentValue.String != null ? currentValue.String :
                         currentValue.Value.ToString();
            // 4. Get the initial index of the substring.
            bool lengthAvailable = Utils.SeparatorExists(data, from);

            Parser.Result init = Utils.GetItem(data, ref from, true /* expectInt */);

            // 5. Get the length of the substring if available.
            if (lengthAvailable)
            {
                Parser.Result length = Utils.GetItem(data, ref from, true /* expectInt */);
                if (init.Value + length.Value > arg.Length)
                {
                    throw new ArgumentException("The total substring length is larger than  [" +
                                                arg + "]");
                }
                substring = arg.Substring((int)init.Value, (int)length.Value);
            }
            else
            {
                substring = arg.Substring((int)init.Value);
            }
            Parser.Result newValue = new Parser.Result(Double.NaN, substring);

            return(newValue);
        }
コード例 #16
0
        public static Parser.Result GetItem(string data, ref int from, bool expectInt = false)
        {
            Parser.Result value = new Parser.Result();
            if (data.Length <= from)
            {
                throw new ArgumentException("Incomplete function definition");
            }
            if (data[from] == Constants.NEXT_ARG)
            {
                from++;
            }

            // Utils.GoToNextStatement(data, ref from);
            if (from < data.Length && data[from] == Constants.QUOTE)
            {
                from++; // skip first quote
                if (from < data.Length && data[from] == Constants.QUOTE)
                {       // the argument is ""
                    value.String = "";
                }
                else
                {
                    value.String = Utils.GetToken(data, ref from, Constants.QUOTE_ARRAY);
                }
                from++; // skip next separation char
            }
            else
            {
                Parser.Result existing = Parser.LoadAndCalculate(data, ref from, Constants.NEXT_OR_END_ARRAY);
                value.Copy(existing);
            }

            if (expectInt && Double.IsNaN(value.Value))
            {
                throw new ArgumentException("Integer expected instead of [" + value.String + "]");
            }

            if (from < data.Length && data[from] == Constants.END_ARG)
            {
                from++;
            }
            return(value);
        }
コード例 #17
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            Parser.Result id = Utils.GetItem(data, ref from, true /* expectInt */);

            int processId = (int)id.Value;

            try
            {
                Process process = Process.GetProcessById(processId);
                process.Kill();
                m_interpreter.AppendOutput("Process " + processId + " killed");
            }
            catch (Exception exc)
            {
                throw new ArgumentException("Couldn't kill process " + processId +
                                            " (" + exc.Message + ")");
            }

            return(new Parser.Result());
        }
コード例 #18
0
ファイル: Functions.cs プロジェクト: zanxi/WPFCSS-Compilator
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(data, ref from, Constants.END_ARG_ARRAY);

            if (from >= data.Length)
            {
                throw new ArgumentException("Couldn't get variable");
            }

            // 2. Get the current value of the variable.
            ParserFunction func = ParserFunction.GetFunction(varName);

            Parser.Result currentValue = func.GetValue(data, ref from);

            // 3. Take either the string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string arg = currentValue.String ?? currentValue.Value.ToString();

            Parser.Result newValue = new Parser.Result(Double.NaN, arg.ToLower());
            return(newValue);
        }
コード例 #19
0
        protected override Parser.Result Evaluate(string data, ref int from)
        {
            // First check if this element is part of an array:
            if (from < data.Length && data[from - 1] == Constants.START_ARG)
            {
                // There is an index given - it may be for an element of the tuple.
                if (m_value.Tuple == null || m_value.Tuple.Count == 0)
                {
                    throw new ArgumentException("No tuple exists for the index");
                }

                Parser.Result index = Utils.GetItem(data, ref from, true /* expectInt */);
                if (index.Value < 0 || index.Value >= m_value.Tuple.Count)
                {
                    throw new ArgumentException("Incorrect index [" + index.Value +
                                                "] for tuple of size " + m_value.Tuple.Count);
                }
                return(m_value.Tuple[(int)index.Value]);
            }

            return(m_value);
        }
コード例 #20
0
 protected override Parser.Result Evaluate(string data, ref int from)
 {
     Parser.Result arg = Parser.LoadAndCalculate(data, ref from, Constants.END_ARG_ARRAY);
     arg.Value = Math.Abs(arg.Value);
     return(arg);
 }
コード例 #21
0
 protected override Parser.Result Evaluate(string data, ref int from)
 {
     Parser.Result result = Parser.LoadAndCalculate(data, ref from, Constants.END_ARG_ARRAY);
     result.Value = Math.Exp(result.Value);
     return(result);
 }
コード例 #22
0
 internal GetVarFunction(Parser.Result value)
 {
     m_value = value;
 }