Exemple #1
0
        public void Set(string name, CQ_Value value)
        {
            if (values == null)
            {
                values = new Dictionary <string, CQ_Value>();
            }
            CQ_Value oldVal = CQ_Value.Null;
            bool     bFind  = values.TryGetValue(name, out oldVal);

            if (bFind)
            {
                //var第一次赋值类型
                if (oldVal.m_type == typeof(Type_Var.var))
                {
                    if (value.m_type != null)
                    {
                        oldVal.m_type = value.m_type;
                    }
                    else if (value.m_stype != null)
                    {
                        oldVal.m_stype = value.m_stype;
                    }
                }

                oldVal.UsingValue(value);
                values[name] = oldVal;
            }
            else
            {
                if (CallType != null)
                {
                    Class_CQuark.Member retM = Class_CQuark.Member.Null;
                    bool bRet = CallType.members.TryGetValue(name, out retM);
                    if (bRet)
                    {
                        if (retM.bStatic)
                        {
                            CQ_Value val = CallType.staticMemberInstance[name];
                            val.UsingValue(value);
                            CallType.staticMemberInstance[name] = val;
                        }
                        else
                        {
                            CQ_Value val = CallThis.member[name];
                            val.UsingValue(value);
                            CallThis.member[name] = val;
                        }
                        return;
                    }
                }
                string err = CallType.Name + "\n";
                foreach (var m in CallType.members)
                {
                    err += m.Key + ",";
                }
                throw new Exception("值没有定义过" + name + "," + err);
            }
        }
Exemple #2
0
        public CQ_Value GetQuiet(string name)
        {
            if (name == "this")
            {
                CQ_Value v = new CQ_Value();
                v.type  = CallType;
                v.value = CallThis;
                return(v);
            }

            CQ_Value retV  = null;
            bool     bFind = false;

            if (values != null)
            {
                bFind = values.TryGetValue(name, out retV);
                if (bFind)//优先上下文变量
                {
                    return(retV);
                }
            }

            if (CallType != null)
            {
                Class_CQuark.Member retM = null;
                bFind = CallType.members.TryGetValue(name, out retM);
                if (bFind)
                {
                    if (retM.bStatic)
                    {
                        return(CallType.staticMemberInstance[name]);
                    }
                    else
                    {
                        return(CallThis.member[name]);
                    }
                }
                if (CallType.functions.ContainsKey(name))
                {
                    CQ_Value v = new CQ_Value();
                    //如果直接得到代理实例,
                    DeleFunction dele = new DeleFunction(CallType, this.CallThis, name);


                    //DeleScript dele =new DeleScript();
                    //dele.function = name;
                    //dele.calltype = CallType;
                    //dele.callthis = CallThis;
                    v.value = dele;
                    v.type  = typeof(DeleFunction);
                    return(v);
                }
            }
            return(null);
        }
Exemple #3
0
        public CQ_Value GetQuiet(string name)
        {
            CQ_Value retV = new CQ_Value();

            if (name == "this")
            {
                retV.SetObject(CallType, CallThis);
                return(retV);
            }

            if (values != null)
            {
                if (values.TryGetValue(name, out retV))//优先上下文变量
                {
                    return(retV);
                }
            }

            if (CallType != null)
            {
                Class_CQuark.Member retM = Class_CQuark.Member.Null;
                if (CallType.members.TryGetValue(name, out retM))
                {
                    if (retM.bStatic)
                    {
                        return(CallType.staticMemberInstance[name]);
                    }
                    else
                    {
                        return(CallThis.member[name]);
                    }
                }
                if (CallType.functions.ContainsKey(name))
                {
                    //如果直接得到代理实例,
                    DeleFunction dele = new DeleFunction(CallType, this.CallThis, name);
                    retV.SetObject(typeof(DeleFunction), dele);
                    return(retV);
                }
            }
            return(retV);//null
        }
