]> gerrit.simantics Code Review - simantics/platform.git/blob
79009374813197eb8aaa7529cd7d159302a57e30
[simantics/platform.git] /
1 package org.simantics.scl.compiler.environment;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
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;
11
12 public class EnvironmentFactoryImpl implements EnvironmentFactory {
13
14     private final ModuleRepository environment;
15     private final ImportDeclaration[] builtinImports;
16     private final UpdateListener listener;
17     
18     public EnvironmentFactoryImpl(ModuleRepository environment,
19             ImportDeclaration[] builtinImports, UpdateListener listener) {
20         this.environment = environment;
21         this.builtinImports = builtinImports;
22         this.listener = listener;
23     }
24
25     @Override
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))
33                     continue loop;
34             acceptedBuiltinImports.add(builtinImport);
35         }
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);
41     }
42     
43     @Override
44     public void addBuiltinDependencies(ConcreteModule module) {
45         for(ImportDeclaration decl : builtinImports) {
46             ImportDeclaration decl2 = new ImportDeclaration(decl.moduleName, null);
47             module.addDependency(decl2);
48         }
49     }
50
51 }