Lua: Autogenerate sysrc.cpp from sysrc.lua

This commit is contained in:
Ilari Liusvaara 2014-01-23 14:26:33 +02:00
parent b54a776f13
commit fa9971c2a0
6 changed files with 134 additions and 34 deletions

View file

@ -72,11 +72,13 @@ bsnes/out/libsnes.$(ARCHIVE_SUFFIX): forcelook
$(REALRANLIB) bsnes/out/libsnes.$(ARCHIVE_SUFFIX)
src/__all_files__: src/core/version.cpp buildaux/mkdeps.exe forcelook
src/__all_files__: src/core/version.cpp buildaux/mkdeps.exe buildaux/txt2cstr.exe forcelook
$(MAKE) -C src precheck
$(MAKE) -C src
cp src/lsnes$(DOT_EXECUTABLE_SUFFIX) .
buildaux/txt2cstr.exe: buildaux/txt2cstr.cpp
$(HOSTCC) $(HOSTCCFLAGS) -o $@ $<
buildaux/version.exe: buildaux/version.cpp VERSION
$(HOSTCC) $(HOSTCCFLAGS) -o $@ $<
buildaux/mkdeps.exe: buildaux/mkdeps.cpp VERSION

91
buildaux/txt2cstr.cpp Normal file
View file

@ -0,0 +1,91 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
const char* hexes = "0123456789ABCDEF";
struct encoder
{
encoder(std::ostream& _output) : output(_output)
{
have_quote = false;
}
size_t operator()(unsigned char* buf, size_t bufuse, bool eof)
{
if(!bufuse) return 0;
std::ostringstream out;
size_t i = 0;
while(i < bufuse) {
if(!have_quote) {
out << "\"";
have_quote = true;
}
unsigned char ch = buf[i];
if(ch == 9) {
out << "\\t";
} else if(ch == 10) {
out << "\\n\"" << std::endl;
have_quote = false;
} else if(ch == 13) {
out << "\\r";
} else if(ch < 32) {
out << "\\x" << hexes[(ch >> 4)] << hexes[ch & 15];
} else if(ch == '\"') {
out << "\\\"";
} else if(ch == '\\') {
out << "\\\\";
} else if(ch < 127) {
out << ch;
} else {
out << "\\x" << hexes[(ch >> 4)] << hexes[ch & 15];
}
i++;
}
output << out.str() << std::endl;
return i;
}
size_t operator()()
{
if(have_quote) {
output << "\"";
have_quote = false;
}
}
private:
std::ostream& output;
bool have_quote;
};
void do_encode(std::istream& input, std::ostream& output)
{
char buf[4096];
size_t bufuse = 0;
bool eof = false;
encoder e(output);
while(true) {
if(!eof) {
input.read(buf + bufuse, 4096 - bufuse);
bufuse += input.gcount();
}
if(!input)
eof = true;
size_t bytes = e(reinterpret_cast<unsigned char*>(buf), bufuse, eof);
memmove(buf, buf + bytes, bufuse - bytes);
bufuse -= bytes;
if(eof && !bufuse) break;
}
e();
}
int main(int argc, char** argv)
{
if(argc != 3) {
std::cerr << "Usage: txt2cstr <symbol> <file>" << std::endl;
return 1;
}
std::ifstream in(argv[2], std::ios::binary);
std::cout << "const char* " << argv[1] << " =" << std::endl;
do_encode(in, std::cout);
std::cout << ";" << std::endl;
}

2
src/lua/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
sysrc.cpp

View file

@ -1,4 +1,4 @@
OBJECTS=$(patsubst %.cpp,%.$(OBJECT_SUFFIX),$(wildcard *.cpp))
OBJECTS=$(patsubst %.cpp,%.$(OBJECT_SUFFIX),$(wildcard *.cpp)) sysrc.$(OBJECT_SUFFIX)
.PRECIOUS: %.$(OBJECT_SUFFIX) %.files
@ -6,6 +6,12 @@ __all__.files: $(OBJECTS)
lua ../genfilelist.lua $^ >$@
echo >__all__.ldflags
sysrc.cpp: sysrc.lua
../../buildaux/txt2cstr.exe lua_sysrc_script $< >$@
sysrc.cpp.dep: sysrc.cpp
touch sysrc.cpp.dep
%.$(OBJECT_SUFFIX): %.cpp %.cpp.dep
$(REALCC) -c -o $@ $< -I../../include $(CFLAGS)
@ -17,4 +23,4 @@ forcelook:
@true
clean:
rm -f *.$(OBJECT_SUFFIX) __all__.ldflags __all__.files
rm -f *.$(OBJECT_SUFFIX) __all__.ldflags __all__.files sysrc.cpp

View file

@ -1,31 +0,0 @@
const char* lua_sysrc_script =
"loopwrapper = function(fn, ...)\n"
" local routine = coroutine.create(fn);\n"
" local resume = function(...)\n"
" if coroutine.status(routine) ~= \"dead\" then\n"
" local x, y;\n"
" x, y = coroutine.resume(routine, ...);\n"
" if not x then\n"
" error(y);\n"
" end\n"
" end\n"
" end\n"
" local yield = function()\n"
" return coroutine.yield(routine);\n"
" end\n"
" resume(yield, ...);\n"
" return resume;\n"
"end;\n"
"print=print2;\n"
"loadfile=loadfile2;\n"
"dofile=dofile2;\n"
"memory2=memory2();\n"
"callback=callback();\n"
"render_queue_function=function(rq)\n"
" local _rq = rq;\n"
" return function()\n"
" _rq:run();\n"
" end;\n"
"end;\n"
"string.byteU=_lsnes_string_byteU;\n"
"string.charU=_lsnes_string_charU;\n";

30
src/lua/sysrc.lua Normal file
View file

@ -0,0 +1,30 @@
loopwrapper = function(fn, ...)
local routine = coroutine.create(fn);
local resume = function(...)
if coroutine.status(routine) ~= "dead" then
local x, y;
x, y = coroutine.resume(routine, ...);
if not x then
error(y);
end
end
end
local yield = function()
return coroutine.yield(routine);
end
resume(yield, ...);
return resume;
end;
print=print2;
loadfile=loadfile2;
dofile=dofile2;
memory2=memory2();
callback=callback();
render_queue_function=function(rq)
local _rq = rq;
return function()
_rq:run();
end;
end;
string.byteU=_lsnes_string_byteU;
string.charU=_lsnes_string_charU;