The lexer for mathematical expressions.
Inheritance: ILexer
コード例 #1
0
ファイル: TruthTablePresenter.cs プロジェクト: sys27/xFunc
        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);
            }
        }
コード例 #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)));
 }