Exemple #1
0
        static async Task <bool> UpdateIfTernaryAsync(ParsingScript script, string token, char ch, List <Variable> listInput, Action <List <Variable> > listToMerge)
        {
            if (listInput.Count < 1 || ch != Constants.TERNARY_OPERATOR || token.Length > 0)
            {
                return(false);
            }

            Variable arg1 = MergeList(listInput);

            script.MoveForwardIf(Constants.TERNARY_OPERATOR);
            Variable arg2 = await script.ExecuteAsync(Constants.TERNARY_SEPARATOR);

            script.MoveForwardIf(Constants.TERNARY_SEPARATOR);
            Variable arg3 = await script.ExecuteAsync(Constants.NEXT_OR_END_ARRAY);

            script.MoveForwardIf(Constants.NEXT_OR_END_ARRAY);

            double   condition = arg1.AsDouble();
            Variable result    = condition != 0 ? arg2 : arg3;

            listInput.Clear();
            listInput.Add(result);
            listToMerge(listInput);

            return(true);
        }
Exemple #2
0
        static bool UpdateIfTernary(ParsingScript script, string token, char ch, List <Variable> listInput, Action <List <Variable> > listToMerge)
        {
            if (listInput.Count < 1 || ch != Constants.TERNARY_OPERATOR || token.Length > 0)
            {
                return(false);
            }

            Variable result;
            Variable arg1 = MergeList(listInput, script);

            script.MoveForwardIf(Constants.TERNARY_OPERATOR);
            double condition = arg1.AsDouble();

            if (condition != 0)
            {
                result = script.Execute(Constants.TERNARY_SEPARATOR);
                script.MoveForwardIf(Constants.TERNARY_SEPARATOR);
                Utils.SkipRestExpr(script, Constants.END_STATEMENT);
            }
            else
            {
                Utils.SkipRestExpr(script, Constants.TERNARY_SEPARATOR[0]);
                script.MoveForwardIf(Constants.TERNARY_SEPARATOR);
                result = script.Execute(Constants.NEXT_OR_END_ARRAY);
            }

            listInput.Clear();
            listInput.Add(result);
            listToMerge(listInput);

            return(true);
        }
Exemple #3
0
        static bool UpdateIfTernary(ParsingScript script, string token, char ch, ref List <Variable> listToMerge)
        {
            if (listToMerge.Count < 1 || ch != Constants.TERNARY_OPERATOR || token.Length > 0)
            {
                return(false);
            }

            Variable arg1 = MergeList(listToMerge);

            script.MoveForwardIf(Constants.TERNARY_OPERATOR);
            Variable arg2 = script.Execute(Constants.TERNARY_SEPARATOR);

            script.MoveForwardIf(Constants.TERNARY_SEPARATOR);
            Variable arg3 = script.Execute(Constants.NEXT_OR_END_ARRAY);

            script.MoveForwardIf(Constants.NEXT_OR_END_ARRAY);

            double   condition = arg1.AsDouble();
            Variable result    = condition != 0 ? arg2 : arg3;

            listToMerge.Clear();
            listToMerge.Add(result);

            return(true);
        }
        protected override Variable Evaluate(ParsingScript script)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(script, Constants.END_ARG_ARRAY);

            Utils.CheckNotEnd(script, m_name);

            List <Variable> arrayIndices = Utils.GetArrayIndices(ref varName);

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

            Utils.CheckNotNull(varName, func);
            Variable currentValue = func.GetValue(script);
            Variable element      = currentValue;

            // 2b. Special case for an array.
            if (arrayIndices.Count > 0)// array element
            {
                element = Utils.ExtractArrayElement(currentValue, arrayIndices);
                script.MoveForwardIf(Constants.END_ARRAY);
            }

            // 3. Convert type to string.
            string type = Constants.TypeToString(element.Type);

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

            Variable newValue = new Variable(type);

            return(newValue);
        }
Exemple #5
0
        public static Variable GetItem(ParsingScript script, bool eatLast = true)
        {
            script.MoveForwardIf(Constants.NEXT_ARG, Constants.SPACE);
            Utils.CheckNotEnd(script);

            bool inQuotes = script.Current == Constants.QUOTE;

            if (script.Current == Constants.START_GROUP)
            {
                // We are extracting a list between curly braces.
                script.Forward(); // Skip the first brace.
                bool     isList = true;
                Variable value  = new Variable();
                value.Tuple = GetArgs(script,
                                      Constants.START_GROUP, Constants.END_GROUP, out isList);
                return(value);
            }

            // A variable, a function, or a number.
            Variable var = script.Execute(Constants.NEXT_OR_END_ARRAY);

            //value = var.Clone();

            if (inQuotes)
            {
                script.MoveForwardIf(Constants.QUOTE);
            }
            if (eatLast)
            {
                script.MoveForwardIf(Constants.END_ARG, Constants.SPACE);
            }
            return(var);
        }
Exemple #6
0
        public static async Task <List <Variable> > GetArgsAsync(ParsingScript script,
                                                                 char start, char end, Action <bool> outList)
        {
            List <Variable> args   = new List <Variable>();
            bool            isList = script.StillValid() && script.Current == Constants.START_GROUP;

            if (!script.StillValid() || script.Current == Constants.END_STATEMENT)
            {
                return(args);
            }

            ParsingScript tempScript = script.GetTempScript(script.String, script.Pointer);

            /*ParsingScript tempScript = new ParsingScript(script.String, script.Pointer);
             * tempScript.ParentScript = script;
             * tempScript.InTryBlock = script.InTryBlock;*/

            if (script.Current != start && script.TryPrev() != start &&
                (script.Current == ' ' || script.TryPrev() == ' '))
            { // Allow functions with space separated arguments
                start = ' ';
                end   = Constants.END_STATEMENT;
            }

            // ScriptingEngine - body is unsed (used in Debugging) but GetBodyBetween has sideeffects
#pragma warning disable 219
            string body = Utils.GetBodyBetween(tempScript, start, end);
#pragma warning restore 219
            // After the statement above tempScript.Parent will point to the last
            // character belonging to the body between start and end characters.

            while (script.Pointer < tempScript.Pointer)
            {
                Variable item = await Utils.GetItemAsync(script, false);

                args.Add(item);
                if (script.Pointer < tempScript.Pointer)
                {
                    script.MoveForwardIf(Constants.END_GROUP);
                    script.MoveForwardIf(Constants.NEXT_ARG);
                }
                if (script.Pointer == tempScript.Pointer - 1)
                {
                    script.MoveForwardIf(Constants.END_ARG, Constants.END_GROUP);
                }
            }

            if (script.Pointer <= tempScript.Pointer)
            {
                // Eat closing parenthesis, if there is one, but only if it closes
                // the current argument list, not one after it.
                script.MoveForwardIf(Constants.END_ARG, end);
            }

            script.MoveForwardIf(Constants.SPACE);
            //script.MoveForwardIf(Constants.SPACE, Constants.END_STATEMENT);
            outList(isList);
            return(args);
        }
Exemple #7
0
        public static List <string> ExtractTokens(ParsingScript script)
        {
            List <string> tokens = new List <string>();

            script.MoveForwardIf(Constants.START_ARG);
            while (script.TryCurrent() != Constants.END_GROUP)
            {
                string propName = Utils.GetToken(script, Constants.TOKEN_SEPARATION);
                script.MoveForwardIf(Constants.NEXT_ARG);
                tokens.Add(propName);
            }
            return(tokens);
        }
Exemple #8
0
        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);
                while (script.MoveForwardIf(Constants.END_ARRAY))
                {
                }

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

            // Otherwise just return the stored value.
            return(m_value);
        }
Exemple #9
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);
        }
        protected override Variable Evaluate(ParsingScript script)
        {
            string funcName = Utils.GetToken(script, Constants.TOKEN_SEPARATION);

            //Interpreter.Instance.AppendOutput("Registering function [" + funcName + "] ...");

            string[] args = Utils.GetFunctionSignature(script);
            if (args.Length == 1 && string.IsNullOrWhiteSpace(args[0]))
            {
                args = new string[0];
            }

            script.MoveForwardIf(Constants.START_GROUP, Constants.SPACE);
            int parentOffset = script.Pointer;

            string body = Utils.GetBodyBetween(script, Constants.START_GROUP, Constants.END_GROUP);

            CustomFunction customFunc = new CustomFunction(funcName, body, args);

            customFunc.ParentScript = script;
            customFunc.ParentOffset = parentOffset;

            ParserFunction.RegisterFunction(funcName, customFunc, false /* not native */);

            return(new Variable(funcName));
        }
Exemple #11
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string funcReturn, funcName;

            Utils.GetCompiledArgs(script, out funcReturn, out funcName);

#if __ANDROID__ == false && __IOS__ == false
            Precompiler.RegisterReturnType(funcName, funcReturn);

            Dictionary <string, Variable> argsMap;
            string[] args = Utils.GetCompiledFunctionSignature(script, out argsMap);

            script.MoveForwardIf(Constants.START_GROUP, Constants.SPACE);
            int parentOffset = script.Pointer;

            string body = Utils.GetBodyBetween(script, Constants.START_GROUP, Constants.END_GROUP);

            Precompiler precompiler = new Precompiler(funcName, args, argsMap, body, script);
            precompiler.Compile();

            CustomCompiledFunction customFunc = new CustomCompiledFunction(funcName, body, args, precompiler, argsMap, script);
            customFunc.ParentScript = script;
            customFunc.ParentOffset = parentOffset;

            ParserFunction.RegisterFunction(funcName, customFunc, false /* not native */);
#endif
            return(new Variable(funcName));
        }
Exemple #12
0
        public static List <Variable> GetArgs(ParsingScript script,
                                              char start, char end, out bool isList)
        {
            List <Variable> args = new List <Variable>();

            isList = script.StillValid() && script.Current == Constants.START_GROUP;

            if (!script.StillValid() || script.Current == Constants.END_STATEMENT)
            {
                return(args);
            }

            ParsingScript tempScript = new ParsingScript(script.String, script.Pointer);

            tempScript.ParentScript = script;
            tempScript.InTryBlock   = script.InTryBlock;

            string body = Utils.GetBodyBetween(tempScript, start, end);

            // After the statement above tempScript.Parent will point to the last
            // character belonging to the body between start and end characters.

            while (script.Pointer < tempScript.Pointer)
            {
                Variable item = Utils.GetItem(script, false);
                args.Add(item);
                if (script.Pointer < tempScript.Pointer)
                {
                    script.MoveForwardIf(Constants.NEXT_ARG);
                }
                if (script.Pointer == tempScript.Pointer - 1)
                {
                    script.MoveForwardIf(Constants.END_ARG);
                }
            }

            if (script.Pointer <= tempScript.Pointer)
            {
                // Eat closing parenthesis, if there is one, but only if it closes
                // the current argument list, not one after it.
                script.MoveForwardIf(Constants.END_ARG);
            }

            script.MoveForwardIf(Constants.SPACE);
            //script.MoveForwardIf(Constants.SPACE, Constants.END_STATEMENT);
            return(args);
        }
Exemple #13
0
        static Variable ExtractObject(ParsingScript script)
        {
            Variable newValue = new Variable(Variable.VarType.ARRAY);

            while (script.StillValid() && (newValue.Count == 0 || script.Current == ','))
            {
                script.Forward();
                string key = Utils.GetToken(script, SEP);
                script.MoveForwardIf(':');

                Variable valueVar = ExtractValue(script);
                newValue.SetHashVariable(key, valueVar);
            }
            script.MoveForwardIf('}');

            return(newValue);
        }
Exemple #14
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string methodName = Utils.GetItem(script).AsString();

            Utils.CheckNotEmpty(script, methodName, m_name);
            script.MoveForwardIf(Constants.NEXT_ARG);

            string paramName = Utils.GetToken(script, Constants.NEXT_ARG_ARRAY);

            Utils.CheckNotEmpty(script, paramName, m_name);
            script.MoveForwardIf(Constants.NEXT_ARG);

            Variable paramValueVar = Utils.GetItem(script);
            string   paramValue    = paramValueVar.AsString();

            var result = Statics.InvokeCall(typeof(Statics),
                                            methodName, paramName, paramValue);

            return(result);
        }
Exemple #15
0
        internal Variable ProcessCase(ParsingScript script, string reason)
        {
            if (reason == Constants.CASE)
            {
                /*var token = */ Utils.GetToken(script, Constants.TOKEN_SEPARATION);
            }
            script.MoveForwardIf(':');

            Variable result = ProcessBlock(script);

            return(result);
        }
        protected override Variable Evaluate(ParsingScript script)
        {
            script.MoveForwardIf(Constants.SPACE);

            Variable result = Utils.GetItem(script);

            // If we are in Return, we are done:
            script.SetDone();
            result.IsReturn = true;

            return(result);
        }
Exemple #17
0
        static Variable ExtractArray(ParsingScript script)
        {
            Variable newValue = new Variable(Variable.VarType.ARRAY);

            while (script.StillValid() && (newValue.Count == 0 || script.Current == ','))
            {
                script.Forward();
                Variable addVariable = ExtractValue(script);
                newValue.AddVariable(addVariable);
            }
            script.MoveForwardIf(']');

            return(newValue);
        }
        protected override Variable Evaluate(ParsingScript script)
        {
            bool prefix = string.IsNullOrWhiteSpace(m_name);

            if (prefix)// If it is a prefix we do not have the variable name yet.
            {
                m_name = Utils.GetToken(script, Constants.TOKEN_SEPARATION);
            }

            // Value to be added to the variable:
            int valueDelta  = m_action == Constants.INCREMENT ? 1 : -1;
            int returnDelta = prefix ? valueDelta : 0;

            // Check if the variable to be set has the form of x[a][b],
            // meaning that this is an array element.
            double          newValue     = 0;
            List <Variable> arrayIndices = Utils.GetArrayIndices(ref m_name);

            ParserFunction func = ParserFunction.GetFunction(m_name);

            Utils.CheckNotNull(m_name, func);

            Variable currentValue = func.GetValue(script);

            if (arrayIndices.Count > 0 || script.TryCurrent() == Constants.START_ARRAY)
            {
                if (prefix)
                {
                    string tmpName = m_name + script.Rest;
                    int    delta   = 0;
                    arrayIndices = Utils.GetArrayIndices(ref tmpName, ref delta);
                    script.Forward(Math.Max(0, delta - tmpName.Length));
                }

                Variable element = Utils.ExtractArrayElement(currentValue, arrayIndices);
                script.MoveForwardIf(Constants.END_ARRAY);

                newValue       = element.Value + returnDelta;
                element.Value += valueDelta;
            }
            else // A normal variable.
            {
                newValue            = currentValue.Value + returnDelta;
                currentValue.Value += valueDelta;
            }

            ParserFunction.AddGlobalOrLocalVariable(m_name,
                                                    new GetVarFunction(currentValue));
            return(new Variable(newValue));
        }
        protected override Variable Evaluate(ParsingScript script)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(script, Constants.END_ARG_ARRAY);

            Utils.CheckNotEnd(script, m_name);

            List <Variable> arrayIndices = Utils.GetArrayIndices(ref varName);

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

            Utils.CheckNotNull(varName, func);
            Variable currentValue = func.GetValue(script);
            Variable element      = currentValue;

            // 2b. Special case for an array.
            if (arrayIndices.Count > 0)// array element
            {
                element = Utils.ExtractArrayElement(currentValue, arrayIndices);
                script.MoveForwardIf(Constants.END_ARRAY);
            }

            // 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 = element.Type == Variable.VarType.ARRAY ?
                       element.Tuple.Count :
                       element.AsString().Length;

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

            Variable newValue = new Variable(size);

            return(newValue);
        }
