Keywords.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text.RegularExpressions;
  8. using NTERA.Core.Interop;
  9. namespace NTERA.Interpreter
  10. {
  11. public partial class Interpreter
  12. {
  13. private void GenerateKeywordDictionary()
  14. {
  15. KeywordMethods = new Dictionary<Token, Action>();
  16. foreach (var method in typeof(Interpreter).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
  17. {
  18. var attribute = method.GetCustomAttributes(typeof(KeywordMethodAttribute), true).FirstOrDefault() as KeywordMethodAttribute;
  19. if (attribute == null)
  20. continue;
  21. KeywordMethods[attribute.Token] = () => method.Invoke(this, null);
  22. }
  23. }
  24. #region Printing
  25. [KeywordMethod(Token.Print)]
  26. private void Print()
  27. {
  28. console.Write(RealExpression().ToString());
  29. }
  30. [KeywordMethod(Token.PrintL)]
  31. private void PrintL()
  32. {
  33. console.PrintSingleLine(RealExpression().ToString());
  34. }
  35. [KeywordMethod(Token.PrintHtml)]
  36. private void PrintHtml()
  37. {
  38. console.PrintHtml(RealExpression().ToString().Trim().Trim('"'), true);
  39. }
  40. [KeywordMethod(Token.PrintImg)]
  41. private void PrintImg()
  42. {
  43. console.PrintImg(RealExpression().ToString().Trim().Trim('"'));
  44. }
  45. [KeywordMethod(Token.PrintButton)]
  46. private void PrintButton()
  47. {
  48. console.PrintButton(RealExpression().ToString(), 0);
  49. }
  50. private static readonly Regex FormRegex = new Regex("{(.*?)}");
  51. [KeywordMethod(Token.PrintFormL)]
  52. private void PrintFormL()
  53. {
  54. string rawString = RealExpression().ToString();
  55. var evaluator = new MatchEvaluator(match => Variables[match.Groups[1].Value].ToString());
  56. console.PrintSingleLine(FormRegex.Replace(rawString, evaluator));
  57. }
  58. [KeywordMethod(Token.DrawLine)]
  59. private void DrawLine()
  60. {
  61. console.PrintBar();
  62. }
  63. [KeywordMethod(Token.DrawLineForm)]
  64. private void DrawLineForm()
  65. {
  66. console.printCustomBar(RealExpression().ToString().Trim());
  67. }
  68. [KeywordMethod(Token.Alignment)]
  69. private void Alignment()
  70. {
  71. AssertToken(Token.Value, false);
  72. console.Alignment = (DisplayLineAlignment)Enum.Parse(typeof(DisplayLineAlignment), Lexer.Value);
  73. GetNextToken();
  74. }
  75. [KeywordMethod(Token.SetColor)]
  76. private void SetColor()
  77. {
  78. if (TryAssertToken(Token.Identifer, false))
  79. {
  80. console.SetStringStyle(Color.FromName(Lexer.Value));
  81. }
  82. else
  83. {
  84. AssertToken(Token.Value, false);
  85. if (TryAssertToken(Token.Comma))
  86. {
  87. int r = (int)Lexer.Value.Real;
  88. AssertToken(Token.Value);
  89. int g = (int)Lexer.Value;
  90. AssertToken(Token.Comma);
  91. AssertToken(Token.Value);
  92. int b = (int)Lexer.Value;
  93. console.SetStringStyle(Color.FromArgb(r, g, b));
  94. }
  95. else
  96. {
  97. int hexResult = int.Parse(Lexer.Identifer.Substring(1), NumberStyles.HexNumber);
  98. int argb = (int)(hexResult | 0xFF000000);
  99. Color c = Color.FromArgb(argb);
  100. console.SetStringStyle(c);
  101. }
  102. }
  103. GetNextToken();
  104. }
  105. #endregion
  106. #region Control
  107. [KeywordMethod(Token.If)]
  108. private void If()
  109. {
  110. bool result = RealExpression().Real == 1;
  111. AssertToken(Token.Then, false);
  112. GetNextToken();
  113. if (result)
  114. {
  115. int i = ifcounter;
  116. while (true)
  117. {
  118. if (lastToken == Token.If)
  119. {
  120. i++;
  121. }
  122. else if (lastToken == Token.Else)
  123. {
  124. if (i == ifcounter)
  125. {
  126. GetNextToken();
  127. return;
  128. }
  129. }
  130. else if (lastToken == Token.EndIf)
  131. {
  132. if (i == ifcounter)
  133. {
  134. GetNextToken();
  135. return;
  136. }
  137. i--;
  138. }
  139. GetNextToken();
  140. }
  141. }
  142. }
  143. [KeywordMethod(Token.Else)]
  144. private void Else()
  145. {
  146. int i = ifcounter;
  147. while (true)
  148. {
  149. if (lastToken == Token.If)
  150. {
  151. i++;
  152. }
  153. else if (lastToken == Token.EndIf)
  154. {
  155. if (i == ifcounter)
  156. {
  157. GetNextToken();
  158. return;
  159. }
  160. i--;
  161. }
  162. GetNextToken();
  163. }
  164. }
  165. [KeywordMethod(Token.EndIf)]
  166. private void EndIf()
  167. {
  168. }
  169. [KeywordMethod(Token.End)]
  170. private void End()
  171. {
  172. exit = true;
  173. }
  174. [KeywordMethod(Token.Let)]
  175. private void Let()
  176. {
  177. if (lastToken != Token.Equal)
  178. {
  179. AssertToken(Token.Identifer, false);
  180. AssertToken(Token.Equal);
  181. }
  182. string id = Lexer.Identifer;
  183. var typeHint = Variables[id].Type;
  184. if (typeHint == ValueType.Real)
  185. {
  186. GetNextToken();
  187. Variables[id] = RealExpression();
  188. }
  189. else
  190. Variables[id] = StringExpression();
  191. }
  192. [KeywordMethod(Token.For)]
  193. private void For()
  194. {
  195. AssertToken(Token.Identifer, false);
  196. string var = Lexer.Identifer;
  197. AssertToken(Token.Equal);
  198. GetNextToken();
  199. Value v = RealExpression();
  200. if (Loops.ContainsKey(var))
  201. {
  202. Loops[var] = lineMarker;
  203. }
  204. else
  205. {
  206. Variables[var] = v;
  207. Loops.Add(var, lineMarker);
  208. }
  209. AssertToken(Token.To, false);
  210. GetNextToken();
  211. v = RealExpression();
  212. if (Variables[var].Operate(v, Token.More).Real == 1)
  213. {
  214. while (true)
  215. {
  216. while (!(GetNextToken() == Token.Identifer && prevToken == Token.Next)) { }
  217. if (Lexer.Identifer == var)
  218. {
  219. Loops.Remove(var);
  220. AssertToken(Token.NewLine);
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. [KeywordMethod(Token.Next)]
  227. private void Next()
  228. {
  229. AssertToken(Token.Identifer, false);
  230. string var = Lexer.Identifer;
  231. Variables[var] = Variables[var].Operate(new Value(1), Token.Plus);
  232. Lexer.GoTo(new Marker(Loops[var].Pointer - 1, Loops[var].Line, Loops[var].Column - 1));
  233. lastToken = Token.NewLine;
  234. }
  235. [KeywordMethod(Token.Times)]
  236. private void Times()
  237. {
  238. AssertToken(Token.Identifer, false);
  239. string var = Lexer.Identifer;
  240. AssertToken(Token.Comma);
  241. GetNextToken();
  242. var arg2 = RealExpression();
  243. Variables[var] = Variables[var].Operate(arg2, Token.Asterisk);
  244. }
  245. #endregion
  246. }
  247. }