Advertisement
Guest User

Untitled

a guest
May 27th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Irony.Parsing;
  6. using Irony.Interpreter.Ast;
  7.  
  8. namespace Irony.Interpreter.nullL
  9. {
  10. public class nullLangRuntime : LanguageRuntime
  11. {
  12. public nullLangRuntime(LanguageData language)
  13. : base(language)
  14. {
  15. }
  16. public override void Init()
  17. {
  18. base.Init();
  19. //add built-in methods, special form IIF, import Math and Environment methods
  20. BuiltIns.AddMethod(BuiltInPrintMethod, "print");
  21. BuiltIns.AddMethod(BuiltInAddMethod, "add");
  22. BuiltIns.AddMethod(BuiltInSubtractMethod, "subtract");
  23. BuiltIns.AddMethod(BuiltInMultiplyMethod, "multiply");
  24. BuiltIns.AddMethod(BuiltInDivideMethod, "divide");
  25. BuiltIns.AddMethod(BuiltInPowerMethod, "power");
  26. BuiltIns.AddMethod(BuiltInCalcLoopMethod, "calcloop");
  27. BuiltIns.AddSpecialForm(SpecialFormsLibrary.Iif, "if", 3, 3);
  28. BuiltIns.AddSpecialForm(SpecialFormsLibrary.loop, "loop", 2, 3);
  29. BuiltIns.ImportStaticMembers(typeof(System.Math));
  30. BuiltIns.ImportStaticMembers(typeof(Environment));
  31. }
  32.  
  33. //Built-in methods
  34. private object BuiltInPrintMethod(ScriptThread thread, object[] args)
  35. {
  36. string text = string.Empty;
  37. switch (args.Length)
  38. {
  39. case 1:
  40. text = string.Empty + args[0]; //compact and safe conversion ToString()
  41. break;
  42. case 0:
  43. break;
  44. default:
  45. text = string.Join(" ", args);
  46. break;
  47. }
  48. thread.App.WriteLine(text);
  49. return null;
  50. }
  51.  
  52.  
  53. private object BuiltInAddMethod(ScriptThread thread, object[] args)
  54. {
  55. if (args == null || args.Length <= 1) return null;
  56. double result = 0;
  57. try
  58. {
  59. result = Convert.ToDouble(args[0]);
  60. }
  61. catch (FormatException e)
  62. {
  63. Console.WriteLine("Input string is not a sequence of digits.");
  64. }
  65. catch (OverflowException e)
  66. {
  67. Console.WriteLine("The number cannot fit in an double.");
  68. }
  69. double num = 0;
  70. for (int i = 1; i < args.Length; i++)
  71. {
  72. try
  73. {
  74. num = Convert.ToDouble(args[i]);
  75. }
  76. catch (FormatException e)
  77. {
  78. Console.WriteLine("Input string is not a sequence of digits.");
  79. }
  80. catch (OverflowException e)
  81. {
  82. Console.WriteLine("The number cannot fit in an double.");
  83. }
  84. result += num;
  85. }
  86. return result;
  87. }
  88.  
  89. private object BuiltInSubtractMethod(ScriptThread thread, object[] args)
  90. {
  91. if (args == null || args.Length <= 1) return null;
  92. double result=0;
  93. try
  94. {
  95. result = Convert.ToDouble(args[0]);
  96. }
  97. catch (FormatException e)
  98. {
  99. Console.WriteLine("Input string is not a sequence of digits.");
  100. }
  101. catch (OverflowException e)
  102. {
  103. Console.WriteLine("The number cannot fit in an double.");
  104. }
  105. double num = 0;
  106. for (int i = 1; i < args.Length; i++)
  107. {
  108. try
  109. {
  110. num = Convert.ToDouble(args[i]);
  111. }
  112. catch (FormatException e)
  113. {
  114. Console.WriteLine("Input string is not a sequence of digits.");
  115. }
  116. catch (OverflowException e)
  117. {
  118. Console.WriteLine("The number cannot fit in an double.");
  119. }
  120. result -= num;
  121. }
  122. return result;
  123. }
  124.  
  125. private object BuiltInMultiplyMethod(ScriptThread thread, object[] args)
  126. {
  127. if (args == null || args.Length <= 1) return null;
  128. double result = 0;
  129. try
  130. {
  131. result = Convert.ToDouble(args[0]);
  132. }
  133. catch (FormatException e)
  134. {
  135. Console.WriteLine("Input string is not a sequence of digits.");
  136. }
  137. catch (OverflowException e)
  138. {
  139. Console.WriteLine("The number cannot fit in an double.");
  140. }
  141. double num = 0;
  142. for (int i = 1; i < args.Length; i++)
  143. {
  144. try
  145. {
  146. num = Convert.ToDouble(args[i]);
  147. }
  148. catch (FormatException e)
  149. {
  150. Console.WriteLine("Input string is not a sequence of digits.");
  151. }
  152. catch (OverflowException e)
  153. {
  154. Console.WriteLine("The number cannot fit in an double.");
  155. }
  156. if (num == 0) return 0;
  157. result *= num;
  158. }
  159. return result;
  160. }
  161.  
  162. private object BuiltInDivideMethod(ScriptThread thread, object[] args)
  163. {
  164. if (args == null || args.Length <= 1) return null;
  165. double first = 0;
  166. try
  167. {
  168. first = Convert.ToDouble(args[0]);
  169. }
  170. catch (FormatException e)
  171. {
  172. Console.WriteLine("Input string is not a sequence of digits.");
  173. }
  174. catch (OverflowException e)
  175. {
  176. Console.WriteLine("The number cannot fit in an double.");
  177. }
  178.  
  179. if (first == 0) return 0;
  180.  
  181. double divider = 1;
  182. double num = 0;
  183.  
  184. for (int i = 1; i < args.Length; i++)
  185. {
  186. try
  187. {
  188. num = Convert.ToDouble(args[i]);
  189. }
  190. catch (FormatException e)
  191. {
  192. Console.WriteLine("Input string is not a sequence of digits.");
  193. }
  194. catch (OverflowException e)
  195. {
  196. Console.WriteLine("The number cannot fit in an double.");
  197. }
  198.  
  199. if (num == 0)
  200. {
  201. Console.Write("Cannot divide by zero.");
  202. }
  203. divider *= num;
  204. }
  205.  
  206. var result = first / divider;
  207.  
  208. return result;
  209. }
  210.  
  211. private object BuiltInCalcLoopMethod(ScriptThread thread, object[] args)
  212. {
  213. if (args == null || args.Length <= 3) return null;
  214.  
  215. int iterations = 0;
  216. string actions = "";
  217. double result = 0;
  218. try
  219. {
  220. iterations = Convert.ToInt32(args[0]);
  221. actions = args[1] as string;
  222. result = Convert.ToDouble(args[2]);
  223. }
  224. catch (FormatException e)
  225. {
  226. Console.WriteLine("Bad formats. Required (int,string,double[]).");
  227. }
  228. if (!(actions.Length == 1 || actions.Length == args.Length - 3))
  229. {
  230. Console.WriteLine("Operators number should be equal to args-1 calcloop(x,\"+-\",args[a,b,c])");
  231. return -7;
  232. }
  233. int op = 0;
  234. double num = 0;
  235. for (int i = 0; i < iterations; i++)
  236. {
  237. for (int j = 3; j < args.Length; j++)
  238. {
  239. try
  240. {
  241. num = Convert.ToInt32(args[j]);
  242. }
  243. catch (FormatException e)
  244. {
  245. return "Bad formats. Required (int,string,double[]).";
  246. }
  247. if (actions.Length != 1)
  248. op = j-3;
  249. if (actions[op] == '+')
  250. result += num;
  251. if (actions[op] == '-')
  252. result -= num;
  253. if (actions[op] == '*')
  254. result *= num;
  255. if (actions[op] == '/')
  256. result /= num;
  257. }
  258. }
  259. return result;
  260. }
  261.  
  262. private object BuiltInPowerMethod(ScriptThread thread, object[] args)
  263. {
  264. if (args == null || args.Length != 2) return null;
  265. double x = 0;
  266. double y = 0;
  267. try
  268. {
  269. x = Convert.ToDouble(args[0]);
  270. y = Convert.ToDouble(args[1]);
  271. }
  272. catch (FormatException e)
  273. {
  274. Console.WriteLine("Bad formats. Required (double, double).");
  275. }
  276. if (x == 0) return 0;
  277. if (y == 0) return 1;
  278.  
  279. double result = 1;
  280. for (int i = 0; i < y; i++ )
  281. {
  282. result *= x;
  283. }
  284. return result;
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement