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