protected override Variable Evaluate(ParsingScript script)
        {
            // First check if this element is part of an array:
            if (script.TryPrev() == Constants.START_ARRAY)
            {
                // There is an index given - it must 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");
                }

                if (m_arrayIndices == null)
                {
                    string startName = script.Substr(script.Pointer - 1);
                    m_arrayIndices = Utils.GetArrayIndices(ref startName, ref m_delta);
                }

                script.Forward(m_delta);

                Variable result = Utils.ExtractArrayElement(m_value, m_arrayIndices);
                return(result);
            }

            // Otherwise just return the stored value.
            return(m_value);
        }
Exemple #2
0
        public static string[] GetCompiledFunctionSignature(ParsingScript script, out Dictionary <string, Variable> dict)
        {
            script.MoveForwardIf(Constants.START_ARG, Constants.SPACE);

            int endArgs = script.FindFirstOf(Constants.END_ARG.ToString());

            if (endArgs < 0)
            {
                throw new ArgumentException("Couldn't extract function signature");
            }

            string        argStr = script.Substr(script.Pointer, endArgs - script.Pointer);
            List <string> args   = GetCompiledArgs(argStr);

            //string[] args = argStr.Split(Constants.NEXT_ARG_ARRAY, StringSplitOptions.RemoveEmptyEntries);

            dict = new Dictionary <string, Variable>(args.Count);
            var sep = new char [] { ' ' };

            for (int i = 0; i < args.Count; i++)
            {
                string[]         pair = args[i].Trim().Split(sep, StringSplitOptions.RemoveEmptyEntries);
                Variable.VarType type = pair.Length > 1 ? Constants.StringToType(pair[0]) : Variable.VarType.STRING;
                dict.Add(pair[pair.Length - 1], new Variable(type));
                args[i] = pair[pair.Length - 1];
            }

            string[] result = args.Select(element => element.Trim()).ToArray();
            script.Pointer = endArgs + 1;

            return(result);
        }
Exemple #3
0
        protected override Variable Evaluate(ParsingScript script)
        {
            if (script.Substr().StartsWith(" .."))
            {
                script.Forward();
            }
            string newDir = Utils.GetStringOrVarValue(script);

            try {
                if (newDir == "..")
                {
                    string        pwd    = Directory.GetCurrentDirectory();
                    DirectoryInfo parent = Directory.GetParent(pwd);
                    if (parent == null)
                    {
                        throw new ArgumentException("No parent exists.");
                    }
                    newDir = parent.FullName;
                }
                if (newDir.Length == 0)
                {
                    newDir = Environment.GetEnvironmentVariable("HOME");
                }
                Directory.SetCurrentDirectory(newDir);

                newDir = Directory.GetCurrentDirectory();
            } catch (Exception exc) {
                throw new ArgumentException("Couldn't change directory: " + exc.Message);
            }

            return(new Variable(newDir));
        }
Exemple #4
0
        private void SkipBlock(ParsingScript script)
        {
            int blockStart = script.Pointer;
            int startCount = 0;
            int endCount   = 0;

            while (startCount == 0 || startCount > endCount)
            {
                if (!script.StillValid())
                {
                    throw new ArgumentException("Couldn't skip block [" +
                                                script.Substr(blockStart,Constants.MAX_CHARS_TO_SHOW) + "]");
                }
                char currentChar = script.CurrentAndForward();
                switch (currentChar)
                {
                case Constants.START_GROUP: startCount++; break;

                case Constants.END_GROUP: endCount++; break;
                }
            }

            if (startCount != endCount)
            {
                throw new ArgumentException("Mismatched parentheses");
            }
        }
Exemple #5
0
        private Variable ProcessBlock(ParsingScript script)
        {
            int      blockStart = script.Pointer;
            Variable result     = null;

            while (script.StillValid())
            {
                int endGroupRead = script.GoToNextStatement();
                if (endGroupRead > 0)
                {
                    return(result != null ? result : new Variable());
                }

                if (!script.StillValid())
                {
                    throw new ArgumentException("Couldn't process block [" +
                                                script.Substr(blockStart,Constants.MAX_CHARS_TO_SHOW) + "]");
                }
                result = script.ExecuteTo();

                if (result.IsReturn ||
                    result.Type == Variable.VarType.BREAK ||
                    result.Type == Variable.VarType.CONTINUE)
                {
                    return(result);
                }
            }
            return(result);
        }
Exemple #6
0
        private void SkipBlock(ParsingScript script)
        {
            int  blockStart = script.Pointer;
            int  startCount = 0;
            int  endCount   = 0;
            bool inQuotes   = false;
            bool inQuotes1  = false;
            bool inQuotes2  = false;
            char previous   = Constants.EMPTY;
            char prevprev   = Constants.EMPTY;

            while (startCount == 0 || startCount > endCount)
            {
                if (!script.StillValid())
                {
                    throw new ArgumentException("Couldn't skip block [" +
                                                script.Substr(blockStart, Constants.MAX_CHARS_TO_SHOW) + "]");
                }
                char currentChar = script.CurrentAndForward();
                switch (currentChar)
                {
                case Constants.QUOTE1:
                    if (!inQuotes2 && (previous != '\\' || prevprev == '\\'))
                    {
                        inQuotes = inQuotes1 = !inQuotes1;
                    }
                    break;

                case Constants.QUOTE:
                    if (!inQuotes1 && (previous != '\\' || prevprev == '\\'))
                    {
                        inQuotes = inQuotes2 = !inQuotes2;
                    }
                    break;

                case Constants.START_GROUP:
                    if (!inQuotes)
                    {
                        startCount++;
                    }
                    break;

                case Constants.END_GROUP:
                    if (!inQuotes)
                    {
                        endCount++;
                    }
                    break;
                }
                prevprev = previous;
                previous = currentChar;
            }

            if (startCount != endCount)
            {
                throw new ArgumentException("Mismatched parentheses");
            }
        }
