Skip to content

Commit 2ba413a

Browse files
committed
+Added files.
1 parent 64f5e68 commit 2ba413a

File tree

12 files changed

+432
-0
lines changed

12 files changed

+432
-0
lines changed

plugin.bit

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package:application/vnd.bitty-archive;
2+
data:text/lua;count=1331;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+
local assets = {
12+
'bf/compiler.lua',
13+
'bf/README.txt'
14+
}
15+
16+
local tips = 'Usage:\n require \'bf/compiler\'\n f = compile(\'source.bf\')\n f()'
17+
local code = 'require \'bf/compiler\'\nf = compile(\'source.bf\')\nf()\n'
18+
19+
function usage()
20+
return { 'compiler' }
21+
end
22+
23+
function schema()
24+
return {
25+
-- Common.
26+
name = 'BF',
27+
extension = 'bf',
28+
29+
-- List of string.
30+
keywords = { },
31+
identifiers = { },
32+
quotes = { '\'', '"' },
33+
-- String.
34+
multiline_comment_start = nil,
35+
multiline_comment_end = nil,
36+
-- C++ regex.
37+
comment_patterns = { '\\#.*' },
38+
number_patterns = { },
39+
identifier_patterns = { },
40+
punctuation_patterns = { '[\\<\\>\\+\\-\\.\\,\\[\\]]' },
41+
-- Boolean.
42+
case_sensitive = true,
43+
-- List of string.
44+
assets = assets
45+
}
46+
end
47+
48+
function compiler()
49+
print('Install BF compiler to the current project.')
50+
51+
waitbox('Installing')
52+
:thus(function (rsp)
53+
local install = function (name)
54+
local data = Project.main:read(name)
55+
data:poke(1)
56+
Project.editing:write(name, data)
57+
end
58+
59+
for _, asset in ipairs(assets) do
60+
install(asset)
61+
end
62+
63+
print('Done.')
64+
65+
msgbox(tips)
66+
:thus(function ()
67+
Platform.setClipboardText(code)
68+
end)
69+
end)
70+
end
71+
72+
data:text/lua;count=1568;path=bf/compiler.lua;
73+
--[[
74+
BF compiler for the Bitty Engine
75+
76+
Copyright (C) 2020 - 2021 Tony Wang, all rights reserved
77+
78+
Homepage: https://paladin-t.github.io/bitty/
79+
]]
80+
81+
-- Based on https://github.com/prapin/LuaBrainFuck.
82+
local interpreter = 'local output = \'\'\n' ..
83+
'local input_ = function()\n' ..
84+
' local i = input()\n' ..
85+
' return i and i:byte(1) or 0\n' ..
86+
'end\n' ..
87+
'local function interpret(s)\n' ..
88+
' local subst = {["+"]="v=v+1 ", ["-"]="v=v-1 ", [">"]="i=i+1 ", ["<"]="i=i-1 ",\n' ..
89+
' ["."] = "w(v)", [","]="v=r()", ["["]="while v~=0 do ", ["]"]="end "}\n' ..
90+
' local env = setmetatable({ i=0, t=setmetatable({},{__index=function() return 0 end}),\n' ..
91+
' r=function() return input_() end, w=function(c) output=output..string.char(c) end },\n' ..
92+
' {__index=function(t,k) return t.t[t.i] end, __newindex=function(t,k,v) t.t[t.i]=v end })\n' ..
93+
' load(s:gsub("[^%+%-<>%.,%[%]]+",""):gsub(".", subst), "brainfuck", "t", env)()\n' ..
94+
' print(output)\n' ..
95+
'end\n'
96+
97+
-- Compiles from an asset.
98+
function compile(asset)
99+
if not asset then
100+
error('Invalid asset.')
101+
end
102+
103+
local bytes = Project.main:read(asset)
104+
if not bytes then
105+
error('Invalid asset.')
106+
end
107+
bytes:poke(1)
108+
109+
local src = bytes:readString()
110+
local dst = ''
111+
for ln in src:gmatch('([^\n]*)\n?') do
112+
if #ln > 0 then
113+
local comment = ln:find('#')
114+
if comment then
115+
ln = ln:sub(1, comment - 1) -- Remove comment.
116+
end
117+
dst = dst .. ln -- Concat lines.
118+
end
119+
end
120+
local full = interpreter .. 'interpret(\'' .. dst .. '\')' -- Link the source code into the interpreter.
121+
122+
return load(full)
123+
end
124+
125+
data:text/txt;count=96;path=bf/README.txt;
126+
# BF compiler for Bitty Engine
127+
128+
Usage:
129+
require 'bf/compiler'
130+
f = compile('source.bf')
131+
f()
132+
133+
data:text/json;count=161;path=info.json;
134+
{
135+
"id": 0,
136+
"title": "BF Compiler",
137+
"description": "Compile BF source to Lua.",
138+
"author": "Tony",
139+
"version": "1.0",
140+
"genre": "COMPILER",
141+
"url": ""
142+
}

project.bit

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
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;
9+
--[[
10+
BF 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+
-- Based on https://github.com/prapin/LuaBrainFuck.
18+
local interpreter = 'local output = \'\'\n' ..
19+
'local input_ = function()\n' ..
20+
' local i = input()\n' ..
21+
' return i and i:byte(1) or 0\n' ..
22+
'end\n' ..
23+
'local function interpret(s)\n' ..
24+
' local subst = {["+"]="v=v+1 ", ["-"]="v=v-1 ", [">"]="i=i+1 ", ["<"]="i=i-1 ",\n' ..
25+
' ["."] = "w(v)", [","]="v=r()", ["["]="while v~=0 do ", ["]"]="end "}\n' ..
26+
' local env = setmetatable({ i=0, t=setmetatable({},{__index=function() return 0 end}),\n' ..
27+
' r=function() return input_() end, w=function(c) output=output..string.char(c) end },\n' ..
28+
' {__index=function(t,k) return t.t[t.i] end, __newindex=function(t,k,v) t.t[t.i]=v end })\n' ..
29+
' load(s:gsub("[^%+%-<>%.,%[%]]+",""):gsub(".", subst), "brainfuck", "t", env)()\n' ..
30+
' print(output)\n' ..
31+
'end\n'
32+
33+
-- Compiles from an asset.
34+
function compile(asset)
35+
if not asset then
36+
error('Invalid asset.')
37+
end
38+
39+
local bytes = Project.main:read(asset)
40+
if not bytes then
41+
error('Invalid asset.')
42+
end
43+
bytes:poke(1)
44+
45+
local src = bytes:readString()
46+
local dst = ''
47+
for ln in src:gmatch('([^\n]*)\n?') do
48+
if #ln > 0 then
49+
local comment = ln:find('#')
50+
if comment then
51+
ln = ln:sub(1, comment - 1)
52+
end
53+
dst = dst .. ln
54+
end
55+
end
56+
local full = interpreter .. 'interpret(\'' .. dst .. '\')'
57+
58+
return load(full)
59+
end
60+
61+
data:text/txt;count=96;path=bf/README.txt;
62+
# BF compiler for Bitty Engine
63+
64+
Usage:
65+
require 'bf/compiler'
66+
f = compile('source.bf')
67+
f()
68+
69+
data:binary/octet;count=124;path=hello world.bf;
70+
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.++ #fasdf
71+
+.------.--------.>+.>.#adsg
72+
data:text/json;count=127;path=info.json;
73+
{
74+
"id": 0,
75+
"title": "BF",
76+
"description": "",
77+
"author": "Tony",
78+
"version": "1.0",
79+
"genre": "TUTORIAL",
80+
"url": ""
81+
}
82+
data:binary/octet;count=6;path=input.bf;
83+
,.,.,.

src/plugin/bf/README.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# BF compiler for Bitty Engine
2+
3+
Usage:
4+
require 'bf/compiler'
5+
f = compile('source.bf')
6+
f()

src/plugin/bf/compiler.lua

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--[[
2+
BF compiler for the Bitty Engine
3+
4+
Copyright (C) 2020 - 2021 Tony Wang, all rights reserved
5+
6+
Homepage: https://paladin-t.github.io/bitty/
7+
]]
8+
9+
-- Based on https://github.com/prapin/LuaBrainFuck.
10+
local interpreter = 'local output = \'\'\n' ..
11+
'local input_ = function()\n' ..
12+
' local i = input()\n' ..
13+
' return i and i:byte(1) or 0\n' ..
14+
'end\n' ..
15+
'local function interpret(s)\n' ..
16+
' local subst = {["+"]="v=v+1 ", ["-"]="v=v-1 ", [">"]="i=i+1 ", ["<"]="i=i-1 ",\n' ..
17+
' ["."] = "w(v)", [","]="v=r()", ["["]="while v~=0 do ", ["]"]="end "}\n' ..
18+
' local env = setmetatable({ i=0, t=setmetatable({},{__index=function() return 0 end}),\n' ..
19+
' r=function() return input_() end, w=function(c) output=output..string.char(c) end },\n' ..
20+
' {__index=function(t,k) return t.t[t.i] end, __newindex=function(t,k,v) t.t[t.i]=v end })\n' ..
21+
' load(s:gsub("[^%+%-<>%.,%[%]]+",""):gsub(".", subst), "brainfuck", "t", env)()\n' ..
22+
' print(output)\n' ..
23+
'end\n'
24+
25+
-- Compiles from an asset.
26+
function compile(asset)
27+
if not asset then
28+
error('Invalid asset.')
29+
end
30+
31+
local bytes = Project.main:read(asset)
32+
if not bytes then
33+
error('Invalid asset.')
34+
end
35+
bytes:poke(1)
36+
37+
local src = bytes:readString()
38+
local dst = ''
39+
for ln in src:gmatch('([^\n]*)\n?') do
40+
if #ln > 0 then
41+
local comment = ln:find('#')
42+
if comment then
43+
ln = ln:sub(1, comment - 1) -- Remove comment.
44+
end
45+
dst = dst .. ln -- Concat lines.
46+
end
47+
end
48+
local full = interpreter .. 'interpret(\'' .. dst .. '\')' -- Link the source code into the interpreter.
49+
50+
return load(full)
51+
end

src/plugin/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 Compiler",
4+
"description": "Compile BF source to Lua.",
5+
"author": "Tony",
6+
"version": "1.0",
7+
"genre": "COMPILER",
8+
"url": ""
9+
}

src/plugin/main.lua

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--[[
2+
Plugin for the Bitty Engine
3+
4+
Copyright (C) 2020 - 2021 Tony Wang, all rights reserved
5+
6+
Homepage: https://paladin-t.github.io/bitty/
7+
]]
8+
9+
local assets = {
10+
'bf/compiler.lua',
11+
'bf/README.txt'
12+
}
13+
14+
local tips = 'Usage:\n require \'bf/compiler\'\n f = compile(\'source.bf\')\n f()'
15+
local code = 'require \'bf/compiler\'\nf = compile(\'source.bf\')\nf()\n'
16+
17+
function usage()
18+
return { 'compiler' }
19+
end
20+
21+
function schema()
22+
return {
23+
-- Common.
24+
name = 'BF',
25+
extension = 'bf',
26+
27+
-- List of string.
28+
keywords = { },
29+
identifiers = { },
30+
quotes = { '\'', '"' },
31+
-- String.
32+
multiline_comment_start = nil,
33+
multiline_comment_end = nil,
34+
-- C++ regex.
35+
comment_patterns = { '\\#.*' },
36+
number_patterns = { },
37+
identifier_patterns = { },
38+
punctuation_patterns = { '[\\<\\>\\+\\-\\.\\,\\[\\]]' },
39+
-- Boolean.
40+
case_sensitive = true,
41+
-- List of string.
42+
assets = assets
43+
}
44+
end
45+
46+
function compiler()
47+
print('Install BF compiler to the current project.')
48+
49+
waitbox('Installing')
50+
:thus(function (rsp)
51+
local install = function (name)
52+
local data = Project.main:read(name)
53+
data:poke(1)
54+
Project.editing:write(name, data)
55+
end
56+
57+
for _, asset in ipairs(assets) do
58+
install(asset)
59+
end
60+
61+
print('Done.')
62+
63+
msgbox(tips)
64+
:thus(function ()
65+
Platform.setClipboardText(code)
66+
end)
67+
end)
68+
end

src/project/bf/README.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# BF compiler for Bitty Engine
2+
3+
Usage:
4+
require 'bf/compiler'
5+
f = compile('source.bf')
6+
f()

0 commit comments

Comments
 (0)