-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
46 lines (38 loc) · 1.32 KB
/
Program.cs
File metadata and controls
46 lines (38 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Runtime.InteropServices;
using SharpArena.Allocators;
using SharpArena.Collections;
using Spectre.Console;
AnsiConsole.MarkupLine("[bold yellow]--- SharpArena Simple Math Parser ---[/]");
AnsiConsole.MarkupLine("[dim]Enter a math expression or 'exit' to quit.[/]");
using var arena = new ArenaAllocator();
while (true)
{
var input = AnsiConsole.Ask<string>("[bold]>[/]");
if (input.Equals("exit", StringComparison.CurrentCultureIgnoreCase))
{
break;
}
try
{
arena.Reset();
var tokens = SimpleMathParser.ArenaMathParser.Tokenize(input, arena);
AnsiConsole.MarkupLine("\n[bold green]Tokens:[/]");
var table = new Table().AddColumn("Type").AddColumn("Value");
foreach (var t in tokens.AsSpan())
{
table.AddRow(t.Type.ToString(), t.GetValueSpan().ToString());
}
AnsiConsole.Write(table);
double result = SimpleMathParser.ArenaMathParser.Evaluate(tokens, arena);
AnsiConsole.MarkupLine($"\n[bold green]Result:[/] {result}");
}
catch (SimpleMathParser.SyntaxErrorException e)
{
AnsiConsole.MarkupLine($"[bold red]Syntax Error:[/] {e.Message}");
}
catch (Exception e)
{
AnsiConsole.WriteException(e);
}
AnsiConsole.MarkupLine("\n[dim]Arena reset for next input.[/]");
}