Interpreter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 prevToken;
  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. private readonly IConsole console;
  16. private int ifcounter;
  17. private Marker lineMarker;
  18. private bool exit;
  19. public Interpreter(IConsole console, string input)
  20. {
  21. this.console = console;
  22. Lexer = new Lexer(input);
  23. ifcounter = 0;
  24. GenerateKeywordDictionary();
  25. GenerateFunctionDictionary();
  26. }
  27. private void Error(string text)
  28. {
  29. throw new Exception(text + " at line: " + lineMarker.Line);
  30. }
  31. protected bool TryAssertToken(Token tok, bool pullNext = true)
  32. {
  33. if (pullNext)
  34. GetNextToken();
  35. return CurrentToken == tok;
  36. }
  37. protected void AssertToken(Token tok, bool pullNext = true)
  38. {
  39. if (!TryAssertToken(tok, pullNext))
  40. Error("Expect " + tok + " got " + CurrentToken);
  41. }
  42. private IEnumerator<Token> tokens;
  43. public void Exec()
  44. {
  45. exit = false;
  46. tokens = Lexer.GetTokens().GetEnumerator();
  47. GetNextToken();
  48. while (!exit)
  49. Line();
  50. }
  51. protected Token GetNextToken()
  52. {
  53. prevToken = CurrentToken;
  54. tokens.MoveNext();
  55. if (CurrentToken == Token.EOF && prevToken == Token.EOF)
  56. Error("Unexpected end of file");
  57. return CurrentToken;
  58. }
  59. private void Line()
  60. {
  61. while (CurrentToken == Token.NewLine)
  62. GetNextToken();
  63. if (CurrentToken == Token.EOF)
  64. {
  65. exit = true;
  66. return;
  67. }
  68. lineMarker = Lexer.TokenMarker;
  69. Statement();
  70. if (CurrentToken != Token.NewLine && CurrentToken != Token.EOF)
  71. Error("Expect new line got " + CurrentToken);
  72. }
  73. private void Statement()
  74. {
  75. Token keyword = CurrentToken;
  76. GetNextToken();
  77. if (KeywordMethods.ContainsKey(keyword))
  78. KeywordMethods[keyword]();
  79. else
  80. {
  81. switch (keyword)
  82. {
  83. case Token.Input:
  84. Input();
  85. break;
  86. case Token.Function:
  87. case Token.Dim:
  88. case Token.Const:
  89. while (GetNextToken() != Token.NewLine) { }
  90. break;
  91. case Token.Identifer:
  92. if (CurrentToken != Token.Equal && CurrentToken != Token.Colon)
  93. Error("Expected assignment got " + CurrentToken);
  94. Let();
  95. break;
  96. case Token.EOF:
  97. exit = true;
  98. break;
  99. default:
  100. Error("Expect keyword got " + keyword);
  101. break;
  102. }
  103. }
  104. }
  105. public Dictionary<string, BasicFunction> FunctionDictionary { get; protected set; }
  106. public Dictionary<Token, Action> KeywordMethods { get; set; }
  107. private void Input()
  108. {
  109. while (true)
  110. {
  111. AssertToken(Token.Identifer);
  112. if (!Variables.ContainsKey(Lexer.Identifer)) Variables.Add(Lexer.Identifer, new Value());
  113. console.WaitInput(new Core.Interop.InputRequest() { InputType = Core.Interop.InputType.StrValue });
  114. string input = console.LastInput;
  115. double d;
  116. if (double.TryParse(input, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d))
  117. Variables[Lexer.Identifer] = new Value(d);
  118. else
  119. Variables[Lexer.Identifer] = new Value(input);
  120. if (!TryAssertToken(Token.Comma))
  121. break;
  122. GetNextToken();
  123. }
  124. }
  125. private static readonly Dictionary<Token, int> OrderOfOps = new Dictionary<Token, int>
  126. {
  127. { Token.Or, 0 }, { Token.And, 0 },
  128. { Token.Equal, 1 }, { Token.NotEqual, 1 },
  129. { Token.Less, 1 }, { Token.More, 1 }, { Token.LessEqual, 1 }, { Token.MoreEqual, 1 },
  130. { Token.Plus, 2 }, { Token.Minus, 2 },
  131. { Token.Asterisk, 3 }, {Token.Slash, 3 },
  132. { Token.Caret, 4 }
  133. };
  134. private Value RealExpression()
  135. {
  136. return RealExpression(Lexer, tokens);
  137. }
  138. private Value RealExpression(string input)
  139. {
  140. return RealExpression(new Lexer(input));
  141. }
  142. private Value RealExpression(Lexer lexer, IEnumerator<Token> enumerator = null)
  143. {
  144. Stack<Value> stack = new Stack<Value>();
  145. Stack<Token> operators = new Stack<Token>();
  146. if (enumerator == null)
  147. {
  148. enumerator = lexer.GetTokens().GetEnumerator();
  149. enumerator.MoveNext();
  150. }
  151. void Operation(Token token)
  152. {
  153. Value b = stack.Pop();
  154. Value a = stack.Pop();
  155. Value result = a.Operate(b, token);
  156. stack.Push(result);
  157. }
  158. int i = 0;
  159. while (true)
  160. {
  161. if (enumerator.Current == Token.Value)
  162. {
  163. stack.Push(lexer.Value);
  164. }
  165. else if (enumerator.Current == Token.Identifer)
  166. {
  167. if (Variables.ContainsKey(lexer.Identifer))
  168. {
  169. stack.Push(Variables[lexer.Identifer]);
  170. }
  171. else if (FunctionDictionary.ContainsKey(lexer.Identifer))
  172. {
  173. string name = lexer.Identifer;
  174. List<Value> args = new List<Value>();
  175. enumerator.MoveNext();
  176. //AssertToken(Token.LParen, false);
  177. while (enumerator.MoveNext() && enumerator.Current != Token.RParen)
  178. {
  179. args.Add(RealExpression(lexer, enumerator));
  180. if (enumerator.Current != Token.Comma)
  181. break;
  182. }
  183. stack.Push(FunctionDictionary[name](args));
  184. }
  185. else
  186. {
  187. Error("Undeclared variable " + lexer.Identifer);
  188. }
  189. }
  190. else if (enumerator.Current == Token.LParen)
  191. {
  192. enumerator.MoveNext();
  193. stack.Push(RealExpression(lexer, enumerator));
  194. //AssertToken(Token.RParen, false);
  195. }
  196. else if (enumerator.Current.IsArithmetic())
  197. {
  198. if (enumerator.Current.IsUnary() && (i == 0 || prevToken == Token.LParen))
  199. {
  200. stack.Push(0);
  201. operators.Push(enumerator.Current);
  202. }
  203. else
  204. {
  205. while (operators.Count > 0 && OrderOfOps[enumerator.Current] <= OrderOfOps[operators.Peek()])
  206. Operation(operators.Pop());
  207. operators.Push(enumerator.Current);
  208. }
  209. }
  210. else
  211. {
  212. if (i == 0)
  213. Error("Empty expression");
  214. break;
  215. }
  216. i++;
  217. enumerator.MoveNext();
  218. }
  219. while (operators.Count > 0)
  220. Operation(operators.Pop());
  221. return stack.Pop();
  222. }
  223. private Value StringExpression()
  224. {
  225. Lexer.Type = LexerType.String;
  226. GetNextToken();
  227. var result = StringExpression(Lexer, tokens);
  228. Lexer.Type = LexerType.Real;
  229. return result;
  230. }
  231. private Value StringExpression(Lexer lexer, IEnumerator<Token> enumerator = null)
  232. {
  233. Stack<Value> stack = new Stack<Value>();
  234. Stack<Token> operators = new Stack<Token>();
  235. if (enumerator == null)
  236. {
  237. enumerator = lexer.GetTokens().GetEnumerator();
  238. enumerator.MoveNext();
  239. }
  240. void Operation(Token token)
  241. {
  242. Value b = stack.Pop();
  243. Value a = stack.Pop();
  244. Value result = a.Operate(b, token);
  245. stack.Push(result);
  246. }
  247. int i = 0;
  248. while (true)
  249. {
  250. if (enumerator.Current == Token.Value)
  251. {
  252. stack.Push(lexer.Value.String);
  253. }
  254. else if (enumerator.Current == Token.Identifer)
  255. {
  256. if (Variables.ContainsKey(lexer.Identifer))
  257. {
  258. stack.Push(Variables[lexer.Identifer]);
  259. }
  260. else if (FunctionDictionary.ContainsKey(lexer.Identifer))
  261. {
  262. string name = lexer.Identifer;
  263. List<Value> args = new List<Value>();
  264. enumerator.MoveNext();
  265. AssertToken(Token.LParen, false);
  266. while (!TryAssertToken(Token.RParen))
  267. {
  268. args.Add(StringExpression(lexer, enumerator));
  269. if (enumerator.Current != Token.Comma)
  270. break;
  271. }
  272. stack.Push(FunctionDictionary[name](args));
  273. }
  274. else
  275. {
  276. if (stack.Count > 0)
  277. operators.Push(Token.Plus);
  278. stack.Push(lexer.Identifer);
  279. }
  280. }
  281. else if (enumerator.Current == Token.LParen)
  282. {
  283. enumerator.MoveNext();
  284. stack.Push(StringExpression(lexer, enumerator));
  285. AssertToken(Token.RParen, false);
  286. }
  287. else if (enumerator.Current.IsStringOp())
  288. {
  289. while (operators.Count > 0 && OrderOfOps[enumerator.Current] <= OrderOfOps[operators.Peek()])
  290. Operation(operators.Pop());
  291. operators.Push(enumerator.Current);
  292. }
  293. else
  294. {
  295. if (i == 0)
  296. Error("Empty expression");
  297. break;
  298. }
  299. i++;
  300. enumerator.MoveNext();
  301. }
  302. while (operators.Count > 0)
  303. Operation(operators.Pop());
  304. return stack.Aggregate((a, b) => b.String + a.String);
  305. }
  306. }
  307. }