@@ -31,10 +31,12 @@ pub enum AstNode {
3131 operands : OperandsList ,
3232 } ,
3333}
34+
3435impl AstNode {
3536 fn debug_dump ( & self ) {
3637 self . debug_dump_impl ( 1 ) ;
3738 }
39+
3840 fn debug_dump_impl ( & self , depth : usize ) {
3941 for _ in 0 ..depth {
4042 print ! ( "\t " , ) ;
@@ -52,7 +54,7 @@ impl AstNode {
5254 operands,
5355 } => {
5456 println ! (
55- "Node( {} ) at #{} (evaluate -> {:?})" ,
57+ "Node( {} ) at #{} ( evaluate -> {:?} )" ,
5658 op_type,
5759 token_idx,
5860 self . evaluate( )
@@ -71,12 +73,14 @@ impl AstNode {
7173 operands,
7274 } )
7375 }
76+
7477 fn new_leaf ( token_idx : usize , value : & str ) -> Box < Self > {
7578 Box :: new ( Self :: Leaf {
7679 token_idx,
7780 value : value. into ( ) ,
7881 } )
7982 }
83+
8084 pub fn evaluate ( & self ) -> Result < String , String > {
8185 match self {
8286 Self :: Leaf { value, .. } => Ok ( value. clone ( ) ) ,
@@ -154,9 +158,27 @@ impl AstNode {
154158 } ,
155159 }
156160 }
161+
157162 pub fn operand_values ( & self ) -> Result < Vec < String > , String > {
158- if let Self :: Node { operands, .. } = self {
163+ if let Self :: Node {
164+ operands, op_type, ..
165+ } = self
166+ {
159167 let mut out = Vec :: with_capacity ( operands. len ( ) ) ;
168+ let mut operands = operands. iter ( ) ;
169+ // check the first value before `|`, stop evaluate and return directly if it is true.
170+ // push dummy to pass the check of `len() == 2`
171+ if op_type == "|" {
172+ if let Some ( value) = operands. next ( ) {
173+ let value = value. evaluate ( ) ?;
174+ out. push ( value. clone ( ) ) ;
175+ if value_as_bool ( & value) {
176+ out. push ( String :: from ( "dummy" ) ) ;
177+ return Ok ( out) ;
178+ }
179+ }
180+ }
181+
160182 for operand in operands {
161183 let value = operand. evaluate ( ) ?;
162184 out. push ( value) ;
@@ -240,6 +262,7 @@ fn ast_from_rpn(rpn: &mut TokenStack) -> Result<Box<AstNode>, String> {
240262 }
241263 }
242264}
265+
243266fn maybe_ast_node (
244267 token_idx : usize ,
245268 op_type : & str ,
@@ -503,13 +526,15 @@ fn prefix_operator_substr(values: &[String]) -> String {
503526fn bool_as_int ( b : bool ) -> u8 {
504527 u8:: from ( b)
505528}
529+
506530fn bool_as_string ( b : bool ) -> String {
507531 if b {
508532 "1" . to_string ( )
509533 } else {
510534 "0" . to_string ( )
511535 }
512536}
537+
513538fn value_as_bool ( s : & str ) -> bool {
514539 if s. is_empty ( ) {
515540 return false ;
0 commit comments