public ICLS_Expression Compiler_Expression_Function(IList<Token> tlist, ICLS_Environment content, int pos, int posend) { CLS_Expression_Function func = new CLS_Expression_Function(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 { ICLS_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 ICLS_Expression Compiler_Expression_Function(IList <Token> tlist, ICLS_Environment content, int pos, int posend) { CLS_Expression_Function func = new CLS_Expression_Function(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 { ICLS_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 ICLS_Expression Compiler_Expression_FunctionTrace(IList <Token> tlist, ICLS_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); } CLS_Expression_Function func = new CLS_Expression_Function(pos, end, tlist[pos].line, tlist[end].line); func.funcname = "trace"; do { ICLS_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); //ICLS_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 ICLS_Expression Compiler_Expression_FunctionTrace(IList<Token> tlist, ICLS_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; } CLS_Expression_Function func = new CLS_Expression_Function(pos, end, tlist[pos].line, tlist[end].line); func.funcname = "trace"; do { ICLS_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); //ICLS_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 ICLS_Expression Compiler_Expression_Math(IList <Token> tlist, ICLS_Environment environment, int pos, int posend) { IList <int> sps = SplitExpressionWithOp(tlist, pos, posend); if (sps == null) { LogError(tlist, "SplitExpressionWithOp return null", pos, posend); } int oppos = GetLowestMathOp(tlist, sps); if (oppos < 0) { if (tlist[pos + 1].type == TokenType.PUNCTUATION && tlist[pos + 1].text == "(")//函数表达式 { CLS_Expression_Function func = (CLS_Expression_Function)Compiler_Expression_Function(tlist, environment, pos, posend); if (func != null) { if (environment.GetFunction(func.funcname) != null) { CLS_Expression_GlobalFunction globalFunc = new CLS_Expression_GlobalFunction(func.listParam, func.tokenBegin, func.tokenEnd, func.lineBegin, func.lineEnd); globalFunc.funcname = func.funcname; return(globalFunc); } } return(func); } return(null); } Token tkCur = tlist[oppos]; if (tkCur.text == "=>") { return(Compiler_Expression_Lambda(tlist, environment, pos, posend)); } else if (tkCur.text == "." && pos == oppos - 1 && tlist[pos].type == TokenType.TYPE) { int right = oppos + 1; int rightend = posend; ICLS_Expression valueright; bool succ2 = Compiler_Expression(tlist, environment, right, rightend, out valueright); if (succ2) { CLS_Expression_GetValue vg = valueright as CLS_Expression_GetValue; CLS_Expression_Function vf = valueright as CLS_Expression_Function; if (vg != null) { // 优化枚举常量表达式 try { System.Type sysType = environment.GetTypeByKeyword(tlist[pos].text).type; if (sysType != null && sysType.IsEnum) { CLS_Expression_Enum enumVal = new CLS_Expression_Enum(sysType); enumVal.tokenBegin = pos; enumVal.tokenEnd = rightend; enumVal.lineBegin = tlist[pos].line; enumVal.lineEnd = tlist[rightend].line; enumVal.value = Enum.Parse(sysType, vg.value_name); return(enumVal); } } catch (Exception ex) { logger.Log_Warn("Enum expression: " + ex.Message); } CLS_Expression_StaticFind value = new CLS_Expression_StaticFind(pos, rightend, tlist[pos].line, tlist[rightend].line); value.staticmembername = vg.value_name; value.type = environment.GetTypeByKeyword(tlist[pos].text); return(value); } else if (vf != null) { CLS_Expression_StaticFunction value = new CLS_Expression_StaticFunction(pos, rightend, tlist[pos].line, tlist[rightend].line); value.functionName = vf.funcname; value.type = environment.GetTypeByKeyword(tlist[pos].text); value.listParam.AddRange(vf.listParam.ToArray()); return(value); } else if (valueright is CLS_Expression_SelfOp) { CLS_Expression_SelfOp vr = valueright as CLS_Expression_SelfOp; CLS_Expression_StaticMath value = new CLS_Expression_StaticMath(pos, rightend, tlist[pos].line, tlist[rightend].line); value.type = environment.GetTypeByKeyword(tlist[pos].text); value.staticmembername = vr.value_name; value.mathop = vr.mathop; return(value); } else { throw new Exception("不可识别的表达式:" + tkCur.ToString()); } } else { throw new Exception("不可识别的表达式:" + tkCur.ToString()); } } else { int left = pos; int leftend = oppos - 1; int right = oppos + 1; int rightend = posend; if (tkCur.text == "(") { int offset = 0; // 如果是(Type[]) if (tlist[oppos + 2].text == "[" && tlist[oppos + 3].text == "]") { offset = 5; } // 普通强转类型(Type) (Type.Type) (Type.Type.Type) else if (tlist[oppos + 2].text == ")") { offset = 3; } else { LogError(tlist, "暂不支持该语法,可以删除多余的括号", pos, posend); return(null); } if (offset > 0) { ICLS_Expression v; if (!Compiler_Expression(tlist, environment, oppos + offset, posend, out v)) { LogError(tlist, "编译表达式失败", right, rightend); return(null); } CLS_Expression_TypeConvert convert = new CLS_Expression_TypeConvert(pos, posend, tlist[pos].line, tlist[posend].line); convert.listParam.Add(v); convert.targettype = environment.GetTypeByKeyword(tlist[oppos + 1].text).type; return(convert); } } ICLS_Expression valueleft; bool succ1 = Compiler_Expression(tlist, environment, left, leftend, out valueleft); ICLS_Expression valueright; if (tkCur.text == "[") { Token tkEnd = tlist[rightend]; if (tkEnd.text == "++" || tkEnd.text == "--") { rightend -= 2; } else { rightend--; } if (!Compiler_Expression(tlist, environment, right, rightend, out valueright)) { LogError(tlist, "编译表达式失败", right, rightend); return(null); } CLS_Expression_IndexFind value = new CLS_Expression_IndexFind(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); value.listParam.Add(valueright); if (tkEnd.text == "++" || tkEnd.text == "--") { CLS_Expression_SelfOpWithValue selfOpExp = new CLS_Expression_SelfOpWithValue(left, posend, tlist[left].line, tlist[posend].line); selfOpExp.listParam.Add(value); selfOpExp.listParam.Add(new CLS_Expression_Value <int>() { value = 1 }); selfOpExp.mathop = tkEnd.text[0]; return(selfOpExp); } return(value); } else if (tkCur.text == "as") { CLS_Expression_TypeConvert convert = new CLS_Expression_TypeConvert(left, oppos + 1, tlist[left].line, tlist[oppos + 1].line); convert.listParam.Add(valueleft); convert.targettype = environment.GetTypeByKeyword(tlist[oppos + 1].text).type; return(convert); } else if (tkCur.text == "is") { CLS_Expression_TypeCheck check = new CLS_Expression_TypeCheck(left, oppos + 1, tlist[left].line, tlist[oppos + 1].line); check.listParam.Add(valueleft); check.targettype = environment.GetTypeByKeyword(tlist[oppos + 1].text).type; return(check); } bool succ2 = Compiler_Expression(tlist, environment, right, rightend, out valueright); if (succ1 && succ2 && valueright != null && valueleft != null) { if (tkCur.text == "=") { //member set CLS_Expression_MemberFind mfinde = valueleft as CLS_Expression_MemberFind; CLS_Expression_StaticFind sfinde = valueleft as CLS_Expression_StaticFind; CLS_Expression_IndexFind ifinde = valueleft as CLS_Expression_IndexFind; if (mfinde != null) { CLS_Expression_MemberSetValue value = new CLS_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) { CLS_Expression_StaticSetValue value = new CLS_Expression_StaticSetValue(left, rightend, tlist[left].line, tlist[rightend].line); value.staticmembername = sfinde.staticmembername; value.type = sfinde.type; value.listParam.Add(valueright); return(value); } else if (ifinde != null) { CLS_Expression_IndexSetValue value = new CLS_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 + " file: " + m_curFileName); } } else if (tkCur.text == ".") { //FindMember CLS_Expression_GetValue vg = valueright as CLS_Expression_GetValue; CLS_Expression_Function vf = valueright as CLS_Expression_Function; if (vg != null) { CLS_Expression_MemberFind value = new CLS_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) { CLS_Expression_MemberFunction value = new CLS_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 CLS_Expression_SelfOp) { CLS_Expression_SelfOp vr = valueright as CLS_Expression_SelfOp; CLS_Expression_MemberMath value = new CLS_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); } else if (tkCur.text == "+=" || tkCur.text == "-=" || tkCur.text == "*=" || tkCur.text == "/=" || tkCur.text == "%=") { CLS_Expression_SelfOpWithValue value = new CLS_Expression_SelfOpWithValue(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 == "^=") { CLS_Expression_SelfOpWithValue value = new CLS_Expression_SelfOpWithValue(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 == "||") { CLS_Expression_Math2ValueAndOr value = new CLS_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 == "!=") { CLS_Expression_Math2ValueLogic value = new CLS_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 == '?') { CLS_Expression_Math3Value value = new CLS_Expression_Math3Value(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); CLS_Expression_Math2Value vvright = valueright as CLS_Expression_Math2Value; if (vvright.mathop != ':') { throw new Exception("三元表达式异常" + tkCur.ToString()); } value.listParam.Add(vvright.listParam[0]); value.listParam.Add(vvright.listParam[1]); return(value); } else { CLS_Expression_Math2Value value = new CLS_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 ICLS_Expression Compiler_Expression_Math(IList <Token> tlist, ICLS_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 // ) //{ // ICLS_Expression v; // bool succ = Compiler_Expression(tlist, content, pos + 3, posend, out v); // CLS_Expression_TypeConvert convert = new CLS_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 == "(")//函数表达式 { return(Compiler_Expression_Function(tlist, content, pos, posend)); } else { //if (!bTest && tlist[expbegin + 1].type == TokenType.PUNCTUATION && tlist[expbegin + 1].text == "(")//函数表达式 //{ // ICLS_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; ICLS_Expression valueright; bool succ2 = Compiler_Expression(tlist, content, right, rightend, out valueright); if (succ2) { CLS_Expression_GetValue vg = valueright as CLS_Expression_GetValue; CLS_Expression_Function vf = valueright as CLS_Expression_Function; if (vg != null) { CLS_Expression_StaticFind value = new CLS_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) { CLS_Expression_StaticFunction value = new CLS_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 CLS_Expression_SelfOp) { CLS_Expression_SelfOp vr = valueright as CLS_Expression_SelfOp; CLS_Expression_StaticMath value = new CLS_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 == "(") { ICLS_Expression v; if (!Compiler_Expression(tlist, content, oppos + 3, posend, out v)) { LogError(tlist, "编译表达式失败", right, rightend); return(null); } CLS_Expression_TypeConvert convert = new CLS_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); } ICLS_Expression valueleft; bool succ1 = Compiler_Expression(tlist, content, left, leftend, out valueleft); ICLS_Expression valueright; if (tkCur.text == "[") { rightend--; if (!Compiler_Expression(tlist, content, right, rightend, out valueright)) { LogError(tlist, "编译表达式失败", right, rightend); return(null); } CLS_Expression_IndexFind value = new CLS_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") { CLS_Expression_TypeConvert convert = new CLS_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") { CLS_Expression_TypeCheck check = new CLS_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 CLS_Expression_MemberFind mfinde = valueleft as CLS_Expression_MemberFind; CLS_Expression_StaticFind sfinde = valueleft as CLS_Expression_StaticFind; CLS_Expression_IndexFind ifinde = valueleft as CLS_Expression_IndexFind; if (mfinde != null) { CLS_Expression_MemberSetValue value = new CLS_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) { CLS_Expression_StaticSetValue value = new CLS_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) { CLS_Expression_IndexSetValue value = new CLS_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 CLS_Expression_GetValue vg = valueright as CLS_Expression_GetValue; CLS_Expression_Function vf = valueright as CLS_Expression_Function; if (vg != null) { CLS_Expression_MemberFind value = new CLS_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) { CLS_Expression_MemberFunction value = new CLS_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 CLS_Expression_SelfOp) { CLS_Expression_SelfOp vr = valueright as CLS_Expression_SelfOp; CLS_Expression_MemberMath value = new CLS_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 CLS_Expression_MemberFind) //{ // CLS_Expression_MemberFind vf = valueleft as CLS_Expression_MemberFind; // CLS_Expression_MemberMath value = new CLS_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 CLS_Expression_Lambda ==false) && valueleft is CLS_Expression_StaticFind) //{ // CLS_Expression_StaticFind vf = valueleft as CLS_Expression_StaticFind; // CLS_Expression_StaticMath value = new CLS_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 { CLS_Expression_SelfOpWithValue value = new CLS_Expression_SelfOpWithValue(left, rightend, tlist[left].line, tlist[rightend].line); //value.value_name = ((CLS_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 == "||") { CLS_Expression_Math2ValueAndOr value = new CLS_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 == "!=") { CLS_Expression_Math2ValueLogic value = new CLS_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 == '?') { CLS_Expression_Math3Value value = new CLS_Expression_Math3Value(left, rightend, tlist[left].line, tlist[rightend].line); value.listParam.Add(valueleft); CLS_Expression_Math2Value vvright = valueright as CLS_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 { CLS_Expression_Math2Value value = new CLS_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); }
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); }
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; }