コード例 #1
0
 public PastelContext(string dir, Language language, IInlineImportCodeLoader codeLoader)
 {
     this.dir        = dir;
     this.CodeLoader = codeLoader;
     this.Language   = language;
     this.Transpiler = LanguageUtil.GetTranspiler(language);
 }
コード例 #2
0
ファイル: PastelCompiler.cs プロジェクト: blakeohare/pastel
        public PastelCompiler(
            PastelContext context,
            Language language,
            IList <PastelCompiler> includedScopes,
            Dictionary <string, int> namespaceToScopeIndex,
            IDictionary <string, object> constants,
            IInlineImportCodeLoader inlineImportCodeLoader,
            ICollection <ExtensibleFunction> extensibleFunctions)
        {
            this.Context = context;
            Dictionary <string, object> langConstants      = LanguageUtil.GetLanguageConstants(language);
            Dictionary <string, object> flattenedConstants = new Dictionary <string, object>(langConstants);

            foreach (string key in constants.Keys)
            {
                flattenedConstants[key] = constants[key];
            }

            this.CodeLoader     = inlineImportCodeLoader;
            this.Transpiler     = LanguageUtil.GetTranspiler(language);
            this.IncludedScopes = includedScopes.ToArray();
            this.IncludedScopeNamespacesToIndex = new Dictionary <string, int>(namespaceToScopeIndex);
            this.ExtensibleFunctions            = extensibleFunctions == null
                ? new Dictionary <string, ExtensibleFunction>()
                : extensibleFunctions.ToDictionary(ef => ef.Name);
            this.StructDefinitions   = new Dictionary <string, StructDefinition>();
            this.EnumDefinitions     = new Dictionary <string, EnumDefinition>();
            this.ConstantDefinitions = new Dictionary <string, VariableDeclaration>();
            this.FunctionDefinitions = new Dictionary <string, FunctionDefinition>();
            this.ClassDefinitions    = new Dictionary <string, ClassDefinition>();
            this.interpreterParser   = new PastelParser(context, flattenedConstants, inlineImportCodeLoader);
        }
コード例 #3
0
ファイル: VmGenerator.cs プロジェクト: elimisteve/crayon
        private Pastel.PastelCompiler GenerateCoreVmParseTree(
            Platform.AbstractPlatform platform,
            IInlineImportCodeLoader codeLoader,
            Dictionary <string, object> constantFlags)
        {
            using (new PerformanceSection("VmGenerator.GenerateCoreVmParseTree"))
            {
                Pastel.PastelCompiler compiler = new Pastel.PastelCompiler(
                    false,
                    null,
                    constantFlags,
                    codeLoader,
                    null,
                    null);

                foreach (string file in INTERPRETER_BASE_FILES)
                {
                    string code = codeLoader.LoadCode(file);
                    compiler.CompileBlobOfCode(file, code);
                }
                compiler.Resolve();

                return(compiler);
            }
        }
コード例 #4
0
ファイル: PastelParser.cs プロジェクト: geofrey/crayon
 public PastelParser(
     IDictionary <string, object> constants,
     IInlineImportCodeLoader importCodeLoader)
 {
     this.constants        = constants;
     this.importCodeLoader = importCodeLoader;
 }
コード例 #5
0
 public PastelCompiler(
     bool isLibrary,
     PastelCompiler sharedScope,
     IDictionary <string, object> constants,
     IInlineImportCodeLoader inlineImportCodeLoader,
     IDictionary <string, PType> returnTypesForNativeMethods,
     IDictionary <string, PType[]> argumentTypesForNativeMethods)
 {
     this.isLibrary   = isLibrary;
     this.SharedScope = sharedScope;
     this.LibraryNativeFunctionReferenceArgumentTypes = argumentTypesForNativeMethods;
     this.LibraryNativeFunctionReferenceReturnTypes   = returnTypesForNativeMethods;
     this.StructDefinitions   = new Dictionary <string, StructDefinition>();
     this.EnumDefinitions     = new Dictionary <string, EnumDefinition>();
     this.Globals             = new Dictionary <string, VariableDeclaration>();
     this.ConstantDefinitions = new Dictionary <string, VariableDeclaration>();
     this.FunctionDefinitions = new Dictionary <string, FunctionDefinition>();
     this.interpreterParser   = new PastelParser(constants, inlineImportCodeLoader);
 }
コード例 #6
0
 public PastelCompiler(
     Language language,
     IList <PastelCompiler> includedScopes,
     IDictionary <string, object> constants,
     IInlineImportCodeLoader inlineImportCodeLoader,
     ICollection <ExtensibleFunction> extensibleFunctions)
 {
     this.CodeLoader          = inlineImportCodeLoader;
     this.Transpiler          = LanguageUtil.GetTranspiler(language);
     this.IncludedScopes      = includedScopes.ToArray();
     this.ExtensibleFunctions = extensibleFunctions == null
         ? new Dictionary <string, ExtensibleFunction>()
         : extensibleFunctions.ToDictionary(ef => ef.Name);
     this.StructDefinitions   = new Dictionary <string, StructDefinition>();
     this.EnumDefinitions     = new Dictionary <string, EnumDefinition>();
     this.Globals             = new Dictionary <string, VariableDeclaration>();
     this.ConstantDefinitions = new Dictionary <string, VariableDeclaration>();
     this.FunctionDefinitions = new Dictionary <string, FunctionDefinition>();
     this.interpreterParser   = new PastelParser(constants, inlineImportCodeLoader);
 }
コード例 #7
0
ファイル: VmGenerator.cs プロジェクト: jonathansharman/crayon
        private PastelContext GenerateCoreVmParseTree(
            Platform.AbstractPlatform platform,
            IInlineImportCodeLoader codeLoader,
            Dictionary <string, object> constantFlags)
        {
            using (new PerformanceSection("VmGenerator.GenerateCoreVmParseTree"))
            {
                PastelContext context = new PastelContext(platform.Language, codeLoader);
                foreach (string key in constantFlags.Keys)
                {
                    context.SetConstant(key, constantFlags[key]);
                }
                foreach (string file in INTERPRETER_BASE_FILES)
                {
                    context.CompileFile(file);
                }
                context.FinalizeCompilation();

                return(context);
            }
        }
コード例 #8
0
ファイル: VmGenerator.cs プロジェクト: elimisteve/crayon
        private List <Platform.LibraryForExport> GetLibrariesForExport(
            Platform.AbstractPlatform platform,
            Dictionary <string, LibraryMetadata> librariesById,
            Dictionary <string, object> constantFlags,
            IInlineImportCodeLoader codeLoader,
            Pastel.PastelCompiler vm)
        {
            using (new PerformanceSection("VmGenerator.GetLibrariesForExport"))
            {
                Dictionary <string, Pastel.PastelCompiler> libraryCompilation = this.GenerateLibraryParseTree(
                    platform,
                    constantFlags,
                    codeLoader,
                    librariesById.Values,
                    vm);

                List <Platform.LibraryForExport>     libraries     = new List <Platform.LibraryForExport>();
                Dictionary <string, LibraryExporter> libraryByName = new Dictionary <string, LibraryExporter>();
                foreach (string libraryId in libraryCompilation.Keys.OrderBy(s => s))
                {
                    LibraryExporter library = LibraryExporter.Get(librariesById[libraryId], platform);
                    libraryByName[library.Metadata.ID] = library;
                    Platform.LibraryForExport libraryForExport = this.CreateLibraryForExport(
                        library.Metadata.ID,
                        library.Metadata.Version,
                        libraryCompilation[library.Metadata.ID],
                        library.Resources);
                    libraries.Add(libraryForExport);
                }

                // Now that all libraries are read and initialized, go through and resolve all deferred DLL's that required all libraries to be loaded.
                foreach (Platform.LibraryForExport lfe in libraries)
                {
                    foreach (Platform.ExportEntity ee in lfe.ExportEntities.GetValueEnumerator())
                    {
                        if (ee.DeferredFileOutputBytesLibraryName != null)
                        {
                            LibraryExporter sourceLibrary;
                            if (!libraryByName.TryGetValue(ee.DeferredFileOutputBytesLibraryName, out sourceLibrary))
                            {
                                throw new InvalidOperationException("The library '" + lfe.Name + "' makes reference to another library '" + ee.DeferredFileOutputBytesLibraryName + "' which could not be found.");
                            }

                            string resourcePath = "resources/" + ee.DeferredFileOutputBytesLibraryPath;
                            byte[] dllFile      = sourceLibrary.Metadata.ReadFileBytes(resourcePath);
                            if (dllFile == null)
                            {
                                throw new InvalidOperationException("Could not find file: '" + resourcePath + "' in library '" + sourceLibrary.Metadata.ID + "'");
                            }
                            ee.FileOutput = new FileOutput()
                            {
                                Type          = FileOutputType.Binary,
                                BinaryContent = dllFile
                            };
                        }
                    }
                }

                return(libraries);
            }
        }
コード例 #9
0
ファイル: VmGenerator.cs プロジェクト: elimisteve/crayon
        private Dictionary <string, Pastel.PastelCompiler> GenerateLibraryParseTree(
            Platform.AbstractPlatform platform,
            Dictionary <string, object> constantFlags,
            IInlineImportCodeLoader codeLoader,
            ICollection <LibraryMetadata> relevantLibraries,
            Pastel.PastelCompiler sharedScope)
        {
            using (new PerformanceSection("VmGenerator.GenerateLibraryParseTree"))
            {
                Dictionary <string, Pastel.PastelCompiler> libraries = new Dictionary <string, Pastel.PastelCompiler>();

                foreach (LibraryMetadata libraryMetadata in relevantLibraries)
                {
                    LibraryExporter library = LibraryExporter.Get(libraryMetadata, platform);

                    Dictionary <string, object> constantsLookup = Util.MergeDictionaries <string, object>(constantFlags, library.CompileTimeConstants);

                    Pastel.PastelCompiler compiler = new Pastel.PastelCompiler(
                        true,
                        sharedScope,
                        constantsLookup,
                        codeLoader,
                        library.GetReturnTypesForNativeMethods(),
                        library.GetArgumentTypesForNativeMethods());
                    libraries[library.Metadata.ID] = compiler;

                    Dictionary <string, string> supplementalCode = library.Metadata.GetSupplementalTranslatedCode();
                    Dictionary <string, string> translatedCode   = library.GetNativeCode();
                    Dictionary <string, string> structCode       = library.Metadata.GetStructFilesCode();
                    // need to load from the actual Library instance, which could have come from either CRAYON_HOME or source

                    string registryCode = library.Metadata.GetRegistryCode();
                    if (registryCode == null)
                    {
                        if (supplementalCode.Count > 0 || translatedCode.Count > 0)
                        {
                            throw new InvalidOperationException("The library '" + library.Metadata.ID + "' has translated code but no function_registry.pst file.");
                        }
                    }
                    else
                    {
                        compiler.CompileBlobOfCode("LIB:" + library.Metadata.ID + "/function_registry.pst", registryCode);

                        foreach (string structFile in structCode.Keys)
                        {
                            string code = structCode[structFile];
                            compiler.CompileBlobOfCode("LIB:" + library.Metadata.ID + "/structs/" + structFile, code);
                        }

                        foreach (string supplementalFile in supplementalCode.Keys)
                        {
                            string code = supplementalCode[supplementalFile];
                            compiler.CompileBlobOfCode("LIB:" + library.Metadata.ID + "/supplemental/" + supplementalFile, code);
                        }
                        foreach (string translatedFile in translatedCode.Keys)
                        {
                            string code = translatedCode[translatedFile];
                            compiler.CompileBlobOfCode("LIB:" + library.Metadata.ID + "/translate/" + translatedFile, code);
                        }
                        compiler.Resolve();
                    }
                }

                return(libraries);
            }
        }
コード例 #10
0
ファイル: VmGenerator.cs プロジェクト: elimisteve/crayon
        public void GenerateVmSourceCodeForPlatform(
            Dictionary <string, FileOutput> output,
            Platform.AbstractPlatform platform,
            CompilationBundle nullableCompilationBundle,
            ResourceDatabase resourceDatabase,
            ICollection <LibraryMetadata> relevantLibraries,
            string verifiedAbsoluteOutputPath,
            IInlineImportCodeLoader codeLoader,
            VmGenerationMode mode)
        {
            using (new PerformanceSection("VmGenerator.GenerateVmSourceCodeForPlatform"))
            {
                Options options = new Options();
                Dictionary <string, object> constantFlags = platform.GetFlattenedConstantFlags() ?? new Dictionary <string, object>();

                this.AddTypeEnumsToConstants(constantFlags);
                Pastel.PastelCompiler vm = this.GenerateCoreVmParseTree(platform, codeLoader, constantFlags);

                Dictionary <string, LibraryMetadata> librariesByID = relevantLibraries.ToDictionary(lib => lib.ID);
                List <Platform.LibraryForExport>     libraries     = this.GetLibrariesForExport(platform, librariesByID, constantFlags, codeLoader, vm);

                LibraryNativeInvocationTranslatorProvider libTranslationProvider =
                    new LibraryNativeInvocationTranslatorProvider(
                        relevantLibraries.ToDictionary(lib => lib.ID),
                        libraries,
                        platform);

                if (mode == VmGenerationMode.EXPORT_SELF_CONTAINED_PROJECT_SOURCE)
                {
                    options
                    .SetOption(ExportOptionKey.PROJECT_ID, nullableCompilationBundle.ProjectID)
                    .SetOption(ExportOptionKey.DESCRIPTION, nullableCompilationBundle.Description)
                    .SetOption(ExportOptionKey.VERSION, nullableCompilationBundle.Version)
                    .SetOption(ExportOptionKey.EMBED_BYTE_CODE, nullableCompilationBundle.GuidSeed)
                    .SetOption(ExportOptionKey.EMBED_BYTE_CODE, true)
                    .SetOption(ExportOptionKey.DEFAULT_TITLE, nullableCompilationBundle.DefaultTitle)
                    .SetOption(ExportOptionKey.LIBRARIES_USED, libraries.Cast <object>().ToArray())
                    .SetOption(ExportOptionKey.HAS_ICON, nullableCompilationBundle.IconPath != null)
                    .SetOption(ExportOptionKey.HAS_LAUNCHSCREEN, nullableCompilationBundle.LaunchScreenPath != null)
                    .SetOption(ExportOptionKey.IOS_BUNDLE_PREFIX, nullableCompilationBundle.IosBundlePrefix)
                    .SetOption(ExportOptionKey.JAVA_PACKAGE, nullableCompilationBundle.JavaPackage)
                    .SetOption(ExportOptionKey.JS_FILE_PREFIX, nullableCompilationBundle.JsFilePrefix)
                    .SetOption(ExportOptionKey.JS_FULL_PAGE, nullableCompilationBundle.JsFullPage)
                    .SetOption(ExportOptionKey.SUPPORTED_ORIENTATION, nullableCompilationBundle.Orientations);

                    if (options.GetBool(ExportOptionKey.HAS_ICON))
                    {
                        options.SetOption(ExportOptionKey.ICON_PATH, nullableCompilationBundle.IconPath);
                    }
                    if (options.GetBool(ExportOptionKey.HAS_LAUNCHSCREEN))
                    {
                        options.SetOption(ExportOptionKey.LAUNCHSCREEN_PATH, nullableCompilationBundle.LaunchScreenPath);
                    }

                    platform.GleanInformationFromPreviouslyExportedProject(options, verifiedAbsoluteOutputPath);

                    platform.ExportProject(
                        output,
                        vm.Globals.Values.OrderBy(v => v.VariableNameToken.Value).ToArray(),
                        vm.StructDefinitions.Values.OrderBy(s => s.NameToken.Value).ToArray(),
                        vm.FunctionDefinitions.Values.OrderBy(f => f.NameToken.Value).ToArray(),
                        libraries,
                        resourceDatabase,
                        options,
                        libTranslationProvider);
                }
                else
                {
                    platform.ExportStandaloneVm(
                        output,
                        vm.Globals.Values.OrderBy(v => v.VariableNameToken.Value).ToArray(),
                        vm.StructDefinitions.Values.OrderBy(s => s.NameToken.Value).ToArray(),
                        vm.FunctionDefinitions.Values.OrderBy(f => f.NameToken.Value).ToArray(),
                        libraries,
                        libTranslationProvider);
                }
            }
        }
コード例 #11
0
 public PastelContext(string dir, string languageId, IInlineImportCodeLoader codeLoader)
     : this(dir, LanguageUtil.ParseLanguage(languageId), codeLoader)
 {
 }
コード例 #12
0
ファイル: VmGenerator.cs プロジェクト: jonathansharman/crayon
        private Dictionary <string, PastelContext> GenerateLibraryParseTree(
            Platform.AbstractPlatform platform,
            Dictionary <string, object> constantFlags,
            IInlineImportCodeLoader codeLoader,
            ICollection <LibraryMetadata> relevantLibraries,
            PastelContext sharedScope)
        {
            using (new PerformanceSection("VmGenerator.GenerateLibraryParseTree"))
            {
                Dictionary <string, PastelContext> libraries = new Dictionary <string, PastelContext>();

                foreach (LibraryMetadata libraryMetadata in relevantLibraries)
                {
                    LibraryExporter library = LibraryExporter.Get(libraryMetadata, platform);

                    Dictionary <string, object> constantsLookup = Util.MergeDictionaries <string, object>(constantFlags, library.CompileTimeConstants);

                    List <ExtensibleFunction> libraryFunctions = library.GetPastelExtensibleFunctions();

                    if (!libraryMetadata.IsMoreThanJustEmbedCode)
                    {
                        continue;
                    }

                    PastelContext context = new PastelContext(platform.Language, codeLoader);
                    Dictionary <string, string> exFnTranslations = library.GetExtensibleFunctionTranslations(platform);
                    foreach (ExtensibleFunction exFn in libraryFunctions)
                    {
                        string exFnTranslation = null;
                        if (exFnTranslations.ContainsKey(exFn.Name))
                        {
                            exFnTranslation = exFnTranslations[exFn.Name];
                        }
                        else if (exFnTranslations.ContainsKey("$" + exFn.Name))
                        {
                            exFnTranslation = exFnTranslations["$" + exFn.Name];
                        }

                        context.AddExtensibleFunction(exFn, exFnTranslation);
                    }
                    context.AddDependency(sharedScope);
                    foreach (string constKey in constantsLookup.Keys)
                    {
                        context.SetConstant(constKey, constantsLookup[constKey]);
                    }

                    libraries[library.Metadata.ID] = context;

                    Dictionary <string, string> supplementalCode = library.Metadata.GetSupplementalTranslatedCode();
                    Dictionary <string, string> translatedCode   = library.GetNativeCode();
                    Dictionary <string, string> structCode       = library.Metadata.GetStructFilesCode();
                    // need to load from the actual Library instance, which could have come from either CRAYON_HOME or source

                    string registryCode = library.Metadata.GetRegistryCode();
                    if (registryCode == null)
                    {
                        if (supplementalCode.Count > 0 || translatedCode.Count > 0)
                        {
                            throw new InvalidOperationException("The library '" + library.Metadata.ID + "' has translated code but no function_registry.pst file.");
                        }
                    }
                    else
                    {
                        string filename = "LIB:" + library.Metadata.ID + "/function_registry.pst";
                        context.CompileCode(filename, registryCode);

                        foreach (string structFile in structCode.Keys)
                        {
                            filename = "LIB:" + library.Metadata.ID + "/structs/" + structFile;
                            context.CompileCode(filename, structCode[structFile]);
                        }

                        foreach (string supplementalFile in supplementalCode.Keys)
                        {
                            filename = "LIB:" + library.Metadata.ID + "/supplemental/" + supplementalFile;
                            context.CompileCode(filename, supplementalCode[supplementalFile]);
                        }
                        foreach (string translatedFile in translatedCode.Keys)
                        {
                            filename = "LIB:" + library.Metadata.ID + "/translate/" + translatedFile;
                            context.CompileCode(filename, translatedCode[translatedFile]);
                        }

                        context.FinalizeCompilation();
                    }
                }

                return(libraries);
            }
        }