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
|
--[[--------------------------------------------------------------------
luac.lua
Primitive luac in Lua
This file is part of Yueliang.
Copyright (c) 2005 Kein-Hong Man <khman@users.sf.net>
The COPYRIGHT file describes the conditions
under which this software may be distributed.
See the ChangeLog for more information.
----------------------------------------------------------------------]]
--[[--------------------------------------------------------------------
-- Notes:
-- * based on luac.lua in the test directory of the 5.0.3 distribution
-- * usage: lua luac.lua file.lua
----------------------------------------------------------------------]]
------------------------------------------------------------------------
-- load and initialize the required modules
------------------------------------------------------------------------
require("lzio.lua")
require("llex.lua")
require("lopcodes.lua")
require("ldump.lua")
require("lcode.lua")
require("lparser.lua")
luaX:init() -- required by llex
local LuaState = {} -- dummy, not actually used, but retained since
-- the intention is to complete a straight port
------------------------------------------------------------------------
-- interfacing to yueliang
------------------------------------------------------------------------
-- currently asserts are enabled because the codebase hasn't been tested
-- much (if you don't want asserts, just comment them out)
function lua_assert(test)
if not test then error("assertion failed!") end
end
function yloadfile(filename)
-- luaZ:make_getF returns a file chunk reader
-- luaZ:init returns a zio input stream
local zio = luaZ:init(luaZ:make_getF(filename), nil, "@"..filename)
if not zio then return end
-- luaY:parser parses the input stream
-- func is the function prototype in tabular form; in C, func can
-- now be used directly by the VM, this can't be done in Lua
local func = luaY:parser(LuaState, zio, nil)
-- luaU:make_setS returns a string chunk writer
local writer, buff = luaU:make_setS()
-- luaU:dump builds a binary chunk
luaU:dump(LuaState, func, writer, buff)
-- a string.dump equivalent in returned
return buff.data
end
------------------------------------------------------------------------
-- command line interface
------------------------------------------------------------------------
assert(arg[1] ~= nil and arg[2] == nil, "usage: lua luac.lua file.lua")
local f = assert(io.open("luac.out","wb"))
f:write(assert(yloadfile(arg[1])))
io.close(f)
--end
|