//Different ways of generating a LexList public static void TestLexList1() { LexList lex1 = LexList.Get(@" public str Convert ( int i ) { return i.ToString() ; }" ); string s1 = lex1.CodeFormatText(false); string[] lines = new string[] { "public str Convert ( int i )", "{", " return i.ToString() ; ", "}" }; LexList lex2 = LexList.Get(lines); string s3 = "public str Convert(int i)\r\n{\r\n return i.ToString();\r\n}\r\n"; string s2 = lex2.CodeFormatText(false); if (s1 != s2 || s3 != s1) { MessageBox.Show("Error in TesetLexList1"); } }
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); } }
public static void ErrorHandlingUsingMessageBox() { LexToken.ShowError = (msg, theList) => { MessageBox.Show( msg + "\n" + theList.CodeFormat, "Error found"); }; LexList.Get("MoreText '"); // will produce an error here. }
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); }
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); }
//Setting up error handling. #if Dialog public static void ErrorHandlingUsingLexErrorDialog() { LexToken.ShowError = (msg, theList) => { new LexErrorDialog() { Message = msg, CompilerList = theList, }.Show(); }; LexList.Get("MoreText '"); // will produce an error here. }
public static void TestLexList3() { LexList lex1 = MakeTheMethod3("Var1"); LexList lex2 = LexList.Get(@"public int GetValue () { return Examples.Var1 ; }"); string s1 = lex1.CodeFormatText(false); string s2 = lex2.CodeFormatText(false); if (s1 != s2 || s1 != "public int GetValue()\r\n{\r\n return Examples.Var1;\r\n}\r\n") { MessageBox.Show("Error in TestLexList2"); } }
public static void TestLexList4() { LexList lex1 = MakeTheMethod4("A", "B"); LexList lex2 = LexList.Get( @"public int GetValue () { return Alpha.A + Alpha.B + Alpha.A ; }"); string s1 = lex1.CodeFormatText(false); string s2 = lex2.CodeFormatText(false); if (s1 != s2 || s1 != "public int GetValue()\r\n{\r\n return Alpha.A+Alpha.B+Alpha.A;\r\n}\r\n") { MessageBox.Show("Error in TestLexList2"); } }
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; } } }
static public void MakeClass1() { SetUpTypeParser(); MakeClass mc = new MakeClass(parser, LexList.Get(@" partial class Examples.TestClass { public int GetTwoTimesTheInt () { return TheInt * 2 ; } public void SetTheString ( string s ) { TheString = s ; } }")); Func <TestClass, int> GetTwoTimesTheInt; Action <TestClass, string> SetTheString; mc.GetFunc <TestClass, int>( "GetTwoTimesTheInt", out GetTwoTimesTheInt); mc.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("MakeClass1 error 2"); } }
public static void TestMakeMethod2() { SetUpTypeParser(); Action <int, double> act = MakeMethod <Action <int, double> > . Compile(parser, LexList.Get(@" public void TheMethod ( int i , double d ) { if (Examples.StrProperty != null && Examples.StrProperty != """") { Examples.StrProperty = Examples.StrProperty + "","" ; } Examples.StrProperty = Examples.StrProperty + i.ToString() + "","" + d.ToString() ; }" )); StrProperty = ""; act(1, 23.4); act(2, 45.23); act(99, 1.23); if (StrProperty != "1,23.4,2,45.23,99,1.23") { MessageBox.Show("Error in TestMakeMethod2"); } }
//Compiling a single method to produce a delegate public static void TestMakeMethod1() { SetUpTypeParser(); Func <int, string> fn = MakeMethod <Func <int, string> > . Compile(parser, LexList.Get(@" public string TheMethod ( int i ) { return i.ToString() ; }" )); string s = fn(123); if (s != "123") { MessageBox.Show("Error in TestMakeMethod1"); } List <int> listOfIntegers = new List <int>() { 99, 120, 4, 134, 18, 19, 200 }; List <string> list = (from i in listOfIntegers where i > 100 select fn(i) ).ToList(); if (list.Count != 3 || list[0] != "120" || list[1] != "134" || list[2] != "200") { MessageBox.Show("Error 2 in TestMakeMethod1"); } }
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"); } }
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"); } }
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); }
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(); }
static public LexList GetLexList(this IEnumerable <LexList> ie) { return(LexList.Get(ie)); }
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 "); } }
static public LexList GetLexList(this string s) { return(LexList.Get(s)); }