org.snapscript.tree.define.ModuleDefinition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap-all Show documentation
Show all versions of snap-all Show documentation
Dynamic scripting for the JVM
package org.snapscript.tree.define;
import static org.snapscript.core.type.Phase.COMPILE;
import static org.snapscript.core.type.Phase.CREATE;
import static org.snapscript.core.type.Phase.DEFINE;
import java.util.concurrent.atomic.AtomicReference;
import org.snapscript.common.Progress;
import org.snapscript.core.Compilation;
import org.snapscript.core.Context;
import org.snapscript.core.Execution;
import org.snapscript.core.Statement;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.error.ErrorHandler;
import org.snapscript.core.module.Module;
import org.snapscript.core.module.Path;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.trace.Trace;
import org.snapscript.core.trace.TraceInterceptor;
import org.snapscript.core.trace.TraceStatement;
import org.snapscript.core.type.Phase;
import org.snapscript.tree.annotation.AnnotationList;
public class ModuleDefinition implements Compilation {
private final Statement definition;
public ModuleDefinition(AnnotationList annotations, ModuleName module, ModulePart... parts) {
this.definition = new CompileResult(annotations, module, parts);
}
@Override
public Statement compile(Module module, Path path, int line) throws Exception {
Context context = module.getContext();
ErrorHandler handler = context.getHandler();
TraceInterceptor interceptor = context.getInterceptor();
Trace trace = Trace.getDefine(module, path, line);
return new TraceStatement(interceptor, handler, definition, trace);
}
private static class CompileResult extends Statement {
private final AtomicReference reference;
private final ModuleBuilder builder;
private final Statement body;
public CompileResult(AnnotationList annotations, ModuleName module, ModulePart... parts) {
this.builder = new ModuleBuilder(annotations, module);
this.reference = new AtomicReference();
this.body = new ModuleBody(parts);
}
@Override
public void create(Scope scope) throws Exception {
Module module = builder.create(scope);
Progress progress = module.getProgress();
Scope inner = module.getScope();
try {
reference.set(module);
body.create(inner);
} finally {
progress.done(CREATE);
}
}
@Override
public boolean define(Scope scope) throws Exception {
Module module = reference.get();
Progress progress = module.getProgress();
Scope inner = module.getScope();
try {
body.define(inner); // must be module scope
} finally {
progress.done(DEFINE);
}
return true;
}
@Override
public Execution compile(Scope scope, Constraint returns) throws Exception {
Module module = reference.get();
Progress progress = module.getProgress();
Scope inner = module.getScope();
Scope local = inner.getStack();
try {
return body.compile(local, null); // must be module scope
} finally {
progress.done(COMPILE);
}
}
}
}