-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cs
42 lines (38 loc) · 1.04 KB
/
parser.cs
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
using System;
using System.Data;
using System.Linq.Expressions;
namespace Mathian
{
public class ExpressionParser
{
private readonly DataTable dataTable;
public ExpressionParser(DataTable dataTable)
{
this.dataTable = dataTable;
}
public Expression Parse(string expression)
{
try
{
return Expression.Constant(dataTable.Compute(expression, null), typeof(double));
}
catch
{
throw new ArgumentException("Invalid expression");
}
}
public Func<double> Compile(Expression expression)
{
try
{
var lambda = Expression.Lambda<Func<double>>(expression);
var compiled = lambda.Compile();
return compiled;
}
catch
{
throw new ArgumentException("Invalid expression");
}
}
}
}