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

org.snapscript.tree.script.ScriptPackage Maven / Gradle / Ivy

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

import java.util.concurrent.atomic.AtomicBoolean;

import org.snapscript.core.InternalStateException;
import org.snapscript.core.Result;
import org.snapscript.core.Scope;
import org.snapscript.core.Statement;

public class ScriptPackage extends Statement {

   private final Statement[] statements;
   private final AtomicBoolean execute;
   private final AtomicBoolean compile;
   private final AtomicBoolean define;
   
   public ScriptPackage(Statement... statements) {
      this.execute = new AtomicBoolean(true);
      this.compile = new AtomicBoolean(true);
      this.define = new AtomicBoolean(true);
      this.statements = statements;
   }
   
   @Override
   public Result define(Scope scope) throws Exception {
      Result last = Result.getNormal();
      
      if(define.compareAndSet(true, false)) {
         for(Statement statement : statements) {
            Result result = statement.define(scope);
            
            if(!result.isNormal()){
               throw new InternalStateException("Illegal statement");
            }
            last = result;
         }
      }
      return last;
   }
   
   @Override
   public Result compile(Scope scope) throws Exception {
      Result last = Result.getNormal();
      
      if(compile.compareAndSet(true, false)) {
         for(Statement statement : statements) {
            Result result = statement.compile(scope);
            
            if(!result.isNormal()){
               throw new InternalStateException("Illegal statement");
            }
            last = result;
         }
      }
      return last;
   }
   
   @Override
   public Result execute(Scope scope) throws Exception {
      Result last = Result.getNormal();
      
      if(execute.compareAndSet(true, false)) {
         for(Statement statement : statements) {
            Result result = statement.execute(scope);
            
            if(!result.isNormal()){
               throw new InternalStateException("Illegal statement");
            }
            last = result;
         }
      }
      return last;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy