示例#1
0
        public Dscr(IOperation val)
        {
            operationString = "--";
            TypeConvertion tpcv = new TypeConvertion("IIDD", 1);

            IOperation[] children = new IOperation[1] {
                val
            };
            returnType = TypeConverter.TryConvert(tpcv, ref children);
            a          = children[0];
        }
示例#2
0
        public More(IOperation left, IOperation right)
        {
            operationString = ">";
            TypeConvertion tpcv = new TypeConvertion("IIBDDB", 2);

            IOperation[] children = new IOperation[2] {
                left, right
            };
            returnType = TypeConverter.TryConvert(tpcv, ref children);
            a          = children[0]; b = children[1];
            returnType = new ValueType(VT.Cboolean);
        }
示例#3
0
        public static ValueType TryConvert(TypeConvertion needType, ref IOperation[] args)
        {
            int vars = needType.to.Length;

            for (int i = 0; i < vars; i++)
            {
                // check i
                List <ValueType> convertList    = new List <ValueType>();
                bool             canBeConverted = true;

                if (args.Length != needType.from[i].Count)
                {
                    continue;                                        // invalid parameter count
                }
                for (int j = 0; j < needType.from[i].Count; j++)
                {
                    if (canConvertFromTypeToType(args[j].returnTypes(), needType.from[i][j]))
                    {
                        convertList.Add(needType.from[i][j]);
                    }
                    else
                    {
                        canBeConverted = false; break;
                    }
                }
                if (!canBeConverted)
                {
                    continue;
                }
                else
                {
                    // variant #i can be performed using some (or noone) convertions

                    // completely convertable
                    // from list apply
                    for (int j = 0; j < convertList.Count; j++)
                    {
                        if (args[j].returnTypes() != convertList[j])
                        {
                            args[j] = applyConvert(args[j], convertList[j]);
                        }
                    }
                    // after this
                    return(needType.to[i]);
                }
            }

            // can not be converted
            throw new Exception("Can not convert!" + ArgsToString(args) + " to " + ArgsToString(needType.from[0].ToArray()));
            //return new ValueType(VT.Cunknown);
        }
        public Summ(IOperation left, IOperation right)
        {
            operationString = "+";
            //TypeConvertion tpcv = new TypeConvertion("IIIDDDSSSAAAAIAIAA", 2);
            //IOperation[] children = new IOperation[2] { left, right };
            //returnType = MISC.CheckTypeCorrect(this, tpcv, ref children);
            //a = children[0]; b = children[1];

            //MISC.ConsoleWriteLine(
            //tpcv.ToString(), ConsoleColor.Magenta);

            TypeConvertion tpcv = new TypeConvertion("IIIDDDSSS", 2);

            IOperation[] children = new IOperation[2] {
                left, right
            };
            returnType = TypeConverter.TryConvertSumm(tpcv, ref children);
            a          = children[0]; b = children[1];
        }
示例#5
0
        public static ValueType TryConvertSumm(TypeConvertion needType, ref IOperation[] args)
        {
            //bool inverted = false;
            if (args.Length != 2)
            {
                throw new Exception("Used not for summ/diff!");
            }
            if (args[0].returnTypes() == args[1].returnTypes() && args[0].returnTypes().pointerLevel > 0)
            {
                return(args[0].returnTypes());
            }
            // can return **int + **int = **int;
            for (int i = 0; i < 2; i++)
            {
                // test this again
                if (args[0].returnTypes().pointerLevel == 0 && args[1].returnTypes().pointerLevel > 0)
                {
                    // int + ***X == ***X; but we should conver this f****r to INT
                    if (args[0].returnTypes().rootType == VT.Cint || args[0].returnTypes().rootType == VT.Cchar || args[0].returnTypes().rootType == VT.Cboolean)
                    {
                        if (args[0].returnTypes().rootType != VT.Cint)
                        {
                            args[0] = applyConvert(args[0], new ValueType(VT.Cint));
                        }
                        if (i == 1)
                        {
                            args = new IOperation[] { args[1], args[0] }
                        }
                        ;

                        return(args[(i == 0) ? 1 : 0].returnTypes());
                    }
                }

                if (i == 0)
                {
                    IOperation temp = args[0]; args[0] = args[1]; args[1] = temp;
                }                                                                   // swap them and check again
            }

            return(TryConvert(needType, ref args));
            //throw new Exception("Can not convert summ!");
        }
