Esempio n. 1
0
 public PastelContext AddDependency(PastelContext context, string pastelNamespace, string referencePrefix)
 {
     this.dependencyReferenceNamespacesToDependencyIndex[pastelNamespace] = this.dependencies.Count;
     this.dependencies.Add(context.GetCompiler());
     this.dependencyReferenceExportPrefixes.Add(referencePrefix);
     return(this);
 }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
 public PastelParser(
     PastelContext context,
     IDictionary <string, object> constants,
     IInlineImportCodeLoader importCodeLoader)
 {
     this.context          = context;
     this.constants        = constants;
     this.importCodeLoader = importCodeLoader;
 }
Esempio n. 4
0
        private static void BuildProject(string projectPath)
        {
            ProjectConfig config = ProjectConfig.Parse(projectPath);

            if (config.Language == Language.NONE)
            {
                throw new InvalidOperationException("Language not defined in " + projectPath);
            }
            PastelContext context = CompilePastelContexts(config);

            GenerateFiles(config, context);
        }
Esempio n. 5
0
        private static PastelContext CompilePastelContexts(ProjectConfig rootConfig)
        {
            Dictionary <string, ProjectConfig> configsLookup = new Dictionary <string, ProjectConfig>();

            string[] contextPaths = GetContextsInDependencyOrder(rootConfig, configsLookup);
            Dictionary <string, PastelContext> contexts = new Dictionary <string, PastelContext>();

            foreach (string contextPath in contextPaths)
            {
                ProjectConfig config  = configsLookup[contextPath];
                PastelContext context = GetContextForConfigImpl(config, contexts, new HashSet <string>());
                context.CompileCode(config.Source, System.IO.File.ReadAllText(config.Source));
                context.FinalizeCompilation();
            }
            return(contexts[rootConfig.Path]);
        }
Esempio n. 6
0
 public string GetDependencyExportPrefix(PastelContext context)
 {
     if (context == this)
     {
         return(null);
     }
     for (int i = 0; i < this.dependencies.Count; ++i)
     {
         if (this.dependencies[i].Context == context)
         {
             return(this.dependencyReferenceExportPrefixes[i]);
         }
     }
     // This is a hard crash, not a ParserException, as this is currently only accessible when
     // you have a resolved function definition, and so it would be impossible to get that
     // reference if you didn't already list it as a dependency.
     throw new System.Exception("This is not a dependency of this context.");
 }
Esempio n. 7
0
        private static Dictionary <string, string> GenerateFiles(ProjectConfig config, PastelContext context)
        {
            Dictionary <string, string> output = new Dictionary <string, string>();

            if (context.UsesClassDefinitions)
            {
                Dictionary <string, string> classDefinitions = context.GetCodeForClasses();
                foreach (string className in classDefinitions.Keys.OrderBy(k => k))
                {
                    string classCode = classDefinitions[className];
                    if (context.ClassDefinitionsInSeparateFiles)
                    {
                        GenerateClassImplementation(config, className, classCode);
                    }
                    else
                    {
                        output["class_def:" + className] = classCode;
                    }
                }

                if (!context.ClassDefinitionsInSeparateFiles)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    foreach (string key in output.Keys.Where(k => k.StartsWith("class_def:")).OrderBy(k => k))
                    {
                        sb.Append(output[key]);
                        sb.Append("\n\n");
                    }
                    string code = sb.ToString().Trim();
                    if (code.Length > 0)
                    {
                        string classOutputDir = System.IO.Path.GetDirectoryName(config.OutputFileFunctions);
                        string path           = System.IO.Path.Combine(classOutputDir, "Classes" + LanguageUtil.GetFileExtension(config.Language));
                        System.IO.File.WriteAllText(path, code + "\n");
                    }
                }
            }

            if (context.UsesStructDefinitions)
            {
                Dictionary <string, string> structDefinitions = context.GetCodeForStructs();
                string[] structOrder = structDefinitions.Keys.OrderBy(k => k.ToLower()).ToArray();
                if (context.HasStructsInSeparateFiles)
                {
                    foreach (string structName in structOrder)
                    {
                        GenerateStructImplementation(config, structName, structDefinitions[structName]);
                    }
                }
                else
                {
                    GenerateStructBundleImplementation(config, structOrder, structDefinitions);
                }

                if (context.UsesStructDeclarations)
                {
                    Dictionary <string, string> structDeclarations = structOrder.ToDictionary(k => context.GetCodeForStructDeclaration(k));

                    foreach (string structName in structOrder)
                    {
                        output["struct_decl:" + structName] = structDeclarations[structName];
                    }
                }
            }

            if (context.UsesFunctionDeclarations)
            {
                string funcDeclarations = context.GetCodeForFunctionDeclarations();
                throw new NotImplementedException();
            }

            GenerateFunctionImplementation(config, context.GetCodeForFunctions());

            return(output);
        }
Esempio n. 8
0
        private static PastelContext GetContextForConfigImpl(
            ProjectConfig config,
            Dictionary <string, PastelContext> contexts,
            HashSet <string> recursionCheck)
        {
            if (contexts.ContainsKey(config.Path))
            {
                return(contexts[config.Path]);
            }
            if (recursionCheck.Contains(config.Path))
            {
                throw new InvalidOperationException("Project config dependencies have a cycle involving: " + config.Path);
            }

            recursionCheck.Add(config.Path);

            ProjectConfig[] requiredScopes = config.DependenciesByPrefix.Values.ToArray();
            for (int i = 0; i < requiredScopes.Length; ++i)
            {
                string path = requiredScopes[i].Path;
                if (!contexts.ContainsKey(path))
                {
                    throw new Exception(); // This shouldn't happen. These are run in dependency order.
                }
            }

            string        sourceRootDir = System.IO.Path.GetDirectoryName(config.Source);
            PastelContext context       = new PastelContext(sourceRootDir, config.Language, new CodeLoader(sourceRootDir));

            foreach (string prefix in config.DependenciesByPrefix.Keys)
            {
                ProjectConfig depConfig = config.DependenciesByPrefix[prefix];

                // TODO: make this a little more generic for other possible languages
                string funcNs       = depConfig.NamespaceForFunctions;
                string classWrapper = depConfig.WrappingClassNameForFunctions;
                string outputPrefix = "";
                if (funcNs != null)
                {
                    outputPrefix = funcNs + ".";
                }
                if (classWrapper != null)
                {
                    outputPrefix += classWrapper + ".";
                }

                context.AddDependency(contexts[depConfig.Path], prefix, outputPrefix);
            }
            context.MarkDependenciesAsFinalized();

            foreach (string constantName in config.Flags.Keys)
            {
                context.SetConstant(constantName, config.Flags[constantName]);
            }

            foreach (ExtensibleFunction exFn in config.GetExtensibleFunctions())
            {
                // TODO(pastel-split): Translation is already set on the extensible function in the
                // new codepath, so the 2nd parameter here ought to be removed.
                context.AddExtensibleFunction(exFn, exFn.Translation);
            }

            contexts[config.Path] = context;
            recursionCheck.Remove(config.Path);
            return(context);
        }
Esempio n. 9
0
 public PastelContext AddDependency(PastelContext context)
 {
     this.dependencies.Add(context.compiler);
     return(this);
 }