Esempio n. 1
0
        public void Test_Global_Var_Decls_002()
        {
            string src = Resources.Global_Var_Decls_002.GetString();

            var result = ParseHandler.Parse(src);

            string a = result.ParseTree.ToStringTree();
            int    b = 5;
        }
Esempio n. 2
0
        public void Test_Global_Var_Decls_001()
        {
            string src = Resources.Global_Var_Decls_001.GetString();

            var result = ParseHandler.Parse(src);

            Assert.IsFalse(result.IsError);
            Assert.IsNull(result.ErrorMessage);
            Assert.IsNotNull(result.ParseTree);
            Assert.AreEqual(2, result.ParseTree.ChildCount);
            Assert.IsInstanceOfType(result.ParseTree.GetChild(0).Payload, typeof(CommonToken));
            Assert.AreEqual("VAR_GLOBAL", ((CommonToken)result.ParseTree.GetChild(0).Payload).Text);
            Assert.IsInstanceOfType(result.ParseTree.GetChild(1).Payload, typeof(CommonToken));
            Assert.AreEqual("END_VAR", ((CommonToken)result.ParseTree.GetChild(1).Payload).Text);
        }
        private void InitCommonListeners()
        {
            // open/exit
            exitToolStripMenuItem.Click   += (o, e) => Application.Exit();
            openToolStripMenuItem.Click   += (o, e) => OpenSourceFile();
            saveToolStripMenuItem.Click   += (o, e) => SaveSourceFile();
            aboutToolStripMenuItem1.Click += (o, e) => _aboutBox.ShowDialog(this);

            // compile button
            compileButton.Click += (o, e) => _ilCodeHandler.Abort();
            compileButton.Click += (o, e) => ClearOutput();
            compileButton.Click += (o, e) => _parseHandler.Parse(inputTextBox.Text);

            // toggle opts
            toggleOptsButton.Click += (o, e) =>
            {
                bool allChecked = true;
                for (int i = 0; i < optsList.Items.Count; ++i)
                {
                    allChecked &= optsList.GetItemChecked(i);
                }

                for (int i = 0; i < optsList.Items.Count; ++i)
                {
                    optsList.SetItemChecked(i, !allChecked);
                }
            };
            optsList.SelectedIndexChanged          += (o, e) => optsList.ClearSelected();
            iterativeAlgoList.SelectedIndexChanged += (o, e) => iterativeAlgoList.ClearSelected();

            // AST
            _parseHandler.ParsingCompleted  += (o, e) => _astHandler.GenerateAstImage(e);
            _astHandler.GenerationCompleted += (o, e) =>
            {
                _astImage           = e;
                ASTPictureBox.Image = _astImage;
                astTrackBar.Value   = astTrackBar.Maximum;
            };

            // 3-addr code
            _parseHandler.ParsingCompleted           += _threeCodeHandler.GenerateThreeAddrCode;
            _threeCodeHandler.PrintableCodeGenerated += (o, e) => threeAddrTextBox.Text = e;
            _threeCodeHandler.GenerationErrored      += (o, ex) =>
            {
                MessageBox.Show(this, $@"Построение трехадресного кода завершилось с ошибкой:{Environment.NewLine} {ex.Message}",
                                @"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            };

            // CFG
            _threeCodeHandler.GenerationCompleted += (o, e) => _cfgHandler.GenerateCFG(e);
            _cfgHandler.GenerationCompleted       += (o, e) =>
            {
                _cfgImage           = e;
                CFGPictureBox.Image = _cfgImage;
                cfgScaleBar.Value   = cfgScaleBar.Maximum;
            };
            _cfgHandler.GenerationErrored += (o, ex) =>
            {
                MessageBox.Show(this, $@"Построение CFG завершилось с ошибкой:{Environment.NewLine} {ex.Message}",
                                @"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            };

            // iterative algorithms
            _cfgHandler.CfgGenerated += (o, e) => _algoHandler.CollectInOutData(e);
            _cfgHandler.CfgGenerated += (o, e) => CFGEdgeClassificationTextBox.Text = _cfgHandler.PrintCFGEdgeClassification(e);
            _algoHandler.PrintableInOutDataCollected += (o, e) => iterativeAlgoTextBox.Text = e;
            _algoHandler.GenerationErrored           += (o, ex) =>
            {
                MessageBox.Show(this, $@"Выполнение итеративных алгоритмов завершилось с ошибкой:{Environment.NewLine} {ex.Message}",
                                @"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            };

            // IL-code
            _threeCodeHandler.GenerationCompleted += (o, e) => _ilCodeHandler.GenerateIlCode(e);
            _ilCodeHandler.GenerationCompleted    += (o, e) =>
            {
                ILCodeTextBox.Text = e.PrintCommands();
                runButton.Enabled  = true;
            };
            _ilCodeHandler.GenerationErrored += (o, ex) =>
            {
                MessageBox.Show(this, $@"Генерация IL кода завершилась с ошибкой:{Environment.NewLine} {ex.Message}",
                                @"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            };

            // run and stop
            // no way to pass cancellation token inside DynMethod's Invoke, so doing it hard way =\
            stopButton.Click += (o, e) => _ilCodeHandler.Abort();
            runButton.Click  += (o, e) => _ilCodeHandler.Run();

            _ilCodeHandler.RuntimeStarted += (o, e) => ClearOutput();
            _ilCodeHandler.Aborted        += (o, e) =>
            {
                if (InvokeRequired)
                {
                    Invoke(new Action(() =>
                    {
                        MessageBox.Show(this, @"Запуск остановлен", @"Остановка", MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }));
                }
                else
                {
                    MessageBox.Show(this, @"Запуск остановлен", @"Остановка", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                }
            };
            _ilCodeHandler.AlreadyRunningErrored += (o, e) =>
            {
                MessageBox.Show(this, @"Программа уже запущена!", @"Ошибка", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            };
            _ilCodeHandler.RuntimeErrored += (o, e) =>
            {
                MessageBox.Show(this, $@"Запуск завершился с ошибкой:{Environment.NewLine} {e.Message}",
                                @"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            };

            // enable UI after all build steps
            _cfgHandler.GenerationCompleted += (o, e) =>
            {
                cfgSaveButton.Enabled = true;
                cfgScaleBar.Enabled   = true;

                astSaveButton.Enabled = true;
                astTrackBar.Enabled   = true;
            };
        }