Interpreter.cs 9.8 KB

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