|
| 1 | +package:application/vnd.bitty-archive; |
| 2 | +data:text/lua;count=3561;path=main.lua; |
| 3 | +--[[ |
| 4 | +Plugin for the Bitty Engine |
| 5 | + |
| 6 | +Copyright (C) 2020 - 2021 Tony Wang, all rights reserved |
| 7 | + |
| 8 | +Homepage: https://paladin-t.github.io/bitty/ |
| 9 | +]] |
| 10 | + |
| 11 | +-- Which assets are supposed to be injected from this plugin to target project. |
| 12 | +local assets = { |
| 13 | + 'luax/compiler.lua', |
| 14 | + 'luax/README.txt' |
| 15 | +} |
| 16 | + |
| 17 | +-- Tips and example code. |
| 18 | +local tips = 'Usage:\n require \'luax/compiler\'\n f = compile(\'source.luax\')\n f()' |
| 19 | +local code = 'require \'luax/compiler\'\nf = compile(\'source.luax\')\nf()\n' |
| 20 | + |
| 21 | +-- Plugin entry, called to determine the usage of this plugin. |
| 22 | +function usage() |
| 23 | + return { 'compiler' } -- This plugin is a compiler. |
| 24 | +end |
| 25 | + |
| 26 | +-- Plugin entry, called to determine the schema of this plugin. |
| 27 | +function schema() |
| 28 | + return { |
| 29 | + -- Common. |
| 30 | + name = 'Luax', -- Asset name registered for this plugin. |
| 31 | + extension = 'luax', -- Asset extension registered for this plugin. |
| 32 | + |
| 33 | + -- List of string. |
| 34 | + keywords = { |
| 35 | + 'and', 'break', 'do', 'else', 'elseif', 'end', |
| 36 | + 'false', 'for', 'function', 'goto', 'if', 'in', |
| 37 | + 'local', 'nil', 'not', 'or', 'repeat', 'return', |
| 38 | + 'then', 'true', 'until', 'while' |
| 39 | + }, |
| 40 | + identifiers = { |
| 41 | + '__add', '__sub', '__mul', '__div', |
| 42 | + '__mod', '__pow', '__unm', '__idiv', |
| 43 | + '__band', '__bor', '__bxor', '__bnot', |
| 44 | + '__shl', '__shr', |
| 45 | + '__concat', '__len', |
| 46 | + '__eq', '__lt', '__le', |
| 47 | + '__index', '__newindex', '__call', |
| 48 | + '__gc', '__close', '__mode', '__name', '__tostring', |
| 49 | + |
| 50 | + 'char', 'len', 'pack', 'type', 'unpack', |
| 51 | + |
| 52 | + '_G', 'assert', 'collectgarbage', 'dofile', 'error', 'getmetatable', 'ipairs', 'load', 'loadfile', 'next', 'pairs', 'pcall', 'print', 'rawequal', 'rawget', 'rawlen', 'rawset', 'select', 'setmetatable', 'tonumber', 'tostring', 'xpcall', |
| 53 | + 'coroutine', 'create', 'isyieldable', 'resume', 'running', 'status', 'wrap', 'yield', |
| 54 | + 'require', 'package', 'config', 'cpath', 'loaded', 'loadlib', 'path', 'preload', 'searchers', 'searchpath', |
| 55 | + 'string', 'byte', 'dump', 'find', 'format', 'gmatch', 'gsub', 'lower', 'match', 'packsize', 'rep', 'reverse', 'sub', 'upper', |
| 56 | + 'utf8', 'charpattern', 'codes', 'codepoint', 'offset', |
| 57 | + 'table', 'concat', 'insert', 'move', 'remove', 'sort', |
| 58 | + 'math', 'abs', 'acos', 'asin', 'atan', 'ceil', 'cos', 'deg', 'exp', 'floor', 'fmod', 'huge', 'log', 'max', 'maxinteger', 'min', 'mininteger', 'modf', 'pi', 'rad', 'random', 'randomseed', 'sin', 'sqrt', 'tan', 'tointeger', 'ult', |
| 59 | + 'self' |
| 60 | + }, |
| 61 | + quotes = { '\'', '"' }, |
| 62 | + -- String. |
| 63 | + multiline_comment_start = '--[[', |
| 64 | + multiline_comment_end = ']]', |
| 65 | + -- C++ regex. |
| 66 | + comment_patterns = { '\\-\\-.*' }, |
| 67 | + number_patterns = { |
| 68 | + '0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?', |
| 69 | + '[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?', |
| 70 | + '[+-]?[0-9]+[Uu]?[lL]?[lL]?' |
| 71 | + }, |
| 72 | + identifier_patterns = { '[a-zA-Z_][a-zA-Z0-9_]*' }, |
| 73 | + punctuation_patterns = { |
| 74 | + '[\\[\\]\\{\\}\\!\\#\\%\\^\\&\\*\\(\\)\\-\\+\\=\\~\\|\\:\\<\\>\\?\\/\\;\\,\\.]' |
| 75 | + }, |
| 76 | + -- Boolean. |
| 77 | + case_sensitive = true, |
| 78 | + -- List of string. |
| 79 | + assets = assets |
| 80 | + } |
| 81 | +end |
| 82 | + |
| 83 | +-- Plugin entry, called to install necessary assets to your target project. |
| 84 | +function compiler() |
| 85 | + print('Install Luax compiler to the current project.') |
| 86 | + |
| 87 | + waitbox('Installing') |
| 88 | + :thus(function (rsp) |
| 89 | + local install = function (name) |
| 90 | + local data = Project.main:read(name) |
| 91 | + data:poke(1) |
| 92 | + Project.editing:write(name, data) -- Write into the target project. |
| 93 | + end |
| 94 | + |
| 95 | + for _, asset in ipairs(assets) do -- Install all necessary assets. |
| 96 | + install(asset) |
| 97 | + end |
| 98 | + |
| 99 | + print('Done.') |
| 100 | + |
| 101 | + msgbox(tips) |
| 102 | + :thus(function () |
| 103 | + Platform.setClipboardText(code) -- Put example code to clipboard. |
| 104 | + end) |
| 105 | + end) |
| 106 | +end |
| 107 | + |
| 108 | +data:text/lua;count=1561;path=luax/compiler.lua; |
| 109 | +--[[ |
| 110 | +Luax compiler for the Bitty Engine |
| 111 | + |
| 112 | +Copyright (C) 2020 - 2021 Tony Wang, all rights reserved |
| 113 | + |
| 114 | +Homepage: https://paladin-t.github.io/bitty/ |
| 115 | +]] |
| 116 | + |
| 117 | +local interpreter = 'local function interpret(code, asset)\n' .. |
| 118 | + ' local chunk, ret = load(code, asset, "t", _ENV)\n' .. |
| 119 | + ' if chunk ~= nil then\n' .. |
| 120 | + ' chunk()\n' .. |
| 121 | + ' else\n' .. |
| 122 | + ' error(ret)\n' .. |
| 123 | + ' end\n' .. |
| 124 | + 'end\n' |
| 125 | + |
| 126 | +local function trim(str) |
| 127 | + return str:match'^%s*(.*%S)' or '' |
| 128 | +end |
| 129 | + |
| 130 | +-- Compiles from an asset. |
| 131 | +function compile(asset) |
| 132 | + if not asset then |
| 133 | + error('Invalid asset.') |
| 134 | + end |
| 135 | + |
| 136 | + local bytes = Project.main:read(asset) |
| 137 | + if not bytes then |
| 138 | + error('Invalid asset.') |
| 139 | + end |
| 140 | + bytes:poke(1) |
| 141 | + |
| 142 | + local src = bytes:readString() |
| 143 | + local dst = '' |
| 144 | + for ln in src:gmatch('([^\n]*)\n?') do |
| 145 | + if #ln > 0 then |
| 146 | + local inc, dec = ln:find('+='), ln:find('-=') |
| 147 | + if inc then |
| 148 | + local head = ln:sub(1, inc - 1) |
| 149 | + local tail = ln:sub(inc + 2) |
| 150 | + local token = trim(head) |
| 151 | + ln = head .. '= ' .. token .. ' +' .. tail -- Concat parts. |
| 152 | + elseif dec then |
| 153 | + local head = ln:sub(1, dec - 1) |
| 154 | + local tail = ln:sub(dec + 2) |
| 155 | + local token = trim(head) |
| 156 | + ln = head .. '= ' .. token .. ' -' .. tail -- Concat parts. |
| 157 | + end |
| 158 | + ln = '\'' .. ln .. '\\n\' ..' |
| 159 | + dst = dst .. ln .. '\n' -- Concat lines. |
| 160 | + else |
| 161 | + ln = '\'\\n\' ..' |
| 162 | + dst = dst .. ln .. '\n' |
| 163 | + end |
| 164 | + end |
| 165 | + dst = dst .. '\'\'\n' |
| 166 | + local full = interpreter .. 'interpret(' .. dst .. ', \'' .. asset .. '\')' -- Link the source code with the interpreter together. |
| 167 | + |
| 168 | + return load(full, asset) -- Return loaded and parsed Lua chunk. |
| 169 | +end |
| 170 | + |
| 171 | +data:text/txt;count=102;path=luax/README.txt; |
| 172 | +# Luax compiler for Bitty Engine |
| 173 | + |
| 174 | +Usage: |
| 175 | + require 'luax/compiler' |
| 176 | + f = compile('source.luax') |
| 177 | + f() |
| 178 | + |
| 179 | +data:text/json;count=211;path=info.json; |
| 180 | +{ |
| 181 | + "id": 0, |
| 182 | + "title": "Luax Compiler", |
| 183 | + "description": "Compile Luax source to Lua.", |
| 184 | + "author": "Tony", |
| 185 | + "version": "1.0", |
| 186 | + "genre": "COMPILER", |
| 187 | + "url": "https://github.com/paladin-t/bitty.luax_plugin" |
| 188 | +} |
0 commit comments