aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/orig-5.1.3/luac.lua
blob: 5f12f790fea4905e1d433712b28ba4e8dd212664 (plain)
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-2007 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.1.2 distribution
-- * usage: lua luac.lua file.lua
----------------------------------------------------------------------]]

------------------------------------------------------------------------
-- load and initialize the required modules
------------------------------------------------------------------------
dofile("lzio.lua")
dofile("llex.lua")
dofile("lopcodes.lua")
dofile("ldump.lua")
dofile("lcode.lua")
dofile("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)
  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, "@"..filename)
  -- 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"))
assert(f:write(assert(yloadfile(arg[1]))))
assert(io.close(f))

--end