//Dictionary<string, ICLS_Type_Dele> deleTypes = new Dictionary<string, ICLS_Type_Dele>(); public void RegType(ICLS_Type type) { types[type.type] = type; string typename = type.keyword; //if (useNamespace) //{ // if (string.IsNullOrEmpty(type._namespace) == false) // { // typename = type._namespace + "." + type.keyword; // } //} if (string.IsNullOrEmpty(typename)) {//匿名自动注册 } else { typess[typename] = type; if (tokenParser.types.Contains(typename) == false) { tokenParser.types.Add(typename); } } }
public ICLS_Expression Compiler_Expression_DefineAndSet(IList <Token> tlist, CLS_Content content, int pos, int posend) { int expbegin = pos + 3; int bdep; int expend = FindCodeAny(tlist, ref expbegin, out bdep); if (expend != posend) { expend = posend; } ICLS_Expression v; bool succ = Compiler_Expression(tlist, content, expbegin, expend, out v); if (succ && v != null) { CLS_Expression_Define define = new CLS_Expression_Define(pos, posend); if (tlist[pos].text == "bool") { define.value_type = typeof(bool); } else { ICLS_Type type = content.environment.GetTypeByKeyword(tlist[pos].text); define.value_type = type.type; } define.value_name = tlist[pos + 1].text; define.listParam.Add(v); return(define); } LogError(tlist, "不正确的定义表达式:", pos, posend); return(null); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); var left = listParam[0].ComputeValue(content); var right = listParam[1].ComputeValue(content); ICLS_Type type = content.environment.GetType(left.type); //if (mathop == "+=") { CLType returntype; object value = type.Math2Value(content, mathop, left.value, right, out returntype); value = type.ConvertTo(content, value, left.type); left.value = value; //content.Set(value_name, value); } //操作变量之 //做数学计算 //从上下文取值 //_value = null; content.OutStack(this); return(null); }
void ReadSInstance(ProtoReader reader, SInstance sInstance, CLS_Environment environment) { List <CLS_Content.Value> values; List <string> keywords; GetSortMembers(sInstance, out values, out keywords); int fieldNumber = 0; while ((fieldNumber = reader.ReadFieldHeader()) > 0) { Type memberT = values[fieldNumber - 1].type; CLS_Content.Value memberV = values[fieldNumber - 1]; string sClassName = keywords[fieldNumber - 1]; if (memberT == null) { memberT = typeof(SInstance); sClassName = ((SType)memberV.type).Name; } Type itemType = GetItemType(memberT); if (itemType != null) { sClassName = sInstance.type.members[sClassName].type.keyword; // 数组判断 if (memberT.IsArray) { string itemClass = sClassName.Substring(0, sClassName.Length - 2); // 从 xxx[] 中提取xxx BasicList list = new BasicList(); do { list.Add(ReadField(reader, itemType, itemClass, environment)); } while (reader.TryReadFieldHeader(fieldNumber)); Array result = Array.CreateInstance(itemType, list.Count); list.CopyTo(result, 0); memberV.value = result; } // 列表判断 else { string itemClass = sClassName.Substring(5, sClassName.Length - 6); // 从 List<xxx> 中提取xxx ICLS_Type iType = environment.GetTypeByKeywordQuiet(sClassName); CLS_Content content = CLS_Content.NewContent(environment); memberV.value = iType.function.New(content, m_emptyParams).value; CLS_Content.PoolContent(content); IList list = (IList)memberV.value; do { list.Add(ReadField(reader, itemType, itemClass, environment)); } while (reader.TryReadFieldHeader(fieldNumber)); } } else { memberV.value = ReadField(reader, memberT, sClassName, environment); } } }
public void RegType(ICLS_Type regType) { if (regType.type != null) { if (dictTypes.ContainsKey(regType.type)) { // 允许多个关键字映射相同注册类型 //logger.Log_Warn("RegType repeat: type=" + regType.type); } else { dictTypes.Add(regType.type, regType); } } if (!string.IsNullOrEmpty(regType.keyword)) { if (dictTypeKeywords.ContainsKey(regType.keyword)) { logger.Log_Warn("RegType repeat: type key=" + regType.keyword); } else { dictTypeKeywords.Add(regType.keyword, regType); } } }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); var left = listParam[0].ComputeValue(content); var right = listParam[1].ComputeValue(content); ICLS_Type type = content.environment.GetType(left.type); //if (mathop == "+=") { CLType returntype; object value = type.Math2Value(content, mathop, left.value, right, out returntype); value = type.ConvertTo(content, value, left.type); left.value = value; Type t = right.type; //if(t.IsSubclassOf(typeof(MulticastDelegate))||t.IsSubclassOf(typeof(Delegate))) //{ //} ////content.Set(value_name, value); //else if (t == typeof(CSLE.DeleLambda) || t == typeof(CSLE.DeleFunction) || t == typeof(CSLE.DeleEvent)) //{ //} //else { if (listParam[0] is CLS_Expression_MemberFind) { CLS_Expression_MemberFind f = listParam[0] as CLS_Expression_MemberFind; var parent = f.listParam[0].ComputeValue(content); if (parent == null) { throw new Exception("调用空对象的方法:" + f.listParam[0].ToString() + ":" + ToString()); } var ptype = content.environment.GetType(parent.type); ptype.function.MemberValueSet(content, parent.value, f.membername, value); } if (listParam[0] is CLS_Expression_StaticFind) { CLS_Expression_StaticFind f = listParam[0] as CLS_Expression_StaticFind; f.type.function.StaticValueSet(content, f.staticmembername, value); } } } //操作变量之 //做数学计算 //从上下文取值 //_value = null; content.OutStack(this); return(null); }
public void RegType(ICLS_Type type) { types[type.type] = type; typess[type.keyword] = type; if (tokenParser.types.Contains(type.keyword) == false) { tokenParser.types.Add(type.keyword); } }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); CLS_Content.Value parent = listParam[0].ComputeValue(content); ICLS_Type type = content.environment.GetType(parent.type); CLS_Content.Value value = type.function.MemberValueGet(content, parent.value, membername, parent.breakBlock == 255); content.OutStack(this); return(value); }
public ICLS_Type GetTypeByKeywordQuiet(string keyword) { ICLS_Type ret = null; if (typess.TryGetValue(keyword, out ret) == false) { return(null); } return(ret); }
public ICLS_Expression Compiler_Expression_DefineArray(IList <Token> tlist, ICLS_Environment content, int pos, int posend) { CLS_Expression_Define define = new CLS_Expression_Define(pos, posend, tlist[pos].line, tlist[posend].line); { ICLS_Type type = content.GetTypeByKeyword(tlist[pos].text + "[]"); define.value_type = type.type; } define.value_name = tlist[pos + 3].text; return(define); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); CLS_Content.Value right = listParam[0].ComputeValue(content); ICLS_Type type = content.environment.GetType(right.type); CLS_Content.Value value = new CLS_Content.Value(); value.type = targettype; value.value = type.ConvertTo(content, right.value, targettype); content.OutStack(this); return(value); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); CLS_Content.Value r = listParam[0].ComputeValue(content); ICLS_Type type = content.environment.GetType(r.type); r.value = type.Math2Value(content, '*', r.value, CLS_Content.Value.OneMinus, out r.type); content.OutStack(this); return(r); }
ICLS_Expression OptimizeSingle(ICLS_Expression expr, CLS_Content content) { if (expr is CLS_Expression_Math2Value || expr is CLS_Expression_Math2ValueAndOr || expr is CLS_Expression_Math2ValueLogic) { if (expr.listParam[0] is ICLS_Value && expr.listParam[1] is ICLS_Value) { CLS_Content.Value result = expr.ComputeValue(content); if ((Type)result.type == typeof(bool)) { CLS_Value_Value <bool> value = new CLS_Value_Value <bool>(); value.value_value = (bool)result.value; value.tokenBegin = expr.listParam[0].tokenBegin; value.tokenEnd = expr.listParam[1].tokenEnd; value.lineBegin = expr.listParam[0].lineBegin; value.lineEnd = expr.listParam[1].lineEnd; return(value); } else { ICLS_Type v = content.environment.GetType(result.type); ICLS_Value value = v.MakeValue(result.value); value.tokenBegin = expr.listParam[0].tokenBegin; value.tokenEnd = expr.listParam[1].tokenEnd; value.lineBegin = expr.listParam[0].lineBegin; value.lineEnd = expr.listParam[1].lineEnd; return(value); } } } if (expr is CLS_Expression_Math3Value) { CLS_Content.Value result = expr.listParam[0].ComputeValue(content); if ((Type)result.type == typeof(bool)) { bool bv = (bool)result.value; if (bv) { return(expr.listParam[1]); } else { return(expr.listParam[2]); } } } return(expr); }
public ICLS_Expression Compiler_Expression_Define(IList <Token> tlist, CLS_Content content, int pos, int posend) { CLS_Expression_Define define = new CLS_Expression_Define(pos, posend); if (tlist[pos].text == "bool") { define.value_type = typeof(bool); } else { ICLS_Type type = content.environment.GetTypeByKeyword(tlist[pos].text); define.value_type = type.type; } define.value_name = tlist[pos + 1].text; return(define); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); CLS_Content.Value oriVal = listParam[0].ComputeValue(content); CLS_Content.Value retVal = new CLS_Content.Value(); ICLS_Type type = content.environment.GetType(oriVal.type); retVal.value = type.Math2Value(content, '*', oriVal.value, CLS_Content.Value.OneMinus, out retVal.type); content.OutStack(this); return(retVal); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); CLS_Content.Value left = content.Get(value_name); ICLS_Type type = content.environment.GetType(left.type); CLType returntype; left.value = type.Math2Value(content, mathop, left.value, CLS_Content.Value.One, out returntype); //left.value = type.ConvertTo(content, left.value, v.type); //content.Set(value_name, left.value); content.OutStack(this); return(content.Get(value_name)); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); CLS_Content.Value parent = listParam[0].ComputeValue(content); ICLS_Type type = content.environment.GetType(parent.type); BetterList <CLS_Content.Value> _params = CLS_Content.NewParamList(); for (int i = 1, count = listParam.Count; i < count; i++) { _params.Add(listParam[i].ComputeValue(content)); } CLS_Content.Value value = type.function.MemberCall(content, parent.value, functionName, _params, parent.breakBlock == 255); CLS_Content.PoolParamList(_params); content.OutStack(this); return(value); }
public ICLS_Type GetType(CLType type) { if (type == null) { return(typess["null"]); } ICLS_Type ret = null; if (types.TryGetValue(type, out ret) == false) { logger.Log_Warn("(CLScript)类型未注册,将自动注册一份匿名:" + type.ToString()); ret = RegHelper_Type.MakeType(type, ""); RegType(ret); } return(ret); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); var right = listParam[0].ComputeValue(content); ICLS_Type type = content.environment.GetType(right.type); CLS_Content.Value value = new CLS_Content.Value(); value.type = typeof(bool); value.value = type.ConvertTo(content, right.value, targettype) != null; //操作变量之 //做数学计算 //从上下文取值 //_value = null; content.OutStack(this); return(value); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); ICLS_Expression leftExp = listParam[0]; CLS_Content.Value left = leftExp.ComputeValue(content); CLS_Content.Value right = listParam[1].ComputeValue(content); ICLS_Type type = content.environment.GetType(left.type); CLType returntype; left.value = type.Math2Value(content, mathop, left.value, right, out returntype); //left.value = type.ConvertTo(content, left.value, left.type); if (leftExp is CLS_Expression_MemberFind) { CLS_Expression_MemberFind f = leftExp as CLS_Expression_MemberFind; CLS_Content.Value parent = f.listParam[0].ComputeValue(content); ICLS_Type ptype = content.environment.GetType(parent.type); ptype.function.MemberValueSet(content, parent.value, f.membername, left.value); } else if (leftExp is CLS_Expression_StaticFind) { CLS_Expression_StaticFind f = leftExp as CLS_Expression_StaticFind; f.type.function.StaticValueSet(content, f.staticmembername, left.value); } else if (leftExp is CLS_Expression_IndexFind) { CLS_Expression_IndexFind f = leftExp as CLS_Expression_IndexFind; CLS_Content.Value parent = f.listParam[0].ComputeValue(content); CLS_Content.Value key = f.listParam[1].ComputeValue(content); ICLS_Type ptype = content.environment.GetType(parent.type); ptype.function.IndexSet(content, parent.value, key.value, left.value); } content.OutStack(this); return(null); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); var v = content.Get(value_name); ICLS_Type type = content.environment.GetType(v.type); CLType returntype; object value = type.Math2Value(content, mathop, v.value, CLS_Content.Value.One, out returntype); value = type.ConvertTo(content, value, v.type); content.Set(value_name, value); //操作变量之 //做数学计算 //从上下文取值 //_value = null; content.OutStack(this); return(null); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); CLS_Content.Value parent = listParam[0].ComputeValue(content); ICLS_Type type = content.environment.GetType(parent.type); CLS_Content.Value getvalue = type.function.MemberValueGet(content, parent.value, membername); CLS_Content.Value vright = CLS_Content.Value.One; if (listParam.Count > 1) { vright = listParam[1].ComputeValue(content); } CLS_Content.Value vout = new CLS_Content.Value(); vout.value = content.environment.GetType(getvalue.type).Math2Value(content, mathop, getvalue.value, vright, out vout.type); type.function.MemberValueSet(content, parent.value, membername, vout.value); content.OutStack(this); return(vout); }
//public ICLS_Type_Dele GetDeleTypeBySign(string sign) //{ // if (deleTypes.ContainsKey(sign) == false) // { // return null; // //logger.Log_Error("(CLScript)类型未注册:" + sign); // } // return deleTypes[sign]; //} public ICLS_Type GetTypeByKeyword(string keyword) { ICLS_Type ret = null; if (string.IsNullOrEmpty(keyword)) { return(null); } if (typess.TryGetValue(keyword, out ret) == false) { if (keyword[keyword.Length - 1] == '>') { int iis = keyword.IndexOf('<'); string func = keyword.Substring(0, iis); List <string> _types = new List <string>(); int istart = iis + 1; int inow = istart; int dep = 0; while (inow < keyword.Length) { if (keyword[inow] == '<') { dep++; } if (keyword[inow] == '>') { dep--; if (dep < 0) { _types.Add(keyword.Substring(istart, inow - istart)); break; } } if (keyword[inow] == ',' && dep == 0) { _types.Add(keyword.Substring(istart, inow - istart)); istart = inow + 1; inow = istart; continue;; } inow++; } //var funk = keyword.Split(new char[] { '<', '>', ',' }, StringSplitOptions.RemoveEmptyEntries); if (typess.ContainsKey(func)) { Type gentype = GetTypeByKeyword(func).type; if (gentype.IsGenericTypeDefinition) { Type[] types = new Type[_types.Count]; for (int i = 0; i < types.Length; i++) { CLType t = GetTypeByKeyword(_types[i]).type; Type rt = t; if (rt == null && t != null) { rt = typeof(object); } types[i] = rt; } Type IType = gentype.MakeGenericType(types); RegType(CSLE.RegHelper_Type.MakeType(IType, keyword)); return(GetTypeByKeyword(keyword)); } } } logger.Log_Error("(CLScript)类型未注册:" + keyword); } return(ret); }
IList <ICLS_Type> _FileCompiler(string filename, IList <Token> tokens, bool embDeubgToken, ICLS_Environment env, bool onlyGotType = false) { List <ICLS_Type> typelist = new List <ICLS_Type>(); List <string> usingList = new List <string>(); //识别using //扫描token有没有要合并的类型 //using的实现在token级别处理即可 bool bJumpClass = false; for (int i = 0; i < tokens.Count; i++) { if (tokens[i].type == TokenType.PUNCTUATION && tokens[i].text == ";") { continue; } if (tokens[i].type == TokenType.COMMENT) { continue; } if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "using") { int dep; int pos = i; int iend = FindCodeAny(tokens, ref pos, out dep); var list = Compiler_Using(tokens, env, pos, iend); string useText = ""; for (int j = 0; j < list.Count; j++) { useText += list[j]; if (j != list.Count - 1) { useText += "."; } } usingList.Add(useText); i = iend; continue; } if (tokens[i].type == TokenType.PUNCTUATION && tokens[i].text == "[") { if (tokens[i + 1].text == "NotScipt" || (tokens[i + 1].text == "CSLE" && tokens[i + 3].text == "NotScipt")) { bJumpClass = true; i = i + 2; continue; } } if (tokens[i].type == TokenType.KEYWORD && (tokens[i].text == "class" || tokens[i].text == "interface")) { string name = tokens[i + 1].text; //在这里检查继承 List <string> typebase = null; int ibegin = i + 2; if (onlyGotType) { while (tokens[ibegin].text != "{") { ibegin++; } } else { if (tokens[ibegin].text == ":") { typebase = new List <string>(); ibegin++; } while (tokens[ibegin].text != "{") { if (tokens[ibegin].type == TokenType.TYPE) { typebase.Add(tokens[ibegin].text); } ibegin++; } } int iend = FindBlock(env, tokens, ibegin); if (iend == -1) { env.logger.Log_Error("查找文件尾失败。"); return(null); } if (bJumpClass) { env.logger.Log("(NotScript)findclass:" + name + "(" + ibegin + "," + iend + ")"); } else if (onlyGotType) { env.logger.Log("(scriptPreParser)findclass:" + name + "(" + ibegin + "," + iend + ")"); } else { env.logger.Log("(scriptParser)findclass:" + name + "(" + ibegin + "," + iend + ")"); } if (bJumpClass) {//忽略这个Class //ICLS_Type type = Compiler_Class(env, name, (tokens[i].text == "interface"), filename, tokens, ibegin, iend, embDeubgToken, true); //bJumpClass = false; } else { ICLS_Type type = Compiler_Class(env, name, (tokens[i].text == "interface"), typebase, filename, tokens, ibegin, iend, embDeubgToken, onlyGotType, usingList); if (type != null) { typelist.Add(type); } } i = iend; continue; } } return(typelist); }
ICLS_Type Compiler_Class(ICLS_Environment env, string classname, bool bInterface, IList <string> basetype, string filename, IList <Token> tokens, int ibegin, int iend, bool EmbDebugToken, bool onlyGotType = false, IList <string> usinglist = null) { CLS_Type_Class stype = env.GetTypeByKeywordQuiet(classname) as CLS_Type_Class; if (stype == null) { stype = new CLS_Type_Class(classname, bInterface, filename); } if (basetype != null && basetype.Count != 0 && onlyGotType == false) { List <ICLS_Type> basetypess = new List <ICLS_Type>(); foreach (var t in basetype) { ICLS_Type type = env.GetTypeByKeyword(t); basetypess.Add(type); } stype.SetBaseType(basetypess); } if (onlyGotType) { return(stype); } //if (env.useNamespace && usinglist != null) //{//使用命名空间,替换token // List<Token> newTokens = new List<Token>(); // for (int i = ibegin; i <= iend; i++) // { // if (tokens[i].type == TokenType.IDENTIFIER) // { // string ntype = null; // string shortname = tokens[i].text; // int startpos = i; // while (ntype == null) // { // foreach (var u in usinglist) // { // string ttype = u + "." + shortname; // if (env.GetTypeByKeywordQuiet(ttype) != null) // { // ntype = ttype; // break; // } // } // if (ntype != null) break; // if ((startpos + 2) <= iend && tokens[startpos + 1].text == "." && tokens[startpos + 2].type == TokenType.IDENTIFIER) // { // shortname += "." + tokens[startpos + 2].text; // startpos += 2; // if (env.GetTypeByKeywordQuiet(shortname) != null) // { // ntype = shortname; // break; // } // continue; // } // else // { // break; // } // } // if (ntype != null) // { // var t = tokens[i]; // t.text = ntype; // t.type = TokenType.TYPE; // newTokens.Add(t); // i = startpos; // continue; // } // } // newTokens.Add(tokens[i]); // } // tokens = newTokens; // ibegin = 0; // iend = tokens.Count - 1; //} stype.compiled = false; (stype.function as SType).functions.Clear(); (stype.function as SType).members.Clear(); //搜寻成员定义和函数 //定义语法 //Type id[= expr]; //函数语法 //Type id([Type id,]){block}; //属性语法 //Type id{get{},set{}}; bool bPublic = false; bool bStatic = false; if (EmbDebugToken)//SType 嵌入Token { stype.EmbDebugToken(tokens); } for (int i = ibegin; i <= iend; i++) { if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "public") { bPublic = true; continue; } else if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "private") { bPublic = false; continue; } else if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "static") { bStatic = true; continue; } else if (tokens[i].type == TokenType.TYPE || (tokens[i].type == TokenType.IDENTIFIER && tokens[i].text == classname))//发现类型 { ICLS_Type idtype = env.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 = env.GetTypeByKeyword(tokens[i].text + "[]"); i += 2; } else if (tokens[i].text == "void") { } else { idtype = env.GetTypeByKeyword(tokens[i].text); } } if (tokens[i + 1].type == CSLE.TokenType.IDENTIFIER || bctor) //类型后面是名称 { string idname = tokens[i + 1].text; if (tokens[i + 2].type == CSLE.TokenType.PUNCTUATION && tokens[i + 2].text == "(")//参数开始,这是函数 { logger.Log("发现函数:" + idname); SType.Function func = new SType.Function(); func.bStatic = bStatic; func.bPublic = bPublic; int funcparambegin = i + 2; int funcparamend = FindBlock(env, tokens, funcparambegin); if (funcparamend - funcparambegin > 1) { int start = funcparambegin + 1; //Dictionary<string, ICLS_Type> _params = new Dictionary<string, ICLS_Type>(); 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; } var pid = tokens[j - 1].text; var type = env.GetTypeByKeyword(ptype); // _params[pid] = type; //func._params.Add(pid, type); func._paramnames.Add(pid); func._paramtypes.Add(type); start = j + 1; } } } int funcbegin = funcparamend + 1; if (tokens[funcbegin].text == "{") { int funcend = FindBlock(env, tokens, funcbegin); this.Compiler_Expression_Block(tokens, env, funcbegin, funcend, out func.expr_runtime); if (func.expr_runtime == null) { logger.Log_Warn("警告,该函数编译为null,请检查"); } (stype.function as SType).functions.Add(idname, func); i = funcend; } else if (tokens[funcbegin].text == ";") { func.expr_runtime = null; (stype.function as SType).functions.Add(idname, func); i = funcbegin; } else { throw new Exception("不可识别的函数表达式"); } } else if (tokens[i + 2].type == CSLE.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 SType.Member(); member.bStatic = bStatic; member.bPublic = bPublic; member.bReadOnly = !(haveset && setpublic); member.type = idtype; logger.Log("发现Get/Set:" + idname); //ICLS_Expression expr = null; if (tokens[i + 2].text == "=") { int jbegin = i + 3; int jdep; int jend = FindCodeAny(tokens, ref jbegin, out jdep); bool b = Compiler_Expression(tokens, env, jbegin, jend, out member.expr_defvalue); i = jend; } (stype.function as SType).members.Add(idname, member); } else if (tokens[i + 2].type == CSLE.TokenType.PUNCTUATION && (tokens[i + 2].text == "=" || tokens[i + 2].text == ";"))//这是成员定义 { logger.Log("发现成员定义:" + idname); var member = new SType.Member(); member.bStatic = bStatic; member.bPublic = bPublic; member.bReadOnly = false; member.type = idtype; //ICLS_Expression expr = null; if (tokens[i + 2].text == "=") { int jbegin = i + 3; int jdep; int jend = FindCodeAny(tokens, ref jbegin, out jdep); bool b = Compiler_Expression(tokens, env, jbegin, jend, out member.expr_defvalue); i = jend; } (stype.function as SType).members.Add(idname, member); } bPublic = false; bStatic = false; continue; } else { throw new Exception("不可识别的表达式"); } } } stype.compiled = true; return(stype); }
ICLS_Type Compiler_Class(ICLS_Environment env, string classname, IList <string> baseTypeNames, string filename, IList <Token> tokens, int ibegin, int iend, bool EmbDebugToken, bool onlyGotType = false, IList <string> usinglist = null) { CLS_Type_Class sClass = env.GetTypeByKeywordQuiet(classname) as CLS_Type_Class; if (sClass == null) { sClass = new CLS_Type_Class(classname, filename); } sClass.compiled = false; (sClass.function as SType).functions.Clear(); (sClass.function as SType).members.Clear(); if (onlyGotType) { return(sClass); } // 调试Token if (EmbDebugToken) { (sClass.function as SType).EmbDebugToken(tokens); } bool bStatic = false; int sortIndex = 0; for (int i = ibegin; i <= iend; i++) { if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "static") { bStatic = true; continue; } if (tokens[i].type == TokenType.ProtoIndex) { sortIndex = int.Parse(tokens[i].text); continue; } if (tokens[i].type == TokenType.TYPE || (tokens[i].type == TokenType.IDENTIFIER && tokens[i].text == classname))//发现类型 { ICLS_Type idtype = env.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 = env.GetTypeByKeyword(tokens[i].text + "[]"); i += 2; } else if (tokens[i].text == "void") { } else { idtype = env.GetTypeByKeyword(tokens[i].text); } } if (tokens[i + 1].type == CSLE.TokenType.IDENTIFIER || bctor) //类型后面是名称 { string idname = tokens[i + 1].text; if (tokens[i + 2].type == CSLE.TokenType.PUNCTUATION && tokens[i + 2].text == "(")//参数开始,这是函数 { logger.Log("发现函数:" + idname); SType.Function func = new SType.Function(); func.bStatic = bStatic; int funcparambegin = i + 2; int funcparamend = FindBlock(env, 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; } var pid = tokens[j - 1].text; var type = env.GetTypeByKeyword(ptype); if (type == null) { throw new Exception(filename + ":不可识别的函数头参数:" + tokens[funcparambegin].ToString()); } func._paramnames.Add(pid); func._paramtypes.Add(type); start = j + 1; } } } int funcbegin = funcparamend + 1; if (tokens[funcbegin].text == "{") { bool coroutine = tokens[i].text == "IEnumerator"; int funcend = FindBlock(env, tokens, funcbegin); this.Compiler_Expression_Block(tokens, env, funcbegin, funcend, out func.expr_runtime); // Unity协程判断 if (coroutine) { int yieldCount = 0; if (func.expr_runtime is CLS_Expression_Yield) { yieldCount++; } else { foreach (var v1 in func.expr_runtime.listParam) { if (v1 is CLS_Expression_Yield) { yieldCount++; } else if (v1 is CLS_Expression_LoopIf) { if (v1.listParam[1] is CLS_Expression_Yield) { yieldCount++; } else if (v1.listParam[1] is CLS_Expression_Block) { if (v1.listParam[1].listParam[v1.listParam[1].listParam.Count - 1] is CLS_Expression_Yield) { yieldCount++; } } } else if (v1 is CLS_Expression_LoopFor) { if (v1.listParam[3] is CLS_Expression_Yield) { yieldCount++; } else if (v1.listParam[3] is CLS_Expression_Block) { foreach (var v2 in v1.listParam[3].listParam) { if (v2 is CLS_Expression_Yield) { yieldCount++; } } } } } } // 暂不支持复杂嵌套的yield表达式 if (yieldCount != m_tempYieldCount) { throw new Exception(filename + "暂不支持复杂嵌套的yield表达式: " + tokens[funcbegin].ToString()); } var expr = func.expr_runtime; func.expr_runtime = new CLS_Expression_Coroutine(expr.listParam, expr.tokenBegin, expr.tokenEnd, expr.lineBegin, expr.lineEnd); } m_tempYieldCount = 0; // 判断是否是派生类的构造函数 if (baseTypeNames != null && baseTypeNames.Count > 0 && bctor) { CLS_Expression_Function baseFun = new CLS_Expression_Function(funcbegin, funcend, tokens[funcbegin].line, tokens[funcbegin].line); baseFun.funcname = classname + "->base"; func.addBaseFun(baseFun, funcbegin, funcend, tokens[funcbegin].line, tokens[funcend].line); } (sClass.function as SType).addFun(idname, func); i = funcend; } else if (tokens[funcbegin].text == ";") { func.expr_runtime = null; (sClass.function as SType).addFun(idname, func); i = funcbegin; } else if (tokens[funcbegin].text == ":") { // 添加 :base()的构造函数 支持 int funcbegin2 = funcbegin; for (; funcbegin2 < iend; funcbegin2++) { if (tokens[funcbegin2].text == "{") { CLS_Expression_Function baseFun = (CLS_Expression_Function)this.Compiler_Expression_Function(tokens, env, funcbegin + 1, funcbegin2 - 1); baseFun.funcname = classname + "->base"; int funcend2 = FindBlock(env, tokens, funcbegin2); this.Compiler_Expression_Block(tokens, env, funcbegin2, funcend2, out func.expr_runtime); func.addBaseFun(baseFun, funcbegin, funcend2, tokens[funcbegin].line, tokens[funcend2].line); (sClass.function as SType).addFun(idname, func); i = funcend2; break; } } } else { throw new Exception(filename + "不可识别的函数表达式" + tokens[funcbegin].ToString()); } } else if (tokens[i + 2].type == CSLE.TokenType.PUNCTUATION && tokens[i + 2].text == "{")//语句块开始,这是 getset属性 { logger.Log("发现Get/Set:" + idname); SType.Member member = new SType.Member(); member.bStatic = bStatic; member.sortIndex = sortIndex; member.type = idtype; i += 3; for (int count = 2; count > 0; count--) { if (tokens[i].text == "get") { // get 函数定义 if (tokens[i + 1].text == "{") { member.getFun = "get_" + idname; SType.Function getFunc = new SType.Function(); getFunc.bStatic = bStatic; int funcbegin = i + 1; int funcend = FindBlock(env, tokens, funcbegin); this.Compiler_Expression_Block(tokens, env, funcbegin, funcend, out getFunc.expr_runtime); (sClass.function as SType).addFun(member.getFun, getFunc); i = funcend + 1; } else { i += 2; // get; 模式 } } else if (tokens[i].text == "set") { // set 函数定义 if (tokens[i + 1].text == "{") { member.setFun = "set_" + idname; SType.Function setFunc = new SType.Function(); setFunc.bStatic = bStatic; setFunc._paramnames.Add("value"); setFunc._paramtypes.Add(member.type); int funcbegin = i + 1; int funcend = FindBlock(env, tokens, funcbegin); this.Compiler_Expression_Block(tokens, env, funcbegin, funcend, out setFunc.expr_runtime); (sClass.function as SType).addFun(member.setFun, setFunc); i = funcend + 1; } else { i += 2; // set; 模式 } } else if (tokens[i].text == "}") { // get/set块结束 break; } else { throw new Exception(filename + "不可识别的Get/Set" + tokens[i].ToString()); } } (sClass.function as SType).addMember(idname, member); } else if (tokens[i + 2].type == CSLE.TokenType.PUNCTUATION && (tokens[i + 2].text == "=" || tokens[i + 2].text == ";"))//这是成员定义 { logger.Log("发现成员定义:" + idname); SType.Member member = new SType.Member(); member.bStatic = bStatic; member.sortIndex = sortIndex; member.type = idtype; 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 = FindCodeAny(tokens, ref jbegin, out jdep); if (jend < posend) { jend = posend; } if (!Compiler_Expression(tokens, env, jbegin, jend, out member.expr_defvalue)) { logger.Log_Error("Get/Set定义错误"); } i = jend; } (sClass.function as SType).addMember(idname, member); } bStatic = false; sortIndex = 0; continue; } else { throw new Exception(filename + "不可识别的表达式" + tokens[i].ToString()); } } } sClass.compiled = true; // 关联基类(目前只支持单继承) if (baseTypeNames != null && baseTypeNames.Count > 0) { SType sBaseType = env.GetTypeByKeywordQuiet(baseTypeNames[0]).function as SType; SType sType = sClass.function as SType; sType.BaseType = sBaseType; bool hasBaseFun = false; foreach (KeyValuePair <string, SType.Function> pair in sBaseType.functions) { if (!sType.functions.ContainsKey(pair.Key)) { if (sBaseType.Name.Equals(pair.Key)) { hasBaseFun = true; sType.functions.Add(sType.Name + "->base", pair.Value); } else { if (pair.Value.ownerType == null) { pair.Value.ownerType = sBaseType; } sType.functions.Add(pair.Key, pair.Value); } } } foreach (KeyValuePair <string, SType.Member> pair in sBaseType.members) { if (!sType.members.ContainsKey(pair.Key)) { sType.members.Add(pair.Key, pair.Value); } } foreach (KeyValuePair <string, SType.Member> pair in sBaseType.propertys) { if (!sType.propertys.ContainsKey(pair.Key)) { sType.propertys.Add(pair.Key, pair.Value); } } // 自动创建可以调用基类函数的构造函数 if (!sType.functions.ContainsKey(sType.Name) && hasBaseFun) { // 处理 自身没构造函数 -- 基类有构造函数 的情况 SType.Function func = new SType.Function(); func.bStatic = false; CLS_Expression_Function baseFun = new CLS_Expression_Function(0, 0, 0, 0); baseFun.funcname = sType.Name + "->base"; func.addBaseFun(baseFun, 0, 0, 0, 0); sType.functions.Add(sType.Name, func); } else if (sType.functions.ContainsKey(sType.Name) && !hasBaseFun) { // 处理 自身有构造函数 -- 基类没有构造函数 的情况 SType.Function func = new SType.Function(); func.bStatic = false; sType.functions.Add(sType.Name + "->base", func); } } return(sClass); }
IList <ICLS_Type> _FileCompiler(string filename, IList <Token> tokens, bool embDeubgToken, ICLS_Environment env, bool onlyGotType = false) { m_curFileName = filename; List <ICLS_Type> typelist = new List <ICLS_Type>(); for (int i = 0; i < tokens.Count; i++) { if (tokens[i].type == TokenType.PUNCTUATION && tokens[i].text == ";") { continue; } if (tokens[i].type == TokenType.KEYWORD && tokens[i].text == "class") { string className = tokens[i + 1].text; // 在这里检查继承 List <string> baseClassNames = null; int ibegin = i + 2; if (onlyGotType) { while (tokens[ibegin].text != "{") { ibegin++; } } else { if (tokens[ibegin].text == ":") { baseClassNames = new List <string>(); ibegin++; } while (tokens[ibegin].text != "{") { if (tokens[ibegin].type == TokenType.TYPE) { baseClassNames.Add(tokens[ibegin].text); } ibegin++; } } int iend = FindBlock(env, tokens, ibegin); if (iend == -1) { env.logger.Log_Error("查找文件尾失败。"); return(null); } if (onlyGotType) { env.logger.Log("(scriptPreParser)findclass:" + className + "(" + ibegin + "," + iend + ")"); } else { env.logger.Log("(scriptParser)findclass:" + className + "(" + ibegin + "," + iend + ")"); } ICLS_Type type = Compiler_Class(env, className, baseClassNames, filename, tokens, ibegin, iend, embDeubgToken, onlyGotType, null); if (type != null) { typelist.Add(type); } i = iend; continue; } } return(typelist); }