Package jfun.yan.xml

Source Code of jfun.yan.xml.ModuleBuilder

package jfun.yan.xml;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jfun.util.dict.Dict;
import jfun.yan.Component;
import jfun.yan.Components;

class ModuleBuilder {
  private final Import[] parents;
  private final Statements stmts;
  private final String[] dependencies;
  private final String name;
  private final String description;
  private final Object module_id;
  private final StringPredicate exported;

  public String getDescription() {
    return description;
  }

  public Object getModuleId() {
    return module_id;
  }

  public String getName() {
    return name;
  }
  /*
  private static void filter(StringPredicate exported, String prefix,
      String[] keys, List result){
    for(int i=0;  i<keys.length; i++){
      final String key = prefix+keys[i];
      if(exported.isString(key))
        result.add(key);
    }
  }
 
  public String[] getExports(Body body){
    final int sz = body.size();
    final ArrayList result = new ArrayList(sz);
    for(int i=0; i<parents.length; i++){
      final Import parent = parents[i];
      final String[] keys = parent.getKeys();
      filter(exported, parent.getPrefix(), keys, result);
    }
    for(int i=0;  i<sz; i++){
      final Object key = body.getKey(i);
      if(key instanceof String && exported.isString((String)key))
        result.add(key);
    }
    final String[] ret = new String[result.size()];
    result.toArray(ret);
    return ret;
  }*/
  ModuleBuilder(Object module_id, String name, String description,
      String[] dependencies, Import[] imports, Statements stmts,
      StringPredicate exported) {
    this.name = name;
    this.description = description;
    this.module_id = module_id;
    this.dependencies = dependencies;
    this.parents = imports;
    this.stmts = stmts;
    this.exported = exported;
  }


  public String[] getDependencies(){
    final Set result = new HashSet();
    for(int i=0; i<parents.length; i++){
      result.addAll(Arrays.asList(parents[i].getImported().getDependencies()));
    }
    result.addAll(Arrays.asList(dependencies));
    final String[] ret = new String[result.size()];
    result.toArray(ret);
    return ret;
  }

  private static Registry toRegistry(final Map map){
    return new Map2Registry(map);
  }
  private Dict withDependencies(Dict initial_frame){
    final Component[] deps = new Component[dependencies.length];
    for(int i=0; i<dependencies.length; i++){
      deps[i] = Components.useKey(dependencies[i]);
    }
    return initial_frame.puts(dependencies, deps);
 
  }
  public Module build(Runtime runtime, Dict initial_frame){
    final Map imported = new HashMap();
    final Registry locals = toRegistry(imported);
    for(int i=0; i<parents.length; i++){
      parents[i].register(locals);
    }
    final Object[] keys = imported.keySet().toArray();
    final Object[] vals = new Object[keys.length];
    for(int i=0; i<keys.length; i++){
      vals[i] = imported.get(keys[i]);
    }
    final Dict with_deps = withDependencies(initial_frame);
    Dict myframe = with_deps.puts(keys, vals);
    final Body body = BodyCompiler.evaluate(
        stmts.getKeys(), stmts.getStmts(),
        runtime, //new FilteringRegistry(exported, reg),
        myframe);
    return new SimpleModule(module_id, name, description,
        getDependencies(), parents, body, exported);
  }
  public StringPredicate getExportFilter() {
    return exported;
  }

}
TOP

Related Classes of jfun.yan.xml.ModuleBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.