1 package org.simantics.scl.compiler.environment;
3 import java.util.ArrayList;
4 import java.util.Arrays;
6 import org.simantics.scl.compiler.compilation.CompilationContext;
7 import org.simantics.scl.compiler.module.ConcreteModule;
8 import org.simantics.scl.compiler.module.ImportDeclaration;
9 import org.simantics.scl.compiler.module.repository.ImportFailureException;
10 import org.simantics.scl.compiler.module.repository.ModuleRepository;
11 import org.simantics.scl.compiler.module.repository.UpdateListener;
13 public class EnvironmentFactoryImpl implements EnvironmentFactory {
15 private final ModuleRepository environment;
16 private final ImportDeclaration[] builtinImports;
17 private final UpdateListener listener;
19 public EnvironmentFactoryImpl(ModuleRepository environment,
20 ImportDeclaration[] builtinImports, UpdateListener listener) {
21 this.environment = environment;
22 this.builtinImports = builtinImports;
23 this.listener = listener;
27 public ModuleRepository getModuleRepository() {
32 public Environment createEnvironment(CompilationContext context, ImportDeclaration[] imports) throws ImportFailureException {
33 ArrayList<ImportDeclaration> acceptedBuiltinImports =
34 new ArrayList<ImportDeclaration>(builtinImports.length);
35 loop: for(ImportDeclaration builtinImport : builtinImports) {
36 for(ImportDeclaration decl : imports)
37 if(decl.moduleName.equals(builtinImport.moduleName) &&
38 decl.localName.equals(builtinImport.localName))
40 acceptedBuiltinImports.add(builtinImport);
42 ImportDeclaration[] is = Arrays.copyOf(
43 acceptedBuiltinImports.toArray(new ImportDeclaration[acceptedBuiltinImports.size()]),
44 acceptedBuiltinImports.size() + imports.length);
45 System.arraycopy(imports, 0, is, acceptedBuiltinImports.size(), imports.length);
46 return environment.createEnvironment(context, is, listener);
50 public void addBuiltinDependencies(ConcreteModule module) {
51 for(ImportDeclaration decl : builtinImports) {
52 ImportDeclaration decl2 = new ImportDeclaration(decl.moduleName, null);
53 module.addDependency(decl2);