1 /** 2 Copyright: 2018 Mark Fisher 3 4 License: 5 Permission is hereby granted, free of charge, to any person obtaining a copy of 6 this software and associated documentation files (the "Software"), to deal in 7 the Software without restriction, including without limitation the rights to 8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 of the Software, and to permit persons to whom the Software is furnished to do 10 so, subject to the following conditions: 11 12 The above copyright notice and this permission notice shall be included in all 13 copies or substantial portions of the Software. 14 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 SOFTWARE. 22 **/ 23 module dxx.app.vaynetmplt; 24 25 private import std.conv; 26 private import std.functional; 27 private import std.stdio; 28 private import std.array; 29 private import std.file; 30 private import std..string; 31 32 private import vayne.compiler; 33 private import vayne.lib; 34 private import vayne.serializer; 35 private import vayne.vm; 36 37 private import dxx.util.log; 38 39 void renderVayneToFile(string inFile,Vars...)(string outputFile) { 40 auto f = File(outputFile,"w"); 41 renderVayne!(typeof(f),inFile,Vars)(f,"en"); 42 } 43 void renderVayneAppender(string inFile,Vars...)(Appender!string output) { 44 renderVayne!(typeof(output),inFile,Vars)(output,"en"); 45 } 46 47 void renderVayne(OutputStreamT, string FileName, Vars...)(OutputStreamT o__, string language__) { 48 alias VayneVM = VM!(); 49 VayneVM vm; 50 51 //auto compiled = unserialize(cast(ubyte[])std.file.read("resources/" ~ FileName ~ ".vayne")); 52 enum txt = import(FileName.chompPrefix("resources/")); 53 auto compiled = unserialize(cast(ubyte[])txt); 54 55 Value[] constants; 56 constants.reserve(compiled.constants.length); 57 58 foreach(i, c; compiled.constants) { 59 final switch (c.type) with (ConstantType) { 60 case Null: 61 constants ~= Value(null); 62 break; 63 case Boolean: 64 constants ~= Value(c.value.to!bool); 65 break; 66 case Integer: 67 constants ~= Value(c.value.to!long); 68 break; 69 case Float: 70 constants ~= Value(c.value.to!double); 71 break; 72 case String: 73 constants ~= Value(c.value.to!string); 74 break; 75 } 76 } 77 78 vm.load(compiled.registerCount, constants, compiled.instrs, compiled.locs, compiled.sources); 79 80 VayneVM.Globals globals; 81 82 bindLibDefault(globals); 83 84 mixin(bindVars!(0, globals, Vars)); 85 86 auto translate(Value[] args) { 87 assert(language__ == "en"); 88 auto tag = args[0].get!string; 89 switch (tag) { 90 case "footer": return "This is the footer translation"; 91 case "empty": return "Move along, nothing to see here"; 92 default: return tag; 93 } 94 } 95 96 static errorHandler(VayneVM.Error error) { 97 MsgLog.warning(error.source, "(", error.line,") template error: ", error.msg); 98 } 99 100 globals["__translate"] = Value(&translate); 101 vm.bindGlobals(globals); 102 103 vm.errorHandler = toDelegate(&errorHandler); 104 vm.execute(o__); 105 }