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

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

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

import static org.snapscript.core.result.Result.NORMAL;

import java.util.List;

import org.snapscript.core.Compilation;
import org.snapscript.core.Execution;
import org.snapscript.core.InternalStateException;
import org.snapscript.core.ModifierType;
import org.snapscript.core.NameFormatter;
import org.snapscript.core.NoExecution;
import org.snapscript.core.Statement;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.function.Function;
import org.snapscript.core.module.Module;
import org.snapscript.core.module.Path;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.type.Type;

public class ImportStatic implements Compilation {   
   
   private final Qualifier qualifier;    
   
   public ImportStatic(Qualifier qualifier) {
      this.qualifier = qualifier;
   }

   @Override
   public Object compile(Module module, Path path, int line) throws Exception {
      String location = qualifier.getLocation();
      String target = qualifier.getTarget();
      String name = qualifier.getName();
      
      return new CompileResult(location, target, name);
   }
   
   private static class CompileResult extends Statement {
      
      private final NameFormatter formatter;  
      private final Execution execution;    
      private final String location;
      private final String target;
      private final String prefix;
      
      public CompileResult(String location, String target, String prefix) {
         this.execution = new NoExecution(NORMAL);
         this.formatter = new NameFormatter();        
         this.location = location;
         this.target = target;
         this.prefix = prefix;
      }
      
      @Override
      public boolean define(Scope scope) throws Exception {
         Module module = scope.getModule();
         String parent = formatter.formatFullName(location, target);
         Type type = module.getType(parent); // this is a type name
         
         if(type == null) {
            throw new InternalStateException("Could not import '" + parent + "'");
         }
         List methods = type.getFunctions();
         List functions = module.getFunctions();
         
         for(Function method : methods){
            int modifiers = method.getModifiers();
            
            if(ModifierType.isStatic(modifiers) && ModifierType.isPublic(modifiers)){
               String name = method.getName();
               
               if(prefix == null || prefix.equals(name)) {
                  functions.add(method);
               }
            }
         }
         return true;
      }
      
      @Override
      public Execution compile(Scope scope, Constraint returns) throws Exception {
         return execution;
      }
      
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy