The lexer for mathematical expressions.
Inheritance: ILexer
Esempio n. 1
0
        public void Generate(string strExp)
        {
            lexer = new Lexer();

            var tokens = lexer.Tokenize(strExp);

            expression = parser.Parse(tokens);
            if (!expression.ResultType.HasFlagNI(ExpressionResultType.Boolean))
                throw new NotSupportedException();

            expressions = Helpers.ConvertExpressionToCollection(expression);
            parameters = Helpers.GetParameters(tokens);
            table = new List<TruthTableRowViewModel>();

            var parametersCount = parameters.Count();
            for (int i = (int)Math.Pow(2, parametersCount) - 1; i >= 0; i--)
            {
                SetBits(i, parametersCount);

                var b = (bool)expression.Execute(parameters);

                var row = new TruthTableRowViewModel(parametersCount, expressions.Count());

                row.Index = (int)Math.Pow(2, parametersCount) - i;
                for (int j = 0; j < parametersCount; j++)
                    row.VarsValues[j] = (bool)parameters[parameters.ElementAt(j).Key];

                for (int j = 0; j < expressions.Count() - 1; j++)
                    row.Values[j] = (bool)expressions.ElementAt(j).Execute(parameters);

                if (expressions.Count() != 0)
                    row.Result = b;

                table.Add(row);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Parses the specified function.
 /// </summary>
 /// <param name="function">The function.</param>
 /// <returns>The parsed expression.</returns>
 /// <exception cref="ArgumentNullException"><paramref name="function"/> is null.</exception>
 /// <exception cref="ParserException">Error while parsing.</exception>
 public IExpression Parse(string function)
 {
     return(Parser.Parse(Lexer.Tokenize(function)));
 }