Exemple #1
0
 public ICLS_Expression Compiler_Expression_Value(Token value, int pos)
 {
     if (value.type == TokenType.VALUE)
     {
         if (value.text[value.text.Length - 1] == 'f')
         {
             CLS_Value_Value <float> number = new CLS_Value_Value <float>();
             number.value_value = float.Parse(value.text.Substring(0, value.text.Length - 1));
             return(number);
         }
         else if (value.text.Contains("."))
         {
             CLS_Value_Value <double> number = new CLS_Value_Value <double>();
             number.value_value = double.Parse(value.text);
             return(number);
         }
         else
         {
             CLS_Value_Value <int> number = new CLS_Value_Value <int>();
             number.value_value = int.Parse(value.text);
             return(number);
         }
     }
     else if (value.type == TokenType.STRING)
     {
         CLS_Value_Value <string> str = new CLS_Value_Value <string>();
         str.value_value = value.text.Substring(1, value.text.Length - 2);
         return(str);
     }
     else if (value.type == TokenType.IDENTIFIER)
     {
         CLS_Expression_GetValue getvalue = new CLS_Expression_GetValue(pos, pos);
         getvalue.value_name = value.text;
         return(getvalue);
     }
     else
     {
         logger.Log_Error("无法识别的简单表达式" + value);
         return(null);
     }
 }
        public ICLS_Expression Compiler_Expression_Math(IList <Token> tlist, CLS_Content 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);
                }
            }
            else if (tlist[oppos].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);
                        value.staticmembername = vg.value_name;
                        value.type             = content.environment.GetTypeByKeyword(tlist[pos].text);
                        return(value);
                    }
                    else if (vf != null)
                    {
                        CLS_Expression_StaticFunction value = new CLS_Expression_StaticFunction(pos, rightend);
                        value.functionName = vf.funcname;
                        value.type         = content.environment.GetTypeByKeyword(tlist[pos].text);
                        //value.listParam.Add(valueleft);
                        value.listParam.AddRange(vf.listParam.ToArray());
                        return(value);
                    }
                    else
                    {
                        throw new Exception("不可识别的表达式");
                    }
                }
                else
                {
                    throw new Exception("不可识别的表达式");
                }
            }
            else
            {
                int left     = pos;
                int leftend  = oppos - 1;
                int right    = oppos + 1;
                int rightend = posend;
                if (tlist[oppos].text == "(")
                {
                    ICLS_Expression            v;
                    bool                       succ    = Compiler_Expression(tlist, content, oppos + 3, posend, out v);
                    CLS_Expression_TypeConvert convert = new CLS_Expression_TypeConvert(pos, posend);
                    convert.listParam.Add(v);
                    convert.targettype = content.environment.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 (tlist[oppos].text == "[")
                {
                    rightend--;
                    bool succs = Compiler_Expression(tlist, content, right, rightend, out valueright);
                    CLS_Expression_IndexFind value = new CLS_Expression_IndexFind(left, rightend);
                    value.listParam.Add(valueleft);
                    value.listParam.Add(valueright);
                    return(value);
                }
                else if (tlist[oppos].text == "as")
                {
                    CLS_Expression_TypeConvert convert = new CLS_Expression_TypeConvert(left, oppos + 1);
                    convert.listParam.Add(valueleft);
                    convert.targettype = content.environment.GetTypeByKeyword(tlist[oppos + 1].text).type;


                    return(convert);
                }
                bool succ2 = Compiler_Expression(tlist, content, right, rightend, out valueright);
                if (succ1 && succ2 && valueright != null && valueleft != null)
                {
                    if (tlist[oppos].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);
                            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);
                            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);
                            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 (tlist[oppos].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);
                            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);
                            value.functionName = vf.funcname;
                            value.listParam.Add(valueleft);
                            value.listParam.AddRange(vf.listParam.ToArray());
                            return(value);
                        }

                        else
                        {
                            throw new Exception("不可识别的表达式" + valueleft + "." + valueright);
                        }

                        //value.listParam.Add(valueright);
                    }
                    else if (tlist[oppos].text == "+=" || tlist[oppos].text == "-=" || tlist[oppos].text == "*=" || tlist[oppos].text == "/=" || tlist[oppos].text == "%=")
                    {
                        CLS_Expression_SelfOpWithValue value = new CLS_Expression_SelfOpWithValue(left, rightend);
                        value.value_name = ((CLS_Expression_GetValue)valueleft).value_name;
                        value.listParam.Add(valueright);
                        value.mathop = tlist[oppos].text[0];
                        return(value);
                    }
                    else if (tlist[oppos].text == "&&" || tlist[oppos].text == "||")
                    {
                        CLS_Expression_Math2ValueAndOr value = new CLS_Expression_Math2ValueAndOr(left, rightend);
                        value.listParam.Add(valueleft);
                        value.listParam.Add(valueright);
                        value.mathop = tlist[oppos].text[0];
                        return(value);
                    }
                    else if (tlist[oppos].text == ">" || tlist[oppos].text == ">=" || tlist[oppos].text == "<" || tlist[oppos].text == "<=" || tlist[oppos].text == "==" || tlist[oppos].text == "!=")
                    {
                        CLS_Expression_Math2ValueLogic value = new CLS_Expression_Math2ValueLogic(left, rightend);
                        value.listParam.Add(valueleft);
                        value.listParam.Add(valueright);
                        logictoken token = logictoken.not_equal;
                        if (tlist[oppos].text == ">")
                        {
                            token = logictoken.more;
                        }
                        else if (tlist[oppos].text == ">=")
                        {
                            token = logictoken.more_equal;
                        }
                        else if (tlist[oppos].text == "<")
                        {
                            token = logictoken.less;
                        }
                        else if (tlist[oppos].text == "<=")
                        {
                            token = logictoken.less_equal;
                        }
                        else if (tlist[oppos].text == "==")
                        {
                            token = logictoken.equal;
                        }
                        else if (tlist[oppos].text == "!=")
                        {
                            token = logictoken.not_equal;
                        }
                        value.mathop = token;
                        return(value);
                    }
                    else
                    {
                        char mathop = tlist[oppos].text[0];
                        if (mathop == '?')
                        {
                            CLS_Expression_Math3Value value = new CLS_Expression_Math3Value(left, rightend);
                            value.listParam.Add(valueleft);

                            CLS_Expression_Math2Value vvright = valueright as CLS_Expression_Math2Value;
                            if (vvright.mathop != ':')
                            {
                                throw new Exception("三元表达式异常");
                            }
                            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);
                            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_Value(Token value,int pos)
 {
     if (value.type == TokenType.VALUE)
     {
         if(value.text[value.text.Length-1]=='f')
         {
             CLS_Value_Value<float> number = new CLS_Value_Value<float>();
             number.value_value = float.Parse(value.text.Substring(0,value.text.Length-1));
             return number;
         }
         else if (value.text.Contains("."))
         {
             CLS_Value_Value<double> number = new CLS_Value_Value<double>();
             number.value_value = double.Parse(value.text);
             return number;
         }
         else
         {
             CLS_Value_Value<int> number = new CLS_Value_Value<int>();
             number.value_value = int.Parse(value.text);
             return number;
         }
     }
     else if (value.type == TokenType.STRING)
     {
         CLS_Value_Value<string> str = new CLS_Value_Value<string>();
         str.value_value = value.text.Substring(1,value.text.Length-2);
         return str;
     }
     else if (value.type == TokenType.IDENTIFIER)
     {
         CLS_Expression_GetValue getvalue = new CLS_Expression_GetValue(pos,pos);
         getvalue.value_name = value.text;
         return getvalue;
     }
     else
     {
         logger.Log_Error("无法识别的简单表达式" + value);
         return null;
     }
 }