Exemple #7
0
        public static string GetNextToken(ParsingScript script)
        {
            if (!script.StillValid())
            {
                return("");
            }
            int end = script.FindFirstOf(Constants.TOKEN_SEPARATION);

            if (end < 0)
            {
                return("");
            }

            string var = script.Substr(script.Pointer, end - script.Pointer);

            script.Pointer = end;
            return(var);
        }
        public static string[] GetFunctionSignature(ParsingScript script)
        {
            script.MoveForwardIf(Constants.START_ARG, Constants.SPACE);

            int endArgs = script.FindFirstOf(Constants.END_ARG.ToString());

            if (endArgs < 0)
            {
                throw new ArgumentException("Couldn't extract function signature");
            }

            string argStr = script.Substr(script.Pointer, endArgs - script.Pointer);

            string[] args = argStr.Split(Constants.NEXT_ARG_ARRAY);

            script.Pointer = endArgs + 1;

            return(args);
        }
Exemple #9
0
        public static string[] GetBaseClasses(ParsingScript script)
        {
            if (script.Current != ':')
            {
                return(new string[0]);
            }
            script.Forward();

            int endArgs = script.FindFirstOf(Constants.START_GROUP.ToString());

            if (endArgs < 0)
            {
                throw new ArgumentException("Couldn't extract base classes");
            }

            string argStr = script.Substr(script.Pointer, endArgs - script.Pointer);

            string[] args = argStr.Split(Constants.NEXT_ARG_ARRAY, StringSplitOptions.RemoveEmptyEntries);

            args           = args.Select(element => Constants.ConvertName(element.Trim())).ToArray();
            script.Pointer = endArgs + 1;

            return(args);
        }
Exemple #10
0
        public static string GetToken(ParsingScript script, char [] to)

        {
            char curr = script.TryCurrent();
            char prev = script.TryPrev();

            if (!to.Contains(Constants.SPACE))
            {
                // Skip a leading space unless we are inside of quotes
                while (curr == Constants.SPACE && prev != Constants.QUOTE)
                {
                    script.Forward();
                    curr = script.TryCurrent();
                    prev = script.TryPrev();
                }
            }

            // String in quotes
            bool inQuotes = curr == Constants.QUOTE;

            if (inQuotes)
            {
                int qend = script.Find(Constants.QUOTE, script.Pointer + 1);
                if (qend == -1)
                {
                    throw new ArgumentException("Unmatched quotes in [" +
                                                script.FromPrev() + "]");
                }
                string result = script.Substr(script.Pointer + 1, qend - script.Pointer - 1);
                script.Pointer = qend + 1;
                return(result);
            }

            script.MoveForwardIf(Constants.QUOTE);

            int end = script.FindFirstOf(to);

            end = end < 0 ? script.Size() : end;

            // Skip found characters that have a backslash before.
            while (end > 0 && end + 1 < script.Size() &&
                   script.String [end - 1] == '\\')
            {
                end = script.FindFirstOf(to, end + 1);
            }

            end = end < 0 ? script.Size() : end;

            if (script.At(end - 1) == Constants.QUOTE)
            {
                end--;
            }

            string var = script.Substr(script.Pointer, end - script.Pointer);

            // \"yes\" --> "yes"
            var            = var.Replace("\\\"", "\"");
            script.Pointer = end;

            script.MoveForwardIf(Constants.QUOTE, Constants.SPACE);

            return(var);
        }
Exemple #11
0
        public static string[] GetFunctionSignature(ParsingScript script)
        {
            script.MoveForwardIf(Constants.START_ARG, Constants.SPACE);

            int endArgs = script.FindFirstOf(Constants.END_ARG.ToString());

            if (endArgs < 0)
            {
                endArgs = script.FindFirstOf(Constants.END_STATEMENT.ToString());
            }

            if (endArgs < 0)
            {
                throw new ArgumentException("Couldn't extract function signature");
            }

            string        argStr     = script.Substr(script.Pointer, endArgs - script.Pointer);
            StringBuilder collect    = new StringBuilder(argStr.Length);
            List <string> args       = new List <string>();
            int           curlyLevel = 0;

            for (int i = 0; i < argStr.Length; i++)
            {
                if (argStr[i] == '{')
                {
                    curlyLevel++;
                }
                else if (argStr[i] == '}')
                {
                    curlyLevel--;
                    if (curlyLevel < 0)
                    {
                        break;
                    }
                }
                else if (argStr[i] == Constants.NEXT_ARG && curlyLevel == 0)
                {
                    string item = collect.ToString().Trim();
                    if (item.Length == 0)
                    {
                        throw new ArgumentException("Empty argument in function signature [" + argStr + "]");
                    }
                    args.Add(item);
                    collect.Clear();
                    continue;
                }
                collect.Append(argStr[i]);
            }

            if (curlyLevel != 0)
            {
                throw new ArgumentException("Unbalanced curly braces in function signature [" + argStr + "]");
            }
            if (collect.Length > 0)
            {
                args.Add(collect.ToString().Trim());
            }

            script.Pointer = endArgs + 1;

            return(args.ToArray());
        }