Exemple #20
0
        protected override Variable Evaluate(ParsingScript script)
        {
            script.MoveForwardIf(Constants.SPACE);
            if (!script.FromPrev(Constants.RETURN.Length).Contains(Constants.RETURN))
            {
                script.Backward();
            }
            Variable result = Utils.GetItem(script);

            // If we are in Return, we are done:
            script.SetDone();
            result.IsReturn = true;

            return(result);
        }
Exemple #21
0
        public static async Task <List <Variable> > GetArrayIndicesAsync(ParsingScript script, string varName, int end, Action <string, int> updateVals)
        {
            List <Variable> indices = new List <Variable>();

            int argStart = varName.IndexOf(Constants.START_ARRAY);

            if (argStart < 0)
            {
                return(indices);
            }
            int firstIndexStart = argStart;

            while (argStart < varName.Length &&
                   varName[argStart] == Constants.START_ARRAY)
            {
                int argEnd = varName.IndexOf(Constants.END_ARRAY, argStart + 1);
                if (argEnd == -1 || argEnd <= argStart + 1)
                {
                    break;
                }

                ParsingScript tempScript = script.GetTempScript(varName, argStart);

                /*ParsingScript tempScript = new ParsingScript(varName, argStart);
                 * tempScript.ParentScript = script;
                 * tempScript.Char2Line = script.Char2Line;
                 * tempScript.Filename = script.Filename;
                 * tempScript.OriginalScript = script.OriginalScript;
                 * tempScript.InTryBlock = script.InTryBlock;*/

                tempScript.MoveForwardIf(Constants.START_ARG, Constants.START_ARRAY);

                Variable index = await tempScript.ExecuteAsync(Constants.END_ARRAY_ARRAY);

                indices.Add(index);
                argStart = argEnd + 1;
            }

            if (indices.Count > 0)
            {
                varName = varName.Substring(0, firstIndexStart);
                end     = argStart - 1;
            }

            updateVals(varName, end);
            return(indices);
        }
Exemple #22
0
        static List <Variable> Split(ParsingScript script, char[] to)
        {
            List <Variable> listToMerge = new List <Variable>(16);

            if (!script.StillValid() || to.Contains(script.Current))
            {
                listToMerge.Add(Variable.EmptyInstance);
                script.Forward();
                return(listToMerge);
            }

            int    arrayIndexDepth = 0;
            bool   inQuotes        = false;
            int    negated         = 0;
            char   ch;
            string action;

            do
            { // Main processing cycle of the first part.
                string token = ExtractNextToken(script, to, ref inQuotes, ref arrayIndexDepth, ref negated, out ch, out action);

                bool ternary = UpdateIfTernary(script, token, ch, listToMerge, (List <Variable> newList) => { listToMerge = newList; });
                if (ternary)
                {
                    return(listToMerge);
                }

                bool negSign = CheckConsistencyAndSign(script, listToMerge, action, ref token);

                // We are done getting the next token. The GetValue() call below may
                // recursively call SplitAndMerge(). This will happen if extracted
                // item is a function or if the next item is starting with a START_ARG '('.
                ParserFunction func    = new ParserFunction(script, token, ch, ref action);
                Variable       current = func.GetValue(script);

                if (UpdateResult(script, to, listToMerge, token, negSign, ref current, ref negated, ref action))
                {
                    return(listToMerge);
                }
            } while (script.StillValid() &&
                     (inQuotes || arrayIndexDepth > 0 || !to.Contains(script.Current)));

            // This happens when called recursively inside of the math expression:
            script.MoveForwardIf(Constants.END_ARG);

            return(listToMerge);
        }