Exemple #4
0
        public void Set(string name, object value)
        {
            if (values == null)
            {
                values = new Dictionary <string, CQ_Value>();
            }
            CQ_Value retV = null;

            bool bFind = values.TryGetValue(name, out retV);

            if (!bFind)
            {
                if (CallType != null)
                {
                    Class_CQuark.Member retM = null;
                    bool bRet = CallType.members.TryGetValue(name, out retM);
                    if (bRet)
                    {
                        if (retM.bStatic)
                        {
                            CallType.staticMemberInstance[name].value = value;
                        }
                        else
                        {
                            CallThis.member[name].value = value;
                        }
                        return;
                    }
                }
                string err = CallType.Name + "\n";
                foreach (var m in CallType.members)
                {
                    err += m.Key + ",";
                }
                throw new Exception("值没有定义过" + name + "," + err);
            }
            if ((Type)retV.type == typeof(Type_Var.var) && value != null)
            {
                retV.type = value.GetType();
            }
            retV.value = value;
        }
        static IType Compiler_Class(string classname, bool bInterface, IList <string> basetype, string filename, IList <Token> tokens, int ibegin, int iend, bool EmbDebugToken, bool onlyGotType, IList <string> usinglist)
        {
            Type_Class typeClass = CQuark.AppDomain.GetTypeByKeywordQuiet(classname) as Type_Class;

            if (typeClass == null)
            {
                typeClass = new Type_Class(classname, bInterface, filename);
            }

            if (basetype != null && basetype.Count != 0 && onlyGotType == false)
            {
                List <IType> basetypess = new List <IType>();
                foreach (string t in basetype)
                {
                    IType type = CQuark.AppDomain.GetTypeByKeyword(t);
                    basetypess.Add(type);
                }
                typeClass.SetBaseType(basetypess);
            }

            if (onlyGotType)
            {
                return(typeClass);
            }

            //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;
            //}

            typeClass.compiled = false;
            (typeClass._class as Class_CQuark).functions.Clear();
            (typeClass._class as Class_CQuark).members.Clear();
            //搜寻成员定义和函数
            //定义语法            //Type id[= expr];
            //函数语法            //Type id([Type id,]){block};
            //属性语法            //Type id{get{},set{}};
            bool bPublic = false;
            bool bStatic = false;

            if (EmbDebugToken)//SType 嵌入Token
            {
                typeClass.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))//发现类型
                {
                    IType idtype = CQuark.AppDomain.GetTypeByKeyword("null");
                    bool  bctor  = false;
                    if (tokens[i].type == TokenType.TYPE)//类型
                    {
                        if (tokens[i].text == classname && tokens[i + 1].text == "(")
                        {//构造函数
                            bctor = true;
                            i--;
                        }
                        else if (tokens[i + 1].text == "[" && tokens[i + 2].text == "]")
                        {
                            idtype = CQuark.AppDomain.GetTypeByKeyword(tokens[i].text + "[]");
                            i     += 2;
                        }
                        else if (tokens[i].text == "void")
                        {
                        }
                        else
                        {
                            idtype = CQuark.AppDomain.GetTypeByKeyword(tokens[i].text);
                        }
                    }

                    if (tokens[i + 1].type == CQuark.TokenType.IDENTIFIER || bctor) //类型后面是名称
                    {
                        string idname = tokens[i + 1].text;
                        if (tokens[i + 2].type == CQuark.TokenType.PUNCTUATION && tokens[i + 2].text == "(")//参数开始,这是函数
                        {
#if CQUARK_DEBUG
                            DebugUtil.Log("发现函数:" + idname);
#endif
                            Class_CQuark.Function func = new Class_CQuark.Function();
                            func.bStatic = bStatic;
                            func.bPublic = bPublic;

                            int funcparambegin = i + 2;
                            int funcparamend   = FindBlock(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 = CQuark.AppDomain.GetTypeByKeyword(ptype);
                                        // _params[pid] = type;
                                        //func._params.Add(pid, type);
                                        if (type == null)
                                        {
                                            throw new Exception(filename + ":不可识别的函数头参数:" + tokens[funcparambegin].ToString() + tokens[funcparambegin].SourcePos());
                                        }
                                        func._paramnames.Add(pid);
                                        func._paramtypes.Add(type);
                                        start = j + 1;
                                    }
                                }
                            }

                            int funcbegin = funcparamend + 1;
                            if (tokens[funcbegin].text == "{")
                            {
                                int funcend = FindBlock(tokens, funcbegin);
                                Compiler_Expression_Block(tokens, funcbegin, funcend, out func.expr_runtime);
                                if (func.expr_runtime == null)
                                {
                                    DebugUtil.LogWarning("警告,该函数编译为null,请检查");
                                }
                                (typeClass._class as Class_CQuark).functions.Add(idname, func);

                                i = funcend;
                            }
                            else if (tokens[funcbegin].text == ";")
                            {
                                func.expr_runtime = null;
                                (typeClass._class as Class_CQuark).functions.Add(idname, func);
                                i = funcbegin;
                            }
                            else
                            {
                                throw new Exception(filename + ":不可识别的函数表达式:" + tokens[funcbegin].ToString() + tokens[funcbegin].SourcePos());
                            }
                        }
                        else if (tokens[i + 2].type == CQuark.TokenType.PUNCTUATION && tokens[i + 2].text == "{")//语句块开始,这是 getset属性
                        {
                            //get set 成员定义

                            bool setpublic = true;
                            bool haveset   = false;
                            for (int j = i + 3; j <= iend; j++)
                            {
                                if (tokens[j].text == "get")
                                {
                                    setpublic = true;
                                }
                                if (tokens[j].text == "private")
                                {
                                    setpublic = false;
                                }
                                if (tokens[j].text == "set")
                                {
                                    haveset = true;
                                }
                                if (tokens[j].text == "}")
                                {
                                    break;
                                }
                            }


                            var member = new Class_CQuark.Member();
                            member.bStatic   = bStatic;
                            member.bPublic   = bPublic;
                            member.bReadOnly = !(haveset && setpublic);
                            member.type      = idtype;
#if CQUARK_DEBUG
                            DebugUtil.Log("发现Get/Set:" + idname);
#endif
                            //ICQ_Expression expr = null;

                            if (tokens[i + 2].text == "=")
                            {
                                int jbegin = i + 3;
                                int jdep;
                                int jend = FindCodeAny(tokens, ref jbegin, out jdep);

                                if (!Compiler_Expression(tokens, jbegin, jend, out member.expr_defvalue))
                                {
                                    DebugUtil.LogError("Get/Set定义错误");
                                }
                                i = jend;
                            }
                            (typeClass._class as Class_CQuark).members.Add(idname, member);
                        }
                        else if (tokens[i + 2].type == CQuark.TokenType.PUNCTUATION && (tokens[i + 2].text == "=" || tokens[i + 2].text == ";"))//这是成员定义
                        {
#if CQUARK_DEBUG
                            DebugUtil.Log("发现成员定义:" + idname);
#endif
                            var member = new Class_CQuark.Member();
                            member.bStatic   = bStatic;
                            member.bPublic   = bPublic;
                            member.bReadOnly = false;
                            member.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, jbegin, jend, out member.expr_defvalue))
                                {
                                    DebugUtil.LogError("成员定义错误");
                                }
                                i = jend;
                            }
                            (typeClass._class as Class_CQuark).members.Add(idname, member);
                        }

                        bPublic = false;
                        bStatic = false;

                        continue;
                    }
                    else
                    {
                        throw new Exception(filename + ":不可识别的表达式:" + tokens[i].ToString() + tokens[i].SourcePos());
                    }
                }
            }
            typeClass.compiled = true;
            return(typeClass);
        }