Skip to content

Commit ab12e54

Browse files
committed
*Updated project and readme.
1 parent 2ba413a commit ab12e54

23 files changed

+225
-62
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
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

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,134 @@
1-
# bitty.compiler_plugin
2-
A compiler plugin example for Bitty Engine.
1+
# Compiler plugin for Bitty Engine
2+
3+
This repository demonstrates how to implement a custom compiler/transpiler plugin for [Bitty Engine](https://github.com/paladin-t/bitty) with a BF example.
4+
5+
## Installing
6+
7+
1. Clone or download this repository.
8+
9+
2. Click "Project", "Browse Data Directory..." from the menu bar.
10+
11+
![](imgs/browse.png)
12+
13+
3. Ensure there is a "plugins" directory under that directory.
14+
15+
4. Put the "plugin.bit" into the new created "plugins" directory. And name it properly.
16+
17+
![](imgs/install.png)
18+
19+
5. Reopen Bitty Engine.
20+
21+
## Injecting
22+
23+
1. Create a new asset, select the registered asset type.
24+
25+
![](imgs/new_asset.png)
26+
27+
2. Click "Install" to inject necessary assets to the target project.
28+
29+
![](imgs/inject.png)
30+
31+
![](imgs/injected.png)
32+
33+
3. Write and run BF code.
34+
35+
![](imgs/run.png)
36+
37+
## Principles
38+
39+
![](imgs/principles.png)
40+
41+
Bitty Engine loads plugin to extend its features once it's installed. It loads a BF compiler in this example which can inject the compiler to a target project. And the injected compiler/transpiler runs as regular Bitty code that translates BF code to runnable Lua chunk.
42+
43+
```lua
44+
require 'bf/compiler' -- Require the compiler.
45+
46+
local f = compile('hello world.bf') -- Compile something.
47+
f() -- Run the compiled chunk.
48+
```
49+
50+
## Write your own compiler plugin
51+
52+
A compiler plugin contains three parts.
53+
54+
### 1. Meta info
55+
56+
Similar to regular Bitty projects, it has an "info.json", define it as you want.
57+
58+
### 2. Main entry
59+
60+
Bitty Engine doesn't run a plugin project directly, it must be installed before running. The execution entry is also named as "main.lua".
61+
62+
But the entry functions are different from regular project. Eg.
63+
64+
```lua
65+
-- Which assets are supposed to be injected from this plugin to target project.
66+
local assets = {
67+
'bf/compiler.lua',
68+
'bf/README.txt'
69+
}
70+
71+
-- Tips and example code.
72+
local tips = 'Usage:\n require \'bf/compiler\'\n f = compile(\'source.bf\')\n f()'
73+
local code = 'require \'bf/compiler\'\nf = compile(\'source.bf\')\nf()\n'
74+
75+
-- Plugin entry, called to determine the usage of this plugin.
76+
function usage()
77+
return { 'compiler' } -- This plugin is a compiler.
78+
end
79+
80+
-- Plugin entry, called to determine the schema of this plugin.
81+
function schema()
82+
return {
83+
-- Common.
84+
name = 'BF', -- Asset name registered for this plugin.
85+
extension = 'bf', -- Asset extension registered for this plugin.
86+
87+
-- List of string.
88+
keywords = { },
89+
identifiers = { },
90+
quotes = { '\'', '"' },
91+
-- String.
92+
multiline_comment_start = nil,
93+
multiline_comment_end = nil,
94+
-- C++ regex.
95+
comment_patterns = { '\\#.*' },
96+
number_patterns = { },
97+
identifier_patterns = { },
98+
punctuation_patterns = { '[\\<\\>\\+\\-\\.\\,\\[\\]]' },
99+
-- Boolean.
100+
case_sensitive = true,
101+
-- List of string.
102+
assets = assets
103+
}
104+
end
105+
106+
-- Plugin entry, called to install necessary assets to your target project.
107+
function compiler()
108+
print('Install BF compiler to the current project.')
109+
110+
waitbox('Installing')
111+
:thus(function (rsp)
112+
local install = function (name)
113+
local data = Project.main:read(name)
114+
data:poke(1)
115+
Project.editing:write(name, data) -- Write into the target project.
116+
end
117+
118+
for _, asset in ipairs(assets) do -- Install all necessary assets.
119+
install(asset)
120+
end
121+
122+
print('Done.')
123+
124+
msgbox(tips)
125+
:thus(function ()
126+
Platform.setClipboardText(code) -- Put example code to clipboard.
127+
end)
128+
end)
129+
end
130+
```
131+
132+
### 3. Assets to be injected
133+
134+
The plugin project also contains assets to be injected. See the plugin in this repository for details.

project.bit renamed to example.bit

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package:application/vnd.bitty-archive;
2-
data:text/lua;count=57;path=main.lua;
3-
require 'bf/compiler'
4-
5-
f = compile('hello world.bf')
6-
f()
7-
8-
data:text/lua;count=1486;path=bf/compiler.lua;
2+
data:text/lua;count=1632;path=bf/compiler.lua;
93
--[[
104
BF compiler for the Bitty Engine
115

@@ -48,14 +42,14 @@ function compile(asset)
4842
if #ln > 0 then
4943
local comment = ln:find('#')
5044
if comment then
51-
ln = ln:sub(1, comment - 1)
45+
ln = ln:sub(1, comment - 1) -- Remove comment.
5246
end
53-
dst = dst .. ln
47+
dst = dst .. ln -- Concat lines.
5448
end
5549
end
56-
local full = interpreter .. 'interpret(\'' .. dst .. '\')'
50+
local full = interpreter .. 'interpret(\'' .. dst .. '\')' -- Link the source code with the interpreter together.
5751

58-
return load(full)
52+
return load(full) -- Return loaded and parsed Lua chunk.
5953
end
6054

6155
data:text/txt;count=96;path=bf/README.txt;
@@ -66,18 +60,24 @@ Usage:
6660
f = compile('source.bf')
6761
f()
6862

69-
data:binary/octet;count=124;path=hello world.bf;
70-
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.++ #fasdf
71-
+.------.--------.>+.>.#adsg
72-
data:text/json;count=127;path=info.json;
63+
data:binary/octet;count=231;path=hello world.bf;
64+
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.++ # The BF compiler supports new lines,
65+
+.------.--------.>+.>. # and comments.
66+
data:text/json;count=206;path=info.json;
7367
{
7468
"id": 0,
75-
"title": "BF",
76-
"description": "",
69+
"title": "BF Example",
70+
"description": "A BF example project.",
7771
"author": "Tony",
7872
"version": "1.0",
7973
"genre": "TUTORIAL",
80-
"url": ""
74+
"url": "https://github.com/paladin-t/bitty.compiler_plugin"
8175
}
8276
data:binary/octet;count=6;path=input.bf;
8377
,.,.,.
78+
data:text/lua;count=183;path=main.lua;
79+
require 'bf/compiler' -- Require the compiler.
80+
81+
local f = compile('hello world.bf') -- Compile something.
82+
f() -- Run the compiled chunk.
83+

imgs/browse.png

11.1 KB
Loading

imgs/inject.png

2.25 KB
Loading

imgs/injected.png

11.3 KB
Loading

imgs/install.png

11.1 KB
Loading

imgs/new_asset.png

3.22 KB
Loading

imgs/principles.png

9.65 KB
Loading

imgs/run.png

3.48 KB
Loading

plugin.bit

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package:application/vnd.bitty-archive;
2-
data:text/lua;count=1331;path=main.lua;
2+
data:text/lua;count=1865;path=main.lua;
33
--[[
44
Plugin for the Bitty Engine
55

@@ -8,23 +8,27 @@ Copyright (C) 2020 - 2021 Tony Wang, all rights reserved
88
Homepage: https://paladin-t.github.io/bitty/
99
]]
1010

11+
-- Which assets are supposed to be injected from this plugin to target project.
1112
local assets = {
1213
'bf/compiler.lua',
1314
'bf/README.txt'
1415
}
1516

17+
-- Tips and example code.
1618
local tips = 'Usage:\n require \'bf/compiler\'\n f = compile(\'source.bf\')\n f()'
1719
local code = 'require \'bf/compiler\'\nf = compile(\'source.bf\')\nf()\n'
1820

21+
-- Plugin entry, called to determine the usage of this plugin.
1922
function usage()
20-
return { 'compiler' }
23+
return { 'compiler' } -- This plugin is a compiler.
2124
end
2225

26+
-- Plugin entry, called to determine the schema of this plugin.
2327
function schema()
2428
return {
2529
-- Common.
26-
name = 'BF',
27-
extension = 'bf',
30+
name = 'BF', -- Asset name registered for this plugin.
31+
extension = 'bf', -- Asset extension registered for this plugin.
2832

2933
-- List of string.
3034
keywords = { },
@@ -45,6 +49,7 @@ function schema()
4549
}
4650
end
4751

52+
-- Plugin entry, called to install necessary assets to your target project.
4853
function compiler()
4954
print('Install BF compiler to the current project.')
5055

@@ -53,23 +58,23 @@ function compiler()
5358
local install = function (name)
5459
local data = Project.main:read(name)
5560
data:poke(1)
56-
Project.editing:write(name, data)
61+
Project.editing:write(name, data) -- Write into the target project.
5762
end
5863

59-
for _, asset in ipairs(assets) do
64+
for _, asset in ipairs(assets) do -- Install all necessary assets.
6065
install(asset)
6166
end
6267

6368
print('Done.')
6469

6570
msgbox(tips)
6671
:thus(function ()
67-
Platform.setClipboardText(code)
72+
Platform.setClipboardText(code) -- Put example code to clipboard.
6873
end)
6974
end)
7075
end
7176

72-
data:text/lua;count=1568;path=bf/compiler.lua;
77+
data:text/lua;count=1632;path=bf/compiler.lua;
7378
--[[
7479
BF compiler for the Bitty Engine
7580

@@ -114,12 +119,12 @@ function compile(asset)
114119
if comment then
115120
ln = ln:sub(1, comment - 1) -- Remove comment.
116121
end
117-
dst = dst .. ln -- Concat lines.
122+
dst = dst .. ln -- Concat lines.
118123
end
119124
end
120-
local full = interpreter .. 'interpret(\'' .. dst .. '\')' -- Link the source code into the interpreter.
125+
local full = interpreter .. 'interpret(\'' .. dst .. '\')' -- Link the source code with the interpreter together.
121126

122-
return load(full)
127+
return load(full) -- Return loaded and parsed Lua chunk.
123128
end
124129

125130
data:text/txt;count=96;path=bf/README.txt;
@@ -130,13 +135,13 @@ Usage:
130135
f = compile('source.bf')
131136
f()
132137

133-
data:text/json;count=161;path=info.json;
138+
data:text/json;count=211;path=info.json;
134139
{
135140
"id": 0,
136141
"title": "BF Compiler",
137142
"description": "Compile BF source to Lua.",
138143
"author": "Tony",
139144
"version": "1.0",
140145
"genre": "COMPILER",
141-
"url": ""
146+
"url": "https://github.com/paladin-t/bitty.compiler_plugin"
142147
}
File renamed without changes.

src/project/bf/compiler.lua renamed to src/example/bf/compiler.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ function compile(asset)
4040
if #ln > 0 then
4141
local comment = ln:find('#')
4242
if comment then
43-
ln = ln:sub(1, comment - 1)
43+
ln = ln:sub(1, comment - 1) -- Remove comment.
4444
end
45-
dst = dst .. ln
45+
dst = dst .. ln -- Concat lines.
4646
end
4747
end
48-
local full = interpreter .. 'interpret(\'' .. dst .. '\')'
48+
local full = interpreter .. 'interpret(\'' .. dst .. '\')' -- Link the source code with the interpreter together.
4949

50-
return load(full)
50+
return load(full) -- Return loaded and parsed Lua chunk.
5151
end

src/example/hello world.bf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.++ # The BF compiler supports new lines,
2+
+.------.--------.>+.>. # and comments.

src/example/info.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": 0,
3+
"title": "BF Example",
4+
"description": "A BF example project.",
5+
"author": "Tony",
6+
"version": "1.0",
7+
"genre": "TUTORIAL",
8+
"url": "https://github.com/paladin-t/bitty.compiler_plugin"
9+
}
File renamed without changes.

src/example/main.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'bf/compiler' -- Require the compiler.
2+
3+
local f = compile('hello world.bf') -- Compile something.
4+
f() -- Run the compiled chunk.

src/plugin/bf/compiler.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ function compile(asset)
4242
if comment then
4343
ln = ln:sub(1, comment - 1) -- Remove comment.
4444
end
45-
dst = dst .. ln -- Concat lines.
45+
dst = dst .. ln -- Concat lines.
4646
end
4747
end
48-
local full = interpreter .. 'interpret(\'' .. dst .. '\')' -- Link the source code into the interpreter.
48+
local full = interpreter .. 'interpret(\'' .. dst .. '\')' -- Link the source code with the interpreter together.
4949

50-
return load(full)
50+
return load(full) -- Return loaded and parsed Lua chunk.
5151
end

src/plugin/info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"author": "Tony",
66
"version": "1.0",
77
"genre": "COMPILER",
8-
"url": ""
8+
"url": "https://github.com/paladin-t/bitty.compiler_plugin"
99
}

0 commit comments

Comments
 (0)