示例#6
0
        public static ValueType CheckType(TypeConvertion accept, params ValueType[] hadTypes)
        {
            // I D
            // IIB DDB CCB

            for (int i = 0; i < accept.from.Length; i++)
            {
                bool found = true;
                for (int j = 0; j < accept.from[i].Count; j++)
                {
                    if (hadTypes[j] != accept.from[i][j])
                    {
                        found = false;
                    }
                }
                if (found)
                {
                    return(accept.to[i]);
                }
            }
            // we can found some kostils
            if (availableConvertation.Count == 0)
            {
                availableConvertation.Add(new Tuple <ValueType, ValueType>(new ValueType(VT.Cboolean), new ValueType(VT.Cint)));
                availableConvertation.Add(new Tuple <ValueType, ValueType>(new ValueType(VT.Cint), new ValueType(VT.Cdouble)));
                availableConvertation.Add(new Tuple <ValueType, ValueType>(new ValueType(VT.Cint), new ValueType(VT.Cstring)));
                availableConvertation.Add(new Tuple <ValueType, ValueType>(new ValueType(VT.Cint), new ValueType(VT.Cboolean)));
                availableConvertation.Add(new Tuple <ValueType, ValueType>(new ValueType(VT.Cdouble), new ValueType(VT.Cstring)));
                availableConvertation.Add(new Tuple <ValueType, ValueType>(new ValueType(VT.Cdouble), new ValueType(VT.Cboolean)));
                availableConvertation.Add(new Tuple <ValueType, ValueType>(new ValueType(VT.Cchar), new ValueType(VT.Cstring)));
                availableConvertation.Add(new Tuple <ValueType, ValueType>(new ValueType(VT.Cchar), new ValueType(VT.Cboolean)));
                availableConvertation.Add(new Tuple <ValueType, ValueType>(new ValueType(VT.Cchar), new ValueType(VT.Cint)));
            }

            // I -> D
            // checking in cyccle
            bool convertionFound = false;

            int[] convertion = new int[hadTypes.Length];
            for (int i = 0; i < convertion.Length; i++)
            {
                convertion[i] = -1;                     // -1 -1 -1 -1 -1 -1 for each variable in signature
            }
            for (int i = 0; i < accept.from.Length; i++)
            {
                bool foundAcceptance = true;
                for (int j = 0; j < hadTypes.Length; j++)
                {
                    bool geted = false;
                    if (hadTypes[j] != accept.from[i][j])
                    {
                        for (int k = 0; k < availableConvertation.Count; k++)
                        {
                            if (hadTypes[j] == availableConvertation[k].Item1 &&
                                accept.from[i][j] == availableConvertation[k].Item2)
                            {
                                geted = true; convertion[j] = k; break;
                            }
                        }
                    }
                    else
                    {
                        geted = true;
                    }
                    // if geted then we have conversion for a current parameter
                    if (!geted)
                    {
                        foundAcceptance = false;
                    }
                }
                if (foundAcceptance)
                {
                    int n = 0;
                    convertionFound = true;
                    break;
                }
            }
            //
            if (convertionFound)
            {
                string ConvertNeed = "";
                for (int i = 0; i < convertion.Length; i++)
                {
                    if (convertion[i] >= 0)
                    {
                        ConvertNeed += "" + availableConvertation[convertion[i]] + "_";
                    }
                    else
                    {
                        ConvertNeed += "-_";
                    }
                }
                throw new Exception(ConvertNeed.Remove(ConvertNeed.Length - 1));
            }
            //
            //return ValueType.Unknown;
            throw new Exception("DID NOT FOUND");
        }
示例#7
0
        //
        public ASTFunction(string S)
        {
            //TypeConvertion tpcv = new TypeConvertion("IIBDDBDIBIDBCCB", 2);
            string s = S.Substring(0, S.IndexOf('('));
            //tpcvString = "";
            List <ValueType> vtList = new List <ValueType>();


            int varType = Math.Max((s.IndexOf("int") >= 0) ? 2 : -1, Math.Max((s.IndexOf("double") >= 0) ? 5 : -1, Math.Max((s.IndexOf("char") >= 0) ? 3 : -1,
                                                                                                                            Math.Max((s.IndexOf("string") >= 0) ? 5 : -1, Math.Max((s.IndexOf("bool") >= 0) ? 3 : -1, (s.IndexOf("void") >= 0) ? 3 : -1)))));

            if (varType >= 0)
            {
                varType++;
                string[] type_name = new
                                     string[] { s.Substring(0, varType), s.Substring(varType, s.Length - varType) };//s.Split(s[varType + 1]);
                name = type_name[1];
                int returnPointerLevel = 0;
                while (name[0] == '*')
                {
                    returnPointerLevel++; name = name.Substring(1);
                }

                if (name.Length == 0)
                {
                    throw new Exception("Invalid function name!");
                }


                // !
                retType = new ValueType(Define.detectType(type_name[0]), returnPointerLevel);
                // try to parse signature and actions
                List <string> vars = MISC.splitBy(MISC.getIn(S, S.IndexOf('(')), ',');
                input = new List <Define>();
                MISC.GoDeep("FDEFINED");

                for (int i = 0; i < vars.Count; i++)
                {
                    input.Add((Define)MonoOperation.ParseFrom(vars[i]));
                    vtList.Add((input[input.Count - 1] as Define).returnTypes());
                    //tpcvString += vars[i][0].ToString().ToUpper();
                }
                //tpcvString += returnTypes().ToString()[1].ToString().ToUpper();
                tpcv = new TypeConvertion(vtList, retType);



                // check name uniq!
                //bool foundFunc = false;
                for (int i = 0; i < ASTTree.funcs.Count; i++)
                {
                    if (ASTTree.funcs[i].actions.CommandCount > 0 && MISC.CompareFunctionSignature(ASTTree.funcs[i], this))
                    {
                        throw new Exception("Can not redefine a function \"" + name + " : " + this.getArgsString + "\"!");
                    }
                }

                if (S.IndexOf('{') >= 0)
                {
                    try
                    {
                        MISC.GoDeep("FUNCTION$" + name + "$" + returnTypes());
                        string actionCode = MISC.getIn(S, S.IndexOf('{'));
                        actions = new CommandOrder(actionCode, ';');
                        MISC.GoBack();
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Problem in function \"" + name + "\"\n" + e.Message);
                    }
                }
                else
                {
                    actions = new CommandOrder();
                }

                MISC.GoBack();
                return;
            }
            // check contain of Return function
            throw new Exception("Can not parse a function\t " + MISC.StringFirstLetters(S, 20, true));
        }