Namespace Designpattern. Interpreter
{
Public Abstract Class Expression
{
Public Abstract BoolInterpret ();
}
Public Class Constant: Expression
{
Private Bool M_val;
Public Constant ( Bool Val)
{
This. M_val=Val;
}
Public Override Bool Interpret ()
{
Return This. M_val;
}
Public Override String Tostring ()
{
Return This. M_val.tostring ();
}
}
Public Class And: Expression
{
Public Expression left, right;
Public And (expression left, expression right)
{
This. Left=Left;
This. Right=Right;
}
Public Override Bool Interpret ()
{
ReturnLeft. interpret ()&&Right. interpret ();
}
Public Override String Tostring ()
{
Return " ( " + This . Left. tostring () + " && " + This . Right. tostring () + " ) " ;
}
}
Public Class Or: Expression
{
Public Expression left, right;
Public Or (expression left, expression right)
{
This. Left=Left;
This. Right=Right;
}
Public Override Bool Interpret ()
{
ReturnLeft. interpret ()|Right. interpret ();
}
Public Override String Tostring ()
{
Return " ( " + This . Left. tostring () + " | " + This . Right. tostring () + " ) " ;
}
}
Public Class Not: Expression
{
Private Expression exp;
Public Not (expression exp)
{
This. Exp=Exp;
}
Public Override Bool Interpret ()
{
Return !Exp. interpret ();
}
Public Override String Tostring ()
{
Return "(!"+Exp. tostring ()+")";
}
}
Public Class Client
{
Public Static Void Main ()
{
Expression exp = New Or ( New And ( New Constant ( True ), New Constant ( False )),
New And ( New Constant ( True ), New Not ( New Constant ( False ))));
Console. writeline (exp. tostring () + " = " + Exp. interpret ());
}
}
}