Пример #1
0
        protected override Variable Evaluate(ParsingScript script)
        {
            List <Variable> args = script.GetFunctionArgs();

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

            string filename = args[0].AsString();

            string[] lines = Utils.GetFileLines(filename);

            string includeFile = string.Join(Environment.NewLine, lines);
            Dictionary <int, int> char2Line;
            string        includeScript = Utils.ConvertToScript(includeFile, out char2Line);
            ParsingScript tempScript    = new ParsingScript(includeScript, 0, char2Line);

            tempScript.Filename       = filename;
            tempScript.OriginalScript = string.Join(Constants.END_LINE.ToString(), lines);
            tempScript.ParentScript   = script;
            tempScript.InTryBlock     = script.InTryBlock;

            Variable result = null;

            if (script.Debugger != null)
            {
                result = script.Debugger.StepInIncludeIfNeeded(tempScript);
            }

            while (tempScript.Pointer < includeScript.Length)
            {
                result = tempScript.ExecuteTo();
                tempScript.GoToNextStatement();
            }
            return(result == null ? Variable.EmptyInstance : result);
        }
Пример #2
0
        public async Task <Variable> ProcessAsync(string script, string filename = "", bool mainFile = false)
        {
            Dictionary <int, int> char2Line;
            string data = Utils.ConvertToScript(script, out char2Line, filename);

            if (string.IsNullOrWhiteSpace(data))
            {
                return(null);
            }

            ParsingScript toParse = new ParsingScript(data, 0, char2Line);

            toParse.OriginalScript = script;
            toParse.Filename       = filename;

            if (mainFile)
            {
                toParse.MainFilename = toParse.Filename;
            }

            Variable result = null;

            while (toParse.Pointer < data.Length)
            {
                result = await toParse.ExecuteAsync();

                toParse.GoToNextStatement();
            }

            return(result);
        }
Пример #3
0
        protected override Variable Evaluate(ParsingScript script)
        {
            List <Variable> args = script.GetFunctionArgs();

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

            string filename = args[0].AsString();

            string[] lines = Utils.GetFileLines(filename);

            string includeFile = string.Join(Environment.NewLine, lines);
            Dictionary <int, int> char2Line;
            string        includeScript = Utils.ConvertToScript(includeFile, out char2Line);
            ParsingScript tempScript    = new ParsingScript(includeScript, 0, char2Line);

            tempScript.Filename       = filename;
            tempScript.OriginalScript = string.Join(Constants.END_LINE.ToString(), lines);

            while (tempScript.Pointer < includeScript.Length)
            {
                tempScript.ExecuteTo();
                tempScript.GoToNextStatement();
            }
            return(Variable.EmptyInstance);
        }
Пример #4
0
        public Variable Process(string script)
        {
            //  Console.WriteLine(script);
            string data = Utils.ConvertToScript(script);

            // Console.WriteLine(data);

            if (string.IsNullOrWhiteSpace(data))
            {
                return(null);
            }

            int currentChar = 0;

            Variable result = null;

            while (currentChar < data.Length)
            {
                result = Parser.LoadAndCalculate(data, ref currentChar, Constants.END_PARSE_ARRAY);//  ;)}\n
                // Console.WriteLine(currentChar); //761
                Utils.GoToNextStatement(data, ref currentChar);
            }

            return(result);
        }
Пример #5
0
        public Variable Process(string script, string filename = "")
        {
            Dictionary <int, int> char2Line;
            string data = Utils.ConvertToScript(script, out char2Line);

            if (string.IsNullOrWhiteSpace(data))
            {
                return(null);
            }

            ParsingScript toParse = new ParsingScript(data, 0, char2Line);

            toParse.OriginalScript = script;
            toParse.Filename       = filename;

            Variable result = null;

            while (toParse.Pointer < data.Length)
            {
                result = toParse.ExecuteTo();
                toParse.GoToNextStatement();
            }

            return(result);
        }
Пример #6
0
        async Task <string> ProcessRepl(string repl, string filename = "")
        {
            ReplMode = true;

            Dictionary <int, int> char2Line;
            string        script     = Utils.ConvertToScript(repl, out char2Line);
            ParsingScript tempScript = new ParsingScript(script, 0, char2Line);

            tempScript.OriginalScript = repl;
            tempScript.Debugger       = this;
            if (!string.IsNullOrWhiteSpace(filename))
            {
                tempScript.Filename = filename;
            }

            Variable result    = null;
            bool     excThrown = false;
            string   stringRes = "";

            try
            {
                while (tempScript.Pointer < script.Length)
                {
                    result = await DebuggerUtils.Execute(tempScript);

                    tempScript.GoToNextStatement();
                    while (tempScript.TryCurrent() == Constants.END_STATEMENT)
                    {
                        tempScript.Forward();
                    }
                }

                if (TrySendFile(result, tempScript, ref excThrown) || excThrown)
                {
                    return("");
                }

                stringRes = string.IsNullOrEmpty(Output) ? "" : Output + (Output.EndsWith("\n") ? "" : "\n");

                stringRes += result == null ? "" : result.AsString();
                stringRes += (stringRes.EndsWith("\n") ? "" : "\n");
            }
            catch (Exception exc)
            {
                Console.WriteLine("ProcessRepl Exception: " + exc);
                return(""); // The exception was already thrown and sent back.
            }
            finally
            {
                ReplMode = false;
            }


            return(stringRes);
        }
Пример #7
0
        public void Process(string script)
        {
            m_data        = Utils.ConvertToScript(script);
            m_currentChar = 0;

            while (m_currentChar < m_data.Length)
            {
                Parser.LoadAndCalculate(m_data, ref m_currentChar, Constants.END_PARSE_ARRAY);
                Utils.GoToNextStatement(m_data, ref m_currentChar);
            }
        }
Пример #8
0
        protected override Variable Evaluate(ParsingScript script)
        {
            List<Variable> args = script.GetFunctionArgs();
            Utils.CheckArgs(args.Count, 1, m_name);

            string json = args[0].AsString();

            Dictionary<int, int> d;
            json = Utils.ConvertToScript(json, out d);

            var tempScript = script.GetTempScript(json);
            Variable result = ExtractValue(tempScript);
            return result;
        }
Пример #9
0
        public ParsingScript GetIncludeFileScript(string filename)
        {
            string pathname = GetFilePath(filename);

            string[] lines = Utils.GetFileLines(pathname);

            string includeFile = string.Join(Environment.NewLine, lines);
            Dictionary <int, int> char2Line;
            var           includeScript = Utils.ConvertToScript(includeFile, out char2Line, pathname);
            ParsingScript tempScript    = new ParsingScript(includeScript, 0, char2Line);

            tempScript.Filename       = pathname;
            tempScript.OriginalScript = string.Join(Constants.END_LINE.ToString(), lines);
            tempScript.ParentScript   = this;
            tempScript.InTryBlock     = InTryBlock;

            return(tempScript);
        }
Пример #10
0
        protected override Variable Evaluate(string data, ref int from)
        {
            string filename = Utils.GetItem(data, ref from).AsString();

            string[] lines = Utils.GetFileLines(filename);

            string includeFile   = string.Join(Environment.NewLine, lines);
            string includeScript = Utils.ConvertToScript(includeFile);

            int filePtr = 0;

            while (filePtr < includeScript.Length)
            {
                Parser.LoadAndCalculate(includeScript, ref filePtr,
                                        Constants.END_LINE_ARRAY);
                Utils.GoToNextStatement(includeScript, ref filePtr);
            }
            return(Variable.EmptyInstance);
        }
Пример #11
0
        string ProcessRepl(string repl)
        {
            ReplMode = true;

            Dictionary <int, int> char2Line;
            string        script     = Utils.ConvertToScript(repl, out char2Line);
            ParsingScript tempScript = new ParsingScript(script, 0, char2Line);

            tempScript.OriginalScript = repl;
            tempScript.Debugger       = this;

            Variable result = null;

            try
            {
                while (tempScript.Pointer < script.Length)
                {
                    result = DebuggerUtils.Execute(tempScript);
                    tempScript.GoToNextStatement();
                }
            }
            catch (Exception exc)
            {
                return("Exception thrown: " + exc.Message);
            }
            finally
            {
                ReplMode = false;
            }

            string stringRes = Output + "\n";

            stringRes += result == null ? "" : result.AsString();

            return(stringRes);
        }
Пример #12
0
        async Task ProcessClientCommand(string data)
        {
            string load = "";

            DebuggerUtils.DebugAction action = DebuggerUtils.StringToAction(data, ref load);
            string result = "N/A";

            SteppingIn     = SteppingOut = false;
            SendBackResult = true;
            m_firstBlock   = true;
            End            = false;
            string responseToken = DebuggerUtils.ResponseMainToken(action);

            //Trace("REQUEST: " + data);
            if (action == DebuggerUtils.DebugAction.REPL ||
                action == DebuggerUtils.DebugAction._REPL)
            {
                string filename = "";
                if (action == DebuggerUtils.DebugAction.REPL)
                {
                    int ind = load.IndexOf('|');
                    if (ind >= 0)
                    {
                        if (ind > 0)
                        {
                            filename = load.Substring(0, ind);
                        }
                        if (ind + 1 < load.Length)
                        {
                            load = load.Substring(ind + 1);
                        }
                    }
                }
                result = await ProcessRepl(load, filename);

                result = responseToken + (result == null ? "" : result);
                SendBack(result, false);
                return;
            }
            if (action == DebuggerUtils.DebugAction.SET_BP)
            {
                TheBreakpoints.AddBreakpoints(this, load);
                return;
            }

            if (action == DebuggerUtils.DebugAction.FILE)
            {
                MainInstance = this;
                string filename  = load;
                string rawScript = Utils.GetFileContents(filename);

                if (string.IsNullOrWhiteSpace(rawScript))
                {
                    ProcessException(null, new ParsingException("Could not load script " + filename));
                    return;
                }

                try
                {
                    m_script = Utils.ConvertToScript(rawScript, out m_char2Line, filename);
                }
                catch (ParsingException exc)
                {
                    ProcessException(m_debugging, exc);
                    return;
                }
                m_debugging                = new ParsingScript(m_script, 0, m_char2Line);
                m_debugging.Filename       = filename;
                m_debugging.MainFilename   = m_debugging.Filename;
                m_debugging.OriginalScript = rawScript;
                m_debugging.Debugger       = this;

                m_steppingIns.Clear();
                m_completedStepIn.Reset();
                ProcessingBlock = SendBackResult = InInclude = End = false;
                m_blockLevel    = m_maxBlockLevel = 0;
            }
            else if (action == DebuggerUtils.DebugAction.VARS)
            {
                result = GetAllVariables(m_debugging);
            }
            else if (action == DebuggerUtils.DebugAction.ALL)
            {
                result = await DebugScript();
            }
            else if (action == DebuggerUtils.DebugAction.STACK)
            {
                result = GetStack();
            }
            else if (action == DebuggerUtils.DebugAction.CONTINUE)
            {
                Continue = true;
                action   = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.STEP_IN)
            {
                SteppingIn = true;
                Continue   = false;
                action     = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.STEP_OUT)
            {
                SteppingOut = true;
                Continue    = false;
                action      = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.NEXT)
            {
                Continue = false;
            }
            else
            {
                Console.WriteLine("UNKNOWN CMD: {0}", data);
                return;
            }

            if (action == DebuggerUtils.DebugAction.NEXT)
            {
                if (m_debugging == null)
                {
                    result = "Error: Not initialized";
                    Console.WriteLine(result);
                }
                else
                {
                    Variable res = await ProcessNext();

                    //Trace("MAIN Ret:" + (LastResult != null && LastResult.IsReturn) +
                    //      " NULL:" + (res == null) + " db.sb=" + m_debugging.Debugger.SendBackResult +
                    //      " PTR:" + m_debugging.Pointer + "/" + m_script.Length);
                    if (End)
                    {
                        responseToken = DebuggerUtils.ResponseMainToken(DebuggerUtils.DebugAction.END);
                    }
                    else if (res == null || !SendBackResult)
                    {
                        // It will be processed by the stepped-out OR by completed stepped-in code.
                        return;
                    }
                    else
                    {
                        result = CreateResult(Output);
                        if (string.IsNullOrEmpty(result))
                        {
                            return;
                        }
                    }
                }
            }

            result = responseToken + result;
            SendBack(result, true);
        }
