-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (87 loc) · 3.2 KB
/
Program.cs
File metadata and controls
98 lines (87 loc) · 3.2 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Diagnostics;
using System.Linq;
using LatexProcessing2.Parsing;
/**
* This parser uses a highly recursive-descent method.
* Binary operators are parsed first and the "terms" (operands) are parsed
* seperately. Terms are broken into pieces (2abc = 2 * a * b * c) and
* recombined into a single Expression unit.
*
* It is designed to be as high performance as possible, both in compiling
* and in the result it produces. It is roughly < ~1000 total lines.
*/
namespace LatexProcessing2
{
internal class Program
{
public static void Main(string[] args)
{
while (true)
{
try
{
// get the expression from console in
Console.Out.Write("Expression > ");
string expression = Console.ReadLine();
var latexFunc = Compile(expression);
// print parameters
var detectedParams = string.Join(", ", latexFunc.Parameters);
Console.Out.WriteLine($"Detected {latexFunc.Parameters.Length} Parameter(s): {detectedParams}");
// get parameters from console in
double[] executionParams;
while (true)
if (GetParams(out executionParams, latexFunc.Parameters.Length))
break;
// print the answer
var answer = latexFunc.Call(executionParams);
Console.Out.WriteLine("answer = {0}", answer);
}
catch (Exception e)
{
Console.Out.WriteLine(e);
}
finally
{
Console.Out.WriteLine("");
}
}
}
private static LatexFunction Compile(string expression)
{
// measure the time to parse & compile
var stopwatch = new Stopwatch();
stopwatch.Start();
var latexFunc = LatexFunction.CompileFrom(expression);
stopwatch.Stop();
Console.Out.WriteLine("Parsed and compiled in {0}ms", stopwatch.ElapsedMilliseconds);
return latexFunc;
}
private static bool GetParams(out double[] foundParams, int requiredLength)
{
if (requiredLength == 0)
{
foundParams = Array.Empty<double>();
return true;
}
try
{
Console.Out.Write("Parameters > ");
var userParams = Console.ReadLine()?.Split(", ");
foundParams = (from str in userParams select double.Parse(str)).ToArray();
if (foundParams.Length != requiredLength)
{
Console.Out.WriteLine($"Expected {requiredLength} parameter(s).");
return false;
}
}
catch (Exception)
{
Console.Out.WriteLine("Parameters must be in form: a, b, c");
foundParams = null;
return false;
}
return true;
}
}
}