实现简单的CSharpShell -- OrcShell (1) 基本结构

简介:

一、程序的基本结构




 

程序的控制核心是Context类,它持有:

·类型管理器TypeManager,管理该运用程序域加载的命名空间及类型的树,树结构如下:

     TypeDictionary(Root)
         
|-- TypeDictionary
         
|         |-- TypeDictionary
         
|         |-- TypeDictionary
         
|         |
         
|         |-- Type
         
|         |-- Type
         
|         |
         
|
         
|-- TypeDictionary
         
|
         
|-- Type
         
|-- Type
         
|

其中TypeDictionary对应的是命名空间,Type对应的是类型。TypeManager还管理一个名为NowTypeDictionary,表示当前所在的TypeDictionary

·AliasCmds ,命令缩写字典。

·Instances,用户变量字典。

·CmdDispatcher是命令指派器。控制台获取指令后传给Context。代码:

             while  ((cmd  =  Console.ReadLine().Trim())  !=   " exit " )
            
{
                
if (!String.IsNullOrEmpty(cmd))
                
{
                    cxt.Invoke(cmd);
                }

                Console.Write(
">> ");
            }

 

Context又传给CmdDispatcher处理。CmdDispatcher解析命令,根据命令的特征选择不同的CmdHandler来处理。目前编写了5CmdDispatcher

CdClassCmdHandler:进出命名空间的处理,针对cdc指令;

ListClassCmdHandler:列出命名空间和类型,针对lsc,dirc指令;

ListInstanceCmdHandler:列出用户变量,针对 my 指令;

ListAliasCmdHandler:列出指令缩写,针对 alias 指令;

CscCmdHandler:编译并运行代码,其它CmdDispatcher 处理不了的都交给它。

CmdDispatcher.Dispatch()方法代码:

 

         public   void  Dispatch()
        
{
            String[] results 
= InputCmdString.Split(SPLITS, StringSplitOptions.None);
            
if(results.Length == 0return;

            String cmd 
= results[0];
            String mark 
= String.Empty;
            IList
<String> args = new List<String>();

            Int32 argIndex 
= 1;

            
if (results.Length > 1 && results[1].StartsWith("-"))
            
{
                argIndex 
++;
                mark 
= results[1];
            }


            
for(;argIndex < results.Length;argIndex++)
            
{
                args.Add(results[argIndex]);
            }


            
switch (cmd.ToLower())
                
{
                    
case "debug":   // 开启debug开关
                        Context.Debug = true;
                        
break;
                    
case "undebug"// 关闭debug开关
                        Context.Debug = false;
                        
break;
                    
case "cdc":     // 改变命名空间
                        new CdClassCmdHandler(Context, InputCmdString, mark, args).Run();
                        
break;
                    
case "lsc":     // 列出命名空间的内容
                    case "dirc":
                        
new ListClassCmdHandler(Context, InputCmdString, mark, args).Run();
                        
break;
                    
case "my":      // 列出用户变量
                        new ListInstanceCmdHandler(Context, InputCmdString, mark, args).Run();
                        
break;
                    
case "alias":   // 列出alias列表
                        new ListAliasCmdHandler(Context, InputCmdString, mark, args).Run();
                        
break;
                    
default:
                        String fullCmd 
= Context.GetFullCmd(cmd);
                        
if (fullCmd != null)    // 处理 alias
                        {
                            
if (mark != null) fullCmd += " " + mark;
                            
if (args != null && args.Count > 0)
                            
{
                                
foreach(String s in args)
                                
{
                                    fullCmd 
+= " " + s;
                                }

                            }


                            Context.Invoke(fullCmd);
                        }

                        
else                   // 编译代码并运行
                        {
                            
new CscCmdHandler(Context, InputCmdString).Run();
                        }

                        
break;
                }


            
return;
        }

本文转自xiaotie博客园博客,原文链接http://www.cnblogs.com/xiaotie/archive/2008/02/29/1085815.html如需转载请自行联系原作者

xiaotie 集异璧实验室(GEBLAB)
相关文章
|
8月前
获取其他结构
获取其他结构
30 0
|
8月前
|
存储 C语言
指针--基础篇
指针--基础篇
32 0
|
9月前
|
C#
C#视频-三大结构
C#视频-三大结构
37 0
|
9月前
|
算法
FFGA原理和结构
FFGA(Fast Free Fuzzy Genetic Algorithm)是一种基于模糊遗传算法的优化算法,主要用于求解复杂的优化问题。其原理和结构可以概括如下:
186 0
C结构中包含自己的嵌套定义
C结构中包含自己的嵌套定义
51 0
|
JavaScript 前端开发 搜索推荐
TypeScriopt之基本结构
TypeScriopt之基本结构
66 0
有关使用Map结构替换掉复杂的if-else结构【项目使用】
有关使用Map结构替换掉复杂的if-else结构【项目使用】
159 0
|
存储 缓存 网络协议
电子邮件系统的组成结构
电子邮件系统的组成结构
172 0
|
知识图谱
欧几里德结构数据与 非欧几里德结构数据
欧几里德结构数据与 非欧几里德结构数据
443 0
欧几里德结构数据与 非欧几里德结构数据