⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 关于CMM语言的解释器
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml;


namespace CmmInterpretor
{
    public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
        }


        //菜单栏部分


        //打开
        private void OpenFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.DefaultExt = "xml";
            openFileDialog1.Filter = "CMM Files (*.cmm)|*.cmm|TXT Files (*.txt)|*.txt|All Files (*.*)|*.*";
            DialogResult answer = openFileDialog1.ShowDialog();
            if (answer == DialogResult.Cancel) return;
            srcPath = @openFileDialog1.FileName;

            if (SourceCodeTextBox.Text.Trim().Length != 0)
            {
                DialogResult response =
                    MessageBox.Show("该操作将清空源程序文本区并打开新文件",
                    "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (response == DialogResult.No)
                {
                    return;
                }
            }
            try
            {
                FileInfo sourceCodeFile = new FileInfo(srcPath);
                TextReader reader = new StreamReader(srcPath, Encoding.Default);
                SourceCodeTextBox.Text = reader.ReadToEnd();
            }
            catch (Exception excp)
            {
                MessageBox.Show(excp.Message, "打开文件失败", MessageBoxButtons.OK);
            }
        }
        
        //保存源代码
        private void SaveSourceCodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (srcPath.Length == 0)
            {
                try
                {
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.DefaultExt = "cmm";
                    saveFileDialog1.Filter = "CMM Files (*.cmm)|*.cmm|TXT Files (*.txt)|*.txt|All Files (*.*)|*.*";
                    if (srcPath.Length != 0) saveFileDialog1.FileName = srcPath;
                    DialogResult answer = saveFileDialog1.ShowDialog();
                    if (answer != DialogResult.Cancel)
                    {
                        srcPath = @saveFileDialog1.FileName;
                        StreamWriter sw = new StreamWriter(saveFileDialog1.OpenFile(), Encoding.Default);
                        foreach (string line in SourceCodeTextBox.Lines)
                        {
                            sw.WriteLine(line.ToString());
                        }
                        sw.Close();
                    }
                }
                catch (Exception excp)
                {
                    MessageBox.Show("保存文件失败! ", "出错", MessageBoxButtons.OK);
                }
                return;
            }
            try
            {
                StreamWriter sw = new StreamWriter(srcPath, false, Encoding.Default);
                foreach (string line in SourceCodeTextBox.Lines)
                {
                    sw.WriteLine(line.ToString());
                }
                sw.Close();
            }
            catch (Exception excp)
            {
                MessageBox.Show("保存文件失败! ", "出错", MessageBoxButtons.OK);
            }
        }
        
        //分析结果另存为
        private void AnalysisResultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.DefaultExt = "txt";
                saveFileDialog1.Filter = "TXT Files (*.txt)|*.txt|All Files (*.*)|*.*";
                DialogResult answer = saveFileDialog1.ShowDialog();
                if (answer != DialogResult.Cancel)
                {
                    StreamWriter sw = new StreamWriter(saveFileDialog1.OpenFile(), Encoding.Default);
                    foreach (string line in ResultTextBox.Lines)
                    {
                        sw.WriteLine(line.ToString());
                    }
                    sw.Close();
                }
            }
            catch (Exception excp)
            {
                MessageBox.Show("保存文件失败! ", "出错", MessageBoxButtons.OK);
            }
        }
        //语法树另存为XML文件
        private void SaveTreeToFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (GrammerTree == null)
            {
                MessageBox.Show("请先进行语法分析获得语法树。", "出错", MessageBoxButtons.OK);
                return;
            }
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.DefaultExt = "xml";
            saveFileDialog1.Filter = "XML Files (*.xml)|*.xml";
            DialogResult answer = saveFileDialog1.ShowDialog();
            if (answer != DialogResult.Cancel)
            {
                Tree tf = new Tree();
                XmlDocument treeXml = tf.Tree2XML(ref GrammerTree);
                if (treeXml != null)
                    treeXml.Save(saveFileDialog1.FileName);
            }
        }
        //退出
        private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        //词法分析
        private void AnalizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripButton2_Click(sender, e);
        }
        //语法分析
        private void GrammerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripButton3_Click(sender, e);
        }
        
        //解释运行
        private void RunToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripButton4_Click(sender, e);
        }
        
        
        //CMM帮助
        private void CMMHelpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CMMHelpForm cmmHelpform = new CMMHelpForm();
            cmmHelpform.Show();
        }
        

        //菜单栏部分结束



        //按钮部分

        
        //清空结果
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            DialogResult response =
                MessageBox.Show("确定清空分析结果?",
                "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (response == System.Windows.Forms.DialogResult.No)
            {
                return;
            }

            ResultTextBox.Text = "";
        }
        
        //词法分析
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            ResultTextBox.Text = "词法分析结果\r\n";
            string sourceCode = this.GetSourceCode();
            if (sourceCode.Length == 0)
            {
                ResultTextBox.Text += "\r\n源代码文本区无内容。\r\n";
                return;
            }

            string analysisText = "";
            string errorText = "";
            WordAnalysis wordAnalysis = new WordAnalysis();
            wordAnalysis.ErrorNum = 0; //归0(set方法已重载)
            wordAnalysis.AnalyzeCode(sourceCode, ref analysisText, ref errorText);

            ResultTextBox.Text += analysisText
                                + "\r\n分析完成,共找到"
                                + Convert.ToString(wordAnalysis.ErrorNum)
                                + "个词法错误。\r\n"
                                + errorText
                                + "\r\n可从菜单“帮助”-“CMM语言说明”获取提示。\r\n";

        }
        //语法分析
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            ResultTextBox.Text = "语法分析结果 \r\n\r\n";
            string sourceCode = this.GetSourceCode().Trim();
            if (sourceCode.Length == 0)
            {
                ResultTextBox.Text += "源代码文本区无内容。\r\n";
                return;
            }

            string analysisText = "";
            GrammerAnalysis grammerAnalysis = new GrammerAnalysis();
            grammerAnalysis.ErrorNum = 0; //归0(set方法已重载)
            grammerAnalysis.AnalyzeCode(sourceCode, ref analysisText, ref GrammerTree);

            ResultTextBox.Text += analysisText
                                + "\r\n分析完成,共找到"
                                + Convert.ToString(grammerAnalysis.ErrorNum)
                                + "个语法错误。\r\n"
                                + "\r\n可从菜单“帮助”-“CMM语言说明”获取提示。";
        }
        
        //解释运行
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            ResultTextBox.Text = "开始执行程序...\r\n\r\n";
            string sourceCode = this.GetSourceCode();
            if (sourceCode.Length == 0)
            {
                ResultTextBox.Text += "\r\n源代码文本区无内容。\r\n";
                return;
            }

            interpretor = new Interpretor();
            interpretor.RunAllCode(ref sourceCode, ref ResultTextBox);

            if (interpretor.IsTmpStopped)
            {
                isInputting = true;
                ResultTextBox.ReadOnly = false;
                ResultTextBox.Focus();
                if (ResultTextBox.Text.Length > 0)
                    ResultTextBox.SelectionStart = ResultTextBox.Text.Length;
                lastInputString = "";
                lastSrc = sourceCode;
            }
            else if (interpretor.HasFinished)
            {
                ResultTextBox.Text += "\r\n执行程序结束。\r\n";
                isInputting = false;
                ResultTextBox.ReadOnly = true;
            }
        }

        //按钮部分结束


 

        private void ResultTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!isInputting)
                ResultTextBox.ReadOnly = true;
        }

        private void ResultTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            char inChar = e.KeyChar;

            //允许输入的字符
            if ((inChar >= '0' && inChar <= '9') || inChar == '.' || inChar == 'e' ||
                inChar == '+' || inChar == '-' || inChar == '*' || inChar == '/' ||
                inChar == '(' || inChar == ')')
            {
                lastInputString += inChar;
            }

            //对退格键的处理
            else if (inChar == (char)Keys.Back)
            {
                if (lastInputString.Length > 0)
                    lastInputString = lastInputString.Remove(lastInputString.Length - 1);
                else
                    e.Handled = true;
            }

            //对回车键的处理
            else if (inChar == (char)Keys.Return)
            {
                e.Handled = true;

                isInputting = false;
                interpretor.IsTmpStopped = false;
                ResultTextBox.Text += "\r\n";
                interpretor.GoOnRunCode(ref lastSrc, lastInputString);
                if (interpretor.IsTmpStopped)
                {
                    isInputting = true;
                    ResultTextBox.ReadOnly = false;
                    lastInputString = "";
                }
                else if (interpretor.HasFinished)
                {
                    ResultTextBox.Text += "\r\n执行程序结束。\r\n";
                    isInputting = false;
                }
            }

            //对其余输入忽略
            else
            {
                e.Handled = true;
            }

            if (ResultTextBox.Text.Length > 0)
                ResultTextBox.SelectionStart = ResultTextBox.Text.Length;
        }

        //获取原代码
        public string GetSourceCode()
        {
            return this.SourceCodeTextBox.Text;
        }
        //清空源程序文本内容
        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            DialogResult response =
                MessageBox.Show("清空源程序文本内容?",
                "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (response == System.Windows.Forms.DialogResult.No)
            {
                return;
            }

            SourceCodeTextBox.Text = "";
        }





        //源文件路径
        string srcPath = "";

        //语法树
        TreeView GrammerTree = null;

        //词句类型常量
        const int ERROR = -1;        //错误

        const int RSVWORD = -11;     //保留字
        const int ID = -12;          //标识符
        const int ARR_ID = -13;      //数组标识符
        const int INTG = -14;        //整数
        const int REAL = -15;        //实数
        const int OPRT = -16;        //算术运算符 + - * /
        const int COMPAR = -17;      //比较运算符 < == <>
        const int SEMI = -18;        //分号
        const int BRACKET = -19;     //括号 ()[]{}
        const int EQUAL = -20;       //等号
        const int SYMBOL = -20;      //符号

        const int NULLST = -31;      //null语句
        const int EMPTY = -32;       //空语句
        const int COMPLEX = -33;     //复合语句
        const int DECLARE = -34;     //普通变量声明语句
        const int DECLARE_ARR = -35; //数组声明语句
        const int ASSIGN = -36;      //赋值语句
        const int ASSIGN_ARR = -37;  //数组成员赋值语句
        const int DEC_ASS = -38;     //声明且赋值
        const int INPUT = -39;       //输入语句
        const int INPUT_ARR = -40;   //输入语句,标识符为数组
        const int OUTPUT = -41;      //输出语句
        const int SELECT = -42;      //if选择语句
        const int IF_ELSE = -43;     //if-else选择语句
        const int ELSE_BR = -44;     //if-else选择语句的else分支
        const int LOOP = -45;        //while循环语句
        const int ARTHM_EXP = -46;   //算术表达式
        const int RELAT_EXP = -47;

        private string lastInputString; //最近一次用户输入
        private Interpretor interpretor;
        private string lastSrc;
        private bool isInputting = false;

        

       

        
        

        
       
        

       
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -