A: 

Ruby provides that capability through the eval function.

> eval("(1 == 1) && (1 > 0)")
=> true

I'm not sure about .net though.

Stephen L
Clearly stated he was on the .NET platform
Joel
Wouldn't "eval" be subject to some injection attack carnage? Running arbitrary user-supplied code seems like a bad idea.
Chris Farmer
@Chris: Depends on where the input comes from, but, yes, in general "eval" should be used with great caution.
David Thornley
@David, yeah... the context here is implying that "eval" is easy, and it's only really easy if you neglect validating the user input!
Chris Farmer
You can run system processes to call ruby from .net, assuming ruby is installed on the end-user's system.
Stefan Kendall
A: 

I don't know of any libraries to make this easier, but you really just have two subproblems here. You need to build an infix to postfix converter, then write a basic calculator for the boolean and math operations.

Once you have your boolean tree/stack built, begin performing operations. If you have anything that's not a number, evaluate it by sending the string/expression to the arithmetic calculator which performs infix->postfix conversion and then returns a value.

If you google "infix to postfix" and "stack rpn calculator", you can probably find more resources.

Stefan Kendall
If you can shell out to a language with "eval", however, the problem is solved. You look for true or false, and for everything else, you know you're invalid.
Stefan Kendall
I think "eval" is entirely the wrong way to go. It's potentially easy, but it's risky letting people write any code that's legal in your language. It's better, IMHO, to have a distinct and limited grammar that's available for these expressions.
Chris Farmer
A: 

You may be able to use the dotMath library to do this.

Tim S. Van Haren
+2  A: 

http://www.antlr.org

Antlr grammars can be designed to allow for both parsing and evaluation.

Here's an example: http://www.antlr.org/wiki/display/ANTLR3/Expression+evaluator

Stephen Caldwell
+1 for ANTLR. If you look at this and dismiss it, thinking that this is too much hassle, please reconsider. I recommend you use ANTLRworks as a grammar development tool and have it output its lexer and parser classes into your Visual Studio project tree. It's relatively seamless and it's easy to iteratively tweak your grammar and quickly see its effects in your .NET world.
Chris Farmer
By "you" above, I mean Thomas the OP.
Chris Farmer
@Chris Farmer: This is targetting C# Compact Framework...might be a bit heavy for that...
tommieb75
I dunno. With a relatively simple expression grammar, the generated lexer and parser will be pretty small. It's worth a shot, IMO. It'll at least be easy for Thomas to realize this up-front if it's not going to work, so there won't be too much time wasted if it doesn't work out.
Chris Farmer
A: 

Here's an excellent evaluation parser on Codeproject, that uses the eval method and does not rely on CodeDOM or anything like that. Here's an excellent article on building an expression evaluator using Antlr, also on the same site..

Hope this helps, Best regards, Tom.

tommieb75
A: 

This type of thing is F#'s bread and butter. You might give that a try. For parsing, use recursive descent, then you can run over the tree that results. If you have control of the input language, you can get by with a quote operation.

Joel
+1  A: 

Assuming you can change your syntax slightly, let an embedded database do the work for you with a query like this T-SQL:

select case when <Expression> then 1 else 0 end as Result

Using your example:

select case when ((1 = 1) and (1 > 0)) then 1 else 0 end as Result
select case when ((1 + 1 * 2) = 1) then 1 else 0 end as Result
Austin Salonen
According to the question, the expression is actually entered by the user. Thus your solution is prone to SQL injection.
Christian.K
A: 

Our project is using NCalc (with ANTLR underneath for lexing/parsing) and we're very happy with it.

NCalc is a mathematical expressions evaluator in .NET. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions.

Our application requires that it be cross-compiled for both Full and Compact Frameworks. With relatively simple tweaks we were able to make both NCalc and ANTLR work for both framework flavours.

Jason Morse