static void Main(string[] args) { string input; do { try { Console.Clear(); Console.Write("Zadajte vzorec:"); input = Console.ReadLine(); ILexer lexer = Setup.Instance.GetLexer(); ILexerContext lexerContext = Setup.Instance.CreateLexerContext(input); LexerResult lexerResult = lexer.Evaluate(lexerContext); IParser parser = Setup.Instance.GetParser(); IParserContext parserContext = Setup.Instance.CreateParserContext(lexerResult.PostfixNotation); ParserResult parserResult = parser.Evaluate(parserContext); Visitor evaluator = new Evaluator(Setup.Instance.CreateEvaluationContext()); Visitor differentiator = new Differentiator(SyntaxToken.Variable("x")); Console.WriteLine($"Vstup: {input}"); Console.WriteLine($"Výsledok: {evaluator.Visit(parserResult.Tree)}"); Console.WriteLine($"Derivácia: {differentiator.Visit(parserResult.Tree)}"); } catch (Exception ex) { Console.Clear(); Console.WriteLine("Vyskytla sa chyba!"); Console.WriteLine("Ohláste to prosím Maťovi."); Console.WriteLine(); Console.WriteLine("Za pochopenie ďakujeme!"); Console.WriteLine("Váš kalkulátor"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Chybová správa"); Console.WriteLine(ex.Message); } Console.ReadLine(); }while (true); }
public LexerResult Evaluate(ILexerContext input) { InputStream stream = input.Stream; List <LexicToken> tokens = new List <LexicToken>(); while (!stream.IsEmpty) { InputStream copy = stream; foreach (TokenReader reader in input.Readers) { LexicToken token = null; stream = reader.TryRead(stream, out token); if (token != null && !(token is WhitespaceLexicToken)) { if (token is NamedLexicToken) { NamedLexicToken nlt = (NamedLexicToken)token; if (input.IsFunction(nlt.Name)) { token = new FunctionLexicToken(nlt.Name); } else { token = new VariableLexicToken(nlt.Name); } } tokens.Add(token); break; } } if (stream == copy) { throw new ParserException($"Unexpected character '{stream.Content[0]}' at position {stream.Position}"); } } this.ValidateLexingOutput(tokens); return(new LexerResult(tokens)); }