-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExtensions.cs
More file actions
81 lines (72 loc) · 3.63 KB
/
Extensions.cs
File metadata and controls
81 lines (72 loc) · 3.63 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
using StringMath.Expressions;
using System;
namespace StringMath
{
/// <summary>Helpful extension methods.</summary>
internal static class Extensions
{
/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="value"/> is null.</summary>
/// <typeparam name="T">The parameter's type.</typeparam>
/// <param name="value">The parameter's value.</param>
/// <param name="name">The parameter's name.</param>
public static void EnsureNotNull<T>(this T value, string name) where T : class
{
if (value == default)
{
throw new ArgumentNullException(name);
}
}
/// <summary>Converts a token type to a readable string.</summary>
/// <param name="tokenType">The token type.</param>
/// <returns>A readable string.</returns>
public static string ToReadableString(this TokenType tokenType)
{
return tokenType switch
{
TokenType.Identifier => "identifier",
TokenType.Number => "number",
TokenType.Operator => "operator",
TokenType.EndOfCode => "[EOC]",
TokenType.OpenParen => "(",
TokenType.CloseParen => ")",
TokenType.Exclamation => "!",
_ => tokenType.ToString(),
};
}
public static IExpression Parse(this string text, IMathContext context)
{
text.EnsureNotNull(nameof(text));
Tokenizer tokenizer = new Tokenizer(text, context);
Parser parser = new Parser(tokenizer, context);
return parser.Parse();
}
}
/// <summary>Extensions for <see cref="MathExpr"/>.</summary>
public static class MathExprExtensions
{
/// <summary>Converts a string expression to a <see cref="MathExpr"/>.</summary>
/// <param name="expr">The string to convert.</param>
/// <returns>A <see cref="MathExpr"/>.</returns>
public static MathExpr ToMathExpr(this string expr) => (MathExpr)expr;
/// <summary>Converts a string expression to a <see cref="MathExpr"/>.</summary>
/// <param name="expr">The string to convert.</param>
/// <param name="context">The context to use for the resulting expression.</param>
/// <returns>A <see cref="MathExpr"/>.</returns>
public static MathExpr ToMathExpr(this string expr, IMathContext context) => new MathExpr(expr, context);
/// <summary>Evaluates a math expression from a string.</summary>
/// <param name="value">The math expression.</param>
/// <returns>The result as a double.</returns>
public static double Eval(this string value) => value.ToMathExpr().Result;
/// <summary>Evaluates a math expression from a string in a given context.</summary>
/// <param name="value">The math expression.</param>
/// <param name="context">The context used to evaluate the expression.</param>
/// <returns>The result as a double.</returns>
public static double Eval(this string value, IMathContext context) => value.ToMathExpr(context).Result;
/// <summary>Converts a string to a <see cref="MathExpr"/> and substitutes the given variable.</summary>
/// <param name="expr">The math expression.</param>
/// <param name="var">The variable's name.</param>
/// <param name="val">The variable's value.</param>
/// <returns>A <see cref="MathExpr"/>.</returns>
public static MathExpr Substitute(this string expr, string var, double val) => expr.ToMathExpr().Substitute(var, val);
}
}