Skip to content

Commit

Permalink
Added Simplify() int othe AST, fixed LASM bug with empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
efrederickson committed Jan 10, 2013
1 parent 6e6c31c commit f515f9d
Show file tree
Hide file tree
Showing 39 changed files with 1,263 additions and 762 deletions.
2 changes: 2 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ supports all standard lua functions. It does not support C Libraries.
To use in your projects, just add a reference to the SharpLua.dll file and use the
SharpLua.LuaInterface class or the SharpLua.LuaRuntime, SharpLua.Lua is the raw Lua API.

NOTE: If you simplify the AST, you CAN NOT use a reconstructor that operates on the tokens unless you patch the tokens, as the Simplification process does not do that

Features Lua 5.1.5 doesn't have:
- table.unpack (similar to _G.unpack)
- a large extension library
Expand Down
10 changes: 5 additions & 5 deletions SharpLua/LASM/Chunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ public string Compile(LuaFile file)
Func<string, string> DumpString = new Func<string, string>(delegate(string s)
{
int len = file.SizeT;
if (s == null || s.Length == 0)
return "\0".Repeat(len);
else
{
//if (s == null || s.Length == 0)
// return "\0".Repeat(len);
//else
//{
string l = DumpInt(s.Length + 1);
return l + s + "\0";
}
//}
});

string c = "";
Expand Down
3 changes: 2 additions & 1 deletion SharpLua/LuaCore/Libraries/lbaselib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ private static int luaB_newproxy(LuaState L)
/// <returns></returns>
static int luaB_wait(LuaState L)
{
int wait = luaL_checkint(L, 1);
int wait = -1;
wait = luaL_optint(L, 1, 25);
if (lua_isboolean(L, 2))
{
if (lua_toboolean(L, 2) == 1)
Expand Down
135 changes: 72 additions & 63 deletions SharpLua/LuaCore/VM/lapi.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
** Lua API
** See Copyright Notice in lua.h
*/
** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
** Lua API
** See Copyright Notice in lua.h
*/

using System;
using System.Collections.Generic;
Expand All @@ -23,9 +23,9 @@ namespace SharpLua
public partial class Lua
{
public const string lua_ident =
"$Lua: " + LUA_RELEASE + " " + LUA_COPYRIGHT + " $\n" +
"$Authors: " + LUA_AUTHORS + " $\n" +
"$URL: www.lua.org $\n";
"$Lua: " + LUA_RELEASE + " " + LUA_COPYRIGHT + " $\n" +
"$Authors: " + LUA_AUTHORS + " $\n" +
"$URL: www.lua.org $\n";

public static void api_checknelems(LuaState L, int n)
{
Expand Down Expand Up @@ -59,31 +59,31 @@ public static TValue index2adr(LuaState L, int idx)
}
else
switch (idx)
{ /* pseudo-indices */
{ /* pseudo-indices */
case LUA_REGISTRYINDEX: return registry(L);
case LUA_ENVIRONINDEX:
{
Closure func = curr_func(L);
sethvalue(L, L.env, func.c.env);
return L.env;
}
case LUA_ENVIRONINDEX:
{
Closure func = curr_func(L);
sethvalue(L, L.env, func.c.env);
return L.env;
}
case LUA_GLOBALSINDEX: return gt(L);
default:
{
Closure func = curr_func(L);
idx = LUA_GLOBALSINDEX - idx;
return (idx <= func.c.nupvalues)
? func.c.upvalue[idx - 1]
: (TValue)luaO_nilobject;
}
}
default:
{
Closure func = curr_func(L);
idx = LUA_GLOBALSINDEX - idx;
return (idx <= func.c.nupvalues)
? func.c.upvalue[idx - 1]
: (TValue)luaO_nilobject;
}
}
}


internal static Table getcurrenv(LuaState L)
{
if (L.ci == L.base_ci[0]) /* no enclosing function? */
return hvalue(gt(L)); /* use global table as environment */
return hvalue(gt(L)); /* use global table as environment */
else
{
Closure func = curr_func(L);
Expand Down Expand Up @@ -166,8 +166,8 @@ public static LuaState lua_newthread(LuaState L)


/*
** basic stack manipulation
*/
** basic stack manipulation
*/


public static int lua_gettop(LuaState L)
Expand Down Expand Up @@ -241,7 +241,7 @@ public static void lua_replace(LuaState L, int idx)
{
setobj(L, o, L.top - 1);
if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
luaC_barrier(L, curr_func(L), L.top - 1);
luaC_barrier(L, curr_func(L), L.top - 1);
}
StkId.dec(ref L.top);
lua_unlock(L);
Expand All @@ -259,8 +259,8 @@ public static void lua_pushvalue(LuaState L, int idx)


/*
** access functions (stack . C)
*/
** access functions (stack . C)
*/


public static int lua_type(LuaState L, int idx)
Expand Down Expand Up @@ -311,7 +311,7 @@ public static int lua_rawequal(LuaState L, int index1, int index2)
StkId o1 = index2adr(L, index1);
StkId o2 = index2adr(L, index2);
return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
: luaO_rawequalObj(o1, o2);
: luaO_rawequalObj(o1, o2);
}


Expand All @@ -336,7 +336,7 @@ public static int lua_lessthan(LuaState L, int index1, int index2)
o1 = index2adr(L, index1);
o2 = index2adr(L, index2);
i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
: luaV_lessthan(L, o1, o2);
: luaV_lessthan(L, o1, o2);
lua_unlock(L);
return i;
}
Expand Down Expand Up @@ -402,8 +402,8 @@ public static uint lua_objlen(LuaState L, int idx)
StkId o = index2adr(L, idx);
switch (ttype(o))
{
case LUA_TSTRING: return tsvalue(o).len;
case LUA_TUSERDATA: return uvalue(o).len;
case LUA_TSTRING: return tsvalue(o).len;
case LUA_TUSERDATA: return uvalue(o).len;
case LUA_TTABLE:
// Table now respects __len metamethod
Table h = hvalue(o);
Expand All @@ -421,7 +421,7 @@ public static uint lua_objlen(LuaState L, int idx)
lua_unlock(L);
return l;
}
default: return 0;
default: return 0;
}
}

