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

org.snapscript.compile.assemble.ProgramLinker Maven / Gradle / Ivy

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

import static org.snapscript.tree.Instruction.SCRIPT_PACKAGE;

import java.util.concurrent.Executor;

import org.snapscript.common.Cache;
import org.snapscript.common.LeastRecentlyUsedCache;
import org.snapscript.core.Context;
import org.snapscript.core.Path;
import org.snapscript.core.link.Package;
import org.snapscript.core.link.PackageLinker;

public class ProgramLinker implements PackageLinker {
   
   private final Cache cache;
   private final PackageBuilder builder;  
   
   public ProgramLinker(Context context, Executor executor) {
      this.cache = new LeastRecentlyUsedCache();
      this.builder = new PackageBuilder(context, executor);
   }
   
   @Override
   public Package link(Path path, String source) throws Exception {
      return link(path, source, SCRIPT_PACKAGE.name);
   }
   
   @Override
   public Package link(Path path, String source, String grammar) throws Exception {
      Package linked = cache.fetch(path);
      
      if(linked == null) {
         linked = builder.create(path, source, grammar); 
         cache.cache(path, linked);
      }
      return linked; 
   } 
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy