Пример #1
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);

            output["code/vm.py"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = string.Join(this.NL, new string[] {
                    this.LoadTextResource("Resources/header.txt", replacements),
                    this.LoadTextResource("Resources/TranslationHelper.txt", replacements),
                    templates.GetCode("vm:globals"),
                    this.LoadTextResource("Resources/LibraryRegistry.txt", replacements),
                    this.LoadTextResource("Resources/ResourceReader.txt", replacements),
                    templates.GetCode("vm:functions"),
                }),
            };

            foreach (LibraryForExport library in libraries)
            {
                TranspilerContext libCtx       = library.PastelContext.GetTranspilerContext();
                string            libraryName  = library.Name;
                List <string>     libraryLines = new List <string>();
                if (library.HasPastelCode)
                {
                    libraryLines.Add("import math");
                    libraryLines.Add("import os");
                    libraryLines.Add("import random");
                    libraryLines.Add("import sys");
                    libraryLines.Add("import time");
                    libraryLines.Add("import inspect");
                    libraryLines.Add("from code.vm import *");
                    libraryLines.Add("");
                    libraryLines.Add(templates.GetCode("library:" + libraryName + ":manifestfunc"));
                    libraryLines.Add(templates.GetCode("library:" + libraryName + ":functions"));
                    libraryLines.Add("");
                    libraryLines.Add("_moduleInfo = ('" + libraryName + "', dict(inspect.getmembers(sys.modules[__name__])))");
                    libraryLines.Add("");
                    libraryLines.AddRange(library.ExportEntities["EMBED_CODE"].Select(entity => entity.StringValue));

                    output["code/lib_" + libraryName.ToLower() + ".py"] = new FileOutput()
                    {
                        Type        = FileOutputType.Text,
                        TextContent = string.Join(this.NL, libraryLines),
                    };
                }
            }

            output["main.py"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = this.LoadTextResource("Resources/main.txt", replacements),
            };

            output["code/__init__.py"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = "",
            };

            output["res/bytecode.txt"]          = resourceDatabase.ByteCodeFile;
            output["res/resource_manifest.txt"] = resourceDatabase.ResourceManifestFile;
            if (resourceDatabase.ImageSheetManifestFile != null)
            {
                output["res/image_sheet_manifest.txt"] = resourceDatabase.ImageSheetManifestFile;
            }

            foreach (FileOutput image in resourceDatabase.ImageResources)
            {
                output["res/images/" + image.CanonicalFileName] = image;
            }

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

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

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

            foreach (FileOutput fontResource in resourceDatabase.FontResources)
            {
                output["res/ttf/" + fontResource.CanonicalFileName] = fontResource;
            }
        }
Пример #2
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;
                    }
                }
            }
        }
Пример #3
0
 public override void Parse(string line, TranspilerContext context)
 {
     context.AddToScript(context.ApplyStacks(new Defrule(Conditions, Actions)));
 }
Пример #4
0
        public string GetFunctionCodeForSpecificFunctionAndPopItFromFutureSerialization(string name, string swapOutWithNewNameOrNull)
        {
            TranspilerContext ctx = this.GetTranspilerContext();

            return(this.compiler.GetFunctionCodeForSpecificFunctionAndPopItFromFutureSerializationTEMP(name, swapOutWithNewNameOrNull, ctx, ""));
        }
Пример #5
0
        public string GetCodeForFunctionDeclarations()
        {
            TranspilerContext ctx = this.GetTranspilerContext();

            return(this.compiler.GetFunctionDeclarationsTEMP(ctx, ""));
        }
Пример #6
0
        public Dictionary <string, string> GetCodeForFunctionsLookup()
        {
            TranspilerContext ctx = this.GetTranspilerContext();

            return(this.compiler.GetFunctionCodeAsLookupTEMP(ctx, ""));
        }
Пример #7
0
        protected (IEnumerable <Defrule> Rules, Dictionary <int, string> GoalToResourceMap) CreateNonEscrowedResourceGoals(TranspilerContext context, IEnumerable <string> resources)
        {
            var rules             = new List <Defrule>();
            var goalToResourceMap = new Dictionary <int, string>();

            context.UsingVolatileGoal(escrowAmountGoal =>
            {
                foreach (var resource in resources)
                {
                    var nonEscrowedAmountGoal = context.CreateVolatileGoal();

                    rules.Add(new Defrule(
                                  new[]
                    {
                        "true",
                    },
                                  new[]
                    {
                        $"up-get-fact resource-amount {resource} {nonEscrowedAmountGoal}",
                        $"up-get-fact escrow-amount {resource} {escrowAmountGoal}",
                        $"up-modify-goal {nonEscrowedAmountGoal} g:- {escrowAmountGoal}",
                    }));

                    goalToResourceMap[nonEscrowedAmountGoal] = resource;
                }
            });

            return(rules, goalToResourceMap);
        }
Пример #8
0
 public abstract void Parse(string line, TranspilerContext context);