Exemple #23
0
        static async Task <List <Variable> > SplitAsync(ParsingScript script, char[] to)
        {
            List <Variable> listToMerge = new List <Variable>(16);

            if (!script.StillValid() || to.Contains(script.Current))
            {
                listToMerge.Add(Variable.EmptyInstance);
                script.Forward();
                return(listToMerge);
            }

            int    arrayIndexDepth = 0;
            bool   inQuotes        = false;
            int    negated         = 0;
            char   ch;
            string action;

            do
            { // Main processing cycle of the first part.
                string token = ExtractNextToken(script, to, ref inQuotes, ref arrayIndexDepth, ref negated, out ch, out action);

                bool ternary = await UpdateIfTernaryAsync(script, token, ch, listToMerge, (List <Variable> newList) => { listToMerge = newList; });

                if (ternary)
                {
                    return(listToMerge);
                }

                bool negSign = CheckConsistencyAndSign(script, listToMerge, action, ref token);

                ParserFunction func    = new ParserFunction(script, token, ch, ref action);
                Variable       current = await func.GetValueAsync(script);

                if (UpdateResult(script, to, listToMerge, token, negSign, ref current, ref negated, ref action))
                {
                    return(listToMerge);
                }
            } while (script.StillValid() &&
                     (inQuotes || arrayIndexDepth > 0 || !to.Contains(script.Current)));

            // This happens when called recursively inside of the math expression:
            script.MoveForwardIf(Constants.END_ARG);

            return(listToMerge);
        }
Exemple #24
0
        public static List <Variable> GetArrayIndices(ParsingScript script, ref string varName, ref int end)
        {
            List <Variable> indices = new List <Variable>();

            int argStart = varName.IndexOf(Constants.START_ARRAY);

            if (argStart < 0)
            {
                return(indices);
            }
            int firstIndexStart = argStart;

            while (argStart < varName.Length &&
                   varName[argStart] == Constants.START_ARRAY)
            {
                int argEnd = varName.IndexOf(Constants.END_ARRAY, argStart + 1);
                if (argEnd == -1 || argEnd <= argStart + 1)
                {
                    break;
                }

                ParsingScript tempScript = new ParsingScript(varName, argStart);
                tempScript.ParentScript   = script;
                tempScript.Char2Line      = script.Char2Line;
                tempScript.Filename       = script.Filename;
                tempScript.OriginalScript = script.OriginalScript;
                tempScript.InTryBlock     = script.InTryBlock;

                tempScript.MoveForwardIf(Constants.START_ARG, Constants.START_ARRAY);

                Variable index = tempScript.ExecuteTo(Constants.END_ARRAY);

                indices.Add(index);
                argStart = argEnd + 1;
            }

            if (indices.Count > 0)
            {
                varName = varName.Substring(0, firstIndexStart);
                end     = argStart - 1;
            }

            return(indices);
        }
Exemple #25
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string source = Utils.GetItem(script).AsString();

            script.MoveForwardIf(Constants.NEXT_ARG, Constants.SPACE);
            string dest = Utils.GetItem(script).AsString();

            string src = Path.GetFullPath(source);
            string dst = Path.GetFullPath(dest);

            bool isFile = File.Exists(src);
            bool isDir  = Directory.Exists(src);

            if (!isFile && !isDir)
            {
                throw new ArgumentException("[" + src + "] doesn't exist");
            }

            if (isFile && Directory.Exists(dst))
            {
                // If filename is missing in the destination file,
                // add it from the source.
                dst = Path.Combine(dst, Path.GetFileName(src));
            }

            try
            {
                if (isFile)
                {
                    File.Move(src, dst);
                }
                else
                {
                    Directory.Move(src, dst);
                }
            }
            catch (Exception exc)
            {
                throw new ArgumentException("Couldn't copy: " + exc.Message);
            }

            return(Variable.EmptyInstance);
        }
        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);
        }
        protected override Variable Evaluate(ParsingScript script)
        {
            // Value to be added to the variable:
            Variable right = Utils.GetItem(script);

            List <Variable> arrayIndices = Utils.GetArrayIndices(ref m_name);

            ParserFunction func = ParserFunction.GetFunction(m_name);

            Utils.CheckNotNull(m_name, func);

            Variable currentValue = func.GetValue(script);
            Variable left         = currentValue;

            if (arrayIndices.Count > 0)// array element
            {
                left = Utils.ExtractArrayElement(currentValue, arrayIndices);
                script.MoveForwardIf(Constants.END_ARRAY);
            }

            if (left.Type == Variable.VarType.NUMBER)
            {
                NumberOperator(left, right, m_action);
            }
            else
            {
                StringOperator(left, right, m_action);
            }

            if (arrayIndices.Count > 0)// array element
            {
                AssignFunction.ExtendArray(currentValue, arrayIndices, 0, left);
                ParserFunction.AddGlobalOrLocalVariable(m_name,
                                                        new GetVarFunction(currentValue));
            }
            else
            {
                ParserFunction.AddGlobalOrLocalVariable(m_name,
                                                        new GetVarFunction(left));
            }
            return(left);
        }
Exemple #28
0
        public static string GetStringOrVarValue(ParsingScript script)
        {
            script.MoveForwardIf(Constants.SPACE);

            // If this token starts with a quote then it is a string constant.
            // Otherwide we treat it as a variable, but if the variable doesn't exist then it
            // will be still treated as a string constant.
            bool stringConstant = script.Rest.StartsWith(Constants.QUOTE.ToString());

            string token = Utils.GetToken(script, Constants.NEXT_OR_END_ARRAY);

            // Check if this is a variable definition:
            stringConstant = stringConstant || !ParserFunction.FunctionExists(token);
            if (!stringConstant)
            {
                Variable sourceValue = ParserFunction.GetFunction(token).GetValue(script);
                token = sourceValue.String;
            }

            return(token);
        }
Exemple #29
0
        public static List <Variable> GetArrayIndices(ParsingScript script, string varName, int end, Action <string, int> updateVals)
        {
            List <Variable> indices = new List <Variable>();

            int argStart = varName.IndexOf(Constants.START_ARRAY);

            if (argStart < 0)
            {
                return(indices);
            }
            int firstIndexStart = argStart;

            while (argStart < varName.Length &&
                   varName[argStart] == Constants.START_ARRAY)
            {
                int argEnd = varName.IndexOf(Constants.END_ARRAY, argStart + 1);
                if (argEnd == -1 || argEnd <= argStart + 1)
                {
                    break;
                }

                ParsingScript tempScript = script.GetTempScript(varName, argStart);
                tempScript.MoveForwardIf(Constants.START_ARG, Constants.START_ARRAY);

                Variable index = tempScript.Execute(Constants.END_ARRAY_ARRAY);

                indices.Add(index);
                argStart = argEnd + 1;
            }

            if (indices.Count > 0)
            {
                varName = varName.Substring(0, firstIndexStart);
                end     = argStart - 1;
            }

            updateVals(varName, end);
            return(indices);
        }
Exemple #30
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string source = Utils.GetItem(script).AsString();

            script.MoveForwardIf(Constants.NEXT_ARG, Constants.SPACE);
            string dest = Utils.GetItem(script).AsString();

            string src = Path.GetFullPath(source);
            string dst = Path.GetFullPath(dest);

            List <Variable> srcPaths      = Utils.GetPathnames(src);
            bool            multipleFiles = srcPaths.Count > 1;

            if (dst.EndsWith("*"))
            {
                dst = dst.Remove(dst.Count() - 1);
            }
            if ((multipleFiles || Directory.Exists(src)) &&
                !Directory.Exists(dst))
            {
                try
                {
                    Directory.CreateDirectory(dst);
                }
                catch (Exception exc)
                {
                    throw new ArgumentException("Couldn't create [" + dst + "] :" + exc.Message);
                }
            }

            foreach (Variable srcPath in srcPaths)
            {
                string filename = Path.GetFileName(srcPath.String);
                //string dstPath  = Path.Combine(dst, filename);
                Utils.Copy(srcPath.String, dst);
            }

            return(Variable.EmptyInstance);
        }