Keywords.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text.RegularExpressions;
  6. namespace NTERA.Interpreter
  7. {
  8. public partial class Interpreter
  9. {
  10. private void GenerateKeywordDictionary()
  11. {
  12. KeywordMethods = new Dictionary<Token, Action>();
  13. foreach (var method in typeof(Interpreter).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
  14. {
  15. var attribute = method.GetCustomAttributes(typeof(KeywordMethodAttribute), true).FirstOrDefault() as KeywordMethodAttribute;
  16. if (attribute == null)
  17. continue;
  18. KeywordMethods[attribute.Token] = () => method.Invoke(this, null);
  19. }
  20. }
  21. [KeywordMethod(Token.Print)]
  22. private void Print()
  23. {
  24. console.Write(Expr().ToString());
  25. }
  26. [KeywordMethod(Token.PrintL)]
  27. private void PrintL()
  28. {
  29. console.PrintSingleLine(Expr().ToString());
  30. }
  31. [KeywordMethod(Token.PrintImg)]
  32. private void PrintImg()
  33. {
  34. console.PrintImg(Expr().ToString().Trim().Trim('"'));
  35. }
  36. [KeywordMethod(Token.PrintButton)]
  37. private void PrintButton()
  38. {
  39. console.PrintButton(Expr().ToString(), 0);
  40. }
  41. private static readonly Regex FormRegex = new Regex("{(.*?)}");
  42. [KeywordMethod(Token.PrintFormL)]
  43. private void PrintFormL()
  44. {
  45. string rawString = Expr().ToString();
  46. var evaluator = new MatchEvaluator(match => Variables[match.Groups[1].Value].ToString());
  47. console.PrintSingleLine(FormRegex.Replace(rawString, evaluator));
  48. }
  49. [KeywordMethod(Token.DrawLine)]
  50. private void DrawLine()
  51. {
  52. console.PrintBar();
  53. }
  54. [KeywordMethod(Token.DrawLineForm)]
  55. private void DrawLineForm()
  56. {
  57. console.printCustomBar(Expr().ToString().Trim());
  58. }
  59. [KeywordMethod(Token.If)]
  60. private void If()
  61. {
  62. bool result = Expr().Real == 1;
  63. AssertToken(Token.Then, false);
  64. GetNextToken();
  65. if (result)
  66. {
  67. int i = ifcounter;
  68. while (true)
  69. {
  70. if (lastToken == Token.If)
  71. {
  72. i++;
  73. }
  74. else if (lastToken == Token.Else)
  75. {
  76. if (i == ifcounter)
  77. {
  78. GetNextToken();
  79. return;
  80. }
  81. }
  82. else if (lastToken == Token.EndIf)
  83. {
  84. if (i == ifcounter)
  85. {
  86. GetNextToken();
  87. return;
  88. }
  89. i--;
  90. }
  91. GetNextToken();
  92. }
  93. }
  94. }
  95. [KeywordMethod(Token.Else)]
  96. private void Else()
  97. {
  98. int i = ifcounter;
  99. while (true)
  100. {
  101. if (lastToken == Token.If)
  102. {
  103. i++;
  104. }
  105. else if (lastToken == Token.EndIf)
  106. {
  107. if (i == ifcounter)
  108. {
  109. GetNextToken();
  110. return;
  111. }
  112. i--;
  113. }
  114. GetNextToken();
  115. }
  116. }
  117. [KeywordMethod(Token.EndIf)]
  118. private void EndIf()
  119. {
  120. }
  121. [KeywordMethod(Token.End)]
  122. private void End()
  123. {
  124. exit = true;
  125. }
  126. [KeywordMethod(Token.Let)]
  127. private void Let()
  128. {
  129. if (lastToken != Token.Equal)
  130. {
  131. AssertToken(Token.Identifer, false);
  132. AssertToken(Token.Equal);
  133. }
  134. string id = lex.Identifer;
  135. GetNextToken();
  136. Variables[id] = Expr();
  137. }
  138. [KeywordMethod(Token.For)]
  139. private void For()
  140. {
  141. AssertToken(Token.Identifer, false);
  142. string var = lex.Identifer;
  143. AssertToken(Token.Equal);
  144. GetNextToken();
  145. Value v = Expr();
  146. if (loops.ContainsKey(var))
  147. {
  148. loops[var] = lineMarker;
  149. }
  150. else
  151. {
  152. Variables[var] = v;
  153. loops.Add(var, lineMarker);
  154. }
  155. AssertToken(Token.To, false);
  156. GetNextToken();
  157. v = Expr();
  158. if (Variables[var].Operate(v, Token.More).Real == 1)
  159. {
  160. while (true)
  161. {
  162. while (!(GetNextToken() == Token.Identifer && prevToken == Token.Next)) { }
  163. if (lex.Identifer == var)
  164. {
  165. loops.Remove(var);
  166. AssertToken(Token.NewLine);
  167. break;
  168. }
  169. }
  170. }
  171. }
  172. [KeywordMethod(Token.Next)]
  173. private void Next()
  174. {
  175. AssertToken(Token.Identifer, false);
  176. string var = lex.Identifer;
  177. Variables[var] = Variables[var].Operate(new Value(1), Token.Plus);
  178. lex.GoTo(new Marker(loops[var].Pointer - 1, loops[var].Line, loops[var].Column - 1));
  179. lastToken = Token.NewLine;
  180. }
  181. [KeywordMethod(Token.Times)]
  182. private void Times()
  183. {
  184. AssertToken(Token.Identifer, false);
  185. string var = lex.Identifer;
  186. AssertToken(Token.Comma);
  187. GetNextToken();
  188. var arg2 = Expr();
  189. Variables[var] = Variables[var].Operate(arg2, Token.Asterisk);
  190. AssertToken(Token.NewLine, false);
  191. }
  192. }
  193. }