Exemplo n.º 1
0
        public CompilerResult Run(string input, RichTextBox textHighlight)
        {
            CompilerResult compilerresult = new CompilerResult();
            string         output         = null;

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

            object scannerinstance = assembly.CreateInstance("TinyPG.Debug.Scanner");
            Type   scanner         = scannerinstance.GetType();

            object parserinstance = (IParser)assembly.CreateInstance("TinyPG.Debug.Parser", true, BindingFlags.CreateInstance, null, new object[] { scannerinstance }, null, null);
            Type   parsertype     = parserinstance.GetType();

            object     treeinstance = parsertype.InvokeMember("Parse", BindingFlags.InvokeMethod, null, parserinstance, new object[] { input });
            IParseTree itree        = treeinstance as IParseTree;

            compilerresult.ParseTree = itree;
            Type treetype = treeinstance.GetType();

            List <IParseError> errors = (List <IParseError>)treetype.InvokeMember("Errors", BindingFlags.GetField, null, treeinstance, null);

            if (textHighlight != null && errors.Count == 0)
            {
                // try highlight the input text
                object highlighterinstance = assembly.CreateInstance("TinyPG.Debug.TextHighlighter", true, BindingFlags.CreateInstance, null, new object[] { textHighlight, scannerinstance, parserinstance }, null, null);
                if (highlighterinstance != null)
                {
                    output += "Highlighting input..." + "\r\n";
                    Type highlightertype = highlighterinstance.GetType();
                    // highlight the input text only once
                    highlightertype.InvokeMember("HighlightText", BindingFlags.InvokeMethod, null, highlighterinstance, null);

                    // let this thread sleep so background thread can highlight the text
                    System.Threading.Thread.Sleep(20);

                    // dispose of the highlighter object
                    highlightertype.InvokeMember("Dispose", BindingFlags.InvokeMethod, null, highlighterinstance, null);
                }
            }
            if (errors.Count > 0)
            {
                foreach (IParseError err in errors)
                {
                    output += string.Format("({0},{1}): {2}\r\n", err.Line, err.Column, err.Message);
                }
            }
            else
            {
                output += "Parse was successful." + "\r\n";
                output += "Evaluating...";

                // parsing was successful, now try to evaluate... this should really be done on a seperate thread.
                // e.g. if the thread hangs, it will hang the entire application (!)
                try
                {
                    compilerresult.Value = itree.Eval(null);
                    if (errors != null && errors.Count > 0)
                    {
                        output += "\r\nSemantics Errors: \r\n";
                        foreach (IParseError err in errors)
                        {
                            output += string.Format("({0},{1}): {2}\r\n", err.Line, err.Column, err.Message);
                        }
                    }
                    else
                    {
                        output += "\r\nResult: " + (compilerresult.Value == null ? "null" : compilerresult.Value.ToString());
                    }
                }
                catch (Exception exc)
                {
                    output += "\r\nException occurred: " + exc.Message;
                    output += "\r\nStacktrace: " + exc.StackTrace;
                }
            }
            compilerresult.Output = output.ToString();
            return(compilerresult);
        }