Пример #1
0
 private void ListAdd(string str, LexKind kind)
 {
     if (kind >= LexKind.Symbol00 && kind <= LexKind.Symbol11 && Expansions.ContainsKey(str))
     {
         object o;
         Expansions.TryGetValue(str, out o);
         if (o is string)
         {
             string so = o as string;
             o = LexList.Get(so);
         }
         if (o is LexList)
         {
             for (int i = 0; i < ((LexList)o).Count; i++)
             {
                 LexToken tok        = ((LexList)o)[i];
                 bool     toPrevious = (kind == LexKind.Symbol10 || kind == LexKind.Symbol11) && i == 0;
                 bool     toNext     = (kind == LexKind.Symbol01 || kind == LexKind.Symbol11) && i == ((LexList)o).Count - 1;
                 ListAddToken(toPrevious, new LexToken(tok), toNext);
             }
         }
         else
         {
             LexToken token = new LexToken(o, List, List.Count);
             List.Add(token);
         }
     }
     else
     {
         ListAddToken(kind == LexKind.Symbol10 || kind == LexKind.Symbol11, new LexToken(str, kind, List, List.Count), kind == LexKind.Symbol01 || kind == LexKind.Symbol11);
     }
 }
Пример #2
0
        private static List <LexToken> Get(ReadOnlyCollection <string> source)
        {
            List <LexToken> list = new List <LexToken>(source.Count);

            foreach (var s in source)
            {
                list.AddRange(LexList.Get(s));
            }
            return(list);
        }
Пример #3
0
        private static List <LexToken> Get(string[] source)
        {
            List <LexToken> list = new List <LexToken>(source.Length);

            foreach (var s in source)
            {
                list.AddRange(LexList.Get(s));
            }
            return(list);
        }
Пример #4
0
        private static void ExamineCurly(string line, ref int nesting, ref bool foundCurly)
        {
            LexList list = LexList.Get(line);

            foreach (var tok in list)
            {
                if (tok.Str == "{")
                {
                    nesting++;
                    foundCurly = true;
                }
                else if (tok.Str == "}")
                {
                    nesting--;
                    foundCurly = true;
                }
            }
        }
Пример #5
0
        private PacketAction ExamineUserInput(ReadOnlyCollection <string> input)
        {
            LexList ll = LexList.Get(input);

            if (ll.Count == 0)
            {
                return(PacketAction.Empty);
            }
            if (ll[0].Kind != LexKind.Delimiter && ll.Count == 1)
            {
                return(PacketAction.Expression);
            }
            Stack <char> nesting = new Stack <char>();

            foreach (LexToken tok in ll)
            {
                if (tok.Str == "(" || tok.Str == "[" || tok.Str == "{")
                {
                    nesting.Push(tok.Str[0]);
                }
                else if (tok.Str == ")")
                {
                    if (nesting.Count == 0 || nesting.Pop() != '(')
                    {
                        return(PacketAction.Empty);
                    }
                }
                else if (tok.Str == "]")
                {
                    if (nesting.Count == 0 || nesting.Pop() != '[')
                    {
                        return(PacketAction.Empty);
                    }
                }
                else if (tok.Str == "}")
                {
                    if (nesting.Count == 0 || nesting.Pop() != '{')
                    {
                        return(PacketAction.Empty);
                    }
                }
            }
            if (nesting.Count != 0)
            {
                return(PacketAction.Empty);
            }
            if (ll.Count < 2)
            {
                return(PacketAction.Empty);
            }
            string first = ll[0].Str;
            string last  = ll[ll.Count - 1].Str;

            if (first == "(" && last == ")")
            {
                return(PacketAction.Expression);
            }
            if (first == "{" && last == "}")
            {
                return(PacketAction.Statement);
            }
            if (CompileOneMethod.IsVarDeclaration(ll) && last == ";")
            {
                return(PacketAction.Field);
            }
            if (first == "public" || first == "private" || first == "[")
            {
                if (last == "}")
                {
                    return(PacketAction.Method);
                }
                return(PacketAction.Empty);
            }
            if (last == ";")
            {
                return(PacketAction.Statement);
            }
            if (last == "+" || last == "-" || last == "*" || last == "/" || last == "||" || last == "&&" || last == "!" || last == "^")
            {
                return(PacketAction.Empty);
            }
            return(PacketAction.Expression);
        }
Пример #6
0
 static public LexList GetLexList(this IEnumerable <LexList> ie)
 {
     return(LexList.Get(ie));
 }
Пример #7
0
 static public LexList GetLexList(this string s)
 {
     return(LexList.Get(s));
 }
Пример #8
0
        public static void Main()
        {
            LexToken.ShowError = (msg, theList) =>
            {
                MessageBox.Show(msg + "\n" + theList.CodeFormat, "Error found");
            };

            TypeParser parser = new TypeParser(Assembly.GetExecutingAssembly(), new List <string>()
            {
                "System",
                "System.Collections.Generic",
                "System.Linq",
                "System.Text",
                "System.Windows",
                "System.Windows.Shapes",
                "System.Windows.Controls",
                "System.Windows.Media",
                "System.IO",
                "System.Reflection",
                "Kamimu"
            }
                                               );

            TypeParser.DefaultParser = parser;


            try {
                {
                    Func <TestClass, int>     getLength;
                    Action <TestClass, int[]> setArray;
                    Action <TestClass>        actInit;
                    MakeClass mc = new MakeClass(parser, LexList.Get(@"
          partial class TestClass 
          {
            public int[] LocalInt ;
            public void SetArray ( int[] input ) 
            {
              LocalInt = input ; 
            }   
            public int GetLength () { return LocalInt.Length ; }
          }")).
                                   GetFunc <TestClass, int>("GetLength", out getLength).
                                   GetAction <TestClass, int[]>("SetArray", out setArray).
                                   GetAction <TestClass>("FieldsInitialiser", out actInit);
                    TestClass tc = new TestClass();
                    actInit(tc);
                    int[] thearray = new int[300];
                    setArray(tc, thearray);
                    if (getLength(tc) != thearray.Length)
                    {
                        MessageBox.Show("There was an error", "Test class with dialog");
                    }
                    else
                    {
                        MessageBox.Show("Ran OK", "Test class with dialog");
                    }
                }
            } catch (Exception ex) {
                MessageBox.Show("There was a compilation or execution error.", "Test class with dialog");
            }
        }
Пример #9
0
        public static void Main()
        {
            LexToken.ShowError = (msg, theList) =>
            {
                new LexErrorDialog()
                {
                    Message      = msg,
                    CompilerList = theList,
                }.Show();
            };

            TypeParser parser = new TypeParser(Assembly.GetExecutingAssembly(), new List <string>()
            {
                "System",
                "System.Collections.Generic",
                "System.Linq",
                "System.Text",
                "System.Windows",
                "System.Windows.Shapes",
                "System.Windows.Controls",
                "System.Windows.Media",
                "System.IO",
                "System.Reflection",
                "Kamimu"
            }
                                               );

            TypeParser.DefaultParser = parser;



            Directory.CreateDirectory(@"C:\KamimuCodeTemp");
            Persist.ReadFromFile(@"C:\KamimuCodeTemp\CsharpEvalConfiguration.xml");

            try {
                {
                    Func <TestClass, int>     getLength;
                    Action <TestClass, int[]> setArray;
                    Action <TestClass>        actInit;
                    MakeClass mc = new MakeClass(parser, LexList.Get(@"
          partial class TestClass 
          {
            public int[] LocalInt ;
            public void SetArray ( int[] input ) 
            {
              LocalInt = input ; 
            }   
            public int GetLength () { return LocalInt.Length ; }
          }")).
                                   GetFunc <TestClass, int>("GetLength", out getLength).
                                   GetAction <TestClass, int[]>("SetArray", out setArray).
                                   GetAction <TestClass>("FieldsInitialiser", out actInit);
                    TestClass tc = new TestClass();
                    actInit(tc);
                    int[] thearray = new int[300];
                    setArray(tc, thearray);
                    if (getLength(tc) != thearray.Length)
                    {
                        MessageBox.Show("There was an error", "Test class with dialog");
                    }
                    else
                    {
                        MessageBox.Show("Ran OK", "Test class with dialog");
                    }
                }
            } catch (Exception ex) {
                MessageBox.Show("There was a compilation or execution error.", "Test class with dialog");
            }

            Persist.WriteToFile();
        }