123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- using System;
- using System.Collections.Generic;
- 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; internal set; } = new VariableDictionary();
- protected Dictionary<string, Marker> Loops { get; } = new Dictionary<string, Marker>();
- public delegate Value BasicFunction(List<Value> args);
- protected readonly IConsole Console;
- protected 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 ParserException(text, LineMarker);
- }
- 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);
- }
- protected IEnumerator<Token> Tokens;
- public void Exec()
- {
- exit = false;
- Tokens = Lexer.GetEnumerator();
- 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:
- 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 Value RealExpression()
- {
- return Lexer.Expression(this);
- }
- private Value RealExpression(string input)
- {
- return new Lexer(input).Expression(this);
- }
- private Value StringExpression()
- {
- Lexer.Type = LexerType.String;
- GetNextToken();
- var result = Lexer.Expression(this);
- Lexer.Type = LexerType.Real;
- return result;
- }
- }
- }
|