Expand All @@ -438,9 +438,9 @@ public static object lua_touserdata(LuaState L, int idx)
StkId o = index2adr(L, idx);
switch (ttype(o))
{
case LUA_TUSERDATA: return (rawuvalue(o).user_data);
case LUA_TLIGHTUSERDATA: return pvalue(o);
default: return null;
case LUA_TUSERDATA: return (rawuvalue(o).user_data);
case LUA_TLIGHTUSERDATA: return pvalue(o);
default: return null;
}
}

Expand All @@ -457,21 +457,21 @@ public static object lua_topointer(LuaState L, int idx)
StkId o = index2adr(L, idx);
switch (ttype(o))
{
case LUA_TTABLE: return hvalue(o);
case LUA_TFUNCTION: return clvalue(o);
case LUA_TTHREAD: return thvalue(o);
case LUA_TTABLE: return hvalue(o);
case LUA_TFUNCTION: return clvalue(o);
case LUA_TTHREAD: return thvalue(o);
case LUA_TUSERDATA:
case LUA_TLIGHTUSERDATA:
return lua_touserdata(L, idx);
default: return null;
default: return null;
}
}



/*
** push functions (C . stack)
*/
** push functions (C . stack)
*/


public static void lua_pushnil(LuaState L)
Expand Down Expand Up @@ -521,7 +521,7 @@ public static void lua_pushstring(LuaState L, CharPtr s)


public static CharPtr lua_pushvfstring(LuaState L, CharPtr fmt,
object[] argp)
object[] argp)
{
CharPtr ret;
lua_lock(L);
Expand Down Expand Up @@ -600,8 +600,8 @@ public static int lua_pushthread(LuaState L)


/*
** get functions (Lua . stack)
*/
** get functions (Lua . stack)
*/


public static void lua_gettable(LuaState L, int idx)
Expand Down Expand Up @@ -720,8 +720,8 @@ public static void lua_getfenv(LuaState L, int idx)


/*
** set functions (stack . Lua)
*/
** set functions (stack . Lua)
*/


public static void lua_settable(LuaState L, int idx)
Expand Down Expand Up @@ -855,8 +855,8 @@ public static int lua_setfenv(LuaState L, int idx)


/*
** `load' and `call' functions (run Lua code)
*/
** `load' and `call' functions (run Lua code)
*/


public static void adjustresults(LuaState L, int nres)
Expand Down Expand Up @@ -887,8 +887,8 @@ public static void lua_call(LuaState L, int nargs, int nresults)


/*
** Execute a protected call.
*/
** Execute a protected call.
*/
public class CallS
{ /* data to `f_call' */
public StkId func;
Expand Down Expand Up @@ -930,8 +930,8 @@ public static int lua_pcall(LuaState L, int nargs, int nresults, int errfunc)


/*
** Execute a protected C call.
*/
** Execute a protected C call.
*/
public class CCallS
{ /* data to `f_Ccall' */
public lua_CFunction func;
Expand Down Expand Up @@ -967,14 +967,14 @@ public static int lua_cpcall(LuaState L, lua_CFunction func, object ud)


public static int lua_load(LuaState L, lua_Reader reader, object data,
CharPtr chunkname)
CharPtr chunkname)
{
ZIO z = new ZIO();
int status;
lua_lock(L);
if (chunkname == null) chunkname = "?";

#if OVERRIDE_LOAD || true
#if OVERRIDE_LOAD || true
//#if false
if (data is LoadS)
{
Expand Down Expand Up @@ -1003,7 +1003,7 @@ public static int lua_load(LuaState L, lua_Reader reader, object data,
{
d.s.index = 0;

// Why isn't the size equal to the chars.Length?
// Why isn't the size equal to the chars.Length?
Debug.WriteLine("Binary data: d.size=" + d.size + " d.s.chars.Length=" + d.s.chars.Length);
Debug.WriteLine("Equal: " + (d.size == d.s.chars.Length));
//Debug.Assert(d.size == d.s.chars.Length);
Expand All @@ -1022,7 +1022,7 @@ public static int lua_load(LuaState L, lua_Reader reader, object data,
ms.WriteByte((byte)lf.f.ReadByte());
ms.Position = 0;

// not binary file
// not binary file
ms.Position = 0;
StringBuilder sb = new StringBuilder();
while (ms.Position < ms.Length)
Expand Down Expand Up @@ -1055,10 +1055,19 @@ public static int lua_load(LuaState L, lua_Reader reader, object data,
lf.f.Position = 0; // reset the read character
}
}
#endif
#endif
luaZ_init(L, z, reader, data);
status = luaD_protectedparser(L, z, chunkname);
lua_unlock(L);
if (data is LoadF)
{
LoadF f = data as LoadF;
if (f.f != null)
{
f.f.Close();
f.f.Dispose();
}
}
return status;
}

Expand Down Expand Up @@ -1086,8 +1095,8 @@ public static int lua_status(LuaState L)


/*
** Garbage-collection function
*/
** Garbage-collection function
*/

public static int lua_gc(LuaState L, int what, int data)
{
Expand Down Expand Up @@ -1164,8 +1173,8 @@ public static int lua_gc(LuaState L, int what, int data)


/*
** miscellaneous functions
*/
** miscellaneous functions
*/


public static int lua_error(LuaState L)
Expand Down
3 changes: 2 additions & 1 deletion SharpLua/LuaCore/VM/ldebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ public static void luaG_errormsg(LuaState L)
if (L.errfunc != 0)
{ /* is there an error handling function? */
StkId errfunc = restorestack(L, L.errfunc);
if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
if (!ttisfunction(errfunc))
luaD_throw(L, LUA_ERRERR);
setobjs2s(L, L.top, L.top - 1); /* move argument */
setobjs2s(L, L.top - 1, errfunc); /* push function */
incr_top(L);
Expand Down
Loading

0 comments on commit f515f9d

Please sign in to comment.