Interpreter.cs 7.8 KB

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