Exemple #1
0
        public static void Main(string[] args)
        {
            NLP_Lexer.Lexer nlpLexer = new NLP_Lexer.Lexer(ACCESS_TOKEN);

            // Test input
            string inp_a = "print add 4 and 5";

            string lex_chunk_a = nlpLexer.Tokenise(inp_a);

            Console.WriteLine("Input = \n\t" + inp_a);

            Console.WriteLine(lex_chunk_a);
            try
            {
                CoreParser.Lexer lexer = new CoreParser.Lexer(lex_chunk_a);
                lexer.Tokenise();
                List <Token>               tokens = lexer.getTokenList();
                CoreParser.Parser.Parser   parser = new CoreParser.Parser.Parser();
                CoreParser.Parser.AST.Node ast    = parser.Parse(tokens);
                Engine.Engine              engine = new Engine.Engine();
                engine.Run(ast);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }
Exemple #2
0
    void RunBtnClicked(object sender, EventArgs eventArgs)
    {
        //Execute the program

        string sourceCode = editor.Buffer.Text;

        if (sourceCode.Length > 1)
        {
            if (this.useNLPLexer)
            {
                // use the NLP lexer to format the code
                var             lines          = sourceCode.Split('\n');
                NLP_Lexer.Lexer nlpLexer       = new NLP_Lexer.Lexer("7DOTRBXV6DLL22FQUJRKOMSCOEUL5XG5"); // Create NLP Lexer with access token for wit.ai
                string          tempSourceCode = "";                                                      // Init
                foreach (var line in lines)                                                               //Loop through lines
                {
                    try {
                        tempSourceCode += nlpLexer.Tokenise(line); // Add wit ai output to sourcecode
                        Console.WriteLine(tempSourceCode);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                // Make sure that there is code in ttempSourceCode before executing it
                if (tempSourceCode.Length > 0)
                {
                    sourceCode = tempSourceCode;
                }
            }

            // Start the exectuion

            CoreParser.Lexer lexer = new CoreParser.Lexer(sourceCode);
            try
            {
                // Tokenise
                lexer.Tokenise();
                List <Token> tokens = lexer.getTokenList();

                // Parse from returned token list
                CoreParser.Parser.Parser   parser = new CoreParser.Parser.Parser();
                CoreParser.Parser.AST.Node ast    = parser.Parse(tokens);

                // Execute the parse tree
                ParserEngine.Engine.Engine engine = new Engine.Engine();
                engine.Run(ast);

                // Display parse tree if user has toggled it
                if (this.useParseTreeDisplay)
                {
                    // Open a new window
                    GUI.ParseTreeDisplay parseTreeDisplay = new GUI.ParseTreeDisplay(ast);
                    parseTreeDisplay.Title = "Parse Tree";
                    parseTreeDisplay.ShowAll();
                }
            }
            // Display errors encountered during execution in alert
            catch (Exception e)
            {
                MessageDialog md = new MessageDialog(this,
                                                     DialogFlags.DestroyWithParent, MessageType.Error,
                                                     ButtonsType.Close, e.Message);
                md.Run();
                md.Destroy();
            }

            // Collect console output
            var consoleOutput = ConsoleOutput.Instance.GetOutput();
            if (consoleOutput != null)
            {
                // As long as there is console output, get the window and display it
                GUI.ConsoleWindow consoleWindow  = GUI.ConsoleWindow.Instance;
                ScrolledWindow    consoleWrapper = new ScrolledWindow(); // Make Console Window scrollable
                TextView          console        = new TextView();
                consoleWrapper.Add(console);
                consoleWindow.Add(consoleWrapper);

                // Write the console output to the console window
                foreach (var line in consoleOutput)
                {
                    // Write output to console window
                    console.Buffer.Text = console.Buffer.Text + "\n" + line;
                }
                consoleWindow.ShowAll();
            }
        }
    }