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 Environment createEnvironment(CompilationContext context, ImportDeclaration[] imports) throws ImportFailureException {
28 ArrayList<ImportDeclaration> acceptedBuiltinImports =
29 new ArrayList<ImportDeclaration>(builtinImports.length);
30 loop: for(ImportDeclaration builtinImport : builtinImports) {
31 for(ImportDeclaration decl : imports)
32 if(decl.moduleName.equals(builtinImport.moduleName) &&
33 decl.localName.equals(builtinImport.localName))
35 acceptedBuiltinImports.add(builtinImport);
37 ImportDeclaration[] is = Arrays.copyOf(
38 acceptedBuiltinImports.toArray(new ImportDeclaration[acceptedBuiltinImports.size()]),
39 acceptedBuiltinImports.size() + imports.length);
40 System.arraycopy(imports, 0, is, acceptedBuiltinImports.size(), imports.length);
41 return environment.createEnvironment(context, is, listener);
45 public void addBuiltinDependencies(ConcreteModule module) {
46 for(ImportDeclaration decl : builtinImports) {
47 ImportDeclaration decl2 = new ImportDeclaration(decl.moduleName, null);
48 module.addDependency(decl2);