Пример #13
0
        string ConvertScript()
        {
            Dictionary <int, int> char2Line = null;

            m_cscsCode = Utils.ConvertToScript(m_cscsCode, out char2Line);

            ParsingScript script = new ParsingScript(m_cscsCode);

            m_converted.Clear();

            int numIndex    = 0;
            int strIndex    = 0;
            int arrNumIndex = 0;
            int arrStrIndex = 0;
            int mapNumIndex = 0;
            int mapStrIndex = 0;

            // Create a mapping from the original function argument to the element array it is in.
            for (int i = 0; i < m_defArgs.Length; i++)
            {
                Variable typeVar = m_argsMap[m_defArgs[i]];
                m_paramMap[m_defArgs[i]] =
                    typeVar.Type == Variable.VarType.STRING ? "__varStr[" + (strIndex++) + "]" :
                    typeVar.Type == Variable.VarType.NUMBER ? "__varNum[" + (numIndex++) + "]" :
                    typeVar.Type == Variable.VarType.ARRAY_STR ? "__varArrStr[" + (arrStrIndex++) + "]" :
                    typeVar.Type == Variable.VarType.ARRAY_NUM ? "__varArrNum[" + (arrNumIndex++) + "]" :
                    typeVar.Type == Variable.VarType.MAP_STR ? "__varMapStr[" + (mapStrIndex++) + "]" :
                    typeVar.Type == Variable.VarType.MAP_NUM ? "__varMapNum[" + (mapNumIndex++) + "]" :
                    "";
            }

            m_converted.AppendLine("using System; using System.Collections; using System.Collections.Generic;\n\n" +
                                   "namespace SplitAndMerge {\n" +
                                   "  public partial class Precompiler {\n" +
                                   "    public static Variable " + m_functionName +
                                   "(List<string> __varStr, List<double> __varNum," +
                                   " List<List<string>> __varArrStr, List<List<double>> __varArrNum," +
                                   " List<Dictionary<string, string>> __varMapStr, List<Dictionary<string, double>> __varMapNum) {");
            m_depth = "      ";

            m_statements  = TokenizeScript(m_cscsCode);
            m_statementId = 0;
            while (m_statementId < m_statements.Count)
            {
                m_currentStatement = m_statements[m_statementId];
                m_nextStatement    = m_statementId < m_statements.Count - 1 ? m_statements[m_statementId + 1] : "";
                string converted = ProcessStatement(m_currentStatement, m_nextStatement);
                if (!string.IsNullOrWhiteSpace(converted) && !converted.StartsWith(m_depth))
                {
                    m_converted.Append(m_depth);
                }
                m_converted.Append(converted);
                m_statementId++;
            }

            if (!m_lastStatementReturn)
            {
                m_converted.AppendLine(m_depth + "return Variable.EmptyInstance;");
            }

            //Debugger.Break ();

            m_converted.AppendLine("\n    }\n  }\n}");
            return(m_converted.ToString());
        }
Пример #14
0
        string ConvertScript(bool cscsStyle = true)
        {
            m_converted.Length = 0;

            int numIndex    = 0;
            int strIndex    = 0;
            int arrNumIndex = 0;
            int arrStrIndex = 0;
            int mapNumIndex = 0;
            int mapStrIndex = 0;
            int varIndex    = 0;

            // Create a mapping from the original function argument to the element array it is in.
            for (int i = 0; i < m_defArgs.Length; i++)
            {
                Variable typeVar = m_argsMap[m_defArgs[i]];
                m_paramMap[m_defArgs[i]] =
                    typeVar.Type == Variable.VarType.STRING ? "__varStr[" + (strIndex++) + "]" :
                    typeVar.Type == Variable.VarType.NUMBER ? "__varNum[" + (numIndex++) + "]" :
                    typeVar.Type == Variable.VarType.ARRAY_STR ? "__varArrStr[" + (arrStrIndex++) + "]" :
                    typeVar.Type == Variable.VarType.ARRAY_NUM ? "__varArrNum[" + (arrNumIndex++) + "]" :
                    typeVar.Type == Variable.VarType.MAP_STR ? "__varMapStr[" + (mapStrIndex++) + "]" :
                    typeVar.Type == Variable.VarType.MAP_NUM ? "__varMapNum[" + (mapNumIndex++) + "]" :
                    typeVar.Type == Variable.VarType.VARIABLE ? "__varVar[" + (varIndex++) + "]" :
                    "";
            }

            m_converted.AppendLine("using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; " +
                                   "using System.Globalization; using System.Linq; using System.Linq.Expressions; using System.Reflection; " +
                                   "using System.Text; using System.Threading;  using static System.Math;");
            for (int i = 0; i < s_namespaces.Count; i++)
            {
                m_converted.AppendLine(s_namespaces[i]);
            }
            m_converted.AppendLine("namespace SplitAndMerge {\n" +
                                   "  public partial class Precompiler {");

            if (AsyncMode)
            {
                m_converted.AppendLine("    public static async Task<Variable> " + m_functionName);
            }
            else
            {
                m_converted.AppendLine("    public static Variable " + m_functionName);
            }
            m_converted.AppendLine(
                "(List<string> __varStr,\n" +
                " List<double> __varNum,\n" +
                " List<List<string>> __varArrStr,\n" +
                " List<List<double>> __varArrNum,\n" +
                " List<Dictionary<string, string>> __varMapStr,\n" +
                " List<Dictionary<string, double>> __varMapNum,\n" +
                " List<Variable> __varVar) {\n");
            m_depth = "      ";

            m_converted.AppendLine("     string __current = \"\";");
            m_converted.AppendLine("     string __argsStr = \"\";");
            m_converted.AppendLine("     string __action = \"\";");
            m_converted.AppendLine("     ParsingScript __script = null;");
            m_converted.AppendLine("     ParserFunction __func = null;");
            m_converted.AppendLine("     Variable __tempVar = null;");
            m_newVariables.Add("__current");
            m_newVariables.Add("__argsStr");
            m_newVariables.Add("__action");
            m_newVariables.Add("__script");
            m_newVariables.Add("__func");
            m_newVariables.Add("__tempVar");

            if (!cscsStyle)
            {
                //m_converted.AppendLine(m_originalCode);
                m_cscsCode = m_originalCode;
            }
            else
            {
                Dictionary <int, int> char2Line;
                m_cscsCode = Utils.ConvertToScript(m_originalCode, out char2Line);
            }
            ParsingScript script = new ParsingScript(m_cscsCode);

            m_cscsCode = m_cscsCode.Trim();
            if (m_cscsCode.Length > 0 && m_cscsCode.First() == '{')
            {
                m_cscsCode = m_cscsCode.Remove(0, 1);
                while (m_cscsCode.Length > 0 && m_cscsCode.Last() == ';')
                {
                    m_cscsCode = m_cscsCode.Remove(m_cscsCode.Length - 1, 1);
                }
                if (m_cscsCode.Length > 0 && m_cscsCode.Last() == '}')
                {
                    m_cscsCode = m_cscsCode.Remove(m_cscsCode.Length - 1, 1);
                }
            }

            m_statements  = TokenizeScript(m_cscsCode);
            m_statementId = 0;
            while (m_statementId < m_statements.Count)
            {
                m_currentStatement = m_statements[m_statementId];
                m_nextStatement    = m_statementId < m_statements.Count - 1 ? m_statements[m_statementId + 1] : "";
                string converted = ProcessStatement(m_currentStatement, m_nextStatement, true, cscsStyle);
                if (!string.IsNullOrEmpty(converted) && !converted.StartsWith(m_depth))
                {
                    m_converted.Append(m_depth);
                }
                m_converted.Append(converted);
                m_statementId++;
            }

            if (!m_lastStatementReturn)
            {
                m_converted.AppendLine(CreateReturnStatement("Variable.EmptyInstance"));
            }

            m_converted.AppendLine("\n    }\n  }\n}");
            return(m_converted.ToString());
        }
Пример #15
0
        void ProcessClientCommand(string data)
        {
            string load = "";

            DebuggerUtils.DebugAction action = DebuggerUtils.StringToAction(data, ref load);
            string result = "N/A";

            SteppingIn     = SteppingOut = false;
            SendBackResult = true;
            m_firstBlock   = true;
            string responseToken = DebuggerUtils.ResponseMainToken(action);

            Trace("REQUEST: " + data);
            if (action == DebuggerUtils.DebugAction.REPL ||
                action == DebuggerUtils.DebugAction._REPL)
            {
                result = responseToken + ProcessRepl(load);
                SendBack(result);
                return;
            }
            if (action == DebuggerUtils.DebugAction.SET_BP)
            {
                TheBreakpoints.AddBreakpoints(this, load);
                return;
            }

            if (action == DebuggerUtils.DebugAction.FILE)
            {
                MainInstance = this;
                string filename  = load;
                string rawScript = Utils.GetFileContents(filename);

                m_script                   = Utils.ConvertToScript(rawScript, out m_char2Line);
                m_debugging                = new ParsingScript(m_script, 0, m_char2Line);
                m_debugging.Filename       = filename;
                m_debugging.OriginalScript = m_script;
                m_debugging.Debugger       = this;
            }
            else if (action == DebuggerUtils.DebugAction.VARS)
            {
                result = GetAllVariables(m_debugging);
            }
            else if (action == DebuggerUtils.DebugAction.ALL)
            {
                result = DebugScript();
            }
            else if (action == DebuggerUtils.DebugAction.STACK)
            {
                result = GetStack();
            }
            else if (action == DebuggerUtils.DebugAction.CONTINUE)
            {
                Continue = true;
                action   = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.STEP_IN)
            {
                SteppingIn = true;
                Continue   = false;
                action     = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.STEP_OUT)
            {
                SteppingOut = true;
                Continue    = false;
                action      = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.NEXT)
            {
                Continue = false;
            }
            else
            {
                Console.WriteLine("UNKNOWN CMD: {0}", data);
                return;
            }

            if (action == DebuggerUtils.DebugAction.NEXT)
            {
                if (m_debugging == null)
                {
                    result = "Error: Not initialized";
                    Console.WriteLine(result);
                }
                else
                {
                    Variable res = ProcessNext();
                    Trace("MAIN Ret:" + (LastResult != null && LastResult.IsReturn) +
                          " NULL:" + (res == null) + " db.sb=" + m_debugging.Debugger.SendBackResult +
                          " PTR:" + m_debugging.Pointer + "/" + m_script.Length);
                    if (End)
                    {
                        responseToken = DebuggerUtils.ResponseMainToken(DebuggerUtils.DebugAction.END);
                    }
                    else if (res == null || !SendBackResult)
                    {
                        // It will be processed by the stepped-out OR by completed stepped-in code.
                        return;
                    }
                    else
                    {
                        result = CreateResult(Output);
                    }
                }
            }

            result = responseToken + result;
            SendBack(result);
        }