org.snapscript.compile.assemble.ProgramLinker Maven / Gradle / Ivy
package org.snapscript.compile.assemble;
import static org.snapscript.tree.Instruction.SCRIPT_PACKAGE;
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) {
this.cache = new LeastRecentlyUsedCache();
this.builder = new PackageBuilder(context);
}
@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;
}
}