org.snapscript.compile.assemble.ProgramLinker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap Show documentation
Show all versions of snap Show documentation
Dynamic scripting for the JVM
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;
}
}