Beispiel #1
0
        public void DoStatement <T>(T instance, ReadOnlyCollection <string> input, bool promoteQuotes)
        {
            LexListBuilder ll = new LexListBuilder();
            Action <T>     act;

            ll.AddAndPromoteQuotes(@"
          partial class `Type 
          {
            public void DoStatement () { ", "Type", typeof(T));
            foreach (string line in input)
            {
                if (promoteQuotes)
                {
                    ll.AddAndPromoteQuotes(line);
                }
                else
                {
                    ll.Add(line);
                }
            }
            ll.AddAndPromoteQuotes("}}");
            AddMethodsAndFields(ll.ToLexList(), true).GetAction <T>("DoStatement", out act);
            try {
                act(instance);
            } catch (Exception ex) {
                ll.ToLexList().ThrowException("This did not run: " + ex.Message);
            }
        }
Beispiel #2
0
        public R DoExpression <T, R>(T instance, ReadOnlyCollection <string> input, bool promoteQuotes)
        {
            LexListBuilder ll = new LexListBuilder();
            Func <T, R>    fn;

            ll.AddAndPromoteQuotes(@" 
          partial class `Type
          {
            public `ReturnType `MethodName() { return ", "Type", typeof(T), "ReturnType", typeof(R), "MethodName", DoExpressionName);
            foreach (string line in input)
            {
                if (promoteQuotes)
                {
                    ll.AddAndPromoteQuotes(line);
                }
                else
                {
                    ll.Add(line);
                }
            }
            ll.AddAndPromoteQuotes(" ;  }}");
            AddMethodsAndFields(ll.ToLexList(), true).GetFunc <T, R>(DoExpressionName, out fn);
            try {
                return(fn(instance));
            } catch (Exception ex) {
                ll.ToLexList().ThrowException("This did not run: " + ex.Message);
                return(default(R));
            }
        }
        static LexList LexListGet(string s)
        {
            LexListBuilder lb = new LexListBuilder();

            lb.Add(s);
            return(lb.ToLexList());
        }
Beispiel #4
0
        private void AddMethod(ReadOnlyCollection <string> input)
        {
            LexListBuilder ll = new LexListBuilder();

            ll.Add("partial class `Type {", "Type", typeof(REPL));
            foreach (string line in input)
            {
                ll.Add(line);
            }
            ll.Add("}");
            Maker.AddMethodsAndFields(ll.ToLexList(), true);
        }
Beispiel #5
0
        private void MakeFieldInitialiser(List <CompileMethod> currentListOfMethods)
        {
            LexListBuilder lb = new LexListBuilder();

            lb.AddAndPromoteQuotes("public void FieldsInitialiser () { ");
            lb.AddAndPromoteQuotes("if (Fields == null) Fields = new List<object> () ;");
            foreach (var f in (from f in MadeFieldsDict orderby f.Value.Index select f))
            {
                Type theType = f.Value.VarType;
                lb.AddAndPromoteQuotes("`type `name ;  if (Fields.Count == `index) Fields.Add ( (object)`name ) ; ", "type", theType, "name", f.Value.VarName, "index", f.Value.Index);
                // The conditional 'if (Fields.Count == f.Value.Index)' bit means that this initialisation function can be called repeatedly, and only the new ones will be initialised.
            }
            lb.AddAndPromoteQuotes("}");
            LexList ll = lb.ToLexList();

            ll.CrosslinkBrackets();
            CompileNextMethodHeader(ll, true, currentListOfMethods);
        }
Beispiel #6
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("");
            }
        }