
Context 為控制中樞,持有四大核心組件:
TypeDictionary(Root) |--TypeDictionary (Namespace) | |--TypeDictionary | | |--Type | |--Type |--TypeDictionary | |--Type |--Type
while ((cmd = Console.ReadLine().Trim()) != "exit") {
if (!String.IsNullOrEmpty(cmd)) cxt.Invoke(cmd);
Console.Write(">> ");
}
Context.Invoke 轉交 CmdDispatcher.Dispatch():
debug/undebug、cdc、lsc/dirc、my、aliasContext.InvokeCscCmdHandler 編譯執行| 處理器 | 指令 | 功能 |
|---|---|---|
| CdClassCmdHandler | cdc | 進出命名空間 |
| ListClassCmdHandler | lsc, dirc | 列出命名空間/型別 |
| ListInstanceCmdHandler | my | 列出環境變數 |
| ListAliasCmdHandler | alias | 列出指令別名 |
| CscCmdHandler | (其它) | 編譯執行 C# 代碼 |
public void Dispatch() {
var parts = InputCmdString.Split(SPLITS, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0) return;
string cmd = parts[0], mark = "";
var args = new List();
int idx = 1;
if (parts.Length > 1 && parts[1].StartsWith("-")) { mark = parts[1]; idx++; }
for (; idx < parts.Length; idx++) args.Add(parts[idx]);
switch (cmd.ToLower()) {
case "debug": Context.Debug = true; break;
case "undebug": 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": new ListAliasCmdHandler(Context, InputCmdString, mark, args).Run(); break;
default:
string fullCmd = Context.GetFullCmd(cmd);
if (fullCmd != null) { // 別名展開
if (!string.IsNullOrEmpty(mark)) fullCmd += " " + mark;
foreach (var a in args) fullCmd += " " + a;
Context.Invoke(fullCmd);
} else { // 代碼片段
new CscCmdHandler(Context, InputCmdString).Run();
}
break;
}
}