private void UpdateResult()
        {
            this.Invoke(new Action(() =>
            {
                dgvErrors.Rows.Clear();

                Assembly = new MathFuncAssemblyCecil();
                Assembly.Init();

                var variable = string.IsNullOrEmpty(tbVar.Text) ? null : new VarNode(tbVar.Text.ToLowerInvariant());

                string input            = tbInput.Text.Replace(Environment.NewLine, "");
                MathFunc simplifiedFunc = null;
                try
                {
                    simplifiedFunc        = new MathFunc(input, tbVar.Text).Simplify();
                    tbSimplification.Text = simplifiedFunc.ToString();
                    tbSimplifiedOpt.Text  = simplifiedFunc.GetPrecompilied().ToString();
                }
                catch (Exception ex)
                {
                    dgvErrors.Rows.Add(string.Empty, ex.Message);
                    //foreach (var error in Helper.Parser.Errors)
                    //	dgvErrors.Rows.Add(error.Position == null ? string.Empty : error.Position.Column.ToString(), error.Message);
                    tbSimplification.Text   = null;
                    tbSimplifiedOpt.Text    = null;
                    tbDerivative.Text       = null;
                    tbDerivativeOpt.Text    = null;
                    tbIlCode.Text           = null;
                    tbDerivativeIlCode.Text = null;
                }

                try
                {
                    var compileFunc = new MathFunc(input, tbVar.Text, true, true);
                    compileFunc.Compile(Assembly, "Func");

                    var sb = new StringBuilder();
                    compileFunc.Instructions.ToList().ForEach(instr => sb.AppendLine(instr.ToString().Replace("IL_0000: ", "")));
                    tbIlCode.Text = sb.ToString();
                }
                catch (Exception ex)
                {
                    dgvErrors.Rows.Add(string.Empty, ex.Message);
                    tbIlCode.Text = null;
                }

                if (tbSimplification.Text != string.Empty)
                {
                    MathFunc derivativeFunc = null;
                    try
                    {
                        derivativeFunc       = new MathFunc(input, tbVar.Text).GetDerivative();
                        tbDerivative.Text    = derivativeFunc.ToString();
                        tbDerivativeOpt.Text = derivativeFunc.GetPrecompilied().ToString();
                    }
                    catch (Exception ex)
                    {
                        dgvErrors.Rows.Add(string.Empty, ex.Message);
                        //foreach (var error in Helper.Parser.Errors)
                        //	dgvErrors.Rows.Add(error.Position == null ? string.Empty : error.Position.Column.ToString(), error.Message);
                        tbDerivative.Text       = null;
                        tbDerivativeOpt.Text    = null;
                        tbDerivativeIlCode.Text = null;
                    }

                    try
                    {
                        var compileDerivativeFunc = new MathFunc(tbDerivative.Text, tbVar.Text, true, true);
                        compileDerivativeFunc.Compile(Assembly, "FuncDerivative");
                        var sb = new StringBuilder();
                        compileDerivativeFunc.Instructions.ToList().ForEach(instr => sb.AppendLine(instr.ToString().Replace("IL_0000: ", "")));

                        tbDerivativeIlCode.Text = sb.ToString();
                    }
                    catch (Exception ex)
                    {
                        dgvErrors.Rows.Add(string.Empty, ex.Message);
                        tbDerivativeIlCode.Text = null;
                    }
                }
            }));
        }
Example #2
0
		private void UpdateResult()
		{
			this.Invoke(new Action(() =>
			{
				dgvErrors.Rows.Clear();

				Assembly = new MathFuncAssemblyCecil();
				Assembly.Init();

				var variable = string.IsNullOrEmpty(tbVar.Text) ? null : new VarNode(tbVar.Text.ToLowerInvariant());

				string input = tbInput.Text.Replace(Environment.NewLine, "");
				MathFunc simplifiedFunc = null;
				try
				{
					simplifiedFunc = new MathFunc(input, tbVar.Text).Simplify();
					tbSimplification.Text = simplifiedFunc.ToString();
					tbSimplifiedOpt.Text = simplifiedFunc.GetPrecompilied().ToString();
				}
				catch (Exception ex)
				{
					dgvErrors.Rows.Add(string.Empty, ex.Message);
					foreach (var error in Helper.Parser.Errors)
						dgvErrors.Rows.Add(error.Position == null ? string.Empty : error.Position.Column.ToString(), error.Message);
					tbSimplification.Text = null;
					tbSimplifiedOpt.Text = null;
					tbDerivative.Text = null;
					tbDerivativeOpt.Text = null;
					tbIlCode.Text = null;
					tbDerivativeIlCode.Text = null;
				}

				try
				{
					var compileFunc = new MathFunc(input, tbVar.Text, true, true);
					compileFunc.Compile(Assembly, "Func");

					var sb = new StringBuilder();
					compileFunc.Instructions.ToList().ForEach(instr => sb.AppendLine(instr.ToString().Replace("IL_0000: ", "")));
					tbIlCode.Text = sb.ToString();
				}
				catch (Exception ex)
				{
					dgvErrors.Rows.Add(string.Empty, ex.Message);
					tbIlCode.Text = null;
				}

				if (tbSimplification.Text != string.Empty)
				{
					MathFunc derivativeFunc = null;
					try
					{
						derivativeFunc = new MathFunc(input, tbVar.Text).GetDerivative();
						tbDerivative.Text = derivativeFunc.ToString();
						tbDerivativeOpt.Text = derivativeFunc.GetPrecompilied().ToString();
					}
					catch (Exception ex)
					{
						dgvErrors.Rows.Add(string.Empty, ex.Message);
						foreach (var error in Helper.Parser.Errors)
							dgvErrors.Rows.Add(error.Position == null ? string.Empty : error.Position.Column.ToString(), error.Message);
						tbDerivative.Text = null;
						tbDerivativeOpt.Text = null;
						tbDerivativeIlCode.Text = null;
					}

					try
					{
						var compileDerivativeFunc = new MathFunc(tbDerivative.Text, tbVar.Text, true, true);
						compileDerivativeFunc.Compile(Assembly, "FuncDerivative");
						var sb = new StringBuilder();
						compileDerivativeFunc.Instructions.ToList().ForEach(instr => sb.AppendLine(instr.ToString().Replace("IL_0000: ", "")));

						tbDerivativeIlCode.Text = sb.ToString();
					}
					catch (Exception ex)
					{
						dgvErrors.Rows.Add(string.Empty, ex.Message);
						tbDerivativeIlCode.Text = null;
					}
				}
			}));
		}