All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.tree.condition.Combination Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.tree.condition;

import org.snapscript.core.BooleanValue;
import org.snapscript.core.Evaluation;
import org.snapscript.core.Scope;
import org.snapscript.core.Value;
import org.snapscript.parse.StringToken;

public class Combination extends Evaluation {
   
   private final ConditionalOperator operator;
   private final Evaluation right;
   private final Evaluation left;
   
   public Combination(Evaluation left) {
      this(left, null, null);
   }
   
   public Combination(Evaluation left, StringToken operator, Evaluation right) {
      this.operator = ConditionalOperator.resolveOperator(operator);
      this.right = right;
      this.left = left;
   }

   @Override
   public void compile(Scope scope) throws Exception { 
      left.compile(scope);
      right.compile(scope);
   }
   
   @Override
   public Value evaluate(Scope scope, Object context) throws Exception { 
      Value first = evaluate(scope, left);
      
      if(first == BooleanValue.TRUE) {
         if(operator != null) {
            if(operator.isAnd()) {
               return evaluate(scope, right);
            }
         }
      } else {
         if(operator != null) {
            if(operator.isOr()) {
               return evaluate(scope, right);
            }
         }
      }
      return first;
   }
   
   private Value evaluate(Scope scope, Evaluation evaluation) throws Exception { 
      Value value = evaluation.evaluate(scope, null);
      boolean result = value.getBoolean();
      
      if(result) {
         return BooleanValue.TRUE;
      }
      return BooleanValue.FALSE;
   } 
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy