Skip to content

Commit c9a58e1

Browse files
committed
+Added files.
1 parent f5a1c05 commit c9a58e1

13 files changed

+613
-2
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (C) 2020 - 2021 Tony Wang
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do 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.

README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
1-
# bitty.luax_plugin
2-
A Lua-extention plugin for Bitty Engine.
1+
# Luax compiler plugin for Bitty Engine
2+
3+
This repository implements a custom compiler/transpiler plugin for [Bitty Engine](https://github.com/paladin-t/bitty) with an extended Lua syntex.
4+
5+
This plugin implements `+=` and `-=` operators which the original Lua doesn't support.
6+
7+
```lua
8+
local a = 0
9+
for i = 1, 42 do
10+
a += 1 -- Extended operator "+=".
11+
end
12+
print(a)
13+
14+
for i = 1, 42 do
15+
a -= 1 -- Extended operator "-=".
16+
end
17+
print(a)
18+
```
19+
20+
Seealso [paladin-t/bitty.compiler_plugin](https://github.com/paladin-t/bitty.compiler_plugin) for details.
21+
22+
## Installing
23+
24+
1. Clone or download this repository.
25+
2. Click "Project", "Browse Data Directory..." from the menu bar.
26+
3. Ensure there is a "plugins" directory under that directory.
27+
4. Put the "plugin.bit" into the new created "plugins" directory. And name it properly.
28+
5. Reopen Bitty Engine.
29+
30+
## Injecting
31+
32+
1. Create a new asset, select the registered "luax" type.
33+
2. Click "Install" to inject necessary assets to the target project.
34+
3. Write and run Luax code.

example.bit

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package:application/vnd.bitty-archive;
2+
data:text/lua;count=177;path=main.lua;
3+
require 'luax/compiler' -- Require the compiler.
4+
5+
local f = compile('example.luax') -- Compile something.
6+
f() -- Run the compiled chunk.
7+
8+
data:text/lua;count=1561;path=luax/compiler.lua;
9+
--[[
10+
Luax compiler for the Bitty Engine
11+
12+
Copyright (C) 2020 - 2021 Tony Wang, all rights reserved
13+
14+
Homepage: https://paladin-t.github.io/bitty/
15+
]]
16+
17+
local interpreter = 'local function interpret(code, asset)\n' ..
18+
' local chunk, ret = load(code, asset, "t", _ENV)\n' ..
19+
' if chunk ~= nil then\n' ..
20+
' chunk()\n' ..
21+
' else\n' ..
22+
' error(ret)\n' ..
23+
' end\n' ..
24+
'end\n'
25+
26+
local function trim(str)
27+
return str:match'^%s*(.*%S)' or ''
28+
end
29+
30+
-- Compiles from an asset.
31+
function compile(asset)
32+
if not asset then
33+
error('Invalid asset.')
34+
end
35+
36+
local bytes = Project.main:read(asset)
37+
if not bytes then
38+
error('Invalid asset.')
39+
end
40+
bytes:poke(1)
41+
42+
local src = bytes:readString()
43+
local dst = ''
44+
for ln in src:gmatch('([^\n]*)\n?') do
45+
if #ln > 0 then
46+
local inc, dec = ln:find('+='), ln:find('-=')
47+
if inc then
48+
local head = ln:sub(1, inc - 1)
49+
local tail = ln:sub(inc + 2)
50+
local token = trim(head)
51+
ln = head .. '= ' .. token .. ' +' .. tail -- Concat parts.
52+
elseif dec then
53+
local head = ln:sub(1, dec - 1)
54+
local tail = ln:sub(dec + 2)
55+
local token = trim(head)
56+
ln = head .. '= ' .. token .. ' -' .. tail -- Concat parts.
57+
end
58+
ln = '\'' .. ln .. '\\n\' ..'
59+
dst = dst .. ln .. '\n' -- Concat lines.
60+
else
61+
ln = '\'\\n\' ..'
62+
dst = dst .. ln .. '\n'
63+
end
64+
end
65+
dst = dst .. '\'\'\n'
66+
local full = interpreter .. 'interpret(' .. dst .. ', \'' .. asset .. '\')' -- Link the source code with the interpreter together.
67+
68+
return load(full, asset) -- Return loaded and parsed Lua chunk.
69+
end
70+
71+
data:text/txt;count=102;path=luax/README.txt;
72+
# Luax compiler for Bitty Engine
73+
74+
Usage:
75+
require 'luax/compiler'
76+
f = compile('source.luax')
77+
f()
78+
79+
data:binary/octet;count=143;path=example.luax;
80+
local a = 0
81+
for i = 1, 42 do
82+
a += 1 -- Extended operator "+=".
83+
end
84+
print(a)
85+
86+
for i = 1, 42 do
87+
a -= 1 -- Extended operator "-=".
88+
end
89+
print(a)
90+
91+
data:text/json;count=206;path=info.json;
92+
{
93+
"id": 0,
94+
"title": "Luax Example",
95+
"description": "A Luax example project.",
96+
"author": "Tony",
97+
"version": "1.0",
98+
"genre": "TUTORIAL",
99+
"url": "https://github.com/paladin-t/bitty.luax_plugin"
100+
}

plugin.bit

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
}

src/example/example.luax

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local a = 0
2+
for i = 1, 42 do
3+
a += 1 -- Extended operator "+=".
4+
end
5+
print(a)
6+
7+
for i = 1, 42 do
8+
a -= 1 -- Extended operator "-=".
9+
end
10+
print(a)

src/example/info.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": 0,
3+
"title": "Luax Example",
4+
"description": "A Luax example project.",
5+
"author": "Tony",
6+
"version": "1.0",
7+
"genre": "TUTORIAL",
8+
"url": "https://github.com/paladin-t/bitty.luax_plugin"
9+
}

src/example/luax/README.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Luax compiler for Bitty Engine
2+
3+
Usage:
4+
require 'luax/compiler'
5+
f = compile('source.luax')
6+
f()

0 commit comments

Comments
 (0)