刚刚才用上C#2.0,原来是这么好用啊,迫不及待的想用熟它;
最近想到平台的体系,认为自己的脚本解释器是必不可少的,于是自己抄刀;花了将近一个星期,总算建设了一个有点象样的框架了。
基本设计:
1、基于C#2.0开发
2、通过反射动态调用LIB的方法
3、完全手写的SCRIPT解释器,不用.NET的编译器做脚本解释器(完全按行模式执行)
4、提供一个基础的控制台程序(SHELL),支持批处理模式反射调用方法
5、提供DEBUG模式的SCRIPT IDE,用于简单调试(目前解释器还有点小BUG,DEBUG功能目前还未完善)
以下是SHELL的外观图:
完全模拟DOS的命令行,所有指令为反射调用的LIB方法;通过全局的配置读入:
Environment.xml配置用于存放LIB的相关信息
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < ModuleInfoConfig xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns ="http://www.csit.com.cn" >
3 < ModuleInfo >
4 < ModuleName > C:\Documents and Settings\Administrator\桌面\MyShell\MyShell\Lib\CSIT.StdLib.dll </ ModuleName >
5 < NameSpace > CSIT.StdLib </ NameSpace >
6 < MainClassName > StdLibClass </ MainClassName >
7 < Key > STD </ Key >
8 </ ModuleInfo >
9 < ModuleInfo >
10 < ModuleName > C:\Documents and Settings\Administrator\桌面\MyShell\MyShell\Lib\CSIT.WinForms.dll </ ModuleName >
11 < NameSpace > CSIT.WinForms </ NameSpace >
12 < MainClassName > Msg </ MainClassName >
13 < Key > MSG </ Key >
14 </ ModuleInfo >
15 </ ModuleInfoConfig >
2 < ModuleInfoConfig xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns ="http://www.csit.com.cn" >
3 < ModuleInfo >
4 < ModuleName > C:\Documents and Settings\Administrator\桌面\MyShell\MyShell\Lib\CSIT.StdLib.dll </ ModuleName >
5 < NameSpace > CSIT.StdLib </ NameSpace >
6 < MainClassName > StdLibClass </ MainClassName >
7 < Key > STD </ Key >
8 </ ModuleInfo >
9 < ModuleInfo >
10 < ModuleName > C:\Documents and Settings\Administrator\桌面\MyShell\MyShell\Lib\CSIT.WinForms.dll </ ModuleName >
11 < NameSpace > CSIT.WinForms </ NameSpace >
12 < MainClassName > Msg </ MainClassName >
13 < Key > MSG </ Key >
14 </ ModuleInfo >
15 </ ModuleInfoConfig >
Command.xml用于存放所有指令的描述信息
<?
xml version="1.0" encoding="utf-8"
?>
< ModuleCommandConfig xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns ="http://www.csit.com.cn" >
< CommandConfig >
< CommandName > Cls </ CommandName >
< Description > 清除屏幕 </ Description >
< CallMethodName > Cls </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList />
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > Print </ CommandName >
< Description > 输出字符串 </ Description >
< CallMethodName > Print </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > ShellExecute </ CommandName >
< Description > 执行程序 </ Description >
< CallMethodName > ShellExecute </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > = </ CommandName >
< Description > 表达式计算 </ Description >
< CallMethodName > Eval </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > Dir </ CommandName >
< Description > 目录 </ Description >
< CallMethodName > Dir </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > CD </ CommandName >
< Description > 更改目录 </ Description >
< CallMethodName > CD </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > MsgBox </ CommandName >
< Description > 消息对话框 </ Description >
< CallMethodName > MsgBox </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > MSG </ Key >
</ CommandConfig >
</ ModuleCommandConfig >
< ModuleCommandConfig xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns ="http://www.csit.com.cn" >
< CommandConfig >
< CommandName > Cls </ CommandName >
< Description > 清除屏幕 </ Description >
< CallMethodName > Cls </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList />
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > Print </ CommandName >
< Description > 输出字符串 </ Description >
< CallMethodName > Print </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > ShellExecute </ CommandName >
< Description > 执行程序 </ Description >
< CallMethodName > ShellExecute </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > = </ CommandName >
< Description > 表达式计算 </ Description >
< CallMethodName > Eval </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > Dir </ CommandName >
< Description > 目录 </ Description >
< CallMethodName > Dir </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > CD </ CommandName >
< Description > 更改目录 </ Description >
< CallMethodName > CD </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > STD </ Key >
</ CommandConfig >
< CommandConfig >
< CommandName > MsgBox </ CommandName >
< Description > 消息对话框 </ Description >
< CallMethodName > MsgBox </ CallMethodName >
< ReturnType > void </ ReturnType >
< ParamsList > string </ ParamsList >
< Key > MSG </ Key >
</ CommandConfig >
</ ModuleCommandConfig >
以上两个配置文件全部通过序列化方式生成,期间还设计到一个指令生成器;(该部分由同事完成)将DLL文件装入后自动建立这些配置信息,命令部分只获取PUBLIC方法。
批处理的调用:
BATCH FROM FILE=C:\Test.cmd
cls
print 开始测试批处理!
msgbox 这个是文本对话框
ShellExecute notepad
print 已经启动了写字板了吗?
print 开始测试表达式!
print 1 + 2 + 3 + 4 = ?
= 1 + 2 + 3 + 4
print 1 * 99 = ?
= 1 * 99
print 列表目录的指令为 dir
print 切换目录的指令为 cd
print 结束执行
print 开始测试批处理!
msgbox 这个是文本对话框
ShellExecute notepad
print 已经启动了写字板了吗?
print 开始测试表达式!
print 1 + 2 + 3 + 4 = ?
= 1 + 2 + 3 + 4
print 1 * 99 = ?
= 1 * 99
print 列表目录的指令为 dir
print 切换目录的指令为 cd
print 结束执行
脚本的调用:
script=c:\Math Board.ssf
begin
NewLine = Chr( 13 ) & Chr( 10 )
N = 9
for (i , 0 , 10 , 1 )
S = S & N & " x " & i & " = " & (N * i) & NewLine
next
call ShowMsg(S , " Math Board Of " & N)
end
NewLine = Chr( 13 ) & Chr( 10 )
N = 9
for (i , 0 , 10 , 1 )
S = S & N & " x " & i & " = " & (N * i) & NewLine
next
call ShowMsg(S , " Math Board Of " & N)
end
脚本调试器的调用
script debug
脚本的语法定义:
begin
statement;
statement;
end
statement = 条件语句
if (cond)
statement
else
statement
endif
或者
if (cond)
statement
endif
statment = For语句
for (val,开始值,结束值,增量值)
statement;
statement;
next
statement = While语句
while (条件)
statment;
statment;
next
statement = call语句
calll func par,par,par
statement;
statement;
end
statement = 条件语句
if (cond)
statement
else
statement
endif
或者
if (cond)
statement
endif
statment = For语句
for (val,开始值,结束值,增量值)
statement;
statement;
next
statement = While语句
while (条件)
statment;
statment;
next
statement = call语句
calll func par,par,par
本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2006/01/24/322361.html,如需转载请自行联系原作者