Interpreter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // Interpreter.cs
  3. //
  4. // (C) 2008 Mainsoft, Inc. (http://www.mainsoft.com)
  5. // (C) 2008 db4objects, Inc. (http://www.db4o.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. using System.Linq;
  28. using System.Linq.Expressions;
  29. using System.Reflection;
  30. namespace System.Linq.jvm {
  31. class Interpreter {
  32. class VoidTypeMarker {
  33. }
  34. static readonly Type VoidMarker = typeof (VoidTypeMarker);
  35. static readonly MethodInfo [] delegates = new MethodInfo [5];
  36. LambdaExpression lambda;
  37. static Interpreter ()
  38. {
  39. var methods = from method in typeof (Interpreter).GetMethods (
  40. BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
  41. where method.Name == "GetDelegate"
  42. select method;
  43. foreach (var method in methods)
  44. delegates [method.GetGenericArguments ().Length - 1] = method;
  45. }
  46. public Interpreter (LambdaExpression lambda)
  47. {
  48. this.lambda = lambda;
  49. }
  50. public Delegate CreateDelegate ()
  51. {
  52. var types = GetGenericSignature ();
  53. var creator = delegates [types.Length - 1].MakeGenericMethod (types);
  54. return (Delegate) creator.Invoke (this, new object [0]);
  55. }
  56. public void Validate ()
  57. {
  58. new ExpressionValidator (lambda).Validate ();
  59. }
  60. Type [] GetGenericSignature ()
  61. {
  62. var count = lambda.Parameters.Count;
  63. var types = new Type [count + 1];
  64. var return_type = lambda.GetReturnType ();
  65. if (return_type == typeof (void))
  66. return_type = VoidMarker;
  67. types [count] = return_type;
  68. for (int i = 0; i < count; i++) {
  69. types [i] = lambda.Parameters [i].Type;
  70. }
  71. return types;
  72. }
  73. object Run (object [] arg)
  74. {
  75. return ExpressionInterpreter.Interpret (lambda, arg);
  76. }
  77. Delegate GetDelegate<TResult> ()
  78. {
  79. if (typeof (TResult) == VoidMarker)
  80. return new Action (ActionRunner);
  81. return new Func<TResult> (FuncRunner<TResult>);
  82. }
  83. TResult FuncRunner<TResult> ()
  84. {
  85. return (TResult) Run (new object [0]);
  86. }
  87. void ActionRunner ()
  88. {
  89. Run (new object [0]);
  90. }
  91. Delegate GetDelegate<T, TResult> ()
  92. {
  93. if (typeof (TResult) == VoidMarker)
  94. return new Action<T> (ActionRunner<T>);
  95. return new Func<T, TResult> (FuncRunner<T, TResult>);
  96. }
  97. TResult FuncRunner<T, TResult> (T arg)
  98. {
  99. return (TResult) Run (new object [] { arg });
  100. }
  101. void ActionRunner<T> (T arg)
  102. {
  103. Run (new object [] { arg });
  104. }
  105. Delegate GetDelegate<T1, T2, TResult> ()
  106. {
  107. if (typeof (TResult) == VoidMarker)
  108. return new Action<T1, T2> (ActionRunner<T1, T2>);
  109. return new Func<T1, T2, TResult> (FuncRunner<T1, T2, TResult>);
  110. }
  111. TResult FuncRunner<T1, T2, TResult> (T1 arg1, T2 arg2)
  112. {
  113. return (TResult) Run (new object [] { arg1, arg2 });
  114. }
  115. void ActionRunner<T1, T2> (T1 arg1, T2 arg2)
  116. {
  117. Run (new object [] { arg1, arg2 });
  118. }
  119. Delegate GetDelegate<T1, T2, T3, TResult> ()
  120. {
  121. if (typeof (TResult) == VoidMarker)
  122. return new Action<T1, T2, T3> (ActionRunner<T1, T2, T3>);
  123. return new Func<T1, T2, T3, TResult> (FuncRunner<T1, T2, T3, TResult>);
  124. }
  125. TResult FuncRunner<T1, T2, T3, TResult> (T1 arg1, T2 arg2, T3 arg3)
  126. {
  127. return (TResult) Run (new object [] { arg1, arg2, arg3 });
  128. }
  129. void ActionRunner<T1, T2, T3> (T1 arg1, T2 arg2, T3 arg3)
  130. {
  131. Run (new object [] { arg1, arg2, arg3 });
  132. }
  133. Delegate GetDelegate<T1, T2, T3, T4, TResult> ()
  134. {
  135. if (typeof (TResult) == VoidMarker)
  136. return new Action<T1, T2, T3, T4> (ActionRunner<T1, T2, T3, T4>);
  137. return new Func<T1, T2, T3, T4, TResult> (FuncRunner<T1, T2, T3, T4, TResult>);
  138. }
  139. TResult FuncRunner<T1, T2, T3, T4, TResult> (T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  140. {
  141. return (TResult) Run (new object [] { arg1, arg2, arg3, arg4 });
  142. }
  143. void ActionRunner<T1, T2, T3, T4> (T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  144. {
  145. Run (new object [] { arg1, arg2, arg3, arg4 });
  146. }
  147. }
  148. }