Exemplo n.º 1
0
 /// <summary>
 /// 向数组添加添加一个新元素
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public Statement Add(Statement item) {
     gItems.Add(item);
     return item;
 }
Exemplo n.º 2
0
        //添加指令到程序
        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;
            }


        }