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

org.snapscript.tree.variable.ModulePointer Maven / Gradle / Ivy

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

import java.util.concurrent.atomic.AtomicReference;

import org.snapscript.core.Module;
import org.snapscript.core.Scope;
import org.snapscript.core.State;
import org.snapscript.core.Type;
import org.snapscript.core.Value;
import org.snapscript.core.ValueType;
import org.snapscript.core.property.Property;
import org.snapscript.core.property.PropertyValue;

public class ModulePointer implements VariablePointer {
   
   private final AtomicReference reference;
   private final ObjectPointer pointer;
   private final String name;
   
   public ModulePointer(String name) {
      this.reference = new AtomicReference();
      this.pointer = new ObjectPointer(name);
      this.name = name;
   }
   
   @Override
   public Value get(Scope scope, Module left) {
      Property property = reference.get();
      
      if(property == null) {
         Scope inner = left.getScope();
         State state = inner.getState();
         Value value = state.get(name);
         
         if(value == null) {
            Type type = left.getType(name);
           
            if(type != null) {
               return ValueType.getTransient(type);
            }
            Property match = pointer.match(scope, left);
            
            if(match != null) {
               reference.set(match);
               return new PropertyValue(match, left, name);
            }
         }
         return value;
      } 
      return new PropertyValue(property, left, name);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy