Skip to content

Commit

Permalink
lua.lua works as a standalone interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
leegao committed Dec 26, 2016
1 parent 95ec338 commit 35e47a3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
3 changes: 3 additions & 0 deletions luainlua-0.1-0.rockspec
Expand Up @@ -20,6 +20,9 @@ dependencies = {
}
build = {
type = "builtin",
install = {
bin = {"luainlua/lua.lua"},
},
modules = {
['luainlua.bytecode.dump'] = 'luainlua/bytecode/dump.lua',
['luainlua.bytecode.ir'] = 'luainlua/bytecode/ir.lua',
Expand Down
8 changes: 4 additions & 4 deletions luainlua/bytecode/undump.lua
Expand Up @@ -38,10 +38,10 @@ local function constant(ctx)
end

function undump.load_header(ctx)
assert(ctx:int() == 0x61754c1b) -- ESC. Lua
assert(ctx:byte() == 0x52) -- version
assert(ctx:byte() == 0) -- format version
assert(ctx:byte() == 1) -- little endian
assert(ctx:int() == 0x61754c1b, "Make sure you're running vanilla Lua 5.2") -- ESC. Lua
assert(ctx:byte() == 0x52, "Make sure you're running vanilla Lua 5.2") -- version
assert(ctx:byte() == 0, "Not expecting extensions.") -- format version
assert(ctx:byte() == 1, "Not expecting big-endianess.") -- little endian
if not undump.sizeof_int then
undump.sizeof_int = assert(ctx:byte()) -- sizeof(int)
undump.sizeof_sizet = assert(ctx:byte()) -- sizeof(size_t)
Expand Down
12 changes: 9 additions & 3 deletions luainlua/lua.lua
Expand Up @@ -3,11 +3,17 @@ local argparse = require 'argparse'

function main(arg)
local parser = argparse("script", "An example.")
parser:argument("input", "Input file.")
parser:argument("input", "Input file.", "--")
parser:option("-d --dump"):args "?"

local args = parser:parse()
local func, bytecode, prototype, dumper = luac.compile(io.open(args.input, 'r'):read('*all'))
local code
if (args.input == '--') then
code = io.read("*all")
else
code = io.open(args.input, 'r'):read('*all')
end
local args = parser:parse()
local func, bytecode, prototype, dumper = luac.compile(code)
if args.dump then
dumper(prototype)
print("--- END OF DUMP ---")
Expand Down

0 comments on commit 35e47a3

Please sign in to comment.