public ICQ_Expression Compile(IList <Token> tlist, ICQ_Environment content) { ICQ_Expression value; int expbegin = 0; int expend = FindCodeBlock(tlist, expbegin); if (expend != tlist.Count - 1) { LogError(tlist, "CodeBlock 识别问题,异常结尾", expbegin, expend); return(null); } bool succ = Compiler_Expression_Block(tlist, content, expbegin, expend, out value); if (succ) { if (value == null) { logger.Log_Warn("编译为null:"); } return(value); } else { LogError(tlist, "编译失败:", expbegin, expend); return(null); } }
public ICQ_Expression Compiler_Expression_Coroutine(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { CQ_Expression_Coroutine func = new CQ_Expression_Coroutine(pos, posend, tlist[pos].line, tlist[posend].line); func.funcname = tlist[pos].text; int begin = pos + 2; int dep; int end = FindCodeAnyInFunc(tlist, ref begin, out dep); if (tlist[pos + 1].type == TokenType.PUNCTUATION && tlist[pos + 1].text == "(") { do { ICQ_Expression param; bool succ = Compiler_Expression(tlist, content, begin, end, out param); if (succ && param != null) { func.listParam.Add(param); func.tokenEnd = end; func.lineEnd = tlist[end].line; } begin = end + 2; end = FindCodeAnyInFunc(tlist, ref begin, out dep); }while (end < posend && begin <= end); return(func); } //一般函数 return(null); }
public ICQ_Expression Compiler_Expression_NegativeValue(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { int expbegin = pos; int bdep; int expend2 = FindCodeAny(tlist, ref expbegin, out bdep); if (expend2 != posend) { LogError(tlist, "无法识别的负号表达式:", expbegin, posend); return(null); } else { ICQ_Expression subvalue; bool succ = Compiler_Expression(tlist, content, expbegin, expend2, out subvalue); if (succ && subvalue != null) { CQ_Expression_NegativeValue v = new CQ_Expression_NegativeValue(pos, expend2, tlist[pos].line, tlist[expend2].line); v.listParam.Add(subvalue); return(v); } else { LogError(tlist, "无法识别的负号表达式:", expbegin, posend); return(null); } } }
public ICQ_Expression Compiler_Expression_DefineAndSet(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { int expbegin = pos + 3; int bdep; int expend = FindCodeAny(tlist, ref expbegin, out bdep); if (expend != posend) { expend = posend; } ICQ_Expression v; bool succ = Compiler_Expression(tlist, content, expbegin, expend, out v); if (succ && v != null) { CQ_Expression_Define define = new CQ_Expression_Define(pos, posend, tlist[pos].line, tlist[posend].line); if (tlist[pos].text == "bool") { define.value_type = typeof(bool); } else { ICQ_Type type = content.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 Delegate CreateDelegate(ICQ_Environment env, DeleFunction delefunc) { DeleFunction _func = delefunc; Delegate _dele = delefunc.cacheFunction(this._type, null); if (_dele != null) { return(_dele); } Action <T, T1, T2> dele = (T param0, T1 param1, T2 param2) => { var func = _func.calltype.functions[_func.function]; if (func.expr_runtime != null) { CQ_Content content = new CQ_Content(env, true); try { content.DepthAdd(); content.CallThis = _func.callthis; content.CallType = _func.calltype; content.function = _func.function; content.DefineAndSet(func._paramnames[0], func._paramtypes[0].type, param0); content.DefineAndSet(func._paramnames[1], func._paramtypes[1].type, param1); content.DefineAndSet(func._paramnames[2], func._paramtypes[2].type, param2); func.expr_runtime.ComputeValue(content); content.DepthRemove(); } catch (Exception err) { string errinfo = "Dump Call in:"; if (_func.calltype != null) { errinfo += _func.calltype.Name + "::"; } if (_func.function != null) { errinfo += _func.function; } errinfo += "\n"; env.logger.Log(errinfo + content.Dump()); throw err; } } }; Delegate d = dele as Delegate; if ((Type)this.type != typeof(Action)) { _dele = Delegate.CreateDelegate(this.type, d.Target, d.Method); } else { _dele = dele; } return(delefunc.cacheFunction(this._type, _dele)); }
public ICQ_Expression Compiler_Expression_Lambda(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { int b1; int fs1 = pos; int fe1 = FindCodeAny(tlist, ref fs1, out b1); CQ_Expression_Lambda value = new CQ_Expression_Lambda(pos, posend, tlist[pos].line, tlist[posend].line); int testbegin = fs1 + 1; if (b1 != 1) { return(null); } //(xxx)=>{...} CQ_Expression_Block block = new CQ_Expression_Block(fs1, fe1, tlist[fs1].line, tlist[fe1].line); do { int fe2 = FindCodeAny(tlist, ref testbegin, out b1); ICQ_Expression subvalue; bool succ = Compiler_Expression(tlist, content, testbegin, fe2, out subvalue); if (!succ) { break; } if (subvalue != null) { block.listParam.Add(subvalue); testbegin = fe2 + 2; } else { block.listParam.Add(null); testbegin = fe2 + 2; } }while (testbegin <= fe1); value.listParam.Add(block); //(...)=>{} ICQ_Expression subvalueblock; int b2; int fs2 = fe1 + 2; int fecode = FindCodeAny(tlist, ref fs2, out b2); bool succ2 = Compiler_Expression_Block(tlist, content, fs2, fecode, out subvalueblock); if (succ2) { //value.tokenEnd = fecode; //value.lineEnd = tlist[fecode].line; value.listParam.Add(subvalueblock); return(value); } return(null); }
public CQ_Content(ICQ_Environment environment, bool useDebug) { this.environment = environment; this.useDebug = useDebug; if (useDebug) { stackExpr = new Stack <ICQ_Expression>(); stackContent = new Stack <CQ_Content>(); } }
public ICQ_Expression Compiler_Expression_Loop_For(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { int b1; int fs1 = pos + 1; int fe1 = FindCodeAny(tlist, ref fs1, out b1); CQ_Expression_LoopFor value = new CQ_Expression_LoopFor(pos, posend, tlist[pos].line, tlist[posend].line); int testbegin = fs1 + 1; if (b1 != 1) { return(null); } do { int fe2 = FindCodeAny(tlist, ref testbegin, out b1); ICQ_Expression subvalue; // bool succ = Compiler_Expression(tlist, content, testbegin, fe2, out subvalue); Compiler_Expression(tlist, content, testbegin, fe2, out subvalue); //if (!succ) return null; // if (subvalue != null) // { value.listParam.Add(subvalue); testbegin = fe2 + 2; // } // else // { // value.listParam.Add(null); // testbegin = fe2 + 2; // } }while (testbegin <= fe1); if (value.listParam.Count != 3) { return(null); } ICQ_Expression subvalueblock; int b2; int fs2 = fe1 + 1; int fecode = FindCodeAny(tlist, ref fs2, out b2); bool succ2 = Compiler_Expression_Block(tlist, content, fs2, fecode, out subvalueblock); if (succ2) { value.tokenEnd = fecode; value.lineEnd = tlist[fecode].line; value.listParam.Add(subvalueblock); return(value); } return(null); }
public ICQ_Expression Compiler_Expression_DefineArray(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { CQ_Expression_Define define = new CQ_Expression_Define(pos, posend, tlist[pos].line, tlist[posend].line); { ICQ_Type type = content.GetTypeByKeyword(tlist[pos].text + "[]"); define.value_type = type.type; } define.value_name = tlist[pos + 3].text; return(define); }
public Delegate CreateDelegate(ICQ_Environment env, DeleFunction delefunc) { DeleFunction _func = delefunc; Delegate _dele = delefunc.cacheFunction(this._type, null); if (_dele != null) { return(_dele); } NonVoidDelegate dele = delegate(T param0, T1 param1, T2 param2) { var func = _func.calltype.functions[_func.function]; if (func.expr_runtime != null) { CQ_Content content = new CQ_Content(env, true); try { content.DepthAdd(); content.CallThis = _func.callthis; content.CallType = _func.calltype; content.function = _func.function; content.DefineAndSet(func._paramnames[0], func._paramtypes[0].type, param0); content.DefineAndSet(func._paramnames[1], func._paramtypes[1].type, param1); content.DefineAndSet(func._paramnames[2], func._paramtypes[2].type, param2); CQ_Content.Value retValue = func.expr_runtime.ComputeValue(content); content.DepthRemove(); return((ReturnType)retValue.value); } catch (Exception err) { string errinfo = "Dump Call in:"; if (_func.calltype != null) { errinfo += _func.calltype.Name + "::"; } if (_func.function != null) { errinfo += _func.function; } errinfo += "\n"; env.logger.Log(errinfo + content.Dump()); throw err; } } return(default(ReturnType)); }; _dele = Delegate.CreateDelegate(this.type, dele.Target, dele.Method); return(delefunc.cacheFunction(this._type, _dele)); }
public ICQ_Expression Optimize(ICQ_Expression value, ICQ_Environment env) { ICQ_Expression expr = value as ICQ_Expression; if (expr == null) { return(value); } else { return(OptimizeDepth(expr, new CQ_Content(env))); } }
public ICQ_Expression Compiler_Expression_Loop_Return(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { CQ_Expression_LoopReturn value = new CQ_Expression_LoopReturn(pos, posend, tlist[pos].line, tlist[posend].line); ICQ_Expression subvalue; bool succ = Compiler_Expression(tlist, content, pos + 1, posend, out subvalue); if (succ) { value.listParam.Add(subvalue); } return(value); }
public ICQ_Expression Compiler_Expression_Loop_Dowhile(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { int b1; int fs1 = pos + 1; int fe1 = FindCodeAny(tlist, ref fs1, out b1); CQ_Expression_LoopDowhile value = new CQ_Expression_LoopDowhile(pos, fe1, tlist[pos].line, tlist[fe1].line); //do(xxx)while(...) { ICQ_Expression subvalue; bool succ = Compiler_Expression_Block(tlist, content, fs1, fe1, out subvalue); if (succ) { value.tokenEnd = fe1; value.lineEnd = tlist[fe1].line; value.listParam.Add(subvalue); } else { return(null); } } //do{...]while(yyy); if (tlist[fe1 + 1].text != "while") { return(null); } int b2; int fs2 = fe1 + 2; int fe2 = FindCodeAny(tlist, ref fs2, out b2); { ICQ_Expression subvalue; bool succ = Compiler_Expression(tlist, content, fs2, fe2, out subvalue); if (succ) { value.tokenEnd = fe2; value.lineEnd = tlist[fe2].line; value.listParam.Add(subvalue); } else { return(null); } } return(value); }
public Delegate CreateDelegate(ICQ_Environment env, DeleLambda lambda) { CQ_Content content = lambda.content.Clone(); var pnames = lambda.paramNames; var expr = lambda.expr_func; Action <T> dele = (T param0) => { if (expr != null) { try { content.DepthAdd(); content.DefineAndSet(pnames[0], typeof(T), param0); expr.ComputeValue(content); content.DepthRemove(); } catch (Exception err) { string errinfo = "Dump Call lambda in:"; if (content.CallType != null) { errinfo += content.CallType.Name + "::"; } if (content.function != null) { errinfo += content.function; } errinfo += "\n"; env.logger.Log(errinfo + content.Dump()); throw err; } } }; Delegate d = dele as Delegate; if ((Type)this.type != typeof(Action <T>)) { return(Delegate.CreateDelegate(this.type, d.Target, d.Method)); } else { return(dele); } }
public ICQ_Expression Compiler_Expression_Define(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { CQ_Expression_Define define = new CQ_Expression_Define(pos, posend, tlist[pos].line, tlist[posend].line); if (tlist[pos].text == "bool") { define.value_type = typeof(bool); } else { ICQ_Type type = content.GetTypeByKeyword(tlist[pos].text); define.value_type = type.type; } define.value_name = tlist[pos + 1].text; return(define); }
List <string> Compiler_Using(IList <Token> tokens, ICQ_Environment env, int pos, int posend) { List <string> _namespace = new List <string>(); for (int i = pos + 1; i <= posend; i++) { if (tokens[i].type == TokenType.PUNCTUATION && tokens[i].text == ".") { continue; } else { _namespace.Add(tokens[i].text); } } return(_namespace); }
public Delegate CreateDelegate(ICQ_Environment env, DeleLambda lambda) { CQ_Content content = lambda.content.Clone(); var pnames = lambda.paramNames; var expr = lambda.expr_func; NonVoidDelegate dele = delegate(T param0, T1 param1) { if (expr != null) { try { content.DepthAdd(); content.DefineAndSet(pnames[0], typeof(T), param0); content.DefineAndSet(pnames[1], typeof(T1), param1); CQ_Content.Value retValue = expr.ComputeValue(content); content.DepthRemove(); return((ReturnType)retValue.value); } catch (Exception err) { string errinfo = "Dump Call lambda in:"; if (content.CallType != null) { errinfo += content.CallType.Name + "::"; } if (content.function != null) { errinfo += content.function; } errinfo += "\n"; env.logger.Log(errinfo + content.Dump()); throw err; } } return(default(ReturnType)); }; Delegate d = dele as Delegate; return(Delegate.CreateDelegate(this.type, d.Target, d.Method)); }
public ICQ_Expression Compiler_Expression_FunctionTrace(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { if (tlist[pos + 1].type == TokenType.PUNCTUATION && tlist[pos + 1].text == "(") { return(Compiler_Expression_Function(tlist, content, pos, posend)); } int begin = pos + 1; int dep; int end = FindCodeAnyInFunc(tlist, ref begin, out dep); if (end != posend) { return(null); } CQ_Expression_Function func = new CQ_Expression_Function(pos, end, tlist[pos].line, tlist[end].line); func.funcname = "trace"; do { ICQ_Expression param; bool succ = Compiler_Expression(tlist, content, begin, end, out param); if (succ && param != null) { func.listParam.Add(param); func.tokenEnd = end; func.lineEnd = tlist[end].line; } begin = end + 2; end = FindCodeAnyInFunc(tlist, ref begin, out dep); }while (end < posend && begin <= end); //ICQ_Expression param0; //bool succ = Compiler_Expression(tlist,content, begin, end, out param0); //if(succ&¶m0!=null) //{ // func.listParam.Add(param0); // return func; //} return(func); //trace ,单值直接dump,否则按逗号分隔的表达式处理 //return null; }
public ICQ_Expression Compiler_Expression_FunctionThrow(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { CQ_Expression_Throw func = new CQ_Expression_Throw(pos, posend, tlist[pos].line, tlist[posend].line); ICQ_Expression subvalue; bool succ = Compiler_Expression(tlist, content, pos + 1, posend, out subvalue); if (succ) { func.listParam.Add(subvalue); } return(func); //trace ,单值直接dump,否则按逗号分隔的表达式处理 //return null; }
//Dictionary<string, functioninfo> funcs = new Dictionary<string, functioninfo>(); int FindBlock(ICQ_Environment env, IList <CQuark.Token> tokens, int start) { if (tokens[start].type != CQuark.TokenType.PUNCTUATION) { env.logger.Log_Error("(script)FindBlock 没有从符号开始"); } string left = tokens[start].text; string right = "}"; if (left == "{") { right = "}"; } if (left == "(") { right = ")"; } if (left == "[") { right = "]"; } int depth = 0; for (int i = start; i < tokens.Count; i++) { if (tokens[i].type == CQuark.TokenType.PUNCTUATION) { if (tokens[i].text == left) { depth++; } else if (tokens[i].text == right) { depth--; if (depth == 0) { return(i); } } } } return(-1); }
public ICQ_Expression Compile_NoBlock(IList <Token> tlist, ICQ_Environment content) { ICQ_Expression value; int expbegin = 0; int expend = tlist.Count - 1; bool succ = Compiler_Expression(tlist, content, expbegin, expend, out value); if (succ) { if (value == null) { logger.Log_Warn("编译为null:"); } return(value); } else { LogError(tlist, "编译失败:", expbegin, expend); return(null); } }
public ICQ_Expression Compiler_Expression_NegativeLogic(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { int expbegin = pos; int bdep; int expend2 = FindCodeAny(tlist, ref expbegin, out bdep); if (expend2 != posend) { //expend2 = posend; LogError(tlist, "无法识别的取反表达式:", expbegin, posend); return(null); } //else { ICQ_Expression subvalue; bool succ = Compiler_Expression(tlist, content, expbegin, expend2, out subvalue); if (succ && subvalue != null) { if (subvalue is CQ_Expression_Math2Value || subvalue is CQ_Expression_Math2ValueAndOr || subvalue is CQ_Expression_Math2ValueLogic) { var pp = subvalue.listParam[0]; CQ_Expression_NegativeLogic v = new CQ_Expression_NegativeLogic(pp.tokenBegin, pp.tokenEnd, pp.lineBegin, pp.lineEnd); v.listParam.Add(pp); subvalue.listParam[0] = v; return(subvalue); } else { CQ_Expression_NegativeLogic v = new CQ_Expression_NegativeLogic(pos, expend2, tlist[pos].line, tlist[expend2].line); v.listParam.Add(subvalue); return(v); } } else { LogError(tlist, "无法识别的取反表达式:", expbegin, posend); return(null); } } }
void NewStatic(ICQ_Environment env) { if (contentMemberCalc == null) { contentMemberCalc = new CQ_Content(env); } if (this.staticMemberInstance == null) { staticMemberInstance = new Dictionary <string, CQ_Content.Value>(); foreach (var i in this.members) { if (i.Value.bStatic == true) { if (i.Value.expr_defvalue == null) { staticMemberInstance[i.Key] = new CQ_Content.Value(); staticMemberInstance[i.Key].type = i.Value.type.type; staticMemberInstance[i.Key].value = i.Value.type.DefValue; } else { var value = i.Value.expr_defvalue.ComputeValue(contentMemberCalc); if (i.Value.type.type != value.type) { staticMemberInstance[i.Key] = new CQ_Content.Value(); staticMemberInstance[i.Key].type = i.Value.type.type; staticMemberInstance[i.Key].value = env.GetType(value.type).ConvertTo(contentMemberCalc, value.value, i.Value.type.type); } else { staticMemberInstance[i.Key] = value; } } } } } }
public ICQ_Expression Compiler_Expression_Set(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { int expbegin = pos + 2; int bdep; int expend = FindCodeAny(tlist, ref expbegin, out bdep); if (expend != posend) { expend = posend; } ICQ_Expression v; bool succ = Compiler_Expression(tlist, content, expbegin, expend, out v); if (succ && v != null) { CQ_Expression_SetValue define = new CQ_Expression_SetValue(pos, expend, tlist[pos].line, tlist[expend].line); define.value_name = tlist[pos].text; define.listParam.Add(v); return(define); } LogError(tlist, "不正确的定义表达式:", pos, posend); return(null); }
ICQ_Type Compiler_Class(ICQ_Environment env, string classname, bool bInterface, IList <string> basetype, string filename, IList <Token> tokens, int ibegin, int iend, bool EmbDebugToken, bool onlyGotType, IList <string> usinglist) { CQ_Type_Class stype = env.GetTypeByKeywordQuiet(classname) as CQ_Type_Class; if (stype == null) { stype = new CQ_Type_Class(classname, bInterface, filename); } if (basetype != null && basetype.Count != 0 && onlyGotType == false) { List <ICQ_Type> basetypess = new List <ICQ_Type>(); foreach (string t in basetype) { ICQ_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))//发现类型 { ICQ_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 == CQuark.TokenType.IDENTIFIER || bctor) //类型后面是名称 { string idname = tokens[i + 1].text; if (tokens[i + 2].type == CQuark.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, ICQ_Type> _params = new Dictionary<string, ICQ_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); 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(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(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 SType.Member(); member.bStatic = bStatic; member.bPublic = bPublic; member.bReadOnly = !(haveset && setpublic); member.type = idtype; logger.Log("发现Get/Set:" + idname); //ICQ_Expression expr = null; if (tokens[i + 2].text == "=") { int jbegin = i + 3; int jdep; int jend = FindCodeAny(tokens, ref jbegin, out jdep); if (!Compiler_Expression(tokens, env, jbegin, jend, out member.expr_defvalue)) { logger.Log_Error("Get/Set定义错误"); } i = jend; } (stype.function as SType).members.Add(idname, member); } else if (tokens[i + 2].type == CQuark.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; //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 = 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("成员定义错误"); } i = jend; } (stype.function as SType).members.Add(idname, member); } bPublic = false; bStatic = false; continue; } else { throw new Exception(filename + ":不可识别的表达式:" + tokens[i].ToString() + tokens[i].SourcePos()); } } } stype.compiled = true; return(stype); }
IList <ICQ_Type> _FileCompiler(string filename, IList <Token> tokens, bool embDeubgToken, ICQ_Environment env, bool onlyGotType) { List <ICQ_Type> typelist = new List <ICQ_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); List <string> 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 == "CQuark" && 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 //ICQ_Type type = Compiler_Class(env, name, (tokens[i].text == "interface"), filename, tokens, ibegin, iend, embDeubgToken, true); //bJumpClass = false; } else { ICQ_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); }
public ICQ_Expression Compiler_Expression_Loop_SwitchCase(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { // UnityEngine.Debug.Log("CompilerLoop : " + GetCodeKeyString(tlist, pos, posend)); int b1; int fs1 = pos + 1; int fe1 = FindCodeAny(tlist, ref fs1, out b1); CQ_Expression_LoopSwitchCase value = new CQ_Expression_LoopSwitchCase(pos, posend, tlist[pos].line, tlist[posend].line); // UnityEngine.Debug.Log("switch : " + GetCodeKeyString(tlist,pos, fe1)); //switch(xxx) ICQ_Expression subvalueblock; bool succ = Compiler_Expression_Block(tlist, content, fs1, fe1, out subvalueblock); if (!succ) { return(null); } value.listParam.Add(subvalueblock); int caseBegin = fe1 + 2; while (caseBegin < posend) { if (tlist[caseBegin].type == TokenType.KEYWORD) { int poscolon = caseBegin; for (; poscolon < posend; poscolon++) { if (tlist[poscolon].type == TokenType.PUNCTUATION && tlist[poscolon].text == ":") { break; } } int sexpr = caseBegin; if (tlist[caseBegin].text == "case") { //case xxx: caseBegin++; // UnityEngine.Debug.Log(GetCodeKeyString(tlist,caseBegin, poscolon - 1)); bool succ2 = Compiler_Expression_Block(tlist, content, caseBegin, poscolon - 1, out subvalueblock); if (succ2) { value.listParam.Add(subvalueblock); sexpr = poscolon + 1; } else { return(null); } } else if (tlist[caseBegin].text == "default") { //default: value.listParam.Add(null); sexpr = poscolon + 1; } else { return(null); } if (tlist[sexpr].type == TokenType.KEYWORD && tlist[sexpr].text == "case") { //switch(..){case ...:case ...} //(no break) value.listParam.Add(null); caseBegin = poscolon + 1; continue; } else { //找到下一个深度为0的case或default或深度为-1的“}” int eexpr = sexpr + 1; // UnityEngine.Debug.Log("case do ~ : " + GetCodeKeyString(tlist, sexpr, posend)); int bracedepth = 0; for (; eexpr < posend; eexpr++) { if (tlist[eexpr].type == TokenType.PUNCTUATION && tlist[eexpr].text == "{") { bracedepth++; } else { if (tlist[eexpr].type == TokenType.KEYWORD && (tlist[eexpr].text == "case" || tlist[eexpr].text == "default")) { if (bracedepth == 0) { break; } } if (tlist[eexpr].type == TokenType.PUNCTUATION && tlist[eexpr].text == "}") { bracedepth--; if (bracedepth == -1) { break; } } } } // UnityEngine.Debug.Log("case do ~ break" + GetCodeKeyString(tlist, sexpr, eexpr - 1)); bool succ3 = Compiler_Expression_Block(tlist, content, sexpr, eexpr - 1, out subvalueblock); if (succ3) { value.listParam.Add(subvalueblock); caseBegin = eexpr; } else { return(null); } } } else { return(null); } } return(value); }
public ICQ_Expression Compiler_Expression_Loop_Try(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { CQ_Expression_LoopTry value = new CQ_Expression_LoopTry(pos, posend, tlist[pos].line, tlist[posend].line); int b1; int fs1 = pos + 1; int fe1 = FindCodeAny(tlist, ref fs1, out b1); if (b1 != 2) { return(null); } //try { ICQ_Expression subvalue; bool succ = Compiler_Expression_Block(tlist, content, fs1, fe1, out subvalue); if (succ) { value.tokenEnd = fe1; value.lineEnd = tlist[fe1].line; value.listParam.Add(subvalue); } else { return(null); } } while (fe1 < posend && tlist[fe1 + 1].text == "catch") { //catch(...) int b2; int fs2 = fe1 + 2; int fe2 = FindCodeAny(tlist, ref fs2, out b2); { if (b2 != 1) { return(null); } ICQ_Expression subvalue; bool succ = Compiler_Expression(tlist, content, fs2, fe2, out subvalue); if (succ) { value.tokenEnd = fe2; value.lineEnd = tlist[fe2].line; value.listParam.Add(subvalue); } else { return(null); } } //catch(){...} { int b3; int fs3 = fe2 + 1; int fe3 = FindCodeAny(tlist, ref fs3, out b3); if (b3 != 2) { return(null); } ICQ_Expression subvalue; bool succ = Compiler_Expression_Block(tlist, content, fs3, fe3, out subvalue); if (succ) { value.tokenEnd = fe3; value.lineEnd = tlist[fe3].line; value.listParam.Add(subvalue); } else { return(null); } fe1 = fe3; } } return(value); }
public ICQ_Expression Compiler_Expression_Math(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { IList <int> sps = SplitExpressionWithOp(tlist, pos, posend); int oppos = GetLowestMathOp(tlist, sps); if (oppos < 0) { ////也有可能是类型转换 //if (posend >= pos + 3 && tlist[pos].text == "(" && tlist[pos].type == TokenType.PUNCTUATION && tlist[pos + 2].text == ")" && tlist[pos + 2].type == TokenType.PUNCTUATION // && tlist[pos + 1].type == TokenType.TYPE // ) //{ // ICQ_Expression v; // bool succ = Compiler_Expression(tlist, content, pos + 3, posend, out v); // CQ_Expression_TypeConvert convert = new CQ_Expression_TypeConvert(); // convert.listParam.Add(v); // convert.targettype = content.environment.GetTypeByKeyword(tlist[pos + 1].text).type; // return convert; //} //else if (tlist[pos + 1].type == TokenType.PUNCTUATION && tlist[pos + 1].text == "[")//函数表达式 //{ // return Compiler_Expression_IndexFind(tlist, content, pos, posend); //} if (tlist[pos + 1].type == TokenType.PUNCTUATION && tlist[pos + 1].text == "(")//函数表达式 { if (content.IsCoroutine(tlist[pos].text)) { // if(tlist[pos].text == "YieldWaitForSecond") return(Compiler_Expression_Coroutine(tlist, content, pos, posend)); } else { return(Compiler_Expression_Function(tlist, content, pos, posend)); } } else { //if (!bTest && tlist[expbegin + 1].type == TokenType.PUNCTUATION && tlist[expbegin + 1].text == "(")//函数表达式 //{ // ICQ_Expression subvalue = Compiler_Expression_Function(tlist,content, expbegin, expend); // if (null == subvalue) return false; // else // values.Add(subvalue); // bTest = true; //} } return(null); } Token tkCur = tlist[oppos]; if (tkCur.text == "=>") { //lambda return(Compiler_Expression_Lambda(tlist, content, pos, posend)); } else if (tkCur.text == "." && pos == oppos - 1 && tlist[pos].type == TokenType.TYPE) { int right = oppos + 1; int rightend = posend; ICQ_Expression valueright; bool succ2 = Compiler_Expression(tlist, content, right, rightend, out valueright); if (succ2) { CQ_Expression_GetValue vg = valueright as CQ_Expression_GetValue; CQ_Expression_Function vf = valueright as CQ_Expression_Function; if (vg != null) { CQ_Expression_StaticFind value = new CQ_Expression_StaticFind(pos, rightend, tlist[pos].line, tlist[rightend].line); value.staticmembername = vg.value_name; value.type = content.GetTypeByKeyword(tlist[pos].text); return(value); } else if (vf != null) { CQ_Expression_StaticFunction value = new CQ_Expression_StaticFunction(pos, rightend, tlist[pos].line, tlist[rightend].line); value.functionName = vf.funcname; value.type = content.GetTypeByKeyword(tlist[pos].text); //value.listParam.Add(valueleft); value.listParam.AddRange(vf.listParam.ToArray()); return(value); } else if (valueright is CQ_Expression_SelfOp) { CQ_Expression_SelfOp vr = valueright as CQ_Expression_SelfOp; CQ_Expression_StaticMath value = new CQ_Expression_StaticMath(pos, rightend, tlist[pos].line, tlist[rightend].line); value.type = content.GetTypeByKeyword(tlist[pos].text); value.staticmembername = vr.value_name; value.mathop = vr.mathop; return(value); } else { throw new Exception("不可识别的表达式:" + tkCur.ToString() + tkCur.SourcePos()); } } else { throw new Exception("不可识别的表达式:" + tkCur.ToString() + tkCur.SourcePos()); } } else { int left = pos; int leftend = oppos - 1; int right = oppos + 1; int rightend = posend; if (tkCur.text == "(") { ICQ_Expression v; if (!Compiler_Expression(tlist, content, oppos + 3, posend, out v)) { LogError(tlist, "编译表达式失败", right, rightend); return(null); } CQ_Expression_TypeConvert convert = new CQ_Expression_TypeConvert(pos, posend, tlist[pos].line, tlist[posend].line); convert.listParam.Add(v); convert.targettype = content.GetTypeByKeyword(tlist[oppos + 1].text).type; return(convert); } ICQ_Expression valueleft; bool succ1 = Compiler_Expression(tlist, content, left, leftend, out valueleft); ICQ_Expression valueright; if (tkCur.text == "[") { rightend--; if (!Compiler_Expression(tlist, content, right, rightend, out valueright)) { LogError(tlist, "编译表达式失败", right, rightend); return(null); } CQ_Expression_IndexFind value = new CQ_Expression_IndexFind(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); value.listParam.Add(valueright); return(value); } else if (tkCur.text == "as") { CQ_Expression_TypeConvert convert = new CQ_Expression_TypeConvert(left, oppos + 1, tlist[left].line, tlist[oppos + 1].line); convert.listParam.Add(valueleft); convert.targettype = content.GetTypeByKeyword(tlist[oppos + 1].text).type; return(convert); } else if (tkCur.text == "is") { CQ_Expression_TypeCheck check = new CQ_Expression_TypeCheck(left, oppos + 1, tlist[left].line, tlist[oppos + 1].line); check.listParam.Add(valueleft); check.targettype = content.GetTypeByKeyword(tlist[oppos + 1].text).type; return(check); } bool succ2 = Compiler_Expression(tlist, content, right, rightend, out valueright); if (succ1 && succ2 && valueright != null && valueleft != null) { if (tkCur.text == "=") { //member set CQ_Expression_MemberFind mfinde = valueleft as CQ_Expression_MemberFind; CQ_Expression_StaticFind sfinde = valueleft as CQ_Expression_StaticFind; CQ_Expression_IndexFind ifinde = valueleft as CQ_Expression_IndexFind; if (mfinde != null) { CQ_Expression_MemberSetValue value = new CQ_Expression_MemberSetValue(left, rightend, tlist[left].line, tlist[rightend].line); value.membername = mfinde.membername; value.listParam.Add(mfinde.listParam[0]); value.listParam.Add(valueright); return(value); } else if (sfinde != null) { CQ_Expression_StaticSetValue value = new CQ_Expression_StaticSetValue(left, rightend, tlist[left].line, tlist[rightend].line); value.staticmembername = sfinde.staticmembername; value.type = sfinde.type; //value.listParam.Add(mfinde.listParam[0]); value.listParam.Add(valueright); return(value); } else if (ifinde != null) { CQ_Expression_IndexSetValue value = new CQ_Expression_IndexSetValue(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(ifinde.listParam[0]); value.listParam.Add(ifinde.listParam[1]); value.listParam.Add(valueright); return(value); } else { throw new Exception("非法的Member Set表达式" + valueleft); } } else if (tkCur.text == ".") { //FindMember CQ_Expression_GetValue vg = valueright as CQ_Expression_GetValue; CQ_Expression_Function vf = valueright as CQ_Expression_Function; if (vg != null) { CQ_Expression_MemberFind value = new CQ_Expression_MemberFind(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); value.membername = vg.value_name; return(value); } else if (vf != null) { CQ_Expression_MemberFunction value = new CQ_Expression_MemberFunction(left, rightend, tlist[left].line, tlist[rightend].line); value.functionName = vf.funcname; value.listParam.Add(valueleft); value.listParam.AddRange(vf.listParam.ToArray()); return(value); } else if (valueright is CQ_Expression_SelfOp) { CQ_Expression_SelfOp vr = valueright as CQ_Expression_SelfOp; CQ_Expression_MemberMath value = new CQ_Expression_MemberMath(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); value.membername = vr.value_name; value.mathop = vr.mathop; return(value); } throw new Exception("不可识别的表达式" + valueleft + "." + valueright); //value.listParam.Add(valueright); } else if (tkCur.text == "+=" || tkCur.text == "-=" || tkCur.text == "*=" || tkCur.text == "/=" || tkCur.text == "%=") { //if (valueleft is CQ_Expression_MemberFind) //{ // CQ_Expression_MemberFind vf = valueleft as CQ_Expression_MemberFind; // CQ_Expression_MemberMath value = new CQ_Expression_MemberMath(left, rightend, tlist[left].line, tlist[rightend].line); // value.listParam.Add(vf.listParam[0]); // value.membername = vf.membername; // value.mathop = tlist[oppos].text[0]; // value.listParam.Add(valueright); // return value; //} //if ((valueright is CQ_Expression_Lambda ==false) && valueleft is CQ_Expression_StaticFind) //{ // CQ_Expression_StaticFind vf = valueleft as CQ_Expression_StaticFind; // CQ_Expression_StaticMath value = new CQ_Expression_StaticMath(left, rightend, tlist[left].line, tlist[rightend].line); // value.type = vf.type; // value.staticmembername = vf.staticmembername; // value.mathop = tlist[oppos].text[0]; // value.listParam.Add(valueright); // return value; //} //else { CQ_Expression_SelfOpWithValue value = new CQ_Expression_SelfOpWithValue(left, rightend, tlist[left].line, tlist[rightend].line); //value.value_name = ((CQ_Expression_GetValue)valueleft).value_name; value.listParam.Add(valueleft); value.listParam.Add(valueright); value.mathop = tkCur.text[0]; return(value); } } else if (tkCur.text == "&&" || tkCur.text == "||") { CQ_Expression_Math2ValueAndOr value = new CQ_Expression_Math2ValueAndOr(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); value.listParam.Add(valueright); value.mathop = tkCur.text[0]; return(value); } else if (tkCur.text == ">" || tkCur.text == ">=" || tkCur.text == "<" || tkCur.text == "<=" || tkCur.text == "==" || tkCur.text == "!=") { CQ_Expression_Math2ValueLogic value = new CQ_Expression_Math2ValueLogic(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); value.listParam.Add(valueright); logictoken token = logictoken.not_equal; if (tkCur.text == ">") { token = logictoken.more; } else if (tkCur.text == ">=") { token = logictoken.more_equal; } else if (tkCur.text == "<") { token = logictoken.less; } else if (tkCur.text == "<=") { token = logictoken.less_equal; } else if (tkCur.text == "==") { token = logictoken.equal; } else if (tkCur.text == "!=") { token = logictoken.not_equal; } value.mathop = token; return(value); } else { char mathop = tkCur.text[0]; if (mathop == '?') { CQ_Expression_Math3Value value = new CQ_Expression_Math3Value(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); CQ_Expression_Math2Value vvright = valueright as CQ_Expression_Math2Value; if (vvright.mathop != ':') { throw new Exception("三元表达式异常" + tkCur.ToString() + tkCur.SourcePos()); } value.listParam.Add(vvright.listParam[0]); value.listParam.Add(vvright.listParam[1]); return(value); } else { CQ_Expression_Math2Value value = new CQ_Expression_Math2Value(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); value.listParam.Add(valueright); value.mathop = mathop; return(value); } } } else { LogError(tlist, "编译表达式失败", right, rightend); } } return(null); }
public ICQ_Expression Compiler_Expression_Loop_If(IList <Token> tlist, ICQ_Environment content, int pos, int posend) { CQ_Expression_LoopIf value = new CQ_Expression_LoopIf(pos, posend, tlist[pos].line, tlist[posend].line); int b1; int fs1 = pos + 1; int fe1 = FindCodeAny(tlist, ref fs1, out b1); if (b1 != 1) { return(null); } //if(xxx) { ICQ_Expression subvalue; bool succ = Compiler_Expression(tlist, content, fs1, fe1, out subvalue); if (succ) { value.tokenEnd = fe1; value.lineEnd = tlist[fe1].line; value.listParam.Add(subvalue); } else { return(null); } } //if(...){yyy} int b2; int fs2 = fe1 + 1; int fe2 = FindCodeAny(tlist, ref fs2, out b2); { ICQ_Expression subvalue; bool succ = Compiler_Expression_Block(tlist, content, fs2, fe2, out subvalue); if (succ) { value.tokenEnd = fe2; value.lineEnd = tlist[fe2].line; value.listParam.Add(subvalue); } else { return(null); } } int nelse = fe2 + 1; if (b2 == 0) { nelse++; } FindCodeAny(tlist, ref nelse, out b2); if (tlist.Count > nelse) { if (tlist[nelse].type == TokenType.KEYWORD && tlist[nelse].text == "else") { //if(...){...}else{zzz} int b3; int fs3 = nelse + 1; int fe3 = FindCodeAny(tlist, ref fs3, out b3); ICQ_Expression subvalue; bool succ = Compiler_Expression_Block(tlist, content, fs3, fe3, out subvalue); if (succ) { value.tokenEnd = fe3; value.lineEnd = tlist[fe3].line; value.listParam.Add(subvalue); } else { return(null); } } } return(value); }