【发布时间】:2015-04-04 19:33:07
【问题描述】:
所以,我被要求为最简单的代码制作一个字节码:
print("Hello, world!")
但我不知道怎么做,我似乎找不到任何关于如何制作的信息......有人可以帮忙吗?我使用 Lua for Windows 作为编译器。非常感谢
【问题讨论】:
标签: lua
所以,我被要求为最简单的代码制作一个字节码:
print("Hello, world!")
但我不知道怎么做,我似乎找不到任何关于如何制作的信息......有人可以帮忙吗?我使用 Lua for Windows 作为编译器。非常感谢
【问题讨论】:
标签: lua
您可以使用Lua compiler(参见luac手册):
# the default output is "luac.out"
echo 'print("Hello, world!")' | luac -
# you can execute this bytecode with the Lua interpreter
lua luac.out
# -> Hello, world!
【讨论】:
你可以在没有 luac 的 Lua 中使用 string.dump 来做到这一点。试一试
f=assert(io.open("luac.out","wb"))
assert(f:write(string.dump(assert(loadfile("foo.lua")))))
assert(f:close())
如果要编译的代码是字符串,使用load(s)。
您也可以将下面的文件保存为luac.lua并从命令行运行:
-- bare-bones luac in Lua
-- usage: lua luac.lua file.lua
assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua")
f=assert(io.open("luac.out","wb"))
assert(f:write(string.dump(assert(loadfile(arg[1])))))
assert(f:close())
【讨论】:
arg[2]。您必须更改第一个 assert 。
bar.lua,因为 Lua 会透明地加载预编译的字节码。