1 package org.simantics.scl.compiler.environment;
3 import java.util.ArrayList;
4 import java.util.Arrays;
6 import org.simantics.scl.compiler.module.ConcreteModule;
7 import org.simantics.scl.compiler.module.ImportDeclaration;
8 import org.simantics.scl.compiler.module.repository.ImportFailureException;
9 import org.simantics.scl.compiler.module.repository.ModuleRepository;
10 import org.simantics.scl.compiler.module.repository.UpdateListener;
12 public class EnvironmentFactoryImpl implements EnvironmentFactory {
14 private final ModuleRepository environment;
15 private final ImportDeclaration[] builtinImports;
16 private final UpdateListener listener;
18 public EnvironmentFactoryImpl(ModuleRepository environment,
19 ImportDeclaration[] builtinImports, UpdateListener listener) {
20 this.environment = environment;
21 this.builtinImports = builtinImports;
22 this.listener = listener;
26 public Environment createEnvironment(ImportDeclaration[] imports) throws ImportFailureException {
27 ArrayList<ImportDeclaration> acceptedBuiltinImports =
28 new ArrayList<ImportDeclaration>(builtinImports.length);
29 loop: for(ImportDeclaration builtinImport : builtinImports) {
30 for(ImportDeclaration decl : imports)
31 if(decl.moduleName.equals(builtinImport.moduleName) &&
32 decl.localName.equals(builtinImport.localName))
34 acceptedBuiltinImports.add(builtinImport);
36 ImportDeclaration[] is = Arrays.copyOf(
37 acceptedBuiltinImports.toArray(new ImportDeclaration[acceptedBuiltinImports.size()]),
38 acceptedBuiltinImports.size() + imports.length);
39 System.arraycopy(imports, 0, is, acceptedBuiltinImports.size(), imports.length);
40 return environment.createEnvironment(is, listener);
44 public void addBuiltinDependencies(ConcreteModule module) {
45 for(ImportDeclaration decl : builtinImports) {
46 ImportDeclaration decl2 = new ImportDeclaration(decl.moduleName, null);
47 module.addDependency(decl2);