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

📄 tree.cs

📁 c--解释器
💻 CS
📖 第 1 页 / 共 5 页
字号:
            catch (ComException ce)
            {
                errorCount++; MyInvoke mi = new MyInvoke(change);
                theOutIn.BeginInvoke(mi, ce.getMessage());
                return;
            }
            catch (Exception e)
            {
                errorCount++; MyInvoke mi = new MyInvoke(change);
                theOutIn.BeginInvoke(mi, e.Message);
                return;
            }
        }

        //数组定义 动作
        void the_array_def_action(TreeNode the_array_def, TreeNode the_father)
        {
            try 
            {
                string key = ((Token)the_array_def.getAChild(0)).getValue();
                int elemCount = 0;
                Token go = (Token)the_array_def.getAChild(2);
                string goValue = go.getValue();

                //要赋初值
                if (go.getNumType() > 0)
                {
                    //确定数值类型
                    string type = ((Token)the_father.getAChild(0)).getValue();
                    //确定数组元素个数
                    elemCount = the_array_def.getChildCount() - 2;

                    // int 型数组
                    if (type == "int")
                    {
                        //记录数组变量
                        Enter(key,1,elemCount,true);

                        //记录每个元素
                        for (int i = 0; i < elemCount; i++)
                        {
                            string elemKey = key + "[" + i + "]";
                            int value = Convert.ToInt32(((Token)the_array_def.getAChild(i + 2)).getValue());
                            Enter(elemKey, 1, value);
                        }
                    }
                    //real 型数组
                    else if (type == "real")
                    {
                        //记录数组变量
                        Enter(key,2,elemCount,true);

                        //记录每个元素
                        for (int i = 0; i < elemCount; i++)
                        {
                            string elemKey = key + "[" + i + "]";
                            double value = Convert.ToDouble(((Token)the_array_def.getAChild(i + 2)).getValue());
                            Enter(elemKey, 2, value);
                        }
                    }
                    else { throw new Exception("错误数值类型"); }
                }
                //仅声明
                else 
                {
                    //确定数组元素个数
                    elemCount = Convert.ToInt32(((Token)the_array_def.getAChild(3)).getValue());

                    if (goValue == "int") 
                    {
                        Enter(key, 1, elemCount, true);

                        //记录每个元素
                        for (int i = 0; i < elemCount; i++) 
                        {
                            string elemKey = key + "[" + i + "]";
                            Enter(elemKey, 1, 0);
                        }
                    }
                    else if (goValue == "real") 
                    {
                        Enter(key, 2, elemCount, true);
                        //记录每个元素
                        for (int i = 0; i < elemCount; i++)
                        {
                            string elemKey = key + "[" + i + "]";
                            
                            Enter(elemKey, 2, 0.0);
                        }
                    }
                    else { throw new Exception("错误数值类型"); }

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

        //变量声明 动作
        void the_var_decl_action(TreeNode the_var_decl) 
        {
            try 
            {
                for (int index = 1; index < the_var_decl.getChildCount(); index++)
                {
                    action((TreeNode)the_var_decl.getAChild(index), the_var_decl);
                }
            }
            catch (Exception e)
            {
                //加入错误队列 
                errorCount++; MyInvoke mi = new MyInvoke(change);
                theOutIn.BeginInvoke(mi, e.Message);

                return;
            }
        }

        //变量定义 动作
        void the_var_def_action(TreeNode the_var_def, TreeNode the_father)
        {
            try
            {
                string key = ((Token)the_var_def.getAChild(0)).getValue();
                string type = ((Token)the_father.getAChild(0)).getValue();

                //int
                if (type == "int") 
                {
                   
                    if (the_var_def.getChildCount() == 1)
                    {
                        Enter(key,1,0); 
                    }
                    else
                    {
                        int value = (Int32)(the_expression_action((TreeNode)the_var_def.getAChild(2)));
                        Enter(key, 1, value);
                    }
                }
                //real
                else if (type == "real") 
                {
                    if (the_var_def.getChildCount() == 1)
                    {
                        Enter(key, 2, 0.0);
                    }
                    else 
                    {
                        double value = 0.0;
                        Object ex= the_expression_action((TreeNode)the_var_def.getAChild(2));
                        value = Convert.ToDouble( ex);

                        Enter(key, 2, value);
                    }
                }
                //错误
                else { throw new Exception("错误数字类型"); }
            }
            catch (Exception e)
            {
                errorCount++; MyInvoke mi = new MyInvoke(change);
                theOutIn.BeginInvoke(mi, e.Message);
                return;
            }
        }

        //表达式 动作
        Object the_expression_action(TreeNode the_expression)
        {
            try
            {
                //int 队伍
                int intResult = 0;
                //double 队伍
                double douResult = 0;
                //记录double
                int showD = 0;
                //循环开始个数
                int i = 1;

                //查看第一个孩子
                Object first = the_expression.getAChild(0);

                if (first is TreeNode)
                {
                    Object fisrtValue = the_item_action((TreeNode)first);
                    if (fisrtValue is Int32) 
                    {
                        intResult += (Int32)fisrtValue;
                    }
                    else if (fisrtValue is Double) 
                    {
                        douResult += (Double)fisrtValue;
                        showD++;
                    }
                    else if (fisrtValue is Symbol) 
                    {
                        return fisrtValue; 
                    }
                    else 
                    { throw new Exception("表达式项类型错误"); }
                }
                else if (first is Token)
                {
                    TreeNode second=(TreeNode)the_expression.getAChild(1);
                    Object secValue = the_item_action(second);
                    
                    if (secValue is Int32)
                    {
                        intResult -= (Int32)secValue;
                      
                    }
                    else if (secValue is Double)
                    {
                        douResult -= (Double)secValue;
                        showD++;
                     }
                    else { throw new Exception("表达式项类型错误"); }
                    
                    i++;
                }
                else
                { throw new Exception("表达式项类型错误"); }

                for (; i < the_expression.getChildCount(); i++) 
                {
                    string sbl = ((Token)the_expression.getAChild(i)).getValue();

                    if (sbl == "+") 
                    {
                        Object next = the_item_action((TreeNode)the_expression.getAChild(++i));

                        if (next is Int32) 
                        {
                            intResult += (Int32)next;
                        }
                        else if (next is Double) 
                        {
                            douResult += (Double)next;
                            showD++;
                        }
                        else {  throw new Exception("项类型错误"); }
                    }
                    else if(sbl=="-")
                    {
                        Object next = the_item_action((TreeNode)the_expression.getAChild(++i));
                        if (next is Int32)
                        {
                            intResult -= (Int32)next;
                        }
                        else if (next is Double)
                        {
                            douResult -= (Double)next;
                            showD++;
                        }
                        else { break; throw new Exception("项类型错误"); }
                    }
                    else { break;  throw new Exception("只能出现一级运算符"); }


                }

                if (showD>0)
                {
                    return intResult + douResult;
                }
                else 
                {
                    return intResult;
                }
              
            }
            catch (Exception e)
            {
                errorCount++; MyInvoke mi = new MyInvoke(change);
                theOutIn.BeginInvoke(mi, e.Message); 
                return null;
            }
        }

        // 项 动作
        Object the_item_action(TreeNode the_item)
        {
            try 
            {
                TreeNode first=(TreeNode)the_item.getAChild(0);
                Object the_base = the_atom_action(first);

                if (the_base is Symbol)
                { 
                    return the_base; 
                }
                else if (the_base is Int32) 
                {
                    //一个int 队伍
                    int basis = (Int32)the_base;
                    //一个double 队伍
                    double basis2=1;
                    //是否有double 出现过
                    int showD=0;

                    for (int i = 1; i < the_item.getChildCount(); i++) 
                    {
                        string sbl = ((Token)the_item.getAChild(i)).getValue();
                        if (sbl == "*") 
                        {
                            Object next = the_atom_action((TreeNode)the_item.getAChild(++i));
                            if (next is Int32)
                            {
                                basis *= ((Int32)next);
                            }
                            else if (next is Double) 
                            {
                                basis2 *= (Double)next;
                                showD++;
                            }
                            else { break; throw new Exception("因子类型错误"); }
                        }
                        else if (sbl == "/") 
                        {
                            Object next = the_atom_action((TreeNode)the_item.getAChild(++i));
                            if (next is Int32)
                            {
                                basis = basis/((Int32)next);
                            }
                            else if (next is Double)
                            {
                                basis2 = basis2/((Double)next);

⌨️ 快捷键说明

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