예제 #1
0
        private static IList <string> CreateCode(ICodeCreator creator, ObjectFormulaTree tree, List <ObjectFormulaTree> busy)
        {
            List <ObjectFormulaTree> l = new List <ObjectFormulaTree>();

            GetList(tree, l, busy);
            IList <string> lvr;
            IList <string> lpr;
            List <string>  cc = new List <string>();

            for (int i = 0; i < l.Count; i++)
            {
                ObjectFormulaTree tr = l[i];
                List <string>     p  = new List <string>();
                string            rr = creator[tr];
                for (int j = 0; j < tr.Count; j++)
                {
                    ObjectFormulaTree chc = tr[j];
                    if (chc == null)
                    {
                        continue;
                    }
                    p.Add(creator[chc]);
                }
                cc.AddRange(creator.CreateCode(tr, rr, p.ToArray(), out lvr, out lpr));
            }
            return(cc);
        }
예제 #2
0
        static Assembly CreateAssembly(IList <string> text, out CodeCreator cc)
        {
            cc = new CodeCreator(text);
            ICodeCreator   icc   = cc;
            IList <string> lcode = icc.CreateCode(cc.subsystem);
            StringBuilder  sb    = new StringBuilder();

            foreach (string s in lcode)
            {
                sb.Append(s + "\r\n");
            }
            string             code          = sb.ToString();
            CompilerParameters compileParams = new CompilerParameters();

            compileParams.IncludeDebugInformation = true;
            compileParams.GenerateExecutable      = false;
            compileParams.GenerateInMemory        = true;
            string dir = AppDomain.CurrentDomain.BaseDirectory;

            compileParams.ReferencedAssemblies.Add(dir + "Simulink.CSharp.Library.dll");
            CompilerResults results =
                compiler.CompileAssemblyFromSource(compileParams, code);

            try
            {
                Assembly ass = results.CompiledAssembly;
                return(ass);
            }
            catch (Exception)
            {
            }
            return(null);
        }
예제 #3
0
        private List <string> PostCreateCode(ICodeCreator local, IList <string> lcode,
                                             IList <string> variables, IList <string> initializers, string consturctor, bool checkValue = false)
        {
            List <string> l = new List <string>();

            l.Add("public void Update()");
            l.Add("{");
            foreach (string s in lcode)
            {
                l.Add("\t" + s);
            }
            // StaticCodeCreator.Add(sb, lcode);
            int nTree = local.Trees.Length;

            if (checkValue)
            {
                for (int i = 0; i < nTree; i++)
                {
                    l.Add("\tcheckValue(var_" + i + ");");
                }
            }
            l.Add("}");
            l.Add("");
            if (checkValue)
            {
                l.Add(consturctor + "(FormulaEditor.ObjectFormulaTree[] trees, BaseTypes.ObjectAction checkValue)");
                l.Add("{");
                l.Add("\tthis.trees = trees;");
                l.Add("\tthis.checkValue = checkValue;");
            }
            else
            {
                l.Add(consturctor + "(FormulaEditor.ObjectFormulaTree[] trees)");
                l.Add("{");
                l.Add("\tthis.trees = trees;");
            }
            foreach (string s in initializers)
            {
                l.Add("\t" + s);
            }
            l.Add("}");
            l.Add("");
            l.Add("public FormulaEditor.GetValue this[FormulaEditor.ObjectFormulaTree tree]");
            l.Add("{ get { return dictionary[tree]; }}");
            l.Add("");
            l.Add("Dictionary<FormulaEditor.ObjectFormulaTree, FormulaEditor.GetValue> dictionary = new Dictionary<FormulaEditor.ObjectFormulaTree, FormulaEditor.GetValue>();");
            l.Add("");
            foreach (string s in variables)
            {
                l.Add("" + s);
            }
            if (checkValue)
            {
                l.Add("");
                l.Add("BaseTypes.ObjectAction checkValue;");
            }
            // l.Add("\t}");
            return(l);
        }
예제 #4
0
 public GameController(ICodeCreator builder, ICodeComparer comparer, IGuessTranslator trans, IGameIO consoleIO)
 {
     _guessTranslate = trans;
     _codeComparer   = comparer;
     _builder        = builder;
     _consoleIO      = consoleIO;
     _guesses        = new List <List <int> >();
     _feedbacks      = new List <string>();
 }
예제 #5
0
 private CreatePasswordRecovery(IPasswordRecoveryRepository passwordRecoveryRepository, IUserRepository userRepository, ICodeCreator codeCreator, string userName, string appKey)
 {
     _passwordRecoveryRepository = passwordRecoveryRepository;
     _userRepository             = userRepository;
     _codeCreator = codeCreator;
     _userName    = userName;
     _appKey      = appKey;
     ValidateInstance();
     UserNotExistsExceptionCreator = () => UserNotExistsException.Create();
 }
예제 #6
0
        /// <summary>
        /// Creates code from trees
        /// </summary>
        /// <param name="trees">The trees</param>
        /// <param name="creator">Code creator</param>
        /// <param name="local">Local code reator</param>
        /// <param name="variables">Strings of variables</param>
        /// <param name="initializers">Strings of initializers</param>
        /// <returns>Strings of code</returns>
        public static IList <string> CreateCode(ObjectFormulaTree[] trees, ICodeCreator creator,
                                                out ICodeCreator local,
                                                out IList <string> variables, out IList <string> initializers)
        {
            List <string> code = new List <string>();
            List <string> vari = new List <string>();
            List <string> init = new List <string>();

            local = creator.Create(trees);
            IList <ObjectFormulaTree> lt = local.Trees;

            if (local.Optional.Count > 0)
            {
                return(CreateOptionalCode(local, out variables, out initializers));
            }
            foreach (ObjectFormulaTree t in lt)
            {
                string         ret = local[t];
                IList <string> par = new List <string>();
                int            n   = t.Count;
                for (int i = 0; i < n; i++)
                {
                    ObjectFormulaTree child = t[i];
                    if (child == null)
                    {
                        continue;
                    }
                    par.Add(local[child]);
                }
                IList <string> lv;
                IList <string> lp;
                IList <string> c = local.CreateCode(t, ret, par.ToArray <string>(),
                                                    out lv, out lp);
                if (lv != null)
                {
                    vari.AddRange(lv);
                }
                if (lp != null)
                {
                    init.AddRange(lp);
                }
                if (creator.GetConstValue(t) == null)
                {
                    code.AddRange(c);
                }
                else if (creator.GetConstValue(t).Equals("\"\""))
                {
                    code.AddRange(c);
                }
            }
            variables    = vari;
            initializers = init;
            return(code);
        }
예제 #7
0
        private IList <string> PreCreateCode(out ICodeCreator local,
                                             out IList <string> variables, out IList <string> initializers)
        {
            IList <string> lcode = CSharpCodeCreator.CreateCode(trees, codeCreator,
                                                                out local, out variables, out initializers);

            ObjectFormulaTree[] tr = local.Trees;
            foreach (ObjectFormulaTree tree in tr)
            {
                AddTree(tree, initializers, variables);
            }
            if (checkValue != null)
            {
            }
            return(lcode);
        }
예제 #8
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="trr">Collection of trees</param>
        protected AbstractCodeCreator(ObjectFormulaTree[] trr)
        {
            codeCreator = this;
            ObjectFormulaTree[] t = trr;
            if (t == null)
            {
                t = new ObjectFormulaTree[0];
            }
            trees = ObjectFormulaTree.CreateList(t, optional).ToArray();
            int i = 0;

            foreach (ObjectFormulaTree tr in trees)
            {
                ident[tr]      = "var_" + i;
                dictionary[tr] = i;
                ++i;
            }
        }
예제 #9
0
        /// <summary>
        /// Gets number of tree
        /// </summary>
        /// <param name="creator">Creator of code</param>
        /// <param name="tree">The tree</param>
        /// <returns>Number of tree</returns>
        public static int GetNumber(ICodeCreator creator, ObjectFormulaTree tree)
        {
            try
            {
                return(creator.GetNumber(tree));

                /*  ObjectFormulaTree[] trees = creator.Trees;
                 * for (int i = 0; i < trees.Length; i++)
                 * {
                 *    if (trees[i] == tree)
                 *    {
                 *        return i;
                 *    }
                 * }*/
            }
            catch (Exception exception)
            {
                throw new Exception("Tree not found");
            }
        }
예제 #10
0
        /// <summary>
        /// Creates code
        /// </summary>
        /// <param name="trees">Trees</param>
        /// <param name="creator">Code creator</param>
        /// <param name="local">Local code creator</param>
        /// <param name="variables">Variables</param>
        /// <param name="initializers">Initializers</param>
        /// <returns>List of code strings</returns>
        public static IList <string> CreateCode(ObjectFormulaTree[] trees, ICodeCreator creator, out ICodeCreator local,
                                                out IList <string> variables, out IList <string> initializers)
        {
            IList <string> l = StaticCodeCreator.CreateCode(trees, creator, out local,
                                                            out variables, out initializers);

            variables.Add("FormulaEditor.ObjectFormulaTree currentTree = null;");
            variables.Add("object[] currentArray = null;");
            variables.Add("double doubleValue = 0;");
            variables.Add("FormulaEditor.ObjectFormulaTree[] trees = null;");
            ObjectFormulaTree[] lt = local.Trees;
            foreach (ObjectFormulaTree tree in lt)
            {
                object ret = tree.ReturnType;
                if (ret.IsEmpty())
                {
                    continue;
                }
                string s = typeCreator.GetType(ret) + " ";
                s += local[tree];
                string cv  = creator.GetConstValue(tree);
                string def = "";
                if (cv == null)
                {
                    def = typeCreator.GetDefaultValue(ret) + "";
                    if (def.Length > 0)
                    {
                        s += " = ";
                    }
                    s += def;
                }
                else
                {
                    s += " = " + cv;
                }
                s += ";";
                variables.Add(s);
            }
            return(l);
        }
