Exemple #1
0
 private AssureSplitStacks topParent(AssureSplitStacks assureSplitStacks)
 {
     if (assureSplitStacks.possibleParent == null)
     {
         return(assureSplitStacks);
     }
     return(topParent(assureSplitStacks.possibleParent));
 }
Exemple #2
0
        public AssureSplitStacks GetSplit()
        {
            var d = new AssureSplitStacks(myWholeList);

            d.possibleParent = topParent(this);
            d.Needs          = new Dictionary <int, Tuple <int, List <PostParseVariable> > >();

            for (int index = 0; index < Items.Length; index++)
            {
                var postParseItem = Items[index];
                if (postParseItem.Any())
                {
                    d.Needs.Add(index, new Tuple <int, List <PostParseVariable> >(getHighestVariableIndex() + 1 + index, postParseItem));
                }
            }
            return(d);
        }
        private void tryToNeeds(PostParseItem item, bool isLeft, AssureSplitStacks assurer)
        {
            switch (item.ItemType)
            {
                case PPItemType.Variable:
                    if (((PostParseVariable)item).Type == VariableType.Stack)
                    {
                        if (isLeft)
                            assurer.Resolve(((PostParseVariable)item).Index, ((PostParseVariable)item));
                        else
                            assurer.Need(((PostParseVariable)item).Index, ((PostParseVariable)item));

                    }
                    break;
                case PPItemType.String:
                    return;

                case PPItemType.Operation:

                    var op = ((PostParseOperation)item);
                    tryToNeeds(op.Left, false, assurer);
                    tryToNeeds(op.Right, false, assurer);

                    break;
                case PPItemType.Statement:
                    var st = ((PostParseStatement)item);
                    foreach (var postParseItem in st.Items)
                    {
                        tryToNeeds(postParseItem, false, assurer);
                    }
                    break;
                case PPItemType.Method:
                    var wst = ((PostParseMethod)item);
                    foreach (var a in wst.Params)
                        tryToNeeds(a, false, assurer);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            return;

            ;
        }
        private void fixThings(List<PostParseExpression> item2, AssureSplitStacks assurer, int index = -1)
        {
            for (index = index == -1 ? item2.Count - 1 : index; index >= 0; index--)
            {
                PostParseExpression postParseExpression = item2[index];
                switch (postParseExpression.ExpressionType)
                {
                    case PPExpressionType.Statement:
                        var st = ((PostParseStatement)postParseExpression);
                        foreach (var a in st.Items)
                            tryToNeeds(a, false, assurer);
                        break;
                    case PPExpressionType.Set:

                        if (((PostParseSet)postParseExpression).Left is PostParseVariable)
                        {
                            var pps = (PostParseVariable)((PostParseSet)postParseExpression).Left;

                            if (pps.Type == VariableType.Stack)
                            {
                                tryToNeeds(((PostParseSet)postParseExpression).Left, true, assurer);
                            }
                        }

                        tryToNeeds(((PostParseSet)postParseExpression).Right, false, assurer);

                        break;
                    case PPExpressionType.String:

                        if (((PostParseString)postParseExpression).GotoString != null)
                        {
                            return;
                        }
                        else if (((PostParseString)postParseExpression).LabelString != null)
                        {
                            var split = assurer.GetSplit();
                            var k = new Dictionary<int, Tuple<int, List<PostParseVariable>>>(split.Needs);
                            foreach (var i in findGoto(item2, ((PostParseString)postParseExpression).LabelString))
                            {
                                fixThings(item2, split, i - 1);
                                split.Needs = new Dictionary<int, Tuple<int, List<PostParseVariable>>>(k);
                            }
                            fixThings(item2, split, index - 1);
                            return;
                        }
                        else
                        {

                        }

                        break;
                    case PPExpressionType.Method:
                        var wst = ((PostParseMethod)postParseExpression);
                        foreach (var a in wst.Params)
                            tryToNeeds(a, false, assurer);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
        }
        public Command buildIt()
        {
            List<Tuple<SpokeMethod, Tuple<string, List<PostParseExpression>>, int>> itemz = new List<Tuple<SpokeMethod, Tuple<string, List<PostParseExpression>>, int>>();

            foreach (var spokeMethod in Methods.Where(a => a.Instructions != null && a.Instructions.Length > 0))
            {
                Tuple<string, List<PostParseExpression>> der = buildMethod(spokeMethod);

                var gotos = der.Item2.Where(a => a.ExpressionType == PPExpressionType.String && ((PostParseString)a).GotoString != null);
                var labels = der.Item2.Where(a => a.ExpressionType == PPExpressionType.String && ((PostParseString)a).LabelString != null).ToArray();

                foreach (var postParseExpression in labels) {
                    if (!gotos.Any(a=>((PostParseString)a).GotoString==((PostParseString)postParseExpression).LabelString)) {
                        for (int index = der.Item2.Count-1; index >= 0; index--)
                        {
                            var parseExpression = der.Item2[index];
                            if (parseExpression.Equals(postParseExpression)) {
                                der.Item2.RemoveAt(index);
                            }
                        }
                    }
                }

                for (int index = der.Item2.Count - 1; index >= 1; index--)
                {
                    var postParseExpression = der.Item2[index];

                    if (postParseExpression.ExpressionType == PPExpressionType.String && der.Item2[index - 1].ExpressionType == PPExpressionType.String)
                    {
                        if (((PostParseString)postParseExpression).LabelString != null && ((PostParseString)der.Item2[index - 1]).GotoString != null)
                        {
                            if (((PostParseString)postParseExpression).LabelString.Equals(((PostParseString)der.Item2[index - 1]).GotoString))
                            {

                                if (!der.Item2.Any(a => a.ExpressionType == PPExpressionType.String && ((PostParseString)a).GotoString == ((PostParseString)postParseExpression).LabelString))
                                {
                                    der.Item2.RemoveAt(index - 1);
                                    der.Item2.RemoveAt(index - 1);
                                    index -= 1;
                                }
                            }
                        }
                    }
                }

                File.WriteAllText("C:\\aabc.txt", der.Item2.Where(a => !a.MarkForRemoval).Aggregate("", (a, b) => a + "\t\t" + b.ToString() + "\r\n"));
                AssureSplitStacks d;
                fixThings(der.Item2, d = new AssureSplitStacks(der.Item2));
                File.WriteAllText("C:\\aabc3.txt", der.Item2.Where(a => !a.MarkForRemoval).Aggregate("", (a, b) => a + "\t\t" + b.ToString() + "\r\n"));
                doThings(der.Item2);
                if (spokeMethod.Class.Name == "Main" && spokeMethod.MethodName == ".ctor")
                    Construct.NumOfVars = spokeMethod.NumOfVars = AssureSplitStacks.getAllVariables(der.Item2).OrderBy(a => a.Index).Last().Index + 1;
                else spokeMethod.NumOfVars = AssureSplitStacks.getAllVariables(der.Item2).OrderBy(a => a.Index).Last().Index + 1;
                itemz.Add(new Tuple<SpokeMethod, Tuple<string, List<PostParseExpression>>, int>(spokeMethod, der, d.NumberOfSpecVariables));

            }

            foreach (var tuple in itemz)
            {
                foreach (var postParseMethod in AssureSplitStacks.getAllMethods(tuple.Item2.Item2).Where(a => !a.TotalMethodName.Contains("[")))
                {
                    var g = itemz.FirstOrDefault(a => a.Item1.Class.Name + a.Item1.CleanMethodName == postParseMethod.TotalMethodName);

                    if (g != null)
                    {
                        postParseMethod.NumOfVariables = g.Item1.NumOfVars;
                    }
                    else
                    {
                        throw new AbandonedMutexException();
                    }
                }
            }

            TOP = @"
            private SpokeMethod[] Methods;
            private Func<SpokeObject[], SpokeObject>[] InternalMethods;

            private SpokeObject FALSE = new SpokeObject(false);
            private SpokeObject TRUE = new SpokeObject(true) ;
            private SpokeObject[] ints;
            private SpokeObject NULL = new SpokeObject(ObjectType.Null);

            private SpokeObject intCache(int index)
            {
               // if (index > 0 && index < 100)
            {
               //     return ints[index];
            }
            return new SpokeObject(index);
            }

            public RunClass()
            {
            }
            public void loadUp(Func<SpokeObject[], SpokeObject>[] internalMethods, SpokeMethod[] mets){
            Methods = mets;
            InternalMethods = internalMethods;
            ints = new SpokeObject[100];
            for (int i = 0; i < 100; i++)
            {
                ints[i] = new SpokeObject(i);
            }
            }

            public SpokeObject Run()
            {
            SpokeObject dm = new SpokeObject(new SpokeObject[" + Construct.NumOfVars + @"]);
            var gm = new SpokeObject[" + Construct.NumOfVars + @"];
            gm[0] = dm;
            return Mainctor(gm);
            }";

            StringBuilder file2 = new StringBuilder();
            StringBuilder file = new StringBuilder();
            file.AppendLine(@"using System;using System.Collections.Generic;using System.Linq;using System.Text;
            namespace ConsoleApplication1{public partial class RunClass:Command{");

            file.AppendLine(TOP);
            file2.Append(@"using System;using System.Collections.Generic;using System.Linq;using System.Text;
            namespace ConsoleApplication1{public partial class RunClass:Command{");
            file2.AppendLine(TOP);

            foreach (var tuple in itemz)
            {
                var spokeMethod = tuple.Item1;
                var der = tuple.Item2;
                file.AppendLine(string.Format("private SpokeObject {0}(SpokeObject[] variables) {{", spokeMethod.Class.Name + spokeMethod.CleanMethodName));
                file.AppendLine(string.Format("SpokeObject[] specVariables =new SpokeObject[" + tuple.Item3 + "]; SpokeObject[] sps;"));
                file.AppendLine(string.Format("SpokeObject bm2;"));
                file.AppendLine(string.Format("SpokeObject bm;"));
                file.AppendLine(string.Format("SpokeObject lastStack;"));

                file2.AppendLine(string.Format("private SpokeObject {0}(SpokeObject[] variables) {{", spokeMethod.Class.Name + spokeMethod.CleanMethodName));
                file2.AppendLine(string.Format("SpokeObject[] stack =new SpokeObject[100]; SpokeObject[] sps;"));
                file2.AppendLine(string.Format("SpokeObject[] specVariables =new SpokeObject[" + tuple.Item3 + "];"));
                file2.AppendLine(string.Format("SpokeObject bm2;"));
                file2.AppendLine(string.Format("SpokeObject bm;"));
                file2.AppendLine(string.Format("SpokeObject lastStack;"));

                file2.AppendLine(der.Item1);

                string dm = der.Item2.Where(a => !a.MarkForRemoval).Aggregate("", (a, b) => a + "\t\t" + b.ToString() + "\r\n");
                file.AppendLine(dm);

                file.AppendLine(string.Format("return null;}}"));
                file2.AppendLine(string.Format("return null;}}"));
            }

            file.AppendLine(string.Format("}}}}"));
            file2.AppendLine(string.Format("}}}}"));

            File.WriteAllText(@"c:\abc.cs", file.ToString());
            File.WriteAllText(@"c:\abc2.cs", file2.ToString());

            return compile(file .ToString());
        }
 private AssureSplitStacks topParent(AssureSplitStacks assureSplitStacks)
 {
     if (assureSplitStacks.possibleParent == null)
     {
         return assureSplitStacks;
     }
     return topParent(assureSplitStacks.possibleParent);
 }
        public AssureSplitStacks GetSplit()
        {
            var d = new AssureSplitStacks(myWholeList);
            d.possibleParent = topParent(this);
            d.Needs = new Dictionary<int, Tuple<int, List<PostParseVariable>>>();

            for (int index = 0; index < Items.Length; index++)
            {
                var postParseItem = Items[index];
                if (postParseItem.Any())
                {

                    d.Needs.Add(index, new Tuple<int, List<PostParseVariable>>(getHighestVariableIndex()+1+index, postParseItem));
                }
            }
            return d;
        }