public Actuator(string szScript, Library.Base lib) { gLibrary = lib; gMgr = new Memery.Pool(); gBlocks = new BlockCollection(); //建立默认程序段 gBlocks.Add(new Block(this, "", true)); //建立{0}指针 Variable.Number num = new Variable.Number(); num.SetName("{0}"); //num = 99; gMgr.Add(num); LoadScript(szScript); }
//调用外部函数 private void ExecuteCall() { string szVarType = ""; if (gObjVar != null) szVarType = gObjVar.Type.ToString(); string szVal = gObjVal.ToString().ToLower(); //获取参数定义 int nIndex = gAct.MemeryManager.GetIndex("{000}"); if (nIndex < 0) throw new Exception("调用外部对象\"" + szVal + "\"之前未设置{000}参数数量!"); Variable.Number num = (Variable.Number)gAct.MemeryManager[nIndex]; bool bFound = false; foreach (System.Reflection.MethodInfo item in gAct.Library.GetType().GetMethods()) { //System.Windows.Forms.MessageBox.Show() // 获取所有的RecordAttributes特性 object[] records = item.GetCustomAttributes(typeof(dyk.Script.Code.Library.DykAttribute), false); if (records.Length > 0) { dyk.Script.Code.Library.DykAttribute dAttr = (dyk.Script.Code.Library.DykAttribute)records[0]; System.Reflection.ParameterInfo[] dParameters = item.GetParameters(); if (szVal == item.Name.ToLower() && num.Value == dAttr.ArgCount) { bFound = true; switch (dAttr.AttributeType) { case Library.AttributeTypes.Sub: #region [=====执行方法=====] if (szVarType != "") throw new Exception("外部调用对象\"" + szVal + "\"为方法,不具有返回值,请将返回变量设置为{}"); //添加执行参数 Variable.Object[] args = new Variable.Object[num.Value]; for (int i = 0; i < num.Value; i++) { string szVarName = "{" + (i + 1).ToString().PadLeft(3, '0') + "}"; //读取参数指针 int nVarIndex = gAct.MemeryManager.GetIndex(szVarName); if (nVarIndex < 0) throw new Exception("调用外部对象\"" + szVal + "\"的参数未设置完整!"); Variable.Number numVal = (Variable.Number)gAct.MemeryManager[nVarIndex]; //将变量对象赋值到参数 string szParameterType = dParameters[i].ParameterType.ToString(); Variable.Object objVar = gAct.MemeryManager[(int)numVal.Value]; if (szParameterType == "dyk.Script.Code.Variable." + objVar.Type.ToString()) { args[i] = objVar; } else if (szParameterType == "dyk.Script.Code.Variable.Decimal") { args[i] = new Variable.Decimal(objVar.ToDecimal()); } else if (szParameterType == "dyk.Script.Code.Variable.Number") { args[i] = new Variable.Number(objVar.ToNumber()); } else if (szParameterType == "dyk.Script.Code.Variable.String") { args[i] = new Variable.String(objVar.ToString()); } else { throw new Exception("调用外部对象\"" + szVal + "\"的参数不正确!"); } } item.Invoke(gAct.Library, args); break; #endregion case Library.AttributeTypes.Function: #region [=====执行函数=====] //添加执行参数 args = new Variable.Object[num.Value]; for (int i = 0; i < num.Value; i++) { string szVarName = "{" + (i + 1).ToString().PadLeft(3, '0') + "}"; //读取参数指针 int nVarIndex = gAct.MemeryManager.GetIndex(szVarName); if (nVarIndex < 0) throw new Exception("调用外部对象\"" + szVal + "\"的参数未设置完整!"); Variable.Number numVal = (Variable.Number)gAct.MemeryManager[nVarIndex]; //将变量对象赋值到参数 //System.Windows.Forms.MessageBox.Show(item.GetParameters()[i].ParameterType.ToString()); string szParameterType = dParameters[i].ParameterType.ToString(); Variable.Object objVar = gAct.MemeryManager[(int)numVal.Value]; if (szParameterType == "dyk.Script.Code.Variable." + objVar.Type.ToString()) { args[i] = objVar; } else if (szParameterType == "dyk.Script.Code.Variable.Decimal") { args[i] = new Variable.Decimal(objVar.ToDecimal()); } else if (szParameterType == "dyk.Script.Code.Variable.Number") { args[i] = new Variable.Number(objVar.ToNumber()); } else if (szParameterType == "dyk.Script.Code.Variable.String") { args[i] = new Variable.String(objVar.ToString()); } else { throw new Exception("调用外部对象\"" + szVal + "\"的参数不正确!"); } } object obj = item.Invoke(gAct.Library, args); //Variable.Number dec = new Variable.Decimal(45); //设置返回值 if (szVarType == "String") { Variable.String strVar = (Variable.String)gObjVar; strVar.Value = obj.ToString(); } else if (szVarType == "Decimal") { double res = 0; if (double.TryParse(obj.ToString(), out res)) { Variable.Decimal decVar = (Variable.Decimal)gObjVar; decVar.Value = res; } else { throw new Exception("类型不正确,无法强制转化为Decimal类型"); } } else if (szVarType == "Number") { long res = 0; if (long.TryParse(obj.ToString(), out res)) { Variable.Number numVar = (Variable.Number)gObjVar; numVar.Value = res; } else { throw new Exception("类型不正确,无法强制转化为Number类型"); } } break; #endregion } } //MessageBox.Show(dAttr.AttributeType.ToString() + ":" + item.IsStatic + ":" + item.Name + "(" + item.GetParameters().Length + ")"); } } if (!bFound) throw new Exception("未找到具有" + num.Value + "个参数定义的\"" + szVal + "\"外部调用对象"); }
//添加指令到程序 private void AddStatement(string szName, string szVar, string szVal, string szNote) { StatementTypes sType = Statement.GetStatementType(szName); switch (sType) { case StatementTypes.NONE: #region [=====处理空指令=====] Statement st = new Statement(gBlocks[gBlocks.Count - 1], sType, null, null, szNote); gBlocks[gBlocks.Count - 1].Statements.Add(st); break; #endregion case StatementTypes.FLAG: #region [=====处理程序段指令=====] Block bk = new Block(this, szVar, szVal == "1" ? true : false); gBlocks.Add(bk); break; #endregion case StatementTypes.DEF: #region [=====处理字符串预设定指令=====] if (szVar.StartsWith("[") && szVar.EndsWith("]")) { if (szVal.StartsWith("\"") && szVal.EndsWith("\"")) { int nIndex = gMgr.GetIndex(szVar); if (nIndex < 0) { Variable.String var = new Variable.String(szVal.Substring(1, szVal.Length - 2)); var.SetName(szVar); gMgr.Add(var); } else { throw new Exception("内部变量" + szVar + "已经存在,DEF指令不可重复定义字符串"); } } else { throw new Exception("DEF指令的值必须是字符串"); } } else { throw new Exception("DEF指令的变量必须是以\"[\"开头\"]\"结尾的内部变量"); } break; #endregion case StatementTypes.VAR: #region [=====处理变量定义指令=====] string szFirstWords = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; string szWords = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; if (szFirstWords.IndexOf(szVar[0].ToString()) < 0) { throw new Exception("自定义变量首字母必须是英文大小写或下划线\"_\"!"); } for (int i = 1; i < szVar.Length; i++) { if (szWords.IndexOf(szVar[i].ToString()) < 0) { throw new Exception("自定义变量必须是由英文大小写或下划线\"_\"或数字组成!"); } } int nVarIndex = gMgr.GetIndex(szVar); if (nVarIndex < 0) { switch (szVal.ToLower()) { case "string": Variable.String str = new Variable.String(); str.SetName(szVar); gMgr.Add(str); break; case "number": Variable.Number num = new Variable.Number(); num.SetName(szVar); gMgr.Add(num); break; case "decimal": Variable.Decimal dec = new Variable.Decimal(); dec.SetName(szVar); gMgr.Add(dec); break; default: throw new Exception("未知变量类型\"" + szVal + "\"!"); } } else { throw new Exception("变量\"" + szVar + "\"已经存在,不可重复定义"); } break; #endregion default: Variable.Object objVar; nVarIndex = gMgr.GetIndex(szVar); if (nVarIndex < 0) { //寄存器指针变量自动建立 if (szVar.StartsWith("{") && szVar.EndsWith("}")) { if (szVar == "{}") { objVar = null; } else { objVar = new Variable.Number(); objVar.SetName(szVar); gMgr.Add(objVar); } } else { throw new Exception("变量\"" + szVar + "\"不存在"); } } else { objVar = gMgr[nVarIndex]; } if (szVal.StartsWith("\"") && szVal.EndsWith("\"")) { //字符串值 Variable.String str = new Variable.String(szVal.Substring(1, szVal.Length - 2)); gMgr.Add(str); Statement stNew = new Statement(gBlocks[gBlocks.Count - 1], sType, objVar, str, szNote); gBlocks[gBlocks.Count - 1].Statements.Add(stNew); } else { double dbTemp = 0; if (double.TryParse(szVal, out dbTemp)) { //数值 Variable.Decimal dec = new Variable.Decimal(dbTemp); gMgr.Add(dec); Statement stNew = new Statement(gBlocks[gBlocks.Count - 1], sType, objVar, dec, szNote); gBlocks[gBlocks.Count - 1].Statements.Add(stNew); } else { int nValIndex = gMgr.GetIndex(szVal); if (nValIndex >= 0) { //变量值 Statement stNew = new Statement(gBlocks[gBlocks.Count - 1], sType, objVar, gMgr[nValIndex], szNote); gBlocks[gBlocks.Count - 1].Statements.Add(stNew); } else { throw new Exception("变量\"" + szVal + "\"不存在"); } } } break; } }