コード例 #1
0
ファイル: ASTTree.cs プロジェクト: P1nkL1on/C_Sharp_Sheets
        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
        public ICommand ParseCommand2(String S)
        {
            int s1 = MISC.IndexOfOnLevel0(S, "(", 0),
                s2 = MISC.IndexOfOnLevel0(S, ")", 0),
                p1 = MISC.IndexOfOnLevel0(S, "{", 0),
                p2 = MISC.IndexOfOnLevel0(S, "}", 0);

            if (s2 < s1 || p2 < p1)
            {
                throw new Exception("Command contains incorrect brackets:\t" + MISC.StringFirstLetters(S, 20, true));
            }

            if (p1 == 0 && p2 == S.Length - 1)
            {
                MISC.GoDeep("Block");
                CommandOrder co = new CommandOrder(MISC.getIn(S, 0), ';');
                MISC.GoBack();
                return(co);
            }

            // Binary operation usuall
            if ((p1 < 0 || (MISC.IndexOfOnLevel0(S, "=", 0) > 0)) &&
                S.ToLower().IndexOf("if") != 0 && S.ToLower().IndexOf("for") != 0)
            {
                IOperation newBO = BinaryOperation.ParseFrom(S);
                if ((newBO as Assum) != null && (newBO as Assum).requiredUpdate != "none")
                {
                    string needUpdate = (newBO as Assum).requiredUpdate;
                    if (needUpdate.IndexOf("structdefineinfor") == 0)
                    {
                        string nam = (newBO as Assum).GetAssumableName;
                        if (nam == "-")
                        {
                            throw new Exception("What are you doing!?");
                        }
                        List <IOperation> values = (newBO as Assum).GetStructDefine();
                        List <ICommand>   res    = new List <ICommand>();
                        for (int i = 0; i < values.Count; i++)
                        {
                            res.Add(new Assum(BinaryOperation.ParseFrom(nam + "[" + i + "]"), values[i]));
                        }

                        return(new CommandOrder(res.ToArray()));
                    }
                }
                else
                {
                    return(newBO);
                }
            }
            // _______________________
            if (s1 > 0)
            {
                string operatorFind = S.Remove(s1);
                // simple if
                if (operatorFind == "if")
                {
                    // we gonna parse IF from this shit!
                    string conditionParse   = MISC.getIn(S, 2),
                           firstActionParse = S.Substring(s2 + 1);

                    int indElse = MISC.IndexOfOnLevel0(firstActionParse, "else", 0);
                    if (indElse > 0)
                    {
                        string secondActionParse = firstActionParse.Substring(indElse + 4);
                        firstActionParse = firstActionParse.Substring(0, indElse);

                        return(new OperatorIf(conditionParse, firstActionParse, secondActionParse));
                    }
                    return(new OperatorIf(conditionParse, firstActionParse, ""));
                }
                // simple while
                if (operatorFind == "while")
                {
                    string conditionParse = MISC.getIn(S, 5),
                           iterationParse = S.Substring(s2 + 1);
                    return(new CycleWhile(conditionParse, iterationParse, false));
                }
                // reverse while
                if (p1 == 2 && S.Remove(p1) == "do")
                {
                    int whilePos = MISC.IndexOfOnLevel0(S, "while", 0);

                    if (whilePos < -1)
                    {
                        throw new Exception("No while, but used \"do\"\t " + MISC.StringFirstLetters(S, 20, true));
                    }

                    string iterationParse = MISC.getIn(S, 2),
                           conditionParse = MISC.getIn(S, s1);

                    return(new CycleWhile(conditionParse, iterationParse, true));
                }
                // FOR mazafaka
                if (operatorFind == "for")
                {
                    string partsParse = MISC.getIn(S, 3),
                           allOther   = S.Substring(s2 + 1);
                    string[] spp      = partsParse.Split(';');
                    if (spp.Length != 3)
                    {
                        throw new Exception("Invalid count of FOR-cycle condition parts\t " + MISC.StringFirstLetters(S, 20, true));
                    }

                    MISC.GoDeep("FOR");
                    this.MergeWith(new CommandOrder(spp[0], ','));
                    if (spp[1] == "")
                    {
                        spp[1] = "true";                // condition
                    }
                    CommandOrder actions = new CommandOrder(allOther, ';'); actions.MergeWith(new CommandOrder(spp[2], ','));


                    CycleFor cf = new CycleFor(spp[1], actions);
                    MISC.GoBack();

                    return(cf);
                }
                throw new Exception("Can not parse a command\t " + MISC.StringFirstLetters(S, 20, true));
            }

            return(new CommandOrder());
        }
コード例 #3
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);
        }
コード例 #4
0
        public ASTFunction(string S)
        {
            infiniteParamsAfter = -1;
            declareOnly         = false;
            //TypeConvertion tpcv = new TypeConvertion("IIBDDBDIBIDBCCB", 2);
            string           s      = S.Substring(0, S.IndexOf('('));
            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");
                MISC.ChangeAdressType(VAT.Parameter);

                for (int i = 0; i < vars.Count; i++)
                {
                    if (vars[i] != "...")
                    {
                        if (infiniteParamsAfter >= 0)
                        {
                            throw new Exception("Can not defined more arguments after ... !");
                        }
                        input.Add(GetDefineFromString(vars[i]));
                        vtList.Add((input[input.Count - 1] as Define).returnTypes());
                    }
                    else
                    {
                        infiniteParamsAfter = i;
                    }
                }
                //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());
                        MISC.ChangeAdressType(VAT.Local);
                        string actionCode = MISC.getIn(S, S.IndexOf('{'));
                        actions = new CommandOrder(actionCode, ';');
                        if (!MISC.GoBack())
                        {
                            actions.MergeWith(new CommandOrder(new ICommand[] { new Ret() }));
                        }
                        MISC.ChangeAdressType(VAT.Global);
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Problem in function \"" + name + "\"\n" + e.Message);
                    }
                }
                else
                {
                    actions     = new CommandOrder();
                    declareOnly = true;
                }
                MISC.GoBack();

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