Interpreter.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.Collections.Generic;
  3. using NTERA.Core;
  4. namespace NTERA.Interpreter
  5. {
  6. public partial class Interpreter
  7. {
  8. protected Lexer Lexer { get; set; }
  9. private Token prevToken;
  10. private Token lastToken;
  11. public VariableDictionary Variables { get; } = new VariableDictionary();
  12. protected Dictionary<string, Marker> Loops { get; } = new Dictionary<string, Marker>();
  13. public delegate Value BasicFunction(List<Value> args);
  14. private readonly IConsole console;
  15. private int ifcounter;
  16. private Marker lineMarker;
  17. private bool exit;
  18. public Interpreter(IConsole console, string input)
  19. {
  20. this.console = console;
  21. Lexer = new Lexer(input);
  22. ifcounter = 0;
  23. GenerateKeywordDictionary();
  24. GenerateFunctionDictionary();
  25. }
  26. private void Error(string text)
  27. {
  28. throw new Exception(text + " at line: " + lineMarker.Line);
  29. }
  30. protected bool TryAssertToken(Token tok, bool pullNext = true)
  31. {
  32. if (pullNext)
  33. GetNextToken();
  34. return lastToken == tok;
  35. }
  36. protected void AssertToken(Token tok, bool pullNext = true)
  37. {
  38. if (!TryAssertToken(tok, pullNext))
  39. Error("Expect " + tok + " got " + lastToken);
  40. }
  41. private IEnumerator<Token> tokens;
  42. public void Exec()
  43. {
  44. exit = false;
  45. tokens = Lexer.GetTokens().GetEnumerator();
  46. GetNextToken();
  47. while (!exit)
  48. Line();
  49. }
  50. protected Token GetNextToken()
  51. {
  52. prevToken = lastToken;
  53. tokens.MoveNext();
  54. lastToken = tokens.Current;
  55. if (lastToken == Token.EOF && prevToken == Token.EOF)
  56. Error("Unexpected end of file");
  57. return lastToken;
  58. }
  59. private void Line()
  60. {
  61. while (lastToken == Token.NewLine)
  62. GetNextToken();
  63. if (lastToken == Token.EOF)
  64. {
  65. exit = true;
  66. return;
  67. }
  68. lineMarker = Lexer.TokenMarker;
  69. Statement();
  70. if (lastToken != Token.NewLine && lastToken != Token.EOF)
  71. Error("Expect new line got " + lastToken);
  72. }
  73. private void Statement()
  74. {
  75. while (true)
  76. {
  77. Token keyword = lastToken;
  78. GetNextToken();
  79. if (KeywordMethods.ContainsKey(keyword))
  80. KeywordMethods[keyword]();
  81. else
  82. {
  83. switch (keyword)
  84. {
  85. case Token.Input:
  86. Input();
  87. break;
  88. case Token.Function:
  89. case Token.Global:
  90. case Token.Dim:
  91. case Token.Const:
  92. while (GetNextToken() != Token.NewLine) { }
  93. break;
  94. case Token.Identifer:
  95. if (lastToken == Token.Equal)
  96. Let();
  97. else
  98. Error("Expected assignment got " + lastToken);
  99. break;
  100. case Token.EOF:
  101. exit = true;
  102. break;
  103. default:
  104. Error("Expect keyword got " + keyword);
  105. break;
  106. }
  107. }
  108. if (lastToken == Token.Colon)
  109. {
  110. GetNextToken();
  111. continue;
  112. }
  113. break;
  114. }
  115. }
  116. public Dictionary<string, BasicFunction> FunctionDictionary { get; protected set; }
  117. public Dictionary<Token, Action> KeywordMethods { get; set; }
  118. private void Input()
  119. {
  120. while (true)
  121. {
  122. AssertToken(Token.Identifer);
  123. if (!Variables.ContainsKey(Lexer.Identifer)) Variables.Add(Lexer.Identifer, new Value());
  124. console.WaitInput(new Core.Interop.InputRequest() { InputType = Core.Interop.InputType.StrValue });
  125. string input = console.LastInput;
  126. double d;
  127. if (double.TryParse(input, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d))
  128. Variables[Lexer.Identifer] = new Value(d);
  129. else
  130. Variables[Lexer.Identifer] = new Value(input);
  131. if (!TryAssertToken(Token.Comma))
  132. break;
  133. GetNextToken();
  134. }
  135. }
  136. private static readonly Dictionary<Token, int> OrderOfOps = new Dictionary<Token, int>
  137. {
  138. { Token.Or, 0 }, { Token.And, 0 },
  139. { Token.Equal, 1 }, { Token.NotEqual, 1 },
  140. { Token.Less, 1 }, { Token.More, 1 }, { Token.LessEqual, 1 }, { Token.MoreEqual, 1 },
  141. { Token.Plus, 2 }, { Token.Minus, 2 },
  142. { Token.Asterisk, 3 }, {Token.Slash, 3 },
  143. { Token.Caret, 4 }
  144. };
  145. private Value RealExpression()
  146. {
  147. Stack<Value> stack = new Stack<Value>();
  148. Stack<Token> operators = new Stack<Token>();
  149. void Operation(Token token)
  150. {
  151. Value b = stack.Pop();
  152. Value a = stack.Pop();
  153. Value result = a.Operate(b, token);
  154. stack.Push(result);
  155. }
  156. int i = 0;
  157. while (true)
  158. {
  159. if (lastToken == Token.Value)
  160. {
  161. stack.Push(Lexer.Value);
  162. }
  163. else if (lastToken == Token.Identifer)
  164. {
  165. if (Variables.ContainsKey(Lexer.Identifer))
  166. {
  167. stack.Push(Variables[Lexer.Identifer]);
  168. }
  169. else if (FunctionDictionary.ContainsKey(Lexer.Identifer))
  170. {
  171. string name = Lexer.Identifer;
  172. List<Value> args = new List<Value>();
  173. GetNextToken();
  174. AssertToken(Token.LParen, false);
  175. while (!TryAssertToken(Token.RParen))
  176. {
  177. args.Add(RealExpression());
  178. if (lastToken != Token.Comma)
  179. break;
  180. }
  181. stack.Push(FunctionDictionary[name](args));
  182. }
  183. else
  184. {
  185. Error("Undeclared variable " + Lexer.Identifer);
  186. }
  187. }
  188. else if (lastToken == Token.LParen)
  189. {
  190. GetNextToken();
  191. stack.Push(RealExpression());
  192. AssertToken(Token.RParen, false);
  193. }
  194. else if (lastToken.IsArithmetic())
  195. {
  196. if (lastToken.IsUnary() && (i == 0 || prevToken == Token.LParen))
  197. {
  198. stack.Push(0);
  199. operators.Push(lastToken);
  200. }
  201. else
  202. {
  203. while (operators.Count > 0 && OrderOfOps[lastToken] <= OrderOfOps[operators.Peek()])
  204. Operation(operators.Pop());
  205. operators.Push(lastToken);
  206. }
  207. }
  208. else
  209. {
  210. if (i == 0)
  211. Error("Empty expression");
  212. break;
  213. }
  214. i++;
  215. GetNextToken();
  216. }
  217. while (operators.Count > 0)
  218. Operation(operators.Pop());
  219. return stack.Pop();
  220. }
  221. private Value StringExpression()
  222. {
  223. foreach (var t in Lexer.ReturnAsLine(Token.Unknown)) { }
  224. lastToken = Token.NewLine;
  225. return Lexer.Value;
  226. }
  227. }
  228. }