예제 #11
0
        static IList <string> CreateOptionalCode(ICodeCreator creator, out IList <string> variables, out IList <string> initializers)
        {
            List <string>             code  = new List <string>();
            List <string>             vari  = new List <string>();
            List <string>             init  = new List <string>();
            IList <ObjectFormulaTree> lt    = creator.Trees;
            IList <ObjectFormulaTree> opt   = creator.Optional;
            List <ObjectFormulaTree>  busy  = new List <ObjectFormulaTree>();
            List <ObjectFormulaTree>  conds = new List <ObjectFormulaTree>();
            Dictionary <ObjectFormulaTree, ObjectFormulaTree> pch = new Dictionary <ObjectFormulaTree, ObjectFormulaTree>();

            foreach (ObjectFormulaTree t in opt)
            {
                for (int i = 0; i < t.Count; i++)
                {
                    pch[t[i]] = t;
                }
            }
            foreach (ObjectFormulaTree t in lt)
            {
                IList <string> par = new List <string>();
                if (busy.Contains(t))
                {
                    continue;
                }
                string ret = creator[t];
                if (pch.ContainsKey(t))
                {
                    ObjectFormulaTree oft = pch[t];
                    string            rcr = creator[oft];
                    busy.Add(oft);
                    ObjectFormulaTree cond = oft[0];
                    for (int i = 0; i < cond.Count; i++)
                    {
                        ObjectFormulaTree chc = cond[i];
                        if (chc == null)
                        {
                            continue;
                        }
                        busy.Add(chc);
                        par.Add(creator[chc]);
                    }
                    IList <string> lvc;
                    IList <string> lpc;
                    string         rc = creator[cond];
                    if (!conds.Contains(cond))
                    {
                        conds.Add(cond);
                        IList <string> cc = creator.CreateCode(cond, rc, par.ToArray(),
                                                               out lvc, out lpc);
                        if (lvc != null)
                        {
                            vari.AddRange(lvc);
                        }
                        if (lpc != null)
                        {
                            init.AddRange(lpc);
                        }
                        if (creator.GetConstValue(cond) == null)
                        {
                            code.AddRange(cc);
                        }
                    }
                    code.Add("if (" + rc + ")");
                    code.Add("{");
                    for (int k = 1; k < 3; k++)
                    {
                        ObjectFormulaTree tt = oft[k];
                        if (k == 0)
                        {
                            IList <string> lvr;
                            IList <string> lpr;
                            string         rr = creator[tt];
                            List <string>  p  = new List <string>();
                            if (k > 0)
                            {
                                for (int i = 0; i < tt.Count; i++)
                                {
                                    ObjectFormulaTree chc = tt[i];
                                    if (chc == null)
                                    {
                                        continue;
                                    }
                                    busy.Add(chc);
                                    p.Add(creator[chc]);
                                }
                            }
                            IList <string> cr = creator.CreateCode(tt, rr, p.ToArray <string>(),
                                                                   out lvr, out lpr);
                            if (lvr != null)
                            {
                                vari.AddRange(lvr);
                            }
                            if (lpr != null)
                            {
                                init.AddRange(lpr);
                            }
                            if (creator.GetConstValue(t) == null)
                            {
                                code.AddRange(cr);
                            }
                        }
                        else
                        {
                            code.AddRange(CreateCode(creator, tt, busy));
                            code.Add(rcr + " = " + creator[tt] + ";");
                        }
                        if (k == 1)
                        {
                            code.Add("}");
                            code.Add("else");
                            code.Add("{");
                        }
                    }
                    code.Add("}");
                    continue;
                }
                busy.Add(t);
                int n = t.Count;
                for (int i = 0; i < n; i++)
                {
                    ObjectFormulaTree child = t[i];
                    busy.Add(child);
                    if (child == null)
                    {
                        continue;
                    }
                    par.Add(creator[child]);
                }
                IList <string> lv;
                IList <string> lp;
                IList <string> c = creator.CreateCode(t, ret, par.ToArray <string>(),
                                                      out lv, out lp);
                if (lv != null)
                {
                    vari.AddRange(lv);
                }
                if (lp != null)
                {
                    init.AddRange(lp);
                }
                if (creator.GetConstValue(t) == null)
                {
                    code.AddRange(c);
                }
            }
            List <string> lvar = new List <string>();

            foreach (string s in vari)
            {
                if (!lvar.Contains(s))
                {
                    lvar.Add(s);
                }
            }
            variables = lvar;
            List <string> lini = new List <string>();

            foreach (string s in init)
            {
                if (!lini.Contains(s))
                {
                    lini.Add(s);
                }
            }
            initializers = lini;
            return(code);
        }
예제 #12
0
 public static async Task <string> Execute(IPasswordRecoveryRepository passwordRecoveryRepository, IUserRepository userRepository, ICodeCreator codeCreator, string userName, string appKey) =>
 await new CreatePasswordRecovery(passwordRecoveryRepository, userRepository, codeCreator, userName, appKey).Do();