Example #1
0
        public void TestOverwritable()
        {
            FunctionRegistry registry = new FunctionRegistry(false);

            Func<double, double, double> testFunction1 = (a, b) => a * b;
            Func<double, double, double> testFunction2 = (a, b) => a * b;
            registry.RegisterFunction("test", testFunction1);
            registry.RegisterFunction("test", testFunction2);
        }
Example #2
0
        public void TestAddFunc2()
        {
            FunctionRegistry registry = new FunctionRegistry(false);

            Func<double, double, double> testFunction = (a, b) => a * b;
            registry.RegisterFunction("test", testFunction);

            FunctionInfo functionInfo = registry.GetFunctionInfo("test");

            Assert.IsNotNull(functionInfo);
            Assert.AreEqual("test", functionInfo.FunctionName);
            Assert.AreEqual(2, functionInfo.NumberOfParameters);
            Assert.AreEqual(testFunction, functionInfo.Function);
        }
Example #3
0
        public void TestNotOverwritable()
        {
            FunctionRegistry registry = new FunctionRegistry(false);

            Func<double, double, double> testFunction1 = (a, b) => a * b;
            Func<double, double, double> testFunction2 = (a, b) => a * b;

            registry.RegisterFunction("test", testFunction1, false);

            AssertExtensions.ThrowsException<Exception>(() =>
                {
                    registry.RegisterFunction("test", testFunction2, false);
                });
        }
Example #4
0
        private void calculateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClearScreen();

                string formula = formulaTextBox.Text;

                TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
                List<Token> tokens = reader.Read(formula);

                ShowTokens(tokens);

                IFunctionRegistry functionRegistry = new FunctionRegistry(false);

                AstBuilder astBuilder = new AstBuilder(functionRegistry);
                Operation operation = astBuilder.Build(tokens);

                ShowAbstractSyntaxTree(operation);

                Dictionary<string, double> variables = new Dictionary<string, double>();
                foreach (Variable variable in GetVariables(operation))
                {
                    double value = AskValueOfVariable(variable);
                    variables.Add(variable.Name, value);
                }

                IExecutor executor = new Interpreter();
                double result = executor.Execute(operation, null, variables);

                resultTextBox.Text = "" + result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }