Keywords.cs 9.0 KB

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