public
Description: convert Lua source code into an equivalent C source code written in terms of Lua C API calls
Homepage: http://lua-users.org/wiki/LuaToCee
Clone URL: git://github.com/davidm/lua2c.git
lua2c / lua2c.lua
100644 100 lines (86 sloc) 2.892 kb
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-- lua2c.lua - Driver for lua2c - converts Lua 5.1 source to C code.
--
-- STATUS:
-- WARNING: This code passes much of the Lua 5.1 test suite,
-- but there could still be errors. In particular, a few
-- language features (e.g. coroutines) are not implemented.
--
-- Unimplemented Lua language features:
-- - deprecated old style vararg (arg) table inside vararg functions
-- (LUA_COMPAT_VARARG)
-- - debug.getinfo(f, 'n').name for C-based functions
-- - setfenv does not permit C-based functions
-- - how well do tail call optimizations work?
-- - how to handle coroutines? (see README)
-- Note: A few things (coroutines) might remain
-- unimplemented--see README file file for details.
--
-- Possible improvements:
-- - Fix: large numerical literals can give gcc warnings such as
-- 'warning: integer constant is too large for "long" type').
-- Literal numbers are rendered as C integers literals (e.g. 123)
-- rather than C double literals (eg. 123.0).
-- - improved debug tracebacks on exceptions
-- - See items marked FIX in below code.
--
-- SOURCE:
--
-- http://lua-users.org/wiki/LuaToCee
--
-- (c) 2008 David Manura. Licensed in the same terms as Lua (MIT license).
-- See included LICENSE file for full licensing details.
-- Please post any patches/improvements.
--
 
local _G = _G
local assert = _G.assert
local error = _G.error
local io = _G.io
local ipairs = _G.ipairs
local os = _G.os
local package = _G.package
local require = _G.require
local string = _G.string
local table = _G.table
 
-- package.path = './lib/?.lua;' .. package.path
 
-- note: includes gg/mlp Lua parsing Libraries taken from Metalua.
require "lexer"
require "gg"
require "mlp_lexer"
require "mlp_misc"
require "mlp_table"
require "mlp_meta"
require "mlp_expr"
require "mlp_stat"
require "mlp_ext"
_G.mlc = {} -- make gg happy
local mlp = assert(_G.mlp)
local A2C = require "lua2c.ast2cast"
local C2S = require "lua2c.cast2string"
 
local function NOTIMPL(s)
  error('FIX - NOT IMPLEMENTED: ' .. s, 2)
end
 
local function DEBUG(...)
  local ts = {...}
  for i,v in ipairs(ts) do
    ts[i] = table.tostring(v,'nohash',60)
  end
  io.stderr:write(table.concat(ts, ' ') .. '\n')
end
 
-- Converts Lua source string to Lua AST (via mlp/gg)
local function string_to_ast(src)
  local lx = mlp.lexer:newstream (src)
  local ast = mlp.chunk (lx)
  return ast
end
 
local src_filename = ...
 
if not src_filename then
  io.stderr:write("usage: lua2c filename.lua\n")
  os.exit(1)
end
 
local src_file = assert(io.open (src_filename, 'r'))
local src = src_file:read '*a'; src_file:close()
src = src:gsub('^#[^\r\n]*', '') -- remove any shebang
 
local ast = string_to_ast(src)
 
local cast = A2C.ast_to_cast(src, ast)
-- DEBUG(cast)
 
io.stdout:write(C2S.cast_to_string(cast))