/// <summary> /// 编辑一个菜单项 /// </summary> /// <param name="ClassId"></param> /// <param name="ClassName"></param> /// <param name="ClassList"></param> /// <param name="ClassPre"></param> /// <param name="ClassTj"></param> /// <returns></returns> public bool ClassSave(Type_Class model) { OleDbParameter[] parameters = { new OleDbParameter("@ClassName", model.ClassName), new OleDbParameter("@ClassList", model.ClassList), new OleDbParameter("@ClassPre", model.ClassPre), new OleDbParameter("@ClassTj", model.ClassTj), new OleDbParameter("@KeyWords", model.KeyWords), new OleDbParameter("@Remark", model.Remark), new OleDbParameter("@ClassId", model.ClassId) }; StringBuilder strSql = new StringBuilder(); strSql.Append("update Type_Class set "); strSql.Append("ClassName=@ClassName,"); strSql.Append("ClassList=@ClassList,"); strSql.Append("ClassPre=@ClassPre,"); strSql.Append("ClassTj=@ClassTj,"); strSql.Append("KeyWords=@KeyWords,"); strSql.Append("Remark=@Remark"); strSql.Append(" where ClassId=@ClassId"); //同步更新子菜单项 DataSet ds = this.GetSubClassList(model.ClassId); if (ds.Tables[0].Rows.Count > 0) { foreach (DataRow dr in ds.Tables[0].Rows) { Type_Class tc = new Type_Class(); string SubClassList = model.ClassList + dr["ClassId"].ToString().Trim() + ","; tc.ClassId = dr["ClassId"].ToString().Trim(); tc.ClassName = dr["ClassName"].ToString().Trim(); tc.ClassList = SubClassList.ToString().Trim(); tc.ClassPre = dr["ClassPre"].ToString().Trim(); tc.ClassTj = model.ClassTj + 1; tc.KeyWords = dr["KeyWords"].ToString(); tc.Remark = dr["Remark"].ToString(); ClassSave(tc); } } return(DBHelper.ExecuteNonqueryBool(strSql.ToString(), parameters)); //return DBHelper.ExecuteNonqueryBool("update Type_Class set ClassName=@ClassName,ClassList=@ClassList,ClassPre=@ClassPre,ClassTj=@ClassTj,KeyWords=@KeyWords,Remark=@Remark where ClassId=@ClassId", parameters); }
/// <summary> /// 添加一个菜单项 /// </summary> /// <param name="ClassId"></param> /// <param name="ClassName"></param> /// <param name="ClassList"></param> /// <param name="ClassPre"></param> /// <param name="ClassTj"></param> /// <returns></returns> public bool ClassAdd(Type_Class model) { OleDbParameter[] parameters = { new OleDbParameter("@ClassId", model.ClassId), new OleDbParameter("@ClassName", model.ClassName), new OleDbParameter("@ClassList", model.ClassList), new OleDbParameter("@ClassPre", model.ClassPre), new OleDbParameter("@ClassTj", model.ClassTj), new OleDbParameter("@ClassKind", model.ClassKind), new OleDbParameter("@KeyWords", model.KeyWords), new OleDbParameter("@Remark", model.Remark) }; return(DBHelper.ExecuteNonqueryBool("insert into Type_Class(ClassId,ClassName,ClassList,ClassPre,ClassTj,ClassKind,KeyWords,Remark) values (@ClassId,@ClassName,@ClassList,@ClassPre,@ClassTj,@ClassKind,@KeyWords,@Remark)", parameters)); }
//指向基类 static void DefineBaseTypes(IList <Token> tokens) { for (int i = 0; i < tokens.Count; i++) { if (tokens[i].type == TokenType.CLASS) //如果是paragraph走到这里也无所谓 //基类 { List <string> strBase = null; int ibegin = i + 1; if (tokens[ibegin].text == ":") { strBase = new List <string>(); ibegin++; } while (tokens[ibegin].text != "{") { //此时类已经合并起来了 if (tokens[ibegin].type == TokenType.TYPE) { strBase.Add(tokens[ibegin].text); } ibegin++; } if (strBase != null && strBase.Count > 0) { Type_Class typeClass = CQuark.AppDomain.GetTypeByKeywordQuiet(tokens[i].text) as Type_Class; List <IType> baseTypes = new List <IType>(); foreach (string t in strBase) { IType type = CQuark.AppDomain.GetTypeByKeyword(t); baseTypes.Add(type); } typeClass.SetBaseType(baseTypes); } // Token tok = tokens[i]; // tok.type = TokenType.TYPE; // tokens[i] = tok; } } }
//编译一个类 static IType CompilerSingleClass(string classname, bool bInterface, string filename, IList <Token> tokens, int ibegin, int iend) { Type_Class typeClass = CQuark.AppDomain.GetTypeByKeywordQuiet(classname) as Type_Class; if (typeClass == null) { typeClass = new Type_Class(classname, bInterface, filename); } //支持partial // (typeClass._class as Class_CQuark).functions.Clear(); // (typeClass._class as Class_CQuark).members.Clear(); //搜寻成员定义和函数 //定义语法 //Type id[= expr]; //函数语法 //Type id([Type id,]){block}; //属性语法 //Type id{get{},set{}}; bool bPublic = false; bool bStatic = false; for (int i = ibegin; i <= iend; i++) { if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "public") { bPublic = true; continue; } if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "private") { bPublic = false; continue; } if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "protected") { bPublic = false; continue; } if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "static") { bStatic = true; continue; } if (tokens[i].type == TokenType.TYPE || (tokens[i].type == TokenType.IDENTIFIER && tokens[i].text == classname)) //发现类型 { IType idtype = CQuark.AppDomain.GetTypeByKeyword("null"); bool bctor = false; if (tokens[i].type == TokenType.TYPE) //类型 { if (tokens[i].text == classname && tokens[i + 1].text == "(") //构造函数 { bctor = true; i--; } else if (tokens[i + 1].text == "[" && tokens[i + 2].text == "]") { idtype = CQuark.AppDomain.GetTypeByKeyword(tokens[i].text + "[]"); i += 2; } else if (tokens[i].text == "void") { } else { idtype = CQuark.AppDomain.GetTypeByKeyword(tokens[i].text); } } if (tokens[i + 1].type == CQuark.TokenType.IDENTIFIER || bctor) //类型后面是名称 { string idname = tokens[i + 1].text; if (tokens[i + 2].type == CQuark.TokenType.PUNCTUATION && tokens[i + 2].text == "(") //参数开始,这是函数 { #if CQUARK_DEBUG DebugUtil.Log("发现函数:" + idname); #endif Class_CQuark.Function func = new Class_CQuark.Function(); func.bStatic = bStatic; func.bPublic = bPublic; if (tokens[i].text != "void") { func._returntype = CQuark.AppDomain.GetTypeByKeyword(tokens[i].text); } int funcparambegin = i + 2; int funcparamend = FindBlock(tokens, funcparambegin); if (funcparamend - funcparambegin > 1) { int start = funcparambegin + 1; for (int j = funcparambegin + 1; j <= funcparamend; j++) { if (tokens[j].text == "," || tokens[j].text == ")") { string ptype = ""; for (int k = start; k <= j - 2; k++) { ptype += tokens[k].text; } string pid = tokens[j - 1].text; IType type = CQuark.AppDomain.GetTypeByKeyword(ptype); // _params[pid] = type; //func._params.Add(pid, type); if (type == null) { throw new Exception(filename + ":不可识别的函数头参数:" + tokens[funcparambegin].ToString() + tokens[funcparambegin].SourcePos()); } func._paramnames.Add(pid); func._paramtypes.Add(type); start = j + 1; } } } int funcbegin = funcparamend + 1; if (tokens[funcbegin].text == "{") { int funcend = FindBlock(tokens, funcbegin); ExprCompileUtil.Compiler_Expression_Block(tokens, funcbegin, funcend, out func.expr_runtime); if (func.expr_runtime == null) { DebugUtil.LogWarning("警告,该函数编译为null,请检查"); } (typeClass._class as Class_CQuark).functions.Add(idname, func); i = funcend; } else if (tokens[funcbegin].text == ";") { func.expr_runtime = null; (typeClass._class as Class_CQuark).functions.Add(idname, func); i = funcbegin; } else { throw new Exception(filename + ":不可识别的函数表达式:" + tokens[funcbegin].ToString() + tokens[funcbegin].SourcePos()); } } else if (tokens[i + 2].type == CQuark.TokenType.PUNCTUATION && tokens[i + 2].text == "{") //语句块开始,这是 getset属性 { //get set 成员定义 bool setpublic = true; bool haveset = false; for (int j = i + 3; j <= iend; j++) { if (tokens[j].text == "get") { setpublic = true; } if (tokens[j].text == "private") { setpublic = false; } if (tokens[j].text == "set") { haveset = true; } if (tokens[j].text == "}") { break; } } var member = new Class_CQuark.Member(); member.bStatic = bStatic; member.bPublic = bPublic; member.bReadOnly = !(haveset && setpublic); member.m_itype = idtype; #if CQUARK_DEBUG DebugUtil.Log("发现Get/Set:" + idname); #endif //ICQ_Expression expr = null; if (tokens[i + 2].text == "=") { int jbegin = i + 3; int jdep; int jend = ExprCompileUtil.FindCodeAny(tokens, ref jbegin, out jdep); if (!ExprCompileUtil.Compiler_Expression(tokens, jbegin, jend, out member.expr_defvalue)) { DebugUtil.LogError("Get/Set定义错误"); } i = jend; } (typeClass._class as Class_CQuark).members.Add(idname, member); } else if (tokens[i + 2].type == CQuark.TokenType.PUNCTUATION && (tokens[i + 2].text == "=" || tokens[i + 2].text == ";")) //这是成员定义 { #if CQUARK_DEBUG DebugUtil.Log("发现成员定义:" + idname); #endif var member = new Class_CQuark.Member(); member.bStatic = bStatic; member.bPublic = bPublic; member.bReadOnly = false; member.m_itype = idtype; //ICQ_Expression expr = null; if (tokens[i + 2].text == "=") { int posend = 0; for (int j = i; j < iend; j++) { if (tokens[j].text == ";") { posend = j - 1; break; } } int jbegin = i + 3; int jdep; int jend = ExprCompileUtil.FindCodeAny(tokens, ref jbegin, out jdep); if (jend < posend) { jend = posend; } if (!ExprCompileUtil.Compiler_Expression(tokens, jbegin, jend, out member.expr_defvalue)) { DebugUtil.LogError("成员定义错误"); } i = jend; } (typeClass._class as Class_CQuark).members.Add(idname, member); } bPublic = false; bStatic = false; continue; } else { throw new Exception(filename + ":不可识别的表达式:" + tokens[i].ToString() + tokens[i].SourcePos()); } } } return(typeClass); }
//找到类型,写入AppDomain和CustomTypes //如果类里有类那么依次加入 public static void RegisterCQClass(string fileName, IList <Token> tokens) { string Namespace = ""; List <string> classStack = new List <string> (); string outterClass = ""; for (int i = 0; i < tokens.Count; i++) { if (tokens[i].type == TokenType.KEYWORD) { if (tokens[i].text == "namespace") { int end = FindToken(tokens, i, "{"); Namespace = CombineReplace(tokens, i + 1, end - i - 1, TokenType.NAMESPACE).text; #if CQUARK_DEBUG DebugUtil.Log("Namespace = " + Namespace); #endif continue; } else if (tokens[i].text == "using") { int end = FindToken(tokens, i, ";"); string Using = CombineReplace(tokens, i + 1, end - i - 1, TokenType.NAMESPACE).text; #if CQUARK_DEBUG DebugUtil.Log("Using = " + Using); #endif continue; } } if (tokens [i].type == TokenType.KEYWORD && (tokens [i].text == "class" || tokens [i].text == "interface" || tokens[i].text == "struct")) { string name = tokens [i + 1].text; outterClass = name; #if CQUARK_DEBUG DebugUtil.Log("(PreCompiler)findclass:" + name + "(" + i + ")"); #endif string fullName = ""; if (!string.IsNullOrEmpty(Namespace)) { fullName = Namespace + "."; } foreach (string s in classStack) { if (!string.IsNullOrEmpty(s)) { fullName += s + "."; } } fullName += name; //把Token里的Type注册到AppDomain里 if (CQuark.AppDomain.GetTypeByKeywordQuiet(fullName) == null) { Type_Class typeClass = new Type_Class(fullName, tokens [i].text == "interface", fileName); AppDomain.RegisterType(typeClass); } //这个Token的名字补足 Token tok = tokens[i + 1]; tok.text = fullName; tok.type = TokenType.CLASS; tokens[i + 1] = tok; } if (tokens[i].type == TokenType.PUNCTUATION) { if (tokens[i].text == "{") { if (string.IsNullOrEmpty(outterClass)) { classStack.Add(""); } else { classStack.Add(outterClass); outterClass = ""; } } else if (tokens[i].text == "}") { classStack.RemoveAt(classStack.Count - 1); } } } }