@@ -9,25 +9,44 @@ public Evaluator(Ast ast)
99 Ast = ast ;
1010 }
1111
12- public List < bool [ ] > RunAll ( )
12+ public List < bool [ ] > EvaluateAll ( )
1313 {
1414 int numCombinations = ( int ) Math . Pow ( 2 , Ast . Variables . Count ) ;
1515 var table = new List < bool [ ] > ( ) ;
1616
1717 for ( int i = 0 ; i < numCombinations ; i ++ )
1818 {
1919 var binary = Convert . ToString ( i , 2 ) . PadLeft ( Ast . Variables . Count , '0' ) ;
20- var row = new bool [ Ast . Variables . Count + 1 ] ;
20+ var values = binary . Select ( c => c == '1' ) . ToArray ( ) ;
2121
22- for ( int j = 0 ; j < Ast . Variables . Count ; j ++ )
23- {
24- row [ j ] = binary [ j ] == '1' ;
25- }
22+ var result = Evaluate ( Ast . Root , values ) ;
23+ table . Add ( values . Append ( result ) . ToArray ( ) ) ;
2624 }
25+
26+ return table ;
2727 }
2828
29- private bool Evaluate ( Node root , bool [ ] values )
29+ private bool Evaluate ( Node node , bool [ ] values )
3030 {
31-
31+ if ( node is VariableNode var )
32+ {
33+ var index = Ast . Variables . IndexOf ( var . Name ) ;
34+ return values [ index ] ;
35+ }
36+ else if ( node is OperatorNode op )
37+ {
38+ var left = Evaluate ( op . Left , values ) ;
39+ var right = Evaluate ( op . Right ! , values ) ;
40+
41+ if ( node is AndOperatorNode ) return left && right ;
42+ if ( node is OrOperatorNode ) return left || right ;
43+ if ( node is NotOperatorNode ) return ! left ;
44+ }
45+ else
46+ {
47+ throw new Exception ( $ "Unknown node type: { node . GetType ( ) } ") ;
48+ }
49+
50+ throw new Exception ( "This should never happen" ) ;
3251 }
3352}
0 commit comments