private void ExportInterpreter(
            TemplateStorage templates,
            string baseDir,
            Dictionary <string, FileOutput> output)
        {
            foreach (string structKey in templates.GetTemplateKeysWithPrefix("vm:struct:"))
            {
                string structName = templates.GetName(structKey);
                output[baseDir + "Structs/" + structName + ".cs"] = new FileOutput()
                {
                    Type        = FileOutputType.Text,
                    TextContent = this.WrapStructCode(templates.GetCode(structKey)),
                };
            }

            string functionCode = templates.GetCode("vm:functions");

            output[baseDir + "Vm/CrayonWrapper.cs"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = string.Join("\r\n", new string[] {
                    "using System;",
                    "using System.Collections.Generic;",
                    "using System.Linq;",
                    "using Interpreter.Structs;",
                    "",
                    "namespace Interpreter.Vm",
                    "{",
                    "    public class CrayonWrapper",
                    "    {",
                    IndentCodeWithSpaces(functionCode, 8),
                    "    }",
                    "}",
                    ""
                }),
            };

            output[baseDir + "Vm/Globals.cs"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = string.Join("\r\n", new string[]
                {
                    "using System;",
                    "using System.Collections.Generic;",
                    "using Interpreter.Structs;",
                    "",
                    "namespace Interpreter.Vm",
                    "{",
                    templates.GetCode("vm:globals"),
                    "}",
                    ""
                }),
            };
        }
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            TemplateStorage templates,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options)
        {
            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);
            StringBuilder cCode = new StringBuilder();

            cCode.Append("#include <stdio.h>");
            cCode.Append(this.NL);
            cCode.Append("#include <stdlib.h>");
            cCode.Append(this.NL);
            cCode.Append("#include <string.h>");
            cCode.Append(this.NL);
            cCode.Append(this.NL);

            cCode.Append(this.LoadTextResource("Resources/List.txt", replacements));
            cCode.Append(this.LoadTextResource("Resources/String.txt", replacements));
            cCode.Append(this.LoadTextResource("Resources/Dictionary.txt", replacements));
            cCode.Append(this.LoadTextResource("Resources/TranslationHelper.txt", replacements));
            cCode.Append(this.NL);

            cCode.Append(templates.GetCode("vm:structsdecl"));
            cCode.Append(this.NL);

            foreach (string structKey in templates.GetTemplateKeysWithPrefix("vm:struct:"))
            {
                string structName = templates.GetName(structKey);
                cCode.Append(templates.GetCode(structKey));
            }
            cCode.Append(this.NL);

            cCode.Append(templates.GetCode("vm:stringtable"));
            cCode.Append(this.NL);
            cCode.Append(templates.GetCode("vm:functionsdecl"));
            cCode.Append(this.NL);
            cCode.Append(templates.GetCode("vm:functions"));
            cCode.Append(this.NL);

            cCode.Append(this.LoadTextResource("Resources/main.txt", replacements));

            output["main.c"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = cCode.ToString(),
            };
        }
Exemple #3
0
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            TemplateStorage templates,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options)
        {
            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);

            string srcPath        = "src";
            string srcPackagePath = srcPath + "/" + replacements["JAVA_PACKAGE"].Replace('.', '/') + "/";

            string[] imports = new string[]
            {
                "import org.crayonlang.interpreter.ResourceReader;",
                "import org.crayonlang.interpreter.AwtTranslationHelper;",
            };

            LangJava.PlatformImpl.ExportJavaLibraries(this, templates, srcPath, libraries, output, imports);

            foreach (string structKey in templates.GetTemplateKeysWithPrefix("vm:struct:"))
            {
                string structName = templates.GetName(structKey);
                string structCode = templates.GetCode(structKey);

                output["src/org/crayonlang/interpreter/structs/" + structName + ".java"] = new FileOutput()
                {
                    Type        = FileOutputType.Text,
                    TextContent = LangJava.PlatformImpl.WrapStructCodeWithImports(this.NL, structCode),
                };
            }

            StringBuilder sb = new StringBuilder();

            sb.Append(string.Join(this.NL, new string[] {
                "package org.crayonlang.interpreter;",
                "",
                "import java.util.ArrayList;",
                "import java.util.HashMap;",
                "import org.crayonlang.interpreter.structs.*;",
                "",
                "public final class Interpreter {",
                "  private Interpreter() {}",
                "",
            }));

            sb.Append(templates.GetCode("vm:functions"));
            sb.Append("}");
            sb.Append(this.NL);

            output["src/org/crayonlang/interpreter/Interpreter.java"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = sb.ToString(),
            };

            output["src/org/crayonlang/interpreter/VmGlobal.java"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = templates.GetCode("vm:globals"),
            };

            // common Java helper files
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/FastList.java", "Resources/FastList.txt", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/LibraryFunctionPointer.java", "Resources/LibraryFunctionPointer.txt", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/LibraryInstance.java", "Resources/LibraryInstance.txt", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/TranslationHelper.java", "Resources/TranslationHelper.txt", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/PlatformTranslationHelper.java", "Resources/PlatformTranslationHelper.txt", replacements);

            // java-app specific files
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/LibraryLoader.java", "Resources/LibraryLoader.txt", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/ResourceReader.java", "Resources/ResourceReader.txt", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/AwtTranslationHelper.java", "Resources/AwtTranslationHelper.txt", replacements);

            // need to move these to library supplemental files
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/GameWindow.java", "Resources/GameWindow.txt", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/RenderEngine.java", "Resources/RenderEngine.txt", replacements);

            this.CopyResourceAsText(output, srcPackagePath + "/Main.java", "Resources/Main.txt", replacements);
            this.CopyResourceAsText(output, "build.xml", "Resources/BuildXml.txt", replacements);

            output["resources/manifest.txt"] = resourceDatabase.ResourceManifestFile;
            output["resources/bytecode.txt"] = resourceDatabase.ByteCodeFile;
            if (resourceDatabase.ImageSheetManifestFile != null)
            {
                output["resources/imagesheetmanifest.txt"] = resourceDatabase.ImageSheetManifestFile;
            }

            foreach (string imageSheetFileName in resourceDatabase.ImageSheetFiles.Keys)
            {
                FileOutput imageSheetFile = resourceDatabase.ImageSheetFiles[imageSheetFileName];
                output["resources/images/" + imageSheetFileName] = imageSheetFile;
            }

            foreach (FileOutput audioResource in resourceDatabase.AudioResources)
            {
                output["resources/audio/" + audioResource.CanonicalFileName] = audioResource;
            }

            foreach (FileOutput textResource in resourceDatabase.TextResources)
            {
                output["resources/text/" + textResource.CanonicalFileName] = textResource;
            }

            foreach (FileOutput fontResource in resourceDatabase.FontResources)
            {
                output["resources/ttf/" + fontResource.CanonicalFileName] = fontResource;
            }

            IEnumerable <FileOutput> javaFiles = output.Keys
                                                 .Where(filename => filename.ToLower().EndsWith(".java"))
                                                 .Select(filename => output[filename]);

            foreach (FileOutput file in javaFiles)
            {
                file.TrimBomIfPresent = true;
            }
        }
        // Returns true if any export is necessary i.e. bytecode-only libraries will return false.
        private bool GetLibraryCode(
            TemplateStorage templates,
            string baseDir,
            LibraryForExport library,
            List <LangCSharp.DllFile> dllsOut,
            Dictionary <string, FileOutput> filesOut)
        {
            string        libraryName  = library.Name;
            List <string> libraryLines = new List <string>();

            if (library.HasPastelCode)
            {
                string libraryDir      = baseDir + "Libraries/" + libraryName;
                string allFunctionCode = templates.GetCode("library:" + libraryName + ":functions");
                libraryLines.Add(allFunctionCode);

                foreach (string structKey in templates.GetTemplateKeysWithPrefix("library:" + libraryName + ":struct:"))
                {
                    string structName = templates.GetName(structKey);
                    filesOut[libraryDir + "/Structs/" + structName + ".cs"] = new FileOutput()
                    {
                        Type        = FileOutputType.Text,
                        TextContent = this.WrapStructCode(templates.GetCode(structKey)),
                    };
                }

                filesOut[libraryDir + "/LibraryWrapper.cs"] = new FileOutput()
                {
                    Type        = FileOutputType.Text,
                    TextContent = string.Join(this.NL,
                                              "using System;",
                                              "using System.Collections.Generic;",
                                              "using System.Linq;",
                                              "using Interpreter;",
                                              "using Interpreter.Structs;",
                                              "using Interpreter.Vm;",
                                              "",
                                              "namespace Interpreter.Libraries." + libraryName,
                                              "{",
                                              "    public static class LibraryWrapper",
                                              "    {",
                                              IndentCodeWithSpaces(string.Join(this.NL, libraryLines), 8),
                                              "    }",
                                              "}",
                                              ""),
                };

                foreach (ExportEntity codeFile in library.ExportEntities["COPY_CODE"])
                {
                    string targetPath = codeFile.Values["target"].Replace("%LIBRARY_PATH%", libraryDir);
                    filesOut[targetPath] = codeFile.FileOutput;
                }

                foreach (ExportEntity dllFile in library.ExportEntities["DOTNET_DLL"])
                {
                    dllsOut.Add(new LangCSharp.DllFile(dllFile));
                }

                return(true);
            }

            return(false);
        }
Exemple #5
0
        public static void ExportJavaLibraries(
            AbstractPlatform platform,
            TemplateStorage templates,
            string srcPath,
            IList <LibraryForExport> libraries,
            Dictionary <string, FileOutput> output,
            string[] extraImports)
        {
            List <string> defaultImports = new List <string>()
            {
                "import java.util.ArrayList;",
                "import java.util.HashMap;",
                "import org.crayonlang.interpreter.FastList;",
                "import org.crayonlang.interpreter.Interpreter;",
                "import org.crayonlang.interpreter.LibraryFunctionPointer;",
                "import org.crayonlang.interpreter.TranslationHelper;",
                "import org.crayonlang.interpreter.VmGlobal;",
                "import org.crayonlang.interpreter.structs.*;",
            };

            defaultImports.AddRange(extraImports);
            defaultImports.Sort();

            foreach (LibraryForExport library in libraries)
            {
                if (library.HasPastelCode)
                {
                    TranspilerContext ctx         = library.PastelContext.GetTranspilerContext();
                    List <string>     libraryCode = new List <string>()
                    {
                        "package org.crayonlang.libraries." + library.Name.ToLower() + ";",
                        "",
                    };
                    libraryCode.AddRange(defaultImports);
                    libraryCode.AddRange(new string[]
                    {
                        "",
                        "public final class LibraryWrapper {",
                        "  private LibraryWrapper() {}",
                        "",
                    });

                    libraryCode.Add(templates.GetCode("library:" + library.Name + ":manifestfunc"));
                    libraryCode.Add(templates.GetCode("library:" + library.Name + ":functions"));

                    libraryCode.Add("}");
                    libraryCode.Add("");

                    string libraryPath = srcPath + "/org/crayonlang/libraries/" + library.Name.ToLower();

                    output[libraryPath + "/LibraryWrapper.java"] = new FileOutput()
                    {
                        Type        = FileOutputType.Text,
                        TextContent = string.Join(platform.NL, libraryCode),
                    };

                    foreach (string structKey in templates.GetTemplateKeysWithPrefix("library:" + library.Name + ":struct:"))
                    {
                        string structName = templates.GetName(structKey);
                        string structCode = templates.GetCode(structKey);

                        structCode = WrapStructCodeWithImports(platform.NL, structCode);

                        // This is kind of a hack.
                        // TODO: better.
                        structCode = structCode.Replace(
                            "package org.crayonlang.interpreter.structs;",
                            "package org.crayonlang.libraries." + library.Name.ToLower() + ";");

                        output[libraryPath + "/" + structName + ".java"] = new FileOutput()
                        {
                            Type        = FileOutputType.Text,
                            TextContent = structCode,
                        };
                    }

                    foreach (ExportEntity supFile in library.ExportEntities["COPY_CODE"])
                    {
                        string path = supFile.Values["target"].Replace("%LIBRARY_PATH%", libraryPath);
                        output[path] = supFile.FileOutput;
                    }
                }
            }
        }