This is not a simple task. You're actually trying to convert user input into C# code - which is essentially the same thing the C# compiler does!
If you want to give it a shot, it is possible to leverage the C# compiler to do the parsing for you.
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Text;
using Microsoft.CSharp;
private static Func<double, double> CompileFunction(string expression)
{
StringBuilder sourceBuilder = new StringBuilder();
sourceBuilder.AppendLine("using System;");
sourceBuilder.AppendLine("using System.Linq.Expressions;");
sourceBuilder.AppendLine("class ExpressionGenerator{");
sourceBuilder.AppendLine("public static Func<double, double> Generate(){");
sourceBuilder.AppendLine("return x => " + expression + ";");
sourceBuilder.AppendLine("}}");
Dictionary<string, string> providerOptions = new Dictionary<string, string>();
providerOptions.Add("CompilerVersion", "v3.5");
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerParameters parameters = new CompilerParameters(new[] { "System.Core.dll" });
CompilerResults results = provider.CompileAssemblyFromSource(parameters, sourceBuilder.ToString());
return (Func<double, double>)results.CompiledAssembly.GetType("ExpressionGenerator").GetMethod("Generate").Invoke(null, null);
}
You can call this function like this:
string input = "x * x"; // Or however you get the input
Func<double, double> myFunction = CompileFunction(input);
You'll still need to handle things like syntax errors in the provided strings, etc.