Exemple #1
0
        static public void MakeClass3()
        {
            SetUpTypeParser();
            Func <TestClass, int>      GetTwoTimesTheInt;
            Action <TestClass, string> SetTheString;
            MakeClass mc = new MakeClass(parser, LexList.Get(@"
       partial class Examples.TestClass   
       {
         public int GetTwoTimesTheInt () 
         {
           return TheInt * 2 ;
         }

         public void SetTheString ( string s )
         {
           TheString = s ; 
         }
       }")).
                           GetFunc <TestClass, int>(
                "GetTwoTimesTheInt", out GetTwoTimesTheInt).
                           GetAction <TestClass, string>(
                "SetTheString", out SetTheString);

            TestClass tc = new TestClass();

            tc.TheInt = 34;
            int i = GetTwoTimesTheInt(tc);

            if (i != 68)
            {
                MessageBox.Show("MakeClass1 error 1");
            }
            SetTheString(tc, "New string value");
            if (tc.TheString != "New string value")
            {
                MessageBox.Show("MakeClass3 error 2");
            }


            Action <TestClass, string, int> SetStringAndInt;

            mc.AddMethodsAndFields(LexList.Get(@"
        partial class Examples.TestClass   
        {
          public void SetStringAndInt ( string s , int i ) 
          {
            TheInt = i ;
            SetTheString ( s ) ; 
          }
        }"), true).
            GetAction <TestClass, string, int>(
                "SetStringAndInt", out SetStringAndInt);

            SetStringAndInt(tc, "Hello", 777);
            if (tc.TheString != "Hello" || tc.TheInt != 777)
            {
                MessageBox.Show("MakeClass3 error 3");
            }


            mc.AddMethodsAndFields(LexList.Get(@"
         partial class Examples.TestClass   
         {
           public void SetStringAndInt ( string s , int i ) 
           {
             TheInt = i * 100 ;
             SetTheString ( s ) ; 
           }
         }"), true);

            SetStringAndInt(tc, "Goodbye", 11);
            if (tc.TheString != "Goodbye" || tc.TheInt != 1100)
            {
                MessageBox.Show("MakeClass3 error 4");
            }
        }
Exemple #2
0
        static public void MakeClass4()
        {
            SetUpTypeParser();
            Func <TestClass, int> GetIntValue;
            Action <TestClass>    Init;
            MakeClass             mc = new MakeClass(parser, LexList.Get(@"
       partial class Examples.TestClass   
       {
         public int LastIntValue  ; 
         public int GetIntValue ()  
         {
           LastIntValue = TheInt ; 
           return TheInt ; 
         }
       }")).
                                       GetFunc <TestClass, int>("GetIntValue", out GetIntValue).
                                       GetAction <TestClass>("FieldsInitialiser", out Init);

            TestClass tc1 = new TestClass();

            tc1.TheInt = 22;
            Init(tc1);
            int i = GetIntValue(tc1);

            // i is 22 and so is LastIntValue
            if (i != 22 || tc1.Fields == null || tc1.Fields.Count < 1 || tc1.Fields[0] == null || !(tc1.Fields[0] is int) || ((int)(tc1.Fields[0])) != 22)
            {
                MessageBox.Show("Error 1 in makeClass4 ");
            }
            tc1.TheInt = 33;
            int j = GetIntValue(tc1);

            // j is 33 and so is LastIntValue
            if (j != 33 || tc1.Fields == null || tc1.Fields.Count < 1 || tc1.Fields[0] == null || !(tc1.Fields[0] is int) || ((int)(tc1.Fields[0])) != 33)
            {
                MessageBox.Show("Error 2 in makeClass4 ");
            }
            TestClass tc2 = new TestClass();

            Init(tc2);
            tc2.TheInt = 100;
            int k = GetIntValue(tc2);

            // k is 100 and so is LastIntValue
            if (k != 100 || tc2.Fields == null || tc2.Fields.Count < 1 || tc2.Fields[0] == null || !(tc2.Fields[0] is int) || ((int)(tc2.Fields[0])) != 100)
            {
                MessageBox.Show("Error 3 in makeClass4 ");
            }

            Action <TestClass, string> AddString;

            mc.AddMethodsAndFields(LexList.Get(@"
        partial class Examples.TestClass 
        {
          public List<string> ListOfStrings ; 
          public void AddString ( string s ) 
          { 
            if (ListOfStrings == null) 
              ListOfStrings = new List<string> () ; 
            ListOfStrings.Add ( s ) ; 
          }
        }"), true).
            GetAction <TestClass, string>("AddString", out AddString);

            Init(tc1);
            AddString(tc1, "String One");
            AddString(tc1, "String Two");
            if (tc1.Fields == null || tc1.Fields.Count < 2 || tc1.Fields[1] == null || !(tc1.Fields[1] is List <string>) ||
                ((List <string>)(tc1.Fields[1])).Count != 2 ||
                ((List <string>)(tc1.Fields[1]))[0] != "String One" ||
                ((List <string>)(tc1.Fields[1]))[1] != "String Two"
                )
            {
                MessageBox.Show("Error 4 in makeClass4 ");
            }
        }
Exemple #3
0
        public string RestoreImage(string filename)
        {
            LexListBuilder allMethodsAndFields = new LexListBuilder();

            allMethodsAndFields.Add("partial class `Type {", "Type", typeof(REPL));
            if (filename == "")
            {
                foreach (var s in Directory.GetFiles(Path.GetDirectoryName(Persist.FileName), "*.cs"))
                {
                    Line(Path.GetFileNameWithoutExtension(s));
                }
                return("");
            }
            else
            {
                SaveImage("LastDeleted");
                Maker.Clear();
                Fields = new List <object>();

                List <REPLData.Packet> list = new List <REPLData.Packet>();
                try {
                    using (StreamReader sr = new StreamReader(CodeTextFileName(filename))) {
                        while (true)
                        {
                            if (sr.EndOfStream)
                            {
                                break;
                            }
                            List <string> user = ReadBlockOfLines(sr);
                            if (sr.EndOfStream)
                            {
                                break;
                            }
                            string        id   = sr.ReadLine();
                            REPLData.Kind kind = REPLData.Kind.Error;
                            switch (id)
                            {
                            case "Reply": kind = REPLData.Kind.Reply; break;

                            case "User": kind = REPLData.Kind.User; break;
                            }
                            if (sr.EndOfStream)
                            {
                                break;
                            }
                            List <string>   reply  = ReadBlockOfLines(sr);
                            REPLData.Packet packet = new REPLData.Packet()
                            {
                                Reply = reply, User = user, ReplyKind = kind
                            };
                            try {
                                switch (ExamineUserInput(user.AsReadOnly()))
                                {
                                case PacketAction.Method:
                                case PacketAction.Field:
                                    foreach (var s in user)
                                    {
                                        allMethodsAndFields.Add(s);
                                    }
                                    break;
                                }
                            } catch (Exception ex) {
                                packet.Reply = new List <string>()
                                {
                                    ex.Message
                                };
                            }
                            list.Add(packet);
                        }
                    }
                } catch (Exception ex) {
                    return("Failed: " + ex.Message);
                }
                ReplPanel.ChangeData(list);
                allMethodsAndFields.Add("}");
                Maker.AddMethodsAndFields(allMethodsAndFields.ToLexList(), true);

                MessageBox.Show(
                    "The image from '" + filename + "' has been restored.\n\n" +
                    "Please remember that only the methods and fields have been restored,\n" +
                    "the contents of the fields have not.\n" +
                    "So all fields are null or 0.", "NOTE!");
                return("");
            }
        }