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"); } }
public static Func <int, bool> GetIntConditionFunction(string condition, string argName = "") { Func <int, bool> condFunc; string str; int outValue; if (condition == null || condition.Trim().Length == 0) { condFunc = new Func <int, bool>((x) => { return(true); //무조건 pass }); //기본함수.. } else if (int.TryParse(condition, out outValue)) { //simple function.. condFunc = new Func <int, bool>((x) => { if (x == outValue) { return(true); } else { return(false); } });//기본함수.. } else { TypeParser.DefaultParser = parser; //이때는 형식을 x: x==1 과 같은 식으로 썼을 때이다. 아니면 x==1과 같이 쓰면 된다. // x: x==1 에서 x값을 바꾸면 cond: cond==1 이라고 쓸 수 있다. //string[] tokens = condition.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); string name; string cond; if (argName.Equals("")) { name = "x"; int val; if (int.TryParse(condition, out val)) { cond = "x==" + condition; } else { cond = condition; } } else { name = argName; cond = condition; } if (cond.Length == 0) { cond = "true"; } str = @" bool Test (int " + name + @") { return " + cond + @"; }"; LexList lexList = LexListGet(str); condFunc = MakeMethod <Func <int, bool> > .Compile(parser, lexList); } return(condFunc); }
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 { { var s = ""; Func <int, string> fn = MakeMethod <Func <int, string> > .Compile(parser, LexListGet(@" string Test (int howMany) { string s = '' ; for ( int i = howMany ; i > 0 ; i -- ) s = s + i.ToString() + `~` ; return s ; }")); bool error = false; if (fn(0) != "") { error = true; } if (fn(1) != "1~") { error = true; } if (fn(2) != "2~1~") { error = true; } if (fn(3) != "3~2~1~") { error = true; } if (fn(-1) != "") { error = true; } if (error) { MessageBox.Show("There was an error", "Test Make with dialog"); } else { MessageBox.Show("Ran OK", "Test Make with dialog"); } } } catch (Exception ex) { MessageBox.Show("There was a compilation or execution error.", "Test Make with dialog"); } Persist.WriteToFile(); }
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 { { var s = ""; Func <int, string> fn = MakeMethod <Func <int, string> > .Compile(parser, LexListGet(@" string Test (int howMany) { string s = '' ; for ( int i = howMany ; i > 0 ; i -- ) s = s + i.ToString() + `~` ; return s ; }")); bool error = false; if (fn(0) != "") { error = true; } if (fn(1) != "1~") { error = true; } if (fn(2) != "2~1~") { error = true; } if (fn(3) != "3~2~1~") { error = true; } if (fn(-1) != "") { error = true; } if (error) { MessageBox.Show("There was an error", "Test Make with dialog"); } else { MessageBox.Show("Ran OK", "Test Make with dialog"); } } } catch (Exception ex) { MessageBox.Show("There was a compilation or execution error.", "Test Make with dialog"); } }