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

org.snapscript.tree.DeclarationAllocator Maven / Gradle / Ivy

package org.snapscript.tree;

import org.snapscript.core.Evaluation;
import org.snapscript.core.ModifierType;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.constraint.DeclarationConstraint;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.scope.index.Local;
import org.snapscript.core.type.Type;
import org.snapscript.core.variable.Value;

public class DeclarationAllocator {

   private final DeclarationConstraint constraint;
   private final DeclarationConverter converter;
   private final Evaluation expression;
   
   public DeclarationAllocator(Constraint constraint, Evaluation expression) {      
      this.constraint = new DeclarationConstraint(constraint);
      this.converter = new DeclarationConverter();
      this.expression = expression;
   }   
   
   public  T compile(Scope scope, String name, int modifiers) throws Exception {
      Type type = constraint.getType(scope);
      Constraint declare = constraint.getConstraint(scope, modifiers);
      
      if(expression != null) {
         Constraint object = expression.compile(scope, null);
         Type real = object.getType(scope);
         
         if(real != null) {
            object = converter.compile(scope, real, constraint, name);        
         }
         return assign(scope, name, null, declare, modifiers);
      }
      return declare(scope, name, declare, modifiers); // nothing assigned yet
   }
   
   
   public  T allocate(Scope scope, String name, int modifiers) throws Exception {
      Type type = constraint.getType(scope);
      Object object = null;
      
      if(expression != null) {
         Value value = expression.evaluate(scope, null);
         Object original = value.getValue();
         
         if(type != null) {
            object = converter.convert(scope, original, constraint, name);
         } else {
            object = original;
         }
         return assign(scope, name, object, constraint, modifiers);
      }
      return declare(scope, name, constraint, modifiers);
   }   
   
   protected  T declare(Scope scope, String name, Constraint type, int modifiers) throws Exception {
      if(ModifierType.isConstant(modifiers)) {
         return (T)Local.getConstant(null, name, type);
      }
      return (T)Local.getReference(null, name, type);
   }
   
   protected  T assign(Scope scope, String name, Object value, Constraint type, int modifiers) throws Exception {
      if(ModifierType.isConstant(modifiers)) {
         return (T)Local.getConstant(value, name, type);
      }
      return (T)Local.getReference(value, name, type);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy