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

📄 tree.cs

📁 c--解释器
💻 CS
📖 第 1 页 / 共 5 页
字号:
using System;
using System.Threading;
using System.Collections;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace compiler
{
    class Tree 
    {
        //是否已经读入
         bool canRead = false;

        //当前的read内容
         string readValue = "";

        //输入 输出位置
        RichTextBox theOutIn;
        //提示位置
        Label lable1;
        
        //初始化代理
        public delegate void MyInvoke(string str);
        public delegate void TheInvoke(string str);
        public delegate void Remind(bool isSee);

        public void change(string s) 
        {
            this.theOutIn.AppendText(s);
        }

         public void setText(string s) 
        {
            this.theOutIn.Text=s;
        }

        public void setRemind(bool isSee) 
        {
            this.lable1.Visible = isSee;
        }

        //当有循环语句时,要检查是否有错误,否则会陷入死循环
        int errorCount = 0;
       
        
        //要输出的buffer
        ArrayList writeBuffer=new ArrayList() ;
        //记录buffer指针
        int bIindex=0;
        
        //设置当前 读入情况
       public void setRead(string readValuefrom) 
        {
           this.readValue = readValuefrom;
            this.canRead = true;
        }

        //打印tree
        string showTree="";

       //树根
        TreeNode the_top;
        TreeNode the_root;

        //符号栈
        Stack the_stack = new Stack();
        
        //方法表
        ArrayList FunctTable = new ArrayList();
        
        //constructor
        public Tree(TreeNode  the_top_from,TreeNode the_root_from)
        {
            this.the_top = the_top_from;
            this.the_root = the_root_from;
        }
        
        //show方法
        void show(Object ob, string father, TreeNode the_father) 
        {
            try 
            {
                //是树的节点
                if(ob is TreeNode)
                {
                    TreeNode tob=(TreeNode)ob;
                    showTree += father+"--"+tob.getName()+"\n";
                    //有孩子
                    if(tob.getChildCount()>0)
                    {
                        string self = "";
                        for (int i = 1; i < (tob.getName().Length) + 3;i++ ) 
                        {
                            self += " ";
                        }

                        self += "|";
                        string befather = "";
                        
                        //其父亲只有有一个孩子
                        if(the_father.getChildCount()==1)
                        {
                            string father2 = father.Remove(father.Length - 2) + " ";
                            befather = father2 + self;
                        }
                        //其父亲有多个孩子
                        else { befather = father + self; }

                        showTree+= befather + "\n";

                        int index = 0;
                        while (index < tob.getChildCount()) 
                        {
                            show(tob.getAChild(index), befather, tob);
                            index++;
                        }
                        return;
                    }
                    //没有孩子
                    else { return; }
                
                }
                else if (ob is Token) 
                {
                    Token tob = (Token)ob;
                    showTree += father + "--" + tob.getValue() + "\n";
                    return;
                }
                else
                { throw new Exception("wrong,,,,,,"); }
            }
            catch (Exception e) { Console.WriteLine(e.ToString()); }
        }

        //返回打印结果
        public string printTree() 
        {
            show(the_root, "    ", the_top);
            return this.showTree;
        
        }

        //语义分析
        public void secondPass()
        {
            try
            {

                this.the_stack.Push(new ArrayList());

               

                //查看是否有入口
                if (ForEnter())
                {

                    //调用动作

                    action(the_root, the_top);
                    
                   // ((AutoResetEvent)stateInfo).Set();

                }
                else { throw new Exception("缺少main方法"); }
            }
            catch (Exception e)
            {
                //加入错误队列 
                errorCount++;
                MyInvoke mi = new MyInvoke(change);
                theOutIn.BeginInvoke(mi, e.Message);
            }
        }

       //查找入口
        bool ForEnter() 
        {
            for (int i=0; i < the_root.getChildCount(); i++) 
            {
                TreeNode the_funct = (TreeNode)((TreeNode)the_root.getAChild(i)).getAChild(0);
                if (the_funct.getType() == 9)
                {
                    string name = ((Token)the_funct.getAChild(1)).getValue();
                    if (name == "main")
                    {
                        return true;
                    }
                }
            }

            return false;
        }


        //动作
        void action(TreeNode the_node, TreeNode the_father) 
        {
            switch(the_node.getType())
            {
                //程序
                case 0: the_root_action(the_node); return;
                
                //分程序
                case 1: action((TreeNode)the_node.getAChild(0), the_node); return;

                //全局声明
                case 2: action((TreeNode)the_node.getAChild(0), the_node); return;

                //常量声明
                case 3: the_con_decl_action(the_node); return;
               
                //常量定义
                case 4: the_con_def_action(the_node); return;
                
                //数组声明
                case 5: action((TreeNode)the_node.getAChild(1), the_node); return;

                //数组定义
                case 6: the_array_def_action(the_node, the_father); return;

                //变量声明
                case 7: the_var_decl_action(the_node); return;
                
                //变量定义
                case 8: the_var_def_action(the_node, the_father); return;
                
                //方法部分
                case 9: funct_action(the_node); return;

                
            }
        }

        //程序 动作
        void the_root_action(TreeNode the_root)
        {
            try 
            {
                for (int index = 0; index < the_root.getChildCount();index++ )
                { 
                    action((TreeNode)the_root.getAChild(index),the_root); 
                }
                return;
            }
            catch(Exception e)
            {
                errorCount++; MyInvoke mi = new MyInvoke(change);
                theOutIn.BeginInvoke(mi, e.Message);
                return;
            }
        }

        //方法部分 动作
        void funct_action(TreeNode the_funct) 
        {
            try 
            {
                string name = ((Token)the_funct.getAChild(1)).getValue();
                if (name == "main")
                {
                    //加一层
                    this.the_stack.Push(new ArrayList());
                    string reType = ((Token)((TreeNode)the_funct.getAChild(0)).getAChild(0)).getValue();
                    if (reType == "void") 
                    {
                        the_function_action(the_funct, null);
                    } 
                    else
                    { throw new Exception("main  方法的返回类型必须为void"); }
                }
                else
                { Enter(name, true, the_funct); }
            }
            catch (Exception e)
            {
                errorCount++; MyInvoke mi = new MyInvoke(change);
                theOutIn.BeginInvoke(mi, e.Message);
                return;
            }

        }


        //常量声明 动作
        void the_con_decl_action(TreeNode the_con_decl) 
        {
            try 
            {
                for (int index = 0; index < the_con_decl.getChildCount(); index++) 
                {
                    action((TreeNode)the_con_decl.getAChild(index), the_con_decl);
                }

                return;
            }
            catch (Exception e)
            {
                errorCount++; MyInvoke mi = new MyInvoke(change);
                theOutIn.BeginInvoke(mi, e.Message);
                return;
            }
        }

        //常量定义 动作
        void the_con_def_action(TreeNode the_con_def) 
        {
            try
            {
                string key = ((Token)the_con_def.getAChild(0)).getValue();

                Token number = (Token)the_con_def.getAChild(2);

                int type = number.getNumType();

                if (type == 1)
                {
                    int value = Convert.ToInt32(number.getValue());
                     Enter(key,1,value);

                    return;
                }
                else if (type == 2)
                {
                    double value = Convert.ToDouble(number.getValue());
                      Enter(key,2,value);

                    return;
                }
                else
                { throw new ComException(number.getRow(), 10); }
            }

⌨️ 快捷键说明

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