Beispiel #1
0
        public Library ImportLibrary(Parser parser, Token throwToken, string name, List <Executable> executablesOut)
        {
            name = name.Split('.')[0];
            Library library = librariesAlreadyImportedIndexByName.ContainsKey(name)
                ? librariesAlreadyImported[librariesAlreadyImportedIndexByName[name]]
                : null;

            if (library == null)
            {
                string libraryManifestPath = this.GetSystemLibraryPath(name, parser.BuildContext.CrayonPath, parser.BuildContext.ProjectDirectory);

                if (libraryManifestPath == null)
                {
                    // No library found. Could just be a local namespace import.
                    // If this is a bogus import, it'll throw in the Resolver.
                    return(null);
                }

                string platformName = parser.BuildContext.Platform;
                Platform.AbstractPlatform platform = platformName == null || this.PlatformProvider == null ? null : this.PlatformProvider.GetPlatform(platformName);
                library = new Library(name, libraryManifestPath, platform);

                this.librariesAlreadyImportedIndexByName[name] = this.librariesAlreadyImported.Count;
                this.librariesAlreadyImported.Add(library);

                this.importedLibraries[name] = library;
                this.librariesByKey[name.ToLowerInvariant()] = library;

                string oldSystemLibrary = parser.CurrentSystemLibrary;
                parser.CurrentSystemLibrary = name;

                Dictionary <string, string> embeddedCode = library.GetEmbeddedCode();
                foreach (string embeddedFile in embeddedCode.Keys)
                {
                    string fakeName = "[" + embeddedFile + "]";
                    string code     = embeddedCode[embeddedFile];
                    parser.PushLocale(ENGLISH_LOCALE_FOR_LIBRARIES);
                    executablesOut.AddRange(parser.ParseInterpretedCode(fakeName, code, name));
                    parser.PopLocale();
                }

                parser.CurrentSystemLibrary = oldSystemLibrary;
            }

            // Even if already imported, still must check to see if this import is allowed here.
            if (!library.IsAllowedImport(parser.CurrentSystemLibrary))
            {
                throw new ParserException(throwToken, "This library cannot be imported from here.");
            }

            return(library);
        }