예제 #1
0
        public static ValueType CheckTypeCorrect(IOperation o, TypeConvertion accept, ref IOperation[] parts)
        {
            try
            {
                ValueType[] inputValueTypes = new ValueType[parts.Length];
                for (int i = 0; i < parts.Length; i++)
                {
                    //if (parts[i].returnTypes().pointerLevel == 0)
                    inputValueTypes[i] = parts[i].returnTypes();
                }
                //else
                //    inputValueTypes[i] = new ValueType(VT.Cadress);

                //if ((parts[i] as GetValByAdress) != null)
                //{
                //    ValueType vt = (parts[i] as GetValByAdress).pointerType;
                //    int n = 0;
                //}
                if ((o as Assum) != null && parts[0].returnTypes() == parts[1].returnTypes())
                {
                    return(parts[0].returnTypes());
                }

                ValueType res = CheckType(accept, inputValueTypes);

                return(res);
            }
            catch (Exception e)
            {
                string   getErr       = e.Message;
                string[] splitedTypes = getErr.Split('_');
                for (int i = 0; i < splitedTypes.Length; i++)
                {
                    if (splitedTypes[i] != "-")
                    {
                        string parseFrom = splitedTypes[i].Substring(splitedTypes[i].IndexOf(", "),
                                                                     splitedTypes[i].IndexOf(")") - splitedTypes[i].IndexOf(", ")).ToLower().Remove(0, 3);
                        ValueType convertType = new ValueType(Define.detectType(parseFrom));
                        parts[i] = new Conv(parts[i], convertType);
                    }
                }
                try
                {
                    ValueType[] inputValueTypes = new ValueType[parts.Length];
                    for (int i = 0; i < parts.Length; i++)
                    {
                        inputValueTypes[i] = parts[i].returnTypes();
                    }

                    ValueType res = CheckType(accept, inputValueTypes);
                    return(res);
                }
                catch (Exception r)
                {
                    throw new Exception("Can not apply convertation chain!");
                    return(new ValueType(VT.Cunknown));
                }
            }
        }
예제 #2
0
        /*for (int i = 0; i < s.Length + 1 - symbols.Length; i++)
         *  {
         *      if (s[i] == leftBracket)
         *          nowLevel++;
         *      if (s[i] == rightBracket)
         *          nowLevel--;
         *      if (nowLevel == 0 && s.Substring(i, symbols.Length) == symbols)
         *          pos = i;
         *  }*/

        public static IOperation ParseFrom(string s)
        {
            try
            {
                string left = "", right = "";
                if (s.IndexOf("return") == 0)
                {
                    return(new Ret(ParseFrom(s.Substring(6))));
                }

                //___________
                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, (s.IndexOf("bool") >= 0) ? 3 : -1))));
                if (varType > 0 && MISC.IndexOfOnLevel0(s, ",", 0) > 0)
                {
                    s = s.Insert(varType + 1, "$");
                    string firstPart = s.Substring(0, s.IndexOf("$"));  // int&a,b

                    int at;
                    do
                    {
                        at = MISC.IndexOfOnLevel0(s, ",", 0);
                        if (at >= 0)
                        {
                            s = s.Substring(0, at) + ';' + firstPart + s.Substring(at + 1);
                        }
                    } while (at >= 0);

                    s = s.Remove(s.IndexOf('$'), 1);
                    throw new Exception("#MDS:" + s);
                }
                //_________________
                if (s.IndexOf('{') == 0 && s.LastIndexOf('}') == s.Length - 1)
                {
                    return(MonoOperation.ParseFrom(s));
                }

                if (!onLevel(s, "==", 0) && !onLevel(s, "!=", 0) && onLevel(s, "=", 0))
                {
                    MISC.separate(s, "=", ref left, ref right, lastIndex);

                    IOperation rightOperation = ParseFrom(right);
                    if (left.Length > 0 && ("+-*/").IndexOf(left[left.Length - 1]) >= 0)
                    {
                        IOperation leftMinusOne = MonoOperation.ParseFrom(left.Remove(left.Length - 1));
                        switch (left[left.Length - 1])
                        {
                        case '+':
                            return(new Assum(leftMinusOne, new Summ(leftMinusOne, rightOperation)));

                        case '-':
                            return(new Assum(leftMinusOne, new Diff(leftMinusOne, rightOperation)));

                        case '*':
                            return(new Assum(leftMinusOne, new Mult(leftMinusOne, rightOperation)));

                        case '/':
                            return(new Assum(leftMinusOne, new Qout(leftMinusOne, rightOperation)));

                        default:
                            break;
                        }
                    }

                    IOperation leftOperation = MonoOperation.ParseFrom(left);

                    if ((rightOperation as StructureDefine) == null)
                    {
                        return(new Assum(leftOperation, rightOperation));
                    }
                    else
                    {
                        Assum needUPdatedAssum = new Assum(leftOperation, rightOperation);
                        needUPdatedAssum.requiredUpdate = "structdefineinfor";
                        return(needUPdatedAssum);
                    }
                }

                if (onLevel(s, "==", 0))
                {
                    MISC.separate(s, "==", ref left, ref right, lastIndex);
                    return(new Equal(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, "!=", 0))
                {
                    MISC.separate(s, "!=", ref left, ref right, lastIndex);
                    return(new Uneq(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, "<=", 0))
                {
                    MISC.separate(s, "<=", ref left, ref right, lastIndex);
                    return(new LsEq(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, ">=", 0))
                {
                    MISC.separate(s, ">=", ref left, ref right, lastIndex);
                    return(new MrEq(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, "<", 0))
                {
                    MISC.separate(s, "<", ref left, ref right, lastIndex);
                    return(new Less(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, ">", 0))
                {
                    MISC.separate(s, ">", ref left, ref right, lastIndex);
                    return(new More(ParseFrom(left), ParseFrom(right)));
                }

                if (onLevel(s, "+", 0))
                {
                    MISC.separate(s, "+", ref left, ref right, lastIndex);
                    return(new Summ(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, "-", 0))
                {
                    MISC.separate(s, "-", ref left, ref right, lastIndex);
                    if (left.Length == 0)
                    {
                        return(MonoOperation.ParseFrom("-" + right));
                    }
                    return(new Diff(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, "*", 0) && s.IndexOf("*") > 0)
                {
                    MISC.separate(s, "*", ref left, ref right, lastIndex);
                    // can be initialization of pointer!
                    if (new ValueType(VT.Cunknown) == Define.detectType(left))
                    {
                        return(new Mult(ParseFrom(left), ParseFrom(right)));
                    }
                }
                if (onLevel(s, "/", 0))
                {
                    MISC.separate(s, "/", ref left, ref right, lastIndex);
                    return(new Qout(ParseFrom(left), ParseFrom(right)));
                }

                if (onLevel(s, "||", 0))
                {
                    MISC.separate(s, "||", ref left, ref right, lastIndex);
                    return(new ORS(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, "|", 0))
                {
                    MISC.separate(s, "|", ref left, ref right, lastIndex);
                    return(new OR(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, "&&", 0))
                {
                    MISC.separate(s, "&&", ref left, ref right, lastIndex);
                    return(new ANDS(ParseFrom(left), ParseFrom(right)));
                }
                if (onLevel(s, "&", 0) && s.IndexOf("&") > 0)
                {
                    MISC.separate(s, "&", ref left, ref right, lastIndex);
                    return(new AND(ParseFrom(left), ParseFrom(right)));
                }


                if (s.IndexOf('(') == 0 && s.LastIndexOf(')') == s.Length - 1)
                {
                    return(ParseFrom(MISC.breakBrackets(s)));
                }
                try
                {
                    return(UniqOperation.ParseFrom(s));
                }
                catch (Exception e)
                {
                    return(MonoOperation.ParseFrom(s));
                }
            }
            catch (Exception e)
            {
                if (e.Message.IndexOf("#MDS:") != 0)
                {
                    throw new Exception("Can not parse \"" + s + "\"\n" + e.Message);
                }
                else
                {
                    throw new Exception(e.Message);
                }
            }
        }
예제 #3
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));
        }