123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using NTERA.Core;
- namespace NTERA.Interpreter
- {
- public partial class Interpreter
- {
- protected Lexer Lexer { get; set; }
- private Token previousToken;
- private Token CurrentToken => Tokens.Current;
- public VariableDictionary Variables { get; } = new VariableDictionary();
- protected Dictionary<string, Marker> Loops { get; } = new Dictionary<string, Marker>();
- public delegate Value BasicFunction(List<Value> args);
- private readonly IConsole console;
- private Marker lineMarker;
- private bool exit;
- public Interpreter(IConsole console, string input)
- {
- this.console = console;
- Lexer = new Lexer(input);
- GenerateKeywordDictionary();
- GenerateFunctionDictionary();
- }
- private void Error(string text)
- {
- throw new Exception(text + " at line: " + lineMarker.Line);
- }
- protected bool TryAssertToken(Token tok, bool pullNext = true)
- {
- if (pullNext)
- GetNextToken();
- return CurrentToken == tok;
- }
- protected void AssertToken(Token tok, bool pullNext = true)
- {
- if (!TryAssertToken(tok, pullNext))
- Error("Expect " + tok + " got " + CurrentToken);
- }
- private IEnumerator<Token> Tokens;
- public void Exec()
- {
- exit = false;
- Tokens = Lexer.GetTokens().GetEnumerator();
- GetNextToken();
- while (!exit)
- Line();
- }
- protected Token GetNextToken()
- {
- previousToken = CurrentToken;
- Tokens.MoveNext();
- if (CurrentToken == Token.EOF && previousToken == Token.EOF)
- Error("Unexpected end of file");
-
- return CurrentToken;
- }
- private void Line()
- {
- while (CurrentToken == Token.NewLine)
- GetNextToken();
- if (CurrentToken == Token.EOF)
- {
- exit = true;
- return;
- }
- lineMarker = Lexer.TokenMarker;
- Statement();
- if (CurrentToken != Token.NewLine && CurrentToken != Token.EOF)
- Error("Expect new line got " + CurrentToken);
- }
- private void Statement()
- {
- Token keyword = CurrentToken;
- GetNextToken();
- if (KeywordMethods.ContainsKey(keyword))
- KeywordMethods[keyword]();
- else
- {
- switch (keyword)
- {
- case Token.Input:
- Input();
- break;
-
- case Token.Function:
- case Token.Dim:
- case Token.Const:
- while (GetNextToken() != Token.NewLine) { }
- break;
- case Token.Identifer:
- if (CurrentToken != Token.Equal && CurrentToken != Token.Colon)
- Error("Expected assignment got " + CurrentToken);
-
- Let();
- break;
- case Token.EOF:
- exit = true;
- break;
- default:
- Error("Expect keyword got " + keyword);
- break;
- }
- }
- }
- public Dictionary<string, BasicFunction> FunctionDictionary { get; protected set; }
- public Dictionary<Token, Action> KeywordMethods { get; set; }
- private void Input()
- {
- while (true)
- {
- AssertToken(Token.Identifer);
-
- if (!Variables.ContainsKey(Lexer.Identifer)) Variables.Add(Lexer.Identifer, new Value());
- console.WaitInput(new Core.Interop.InputRequest() { InputType = Core.Interop.InputType.StrValue });
- string input = console.LastInput;
- if (double.TryParse(input, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var d))
- Variables[Lexer.Identifer] = new Value(d);
- else
- Variables[Lexer.Identifer] = new Value(input);
-
- if (!TryAssertToken(Token.Comma))
- break;
- GetNextToken();
- }
- }
- private static readonly Dictionary<Token, int> OrderOfOps = new Dictionary<Token, int>
- {
- { Token.Or, 0 }, { Token.And, 0 },
- { Token.Equal, 1 }, { Token.NotEqual, 1 },
- { Token.Less, 1 }, { Token.More, 1 }, { Token.LessEqual, 1 }, { Token.MoreEqual, 1 },
- { Token.Plus, 2 }, { Token.Minus, 2 },
- { Token.Asterisk, 3 }, {Token.Slash, 3 },
- { Token.Caret, 4 }
- };
- private Value RealExpression()
- {
- return Expression(Lexer, Tokens);
- }
- private Value RealExpression(string input)
- {
- return Expression(new Lexer(input));
- }
- private Value StringExpression()
- {
- Lexer.Type = LexerType.String;
- GetNextToken();
- var result = Expression(Lexer, Tokens, LexerType.String);
- Lexer.Type = LexerType.Real;
- return result;
- }
- private Value Expression(Lexer lexer, IEnumerator<Token> enumerator = null, LexerType type = LexerType.Real)
- {
- Stack<Value> stack = new Stack<Value>();
- Stack<Token> operators = new Stack<Token>();
- if (enumerator == null)
- {
- enumerator = lexer.GetTokens().GetEnumerator();
- enumerator.MoveNext();
- }
- void Operation(Token token)
- {
- Value b = stack.Pop();
- Value a = stack.Pop();
- Value result = a.Operate(b, token);
- stack.Push(result);
- }
- int i = 0;
- while (true)
- {
- if (enumerator.Current == Token.Value)
- {
- stack.Push(lexer.Value);
- }
- else if (enumerator.Current == Token.Identifer)
- {
- if (Variables.ContainsKey(lexer.Identifer))
- {
- stack.Push(Variables[lexer.Identifer]);
- }
- else if (FunctionDictionary.ContainsKey(lexer.Identifer))
- {
- string name = lexer.Identifer;
- List<Value> args = new List<Value>();
- enumerator.MoveNext();
- if (enumerator.Current != Token.LParen)
- Error($"Was expecting [LParen] got [{enumerator.Current}]");
- while (enumerator.MoveNext() && enumerator.Current != Token.RParen)
- {
- args.Add(Expression(lexer, enumerator, type));
- if (enumerator.Current != Token.Comma)
- break;
- }
- stack.Push(FunctionDictionary[name](args));
- }
- else
- {
- if (type == LexerType.String)
- stack.Push(lexer.Identifer);
- else
- Error("Undeclared variable " + lexer.Identifer);
- }
- }
- else if (enumerator.Current == Token.LParen)
- {
- enumerator.MoveNext();
- stack.Push(Expression(lexer, enumerator, type));
- if (enumerator.Current != Token.RParen)
- Error($"Was expecting [LParen] got [{enumerator.Current}]");
- }
- else if (type == LexerType.Real && enumerator.Current.IsArithmetic()
- && enumerator.Current.IsUnary() && (i == 0 || previousToken == Token.LParen))
- {
- stack.Push(0);
- operators.Push(enumerator.Current);
- }
- else if (type == LexerType.String && enumerator.Current.IsStringOp()
- || type == LexerType.Real && enumerator.Current.IsArithmetic())
- {
- while (operators.Count > 0 && OrderOfOps[enumerator.Current] <= OrderOfOps[operators.Peek()])
- Operation(operators.Pop());
- operators.Push(enumerator.Current);
- }
- else
- {
- if (i == 0)
- Error("Empty expression");
- break;
- }
- i++;
- enumerator.MoveNext();
- }
- while (operators.Count > 0)
- Operation(operators.Pop());
- return type == LexerType.String
- ? stack.Aggregate((a, b) => b.String + a.String)
- : stack.Pop();
- }
- }
- }
|