예제 #1
0
        public ASTTree(string s)
        {
            string sTrim = "";

            ClearTree();

            sTrim    = FuncTrimmer(s); // remove last ^
            original = s;
            string[] funcParseMaterial = sTrim.Split('^');
            try
            {
                for (int i = 0; i < funcParseMaterial.Length; i++)
                {
                    if (funcParseMaterial[i].IndexOf("(") >= 0)
                    {
                        funcs.Add(new ASTFunction(funcParseMaterial[i]));
                    }
                    else
                    {
                        IOperation def = BinaryOperation.ParseFrom(funcParseMaterial[i]);
                        if ((def as Assum) == null && (def as Define) == null)
                        {
                            throw new Exception("Can not parse function or define:\t " + MISC.StringFirstLetters(funcParseMaterial[i], 20, true));
                        }
                        else
                        {
                            GlobalVars.MergeWith(new CommandOrder(new ICommand[] { def }));
                        }
                    }
                }
                // after function declaration we have int foo(); int foo(){return 0;}; need to make them a one function
                for (int i = 0; i < funcs.Count; i++)
                {
                    for (int j = i + 1; j < funcs.Count; j++)
                    {
                        if (funcs[i] != null && funcs[j] != null)
                        {
                            if (MISC.CompareFunctionSignature(funcs[i], funcs[j]))
                            {
                                funcs[i] = funcs[j]; funcs[j] = null;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MISC.ConsoleWriteLine("ERROR:\n" + e.Message, ConsoleColor.Red);
                ClearTree();
                return;
            }
        }
예제 #2
0
파일: ASTTree.cs 프로젝트: P1nkL1on/Compile
        public ASTTree(string s, string path)
        {
            LLVM.CurrentCode = "";
            this.path        = path;
            string sTrim = "";

            ClearTree();

            data = DateTime.Now;

            sTrim    = FuncTrimmer(s); // remove last ^
            original = s;
            string[] funcParseMaterial = sTrim.Split('^');
            generatedTime.Add((DateTime.Now - data).Seconds * 1000 + (DateTime.Now - data).Milliseconds); data = DateTime.Now;
            try
            {
                for (int i = 0; i < funcParseMaterial.Length; i++)
                {
                    if (funcParseMaterial[i].IndexOf("(") >= 0)
                    {
                        funcs.Add(new ASTFunction(funcParseMaterial[i]));

                        int found = -1;
                        for (int f = 0; f < funcs.Count - 1; f++)
                        {
                            if (funcs[f].getName == funcs[funcs.Count - 1].getName)
                            {
                                found++;
                            }
                        }
                        funcs[funcs.Count - 1].LLVMnumber = found;
                    }
                    else
                    {
                        IOperation def = BinaryOperation.ParseFrom(funcParseMaterial[i]);
                        if ((def as Assum) == null && (def as Define) == null)
                        {
                            throw new Exception("Can not parse function or define:\t " + MISC.StringFirstLetters(funcParseMaterial[i], 20, true));
                        }
                        else
                        {
                            GlobalVars.MergeWith(new CommandOrder(new ICommand[] { def }));
                        }
                    }
                }
                // after function declaration we have int foo(); int foo(){return 0;}; need to make them a one function
                for (int i = 0; i < funcs.Count; i++)
                {
                    for (int j = i + 1; j < funcs.Count; j++)
                    {
                        if (funcs[i] != null && funcs[j] != null)
                        {
                            if (MISC.CompareFunctionSignature(funcs[i], funcs[j]))
                            {
                                funcs[i] = funcs[j]; funcs[j] = null;
                            }
                        }
                    }
                }

                //GlobalVarsVars = GlobalVars.findAllVariables();
                foreach (ASTvariable va in variables)
                {
                    if (va.adress.typ == VAT.Global && va.everUsed > 0)
                    {
                        GlobalVarsVars.Add(va);
                    }
                }
            }
            catch (Exception e)
            {
                MISC.ConsoleWriteLine("ERROR:\n" + e.Message, ConsoleColor.Red);
                ClearTree();
                return;
            }
            generatedTime.Add((DateTime.Now - data).Seconds * 1000 + (DateTime.Now - data).Milliseconds);
        }
예제 #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));
        }