コード例 #1
0
        private ABnfGuessError ReplaceTemplate(ABnfGuess guess, out ABnfGuess result)
        {
            result = null;
            if (m_class_guess == null)
            {
                result = guess;
                return(null);
            }

            if (guess is ALittleScriptGuessTemplate && m_class_guess.template_map.Count > 0)
            {
                if (m_class_guess.template_map.TryGetValue(guess.GetValueWithoutConst(), out ABnfGuess guess_template))
                {
                    if (guess.is_const && !guess_template.is_const)
                    {
                        guess_template          = guess_template.Clone();
                        guess_template.is_const = true;
                        guess_template.UpdateValue();
                    }
                    result = guess_template;
                }
                else
                {
                    result = guess;
                }
                return(null);
            }

            if (guess is ALittleScriptGuessFunctor)
            {
                var guess_functor = guess as ALittleScriptGuessFunctor;
                var info          = new ALittleScriptGuessFunctor(guess_functor.element);
                info.await_modifier = guess_functor.await_modifier;
                info.const_modifier = guess_functor.const_modifier;
                info.proto          = guess_functor.proto;
                info.template_param_list.AddRange(guess_functor.template_param_list);
                info.param_tail = guess_functor.param_tail;
                info.param_name_list.AddRange(guess_functor.param_name_list);
                info.return_tail = guess_functor.return_tail;

                int start_index = 0;
                if (guess_functor.element is ALittleScriptClassMethodDecElement ||
                    guess_functor.element is ALittleScriptClassSetterDecElement ||
                    guess_functor.element is ALittleScriptClassGetterDecElement)
                {
                    info.param_list.Add(m_class_guess);
                    info.param_nullable_list.Add(false);
                    if (info.param_name_list.Count > 0)
                    {
                        info.param_name_list[0] = m_class_guess.GetValue();
                    }
                    start_index = 1;
                }
                for (int i = start_index; i < guess_functor.param_list.Count; ++i)
                {
                    var error = ReplaceTemplate(guess_functor.param_list[i], out ABnfGuess guess_info);
                    if (error != null)
                    {
                        return(error);
                    }
                    info.param_list.Add(guess_info);
                }
                for (int i = start_index; i < guess_functor.param_nullable_list.Count; ++i)
                {
                    info.param_nullable_list.Add(guess_functor.param_nullable_list[i]);
                }
                for (int i = 0; i < guess_functor.return_list.Count; ++i)
                {
                    var error = ReplaceTemplate(guess_functor.return_list[i], out ABnfGuess guess_info);
                    if (error != null)
                    {
                        return(error);
                    }
                    info.return_list.Add(guess_info);
                }
                info.UpdateValue();
                result = info;
                return(null);
            }

            if (guess is ALittleScriptGuessList)
            {
                var guess_list = guess as ALittleScriptGuessList;
                var error      = ReplaceTemplate(guess_list.sub_type, out ABnfGuess sub_info);
                if (error != null)
                {
                    return(error);
                }
                var info = new ALittleScriptGuessList(sub_info, guess_list.is_const, guess_list.is_native);
                info.UpdateValue();
                result = info;
                return(null);
            }

            if (guess is ALittleScriptGuessMap)
            {
                var guess_map = guess as ALittleScriptGuessMap;
                var error     = ReplaceTemplate(guess_map.key_type, out ABnfGuess key_info);
                if (error != null)
                {
                    return(error);
                }
                error = ReplaceTemplate(guess_map.value_type, out ABnfGuess value_info);
                if (error != null)
                {
                    return(error);
                }

                var info = new ALittleScriptGuessMap(key_info, value_info, guess.is_const);
                info.UpdateValue();
                result = info;
                return(null);
            }

            if (guess is ALittleScriptGuessClass)
            {
                var guess_class = guess as ALittleScriptGuessClass;
                var info        = new ALittleScriptGuessClass(guess_class.namespace_name,
                                                              guess_class.class_name, guess_class.class_dec, guess_class.using_name, guess_class.is_const, guess_class.is_native);
                info.template_list.AddRange(guess_class.template_list);
                foreach (var pair in guess_class.template_map)
                {
                    if (info.template_map.ContainsKey(pair.Key))
                    {
                        info.template_map.Remove(pair.Key);
                    }
                    var error = ReplaceTemplate(pair.Value, out ABnfGuess replace_guess);
                    if (error != null)
                    {
                        return(error);
                    }
                    info.template_map.Add(pair.Key, replace_guess);
                }

                var src_class_dec      = guess_class.class_dec;
                var src_class_name_dec = src_class_dec.GetClassNameDec();
                if (src_class_name_dec == null)
                {
                    return(new ABnfGuessError(m_element, "类模板没有定义类名"));
                }
                info.UpdateValue();
                result = info;
                return(null);
            }

            result = guess;
            return(null);
        }
        private ABnfGuessError AnalysisTemplate(Dictionary <string, ABnfGuess> fill_map, ABnfGuess left_guess, ABnfElement right_src, ABnfGuess right_guess, bool assign_or_call)
        {
            // 如果值等于null,那么可以赋值
            if (right_guess.GetValue() == "null")
            {
                return(null);
            }

            // const是否可以赋值给非const
            if (assign_or_call)
            {
                if (left_guess.is_const && !right_guess.is_const)
                {
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ", 不能是:" + right_guess.GetValue()));
                }
            }
            else
            {
                // 如果不是基本变量类型(排除any),基本都是值传递,函数调用时就不用检查const
                if (!(left_guess is ALittleScriptGuessPrimitive) || left_guess.GetValueWithoutConst() == "any")
                {
                    if (!left_guess.is_const && right_guess.is_const)
                    {
                        return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ", 不能是:" + right_guess.GetValue()));
                    }
                }
            }

            // 如果任何一方是any,那么就认为可以相等
            if (left_guess is ALittleScriptGuessAny)
            {
                return(null);
            }

            if (left_guess is ALittleScriptGuessPrimitive ||
                left_guess is ALittleScriptGuessStruct)
            {
                return(ALittleScriptOp.GuessTypeEqual(left_guess, right_src, right_guess, assign_or_call, false));
            }

            if (left_guess is ALittleScriptGuessMap)
            {
                if (!(right_guess is ALittleScriptGuessMap))
                {
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }


                var error = AnalysisTemplate(fill_map, ((ALittleScriptGuessMap)left_guess).key_type, right_src, ((ALittleScriptGuessMap)right_guess).key_type, false);
                if (error != null)
                {
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }
                error = AnalysisTemplate(fill_map, ((ALittleScriptGuessMap)left_guess).value_type, right_src, ((ALittleScriptGuessMap)right_guess).value_type, false);
                if (error != null)
                {
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }
                return(null);
            }

            if (left_guess is ALittleScriptGuessList)
            {
                if (!(right_guess is ALittleScriptGuessList))
                {
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }
                var error = AnalysisTemplate(fill_map, ((ALittleScriptGuessList)left_guess).sub_type, right_src, ((ALittleScriptGuessList)right_guess).sub_type, false);
                if (error != null)
                {
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }
                return(null);
            }

            if (left_guess is ALittleScriptGuessFunctor)
            {
                if (!(right_guess is ALittleScriptGuessFunctor))
                {
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }
                ALittleScriptGuessFunctor left_guess_functor  = (ALittleScriptGuessFunctor)left_guess;
                ALittleScriptGuessFunctor right_guess_functor = (ALittleScriptGuessFunctor)right_guess;

                if (left_guess_functor.param_list.Count != right_guess_functor.param_list.Count ||
                    left_guess_functor.param_nullable_list.Count != right_guess_functor.param_nullable_list.Count ||
                    left_guess_functor.return_list.Count != right_guess_functor.return_list.Count ||
                    left_guess_functor.template_param_list.Count != right_guess_functor.template_param_list.Count ||
                    left_guess_functor.await_modifier != right_guess_functor.await_modifier ||
                    left_guess_functor.proto == null && right_guess_functor.proto != null ||
                    left_guess_functor.proto != null && right_guess_functor.proto == null ||
                    (left_guess_functor.proto != null && left_guess_functor.proto != right_guess_functor.proto) ||
                    left_guess_functor.param_tail == null && right_guess_functor.param_tail != null ||
                    left_guess_functor.param_tail != null && right_guess_functor.param_tail == null ||
                    left_guess_functor.return_tail == null && right_guess_functor.return_tail != null ||
                    left_guess_functor.return_tail != null && right_guess_functor.return_tail == null
                    )
                {
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }

                for (int i = 0; i < left_guess_functor.template_param_list.Count; ++i)
                {
                    var error = AnalysisTemplate(fill_map, left_guess_functor.template_param_list[i], right_src, right_guess_functor.template_param_list[i], false);
                    if (error != null)
                    {
                        return(error);
                    }
                }

                for (int i = 0; i < left_guess_functor.param_list.Count; ++i)
                {
                    var error = AnalysisTemplate(fill_map, left_guess_functor.param_list[i], right_src, right_guess_functor.param_list[i], false);
                    if (error != null)
                    {
                        return(error);
                    }
                }

                for (int i = 0; i < left_guess_functor.param_nullable_list.Count; ++i)
                {
                    if (left_guess_functor.param_nullable_list[i] != right_guess_functor.param_nullable_list[i])
                    {
                        return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                    }
                }

                for (int i = 0; i < left_guess_functor.return_list.Count; ++i)
                {
                    var error = AnalysisTemplate(fill_map, left_guess_functor.return_list[i], right_src, right_guess_functor.return_list[i], false);
                    if (error != null)
                    {
                        return(error);
                    }
                }
                return(null);
            }

            if (left_guess is ALittleScriptGuessClass)
            {
                if (right_guess is ALittleScriptGuessTemplate)
                {
                    right_guess = (right_guess as ALittleScriptGuessTemplate).template_extends;
                }

                if (!(right_guess is ALittleScriptGuessClass))
                {
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }

                if (left_guess.GetValue() == right_guess.GetValue())
                {
                    return(null);
                }

                var error = ALittleScriptUtility.IsClassSuper((left_guess as ALittleScriptGuessClass).class_dec, right_guess.GetValue(), out bool result);
                if (error != null)
                {
                    return(error);
                }
                if (result)
                {
                    return(null);
                }
                error = ALittleScriptUtility.IsClassSuper((right_guess as ALittleScriptGuessClass).class_dec, left_guess.GetValue(), out result);
                if (error != null)
                {
                    return(error);
                }
                if (result)
                {
                    return(null);
                }

                return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
            }

            if (left_guess is ALittleScriptGuessTemplate)
            {
                var left_guess_template = left_guess as ALittleScriptGuessTemplate;

                // 查看模板是否已经被填充,那么就按填充的检查
                if (fill_map.TryGetValue(left_guess_template.GetValue(), out ABnfGuess fill_guess))
                {
                    return(ALittleScriptOp.GuessTypeEqual(fill_guess, right_src, right_guess, false, false));
                }

                // 处理还未填充
                if (left_guess_template.template_extends != null)
                {
                    var error = AnalysisTemplate(fill_map, left_guess_template.template_extends, right_src, right_guess, false);
                    if (error != null)
                    {
                        return(error);
                    }
                    if (right_guess.is_const)
                    {
                        right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue();
                    }
                    fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess);
                    return(null);
                }
                else if (left_guess_template.is_class)
                {
                    if (right_guess is ALittleScriptGuessClass)
                    {
                        if (right_guess.is_const)
                        {
                            right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue();
                        }
                        fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess);
                        return(null);
                    }
                    else if (right_guess is ALittleScriptGuessTemplate)
                    {
                        var right_guess_template = right_guess as ALittleScriptGuessTemplate;
                        if (right_guess_template.template_extends is ALittleScriptGuessClass || right_guess_template.is_class)
                        {
                            if (right_guess.is_const)
                            {
                                right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue();
                            }
                            fill_map.Add(right_guess_template.GetValueWithoutConst(), right_guess);
                            return(null);
                        }
                    }
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }
                else if (left_guess_template.is_struct)
                {
                    if (right_guess is ALittleScriptGuessStruct)
                    {
                        if (right_guess.is_const)
                        {
                            right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue();
                        }
                        fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess);
                        return(null);
                    }
                    else if (right_guess is ALittleScriptGuessTemplate)
                    {
                        var right_guess_template = right_guess as ALittleScriptGuessTemplate;
                        if (right_guess_template.template_extends is ALittleScriptGuessStruct || right_guess_template.is_struct)
                        {
                            if (right_guess.is_const)
                            {
                                right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue();
                            }
                            fill_map.Add(left_guess_template.GetValue(), right_guess);
                            return(null);
                        }
                    }
                    return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
                }

                if (right_guess.is_const)
                {
                    right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue();
                }
                fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess);
                return(null);
            }

            return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue()));
        }
コード例 #3
0
        public override ABnfGuessError GuessTypes(out List <ABnfGuess> guess_list)
        {
            guess_list = new List <ABnfGuess>();

            m_getter_list = null;
            m_setter_list = null;
            m_class_guess = null;

            var error = CalcResolve(out List <ABnfElement> result_list, out ABnfGuess pre_type);

            if (error != null)
            {
                return(error);
            }
            foreach (var result in result_list)
            {
                ABnfGuess guess = null;
                if (result is ALittleScriptClassVarDecElement)
                {
                    error = result.GuessType(out guess);
                    if (error != null)
                    {
                        return(error);
                    }

                    if (m_class_guess != null && guess is ALittleScriptGuessTemplate)
                    {
                        if (!m_class_guess.template_map.TryGetValue(guess.GetValueWithoutConst(), out ABnfGuess guess_template))
                        {
                            for (int i = 0; i < m_class_guess.template_list.Count; ++i)
                            {
                                guess_template = m_class_guess.template_list[i];

                                if (guess_template.GetValueWithoutConst() == guess.GetValueWithoutConst())
                                {
                                    break;
                                }
                            }
                        }

                        if (guess_template != null)
                        {
                            if (guess.is_const && !guess_template.is_const)
                            {
                                guess_template          = guess_template.Clone();
                                guess_template.is_const = true;
                                guess_template.UpdateValue();
                            }

                            guess = guess_template;
                        }
                    }
                }
                else if (result is ALittleScriptStructVarDecElement)
                {
                    error = result.GuessType(out guess);
                    if (error != null)
                    {
                        return(error);
                    }
                }
                else if (result is ALittleScriptEnumVarDecElement)
                {
                    error = result.GuessType(out guess);
                    if (error != null)
                    {
                        return(error);
                    }
                }
                else if (result is ALittleScriptMethodNameDecElement)
                {
                    error = result.GuessType(out guess);
                    if (error != null)
                    {
                        return(error);
                    }

                    // 如果前一个数据是const,那么调用的函数也必须是const
                    if (pre_type != null && pre_type.is_const)
                    {
                        var guess_functor = guess as ALittleScriptGuessFunctor;
                        if (guess_functor != null && !guess_functor.const_modifier)
                        {
                            return(new ABnfGuessError(m_element, "请使用带Const修饰的函数"));
                        }
                    }

                    if (result.GetParent() is ALittleScriptClassGetterDecElement)
                    {
                        if (m_getter_list != null && m_getter_list.IndexOf(result) >= 0 && guess is ALittleScriptGuessFunctor)
                        {
                            guess = ((ALittleScriptGuessFunctor)guess).return_list[0];
                        }
                    }
                    else if (result.GetParent() is ALittleScriptClassSetterDecElement)
                    {
                        if (m_setter_list != null && m_setter_list.IndexOf(result) >= 0 && guess is ALittleScriptGuessFunctor)
                        {
                            guess = ((ALittleScriptGuessFunctor)guess).param_list[1];
                        }
                    }
                    error = ReplaceTemplate(guess, out guess);
                    if (error != null)
                    {
                        return(error);
                    }
                }
                else if (result is ALittleScriptVarAssignNameDecElement)
                {
                    error = result.GuessType(out guess);
                    if (error != null)
                    {
                        return(error);
                    }
                }
                else if (result is ALittleScriptEnumNameDecElement)
                {
                    error = result.GuessType(out ABnfGuess enum_guess);
                    if (error != null)
                    {
                        return(error);
                    }
                    if (!(enum_guess is ALittleScriptGuessEnum))
                    {
                        return(new ABnfGuessError(m_element, "ALittleEnumNameDec.guessType的结果不是ALittleGuessEnum"));
                    }
                    var enum_guess_enum = enum_guess as ALittleScriptGuessEnum;
                    var info            = new ALittleScriptGuessEnumName(enum_guess_enum.namespace_name, enum_guess_enum.enum_name, result as ALittleScriptEnumNameDecElement);
                    info.UpdateValue();
                    guess = info;
                }
                else if (result is ALittleScriptStructNameDecElement)
                {
                    error = result.GuessType(out ABnfGuess struct_guess);
                    if (error != null)
                    {
                        return(error);
                    }
                    if (!(struct_guess is ALittleScriptGuessStruct))
                    {
                        return(new ABnfGuessError(m_element, "ALittleStructNameDec.guessType的结果不是ALittleGuessStruct"));
                    }
                    var struct_guess_struct = struct_guess as ALittleScriptGuessStruct;
                    var info = new ALittleScriptGuessStructName(struct_guess_struct.namespace_name, struct_guess_struct.struct_name, result as ALittleScriptStructNameDecElement);
                    info.UpdateValue();
                    guess = info;
                }
                else if (result is ALittleScriptClassNameDecElement)
                {
                    error = result.GuessType(out ABnfGuess class_guess);
                    if (error != null)
                    {
                        return(error);
                    }
                    if (!(class_guess is ALittleScriptGuessClass))
                    {
                        return(new ABnfGuessError(m_element, "ALittleClassNameDec.guessType的结果不是ALittleGuessClass"));
                    }
                    var class_guess_class = class_guess as ALittleScriptGuessClass;
                    if (class_guess_class.template_list.Count > 0)
                    {
                        return(new ABnfGuessError(m_element, "模板类" + class_guess_class.GetValue() + "不能直接使用"));
                    }
                    var info = new ALittleScriptGuessClassName(class_guess_class.namespace_name, class_guess_class.class_name, result as ALittleScriptClassNameDecElement);
                    info.UpdateValue();
                    guess = info;
                }

                if (guess != null)
                {
                    if (pre_type != null && pre_type.is_const && !guess.is_const)
                    {
                        if (guess is ALittleScriptGuessPrimitive)
                        {
                            var guess_value = guess.GetValue();
                            ALittleScriptIndex.inst.sPrimitiveGuessMap.TryGetValue("const " + guess.GetValue(), out guess);
                            if (guess == null)
                            {
                                return(new ABnfGuessError(m_element, "找不到const " + guess_value));
                            }
                        }
                        else
                        {
                            guess          = guess.Clone();
                            guess.is_const = true;
                            guess.UpdateValue();
                        }
                    }
                    guess_list.Add(guess);
                }
            }

            m_getter_list = null;
            m_setter_list = null;
            m_class_guess = null;

            return(null);
        }