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

org.snapscript.tree.define.ModuleDefinition Maven / Gradle / Ivy

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

import static org.snapscript.core.Reserved.TYPE_THIS;

import java.util.concurrent.atomic.AtomicReference;

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.scope.State;
import org.snapscript.core.trace.Trace;
import org.snapscript.core.trace.TraceInterceptor;
import org.snapscript.core.trace.TraceStatement;
import org.snapscript.core.variable.Value;
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);
         Scope inner = module.getScope();
         
         reference.set(module);
         body.create(inner); 
      }
   
      @Override
      public boolean define(Scope scope) throws Exception {
         Module module = reference.get();
         Value value = Value.getTransient(module);
         Scope inner = module.getScope();
         State state = inner.getState();
         
         state.add(TYPE_THIS, value);
         body.define(inner); // must be module scope
         
         return true;
      }
      
      @Override
      public Execution compile(Scope scope, Constraint returns) throws Exception {
         Module module = reference.get();
         Scope inner = module.getScope();
         Scope local = inner.getStack();
         
         return body.compile(local, null); // must be module scope
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy