<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,17 +1,22 @@
+2008-08-01
+  added: -C (C source generation only) command-line switch to clua.
+  added: debugging: assert stack checks between statements
+  fixed: warning from &quot;unused variable `lc_nextra'&quot;
+
 2008-07-31
+  added: -c (compile only) command-line switch to clua.
   fixed numeric ops with metamethods returning non-numbers
   fixed: handle &quot;(error object is not a string)&quot;
   fixed: elseif expression evaluation evaluation order
-  added: -c (compile only) command-line switch to clua.
 
 2008-07-30
+  implemented: ignore shebang lines (#...)
+  implemented: globals via lua_getfield/lua_setfield/LUA_ENVIRONINDEX
+    rather than lua_getglobal/lua_setglobal.
   fixed bug in local function when an upvalue.
   fixed mlp/gg lexer bug in numeric escapes \ddd in strings.
   fixed mlp/gg lexer bug in &quot;\\\n&quot; in strings.
   fixed mlp/gg lexer bug in numbers, e.g. &quot;.1&quot;
-  implemented: ignore shebang lines (#...)
-  implemented: globals via lua_getfield/lua_setfield/LUA_ENVIRONINDEX
-    rather than lua_getglobal/lua_setglobal.
 
 2008-07-29
   optimized locals not used as upvalues</diff>
      <filename>CHANGES</filename>
    </modified>
    <modified>
      <diff>@@ -9,13 +9,11 @@ dist :
 	DIR=lua2c-$(TAG) ; \
 	    (rm -fr $$DIR &amp;&amp; \
 	     mkdir $$DIR &amp;&amp; \
-	     mkdir $$DIR/examples &amp;&amp; \
 	     mkdir $$DIR/examples-lua &amp;&amp; \
 	     mkdir $$DIR/lib &amp;&amp; \
 	     mkdir $$DIR/lib/metalua &amp;&amp; \
 	     cp README LICENSE CHANGES Makefile lua2c.lua clua \
 	       $$DIR &amp;&amp; \
-	     cp examples/*.lua $$DIR/examples/ &amp;&amp; \
 	     cp examples-lua/*.lua $$DIR/examples-lua/ &amp;&amp; \
 	     cp lib/*.lua $$DIR/lib/ &amp;&amp; \
 	     cp lib/metalua/*.lua $$DIR/lib/metalua/ &amp;&amp; \</diff>
      <filename>Makefile</filename>
    </modified>
    <modified>
      <diff>@@ -3,14 +3,16 @@ lua2c - converts Lua 5.1 source code to C code.
 == Description ==
 
 This utility converts a given Lua source file into an equivalent C
-source file containing Lua C API calls.  At least, this is true for a
-large subset of the Lua language (see limitations below).
+source file written in terms of Lua C API calls.  At least, this works
+for a large subset of the Lua language (see limitations below).
+
+The compiler is written entirely in Lua, and no build/install is
+needed.  This project reuses Metalua's gg/mlp parser to convert Lua
+source to a Metalua-style[1] AST over which the compiler then
+operates.  lua2c does not require Metalua itself though since gg/mlp
+is bundled in the distribution (patched metalua-0.4.1-rc1) and is
+written in pure Lua.
 
-The compiler is written entirely in Lua.  This project reuses
-Metalua's gg/mlp parser to convert Lua source to a Metalua-style[1]
-AST over which the compiler then operates.  gg/mlp is bundled in the
-distribution (patched metalua-0.4.1-rc1) and is written in pure Lua,
-so no build/install is needed.  lua2c does not require Metalua itself.
 
 == Usage ==
 
@@ -22,8 +24,8 @@ which generates a C file to standard output.
 
 You may also use the shell script &quot;clua&quot; to compile Lua-&gt;C-&gt;machine
 code and execute all in one step.  However, you may need to edit the
-variables in the file to match your system.  This utility invokes the
-C compiler.
+variables in the file to match your system since this utility invokes
+the C compiler.
 
   ./clua test/bisect.lua
 
@@ -36,16 +38,16 @@ without running.)
 
 == Related Work ==
 
-  * luac2c - This related effort Hisham Muhammad[2] converts Lua
+  * luac2c - This related effort by Hisham Muhammad[2] converts Lua
     bytecodes to C source, whereas this project converts Lua
     source to C source.  luac2c runs into a few similar
-    limitations as given above.  luac2c has remained experimental.
-    It superseded Luiz Henrique De Figueiredo's very basic but
-    similarly named lua2c for Lua 4.0.[3]
+    limitations as given below.  luac2c has remained experimental but
+    superseded Luiz Henrique De Figueiredo's very basic but similarly
+    named lua2c for Lua 4.0[3].
 
   * luagen++[4] uses C++ expression templates to translate
-    Lua-like C++ statements to C API calls.  The code generation
-    structure is actually fairly similar to lua2c.
+    Lua-like C++ statements to C API calls.  Some of the code
+    generation structure is actually fairly similar to lua2c.
 
   * Python Pyrex[5] does something similar in Python but has the
     added goal of lowering the boundary between C and Python
@@ -55,16 +57,19 @@ without running.)
   * Clue by David Given[6] does the opposite: convert C source
     to Lua source.
 
+  * luac + BinToCee allow compilation of Lua source to bytecodes
+    and/or embedding in a C file.
+
 == Potential Uses ==
 
-I think this project not only is nice theoretically to have but
+I think this project not only is theoretically nice to have but
 has a number of potential uses:
 
   * Provide another approach of compiling Lua to machine code
-    (rather than luac + bin2c)
+    (rather than luac + bin2c).
 
   * Streamline the interface between C and Lua code and allow
-    efficient access to C datatypes from Lua (see Pyrex above)
+    efficient access to C datatypes from Lua (see Pyrex above).
 
   * Compile Lua to optimized C.  For example, by statically
     determining that certain variables are used in a restricted
@@ -72,25 +77,27 @@ has a number of potential uses:
     determining this implicitly by inference), certain code
     constructs might be simplified to use plain C rather that
     the Lua C API.  This could allow certain Lua code written
-    with sufficient care to run at native speeds.
+    with sufficient care to run at native speeds.  Since it compiles
+    to C, it will even work on CPUs where LuaJIT is not available.
 
 == Limitations / Status ==
 
-lua2c does not currently support coroutines, tail call optimizations,
-and functions that normally reject C functions (e.g. setfenv).
-These don't have the exact analogue in C.  Coroutines might not ever
-be supported. However, some solutions might be explored (Coco or
-simulating coroutines in C with a lot of ugly code).
-
-Closures and upvalues are implemented, but creating and accessing
-upvalues is somewhat slow.
-
 WARNING: This code passes much of the Lua 5.1 test suite[7] and can
 compile itself, but the code is new and there can still be errors.
 In particular, a few language features (e.g. coroutines) are not
 implemented.  See comments in lua2c.lua for details.  Please report
 bugs/patches on the wiki.
 
+lua2c does not currently support coroutines, functions that normally
+reject C functions (e.g. setfenv), and possibly tail call
+optimizations.  Not all of these have the exact analogue in C.
+Coroutines might not ever be supported. However, some solutions might
+be explored[8][9], including possibly generating code that maintains
+the coroutine context in Lua tables.
+
+Closures and upvalues are implemented, but creating and accessing
+upvalues is somewhat slow.
+
 Once the code is more complete/robust, more attention can be given to
 optimizing the code generation.
 
@@ -102,6 +109,7 @@ The project page is currently http://lua-users.org/wiki/LuaToCee .
 
 (c) 2008 David Manura.  Licensed under the same terms as Lua (MIT
 license).  See included LICENSE file for full licensing details.
+Please post any patches/improvements.
 
 == References ==
 
@@ -113,3 +121,9 @@ license).  See included LICENSE file for full licensing details.
   * [6] Clue - http://cluecc.sourceforge.net/
   * [7] Lua 5.1 test suite -
         http://www.inf.puc-rio.br/~roberto/lua/lua5.1-tests.tar.gz
+  * [8] Wikipedia:Coroutine - Implementations for C
+        http://en.wikipedia.org/wiki/Coroutine#Implementations_for_C
+  * [9] Coco - http://luajit.org/coco.html Coco -
+        True C coroutine semantics (used in LuaJIT)
+  * [10] BinToCee - http://lua-users.org/wiki/BinToCee
+</diff>
      <filename>README</filename>
    </modified>
    <modified>
      <diff>@@ -2,30 +2,50 @@
 #
 # Shell script wrapper to convert Lua to C and optionally
 # compile and run it.
+#
+# option -c causes compilation only (no run)
+# option -C causes C source generation only (no compile or run)
 
 # You may need to change these variables:
 LUA=lua
 CC=gcc
-#CFLAGS=-g
 CFLAGS=&quot;-O2 -DNDEBUG -Wall -Ilua/src&quot;
+#CFLAGS=-g
 LFLAGS=&quot;-Llua/src -llua&quot;
+LUA2C=&quot;${LUA} lua2c.lua&quot;
+#LUA2C=&quot;./lua2c&quot;
 
-# option -c causes compilation only (no run)
 COMPILEONLY=
 if [ &quot;$1&quot; = &quot;-c&quot; ]
 then
   COMPILEONLY=1
   shift
+elif [ &quot;$1&quot; = &quot;-C&quot; ]
+then
+  COMPILEONLY=2
+  shift
+fi
+if [ &quot;$1&quot; = &quot;&quot; ]
+then
+  echo &quot;usage: clua [options] [filename.lua] ...&quot;
+  echo &quot;  options:&quot;
+  echo &quot;    -c   compile only (no run)&quot;
+  echo &quot;    -C   generate C source only (no compile or run)&quot;
+  exit 1
 fi
 
 LUAFILE=$1
 FILENAME=${LUAFILE%.*}
 CFILE=${FILENAME}.c
 
-${LUA} lua2c.lua ${LUAFILE} &gt; ${CFILE} || exit 1
-${CC} ${CFLAGS} ${CFILE} -o ${FILENAME} ${LFLAGS} || exit 1
+${LUA2C} ${LUAFILE} &gt; ${CFILE} || exit 1
+if [ &quot;$COMPILEONLY&quot; = &quot;2&quot; ]
+then
+  exit 0
+fi
 
-if [ $COMPILEONLY ]
+${CC} ${CFLAGS} ${CFILE} -o ${FILENAME} ${LFLAGS} || exit 1
+if [ &quot;$COMPILEONLY&quot; = &quot;1&quot; ]
 then
   exit 0
 fi</diff>
      <filename>clua</filename>
    </modified>
    <modified>
      <diff>@@ -8,20 +8,18 @@
 --   Unimplemented Lua language features:
 --    - deprecated old style vararg (arg) table inside vararg functions
 --      (LUA_COMPAT_VARARG)
---    - debug.getinfo(f, 'n').name for functions implemented in C
---    - setfenv does not permit functions implemented in C
+--    - 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?
---      Coco useful here? [ http://luajit.org/coco.html ]
+--    - how to handle coroutines? (see README)
 --    Note: A few things (coroutines) might remain
 --      unimplemented--see README file file for details.
 --
 --   Possible improvements:
---    - gcc warning: unused variable `lc_nextra'
---    - Literal numbers are rendered as C integers literals (e.g. 123)
---      rather than C double literals (eg. 123.0).  Therefore, large values
---      can give gcc warnings such as 'warning: integer constant is too
---      large for &quot;long&quot; type').
+--    - Fix: large numerical literals can give gcc warnings such as
+--      'warning: integer constant is too large for &quot;long&quot; type').
+--      Literal numbers are rendered as C integers literals (e.g. 123)
+--      rather than C double literals (eg. 123.0).  
 --    - Generated C functions could be named based on the Lua function
 --      name.
 --    - improved debug tracebacks on exceptions
@@ -31,6 +29,7 @@
 --
 --   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.
 --
 
@@ -124,6 +123,7 @@ local currentscope = {['.closurelevel'] = 0}
 -- Information on function currently being compiled.
 local funcinfo = {is_vararg=false, nformalparams=0,
   is_lc_nextra_used=false, is_lc_nactualargs_used=false,
+  is_lc_nextra_used_debug=false, is_lc_nactualargs_used_debug=false,
   idxtopmax = 0
   --old: outervars={}
 }
@@ -280,8 +280,7 @@ local function realidx(idx)
      (tag(idxreal) == 'Id' or idxreal &gt; funcinfo.nformalparams)
   then
     if tag(idx) == 'Upval' then
-      idx = {tag='Upval', C.Op('+', idxreal, C.Id'lc_nextra'), idx[2], idx[3]}
-      funcinfo.is_lc_nextra_used = true
+      -- no adjustment
     else
       idx = C.Op('+', idx, C.Id'lc_nextra')
       funcinfo.is_lc_nextra_used = true
@@ -311,7 +310,7 @@ local function adjustidx(offset, idx)
   if type(idx) == 'table' or idx &gt; 0 then
     return idx
   else -- relative stack index or pseudoindex
-     -- FIX:pseudoindices not supported
+     -- FIX:pseudoindices not supported?
     return idx + offset + 1
   end
 end
@@ -813,6 +812,7 @@ local function genfunctiondef(ast, ismain)
   local nformalargs = #params_ast - (has_vararg and 1 or 0)
   funcinfo = {is_vararg = has_vararg, nformalparams = nformalargs,
     is_lc_nextra_used = false, is_lc_nactualargs_used=false,
+    is_lc_nextra_used_debug = false, is_lc_nactualargs_used_debug=false,
     idxtopmax = 0
     --old: outervars = currentscope
   }
@@ -890,13 +890,25 @@ local function genfunctiondef(ast, ismain)
     -- (important for genexpr of `Id)
     if funcinfo.is_lc_nextra_used then
       funcinfo.is_lc_nactualargs_used = true
+    elseif funcinfo.is_lc_nextra_used_debug then
+      funcinfo.is_lc_nactualargs_used_debug = true
     end
+
     if funcinfo.is_lc_nactualargs_used then
       bodypre_cast:append(C.LetInt('lc_nactualargs', C.lua_gettop()))
+    elseif funcinfo.is_lc_nactualargs_used_debug then
+      bodypre_cast:append(C.C'#ifndef NDEBUG')
+      bodypre_cast:append(C.LetInt('lc_nactualargs', C.lua_gettop()))
+      bodypre_cast:append(C.C'#endif')
     end
     if funcinfo.is_lc_nextra_used then
       bodypre_cast:append(C.LetInt('lc_nextra',
         C.Op('-', C.Id'lc_nactualargs', C.Id'lc_nformalargs')))
+    elseif funcinfo.is_lc_nextra_used_debug then
+      bodypre_cast:append(C.C'#ifndef NDEBUG')
+      bodypre_cast:append(C.LetInt('lc_nextra',
+        C.Op('-', C.Id'lc_nactualargs', C.Id'lc_nformalargs')))
+      bodypre_cast:append(C.C'#endif')
     end
   else
     bodypre_cast:append(C.lua_settop(#params_ast))
@@ -1820,9 +1832,18 @@ function genblock(ast)
   for _,stat_ast in ipairs(ast) do
     cast:append(genstatement(stat_ast))
 
-    --FIX:DEBUG: funcinfo.is_lc_nextra_used = true
-    --cast:append(C.C([[ assert(lua_gettop(L) - lc_nextra == ]]
-    -- .. idxtop .. [[);]]))
+    -- DEBUG
+    if true then
+      if funcinfo.is_vararg then
+        funcinfo.is_lc_nextra_used_debug = true
+      end
+      if not is_created['assert.h'] then
+        append_array(cast.pre, {C.Include'&lt;assert.h&gt;'})
+        is_created['assert.h'] = true
+      end
+      cast:append(C.C(string.format([[assert(lua_gettop(L) %s== %d);]],
+        funcinfo.is_lc_nextra_used_debug and &quot;- lc_nextra &quot; or &quot;&quot;, idxtop)))
+    end
 
   end
   return cast
@@ -2251,7 +2272,7 @@ if not src_filename then
   os.exit(1)
 end
 
-local src_file = io.open (src_filename, 'r')
+local src_file = assert(io.open (src_filename, 'r'))
 src            = src_file:read '*a'; src_file:close()
 src = src:gsub('^#[^\r\n]*', '') -- remove any shebang
 </diff>
      <filename>lua2c.lua</filename>
    </modified>
  </modified>
  <removed type="array">
    <removed>
      <filename>examples/test1.lua</filename>
    </removed>
  </removed>
  <parents type="array">
    <parent>
      <id>8ef69e1343c51e0fa507f07d5507b8c4f467395b</id>
    </parent>
  </parents>
  <author>
    <name>David Manura</name>
    <email>dm.git@math2.org</email>
  </author>
  <url>http://github.com/davidm/lua2c/commit/f4cc79b1e0d2efa85f33d75586c69efe336a161d</url>
  <id>f4cc79b1e0d2efa85f33d75586c69efe336a161d</id>
  <committed-date>2008-07-31T20:50:31-07:00</committed-date>
  <authored-date>2008-07-31T20:50:31-07:00</authored-date>
  <message>2008-08-01
  added: -C (C source generation only) command-line switch to clua.
  added: debugging: assert stack checks between statements
  fixed: warning from &quot;unused variable `lc_nextra'&quot;

Imported from lua2c-0.2-20080801.tar.gz.</message>
  <tree>9eb6d68d90394187af7ea09f469ad512665bc455</tree>
  <committer>
    <name>David Manura</name>
    <email>dm.git@math2.org</email>
  </committer>
</commit>
