diff --git a/src/main/java/lua/Builtin.java b/src/main/java/lua/Builtin.java index 657a8a82..1ebd522c 100644 --- a/src/main/java/lua/Builtin.java +++ b/src/main/java/lua/Builtin.java @@ -6,24 +6,43 @@ package lua; import lua.value.LFunction; import lua.value.LString; import lua.value.LTable; +import lua.value.LValue; final class Builtin extends LFunction { static void addBuiltins(LTable table) { - table.luaSetTable( new LString( "print" ), new Builtin(0) ); + for ( int i=0; i 0 ) + System.out.print( "\t" ); + System.out.print( String.valueOf(state.stack[base+1+i]) ); + } + System.out.println(); + return; + case PAIRS: + LValue value = state.stack[base+1].luaPairs(); + state.stack[base] = value; return; default: luaUnsupportedOperation(); diff --git a/src/main/java/lua/value/LDouble.java b/src/main/java/lua/value/LDouble.java index be9a26d0..7a7391ea 100644 --- a/src/main/java/lua/value/LDouble.java +++ b/src/main/java/lua/value/LDouble.java @@ -10,6 +10,14 @@ public class LDouble extends LNumber { this.m_value = value; } + public boolean equals(Object o) { + return o != null && o instanceof LDouble && m_value == ((LDouble)o).m_value; + } + + public int hashCode() { + return (int) m_value; + } + public String luaAsString() { return String.valueOf(m_value); } diff --git a/src/main/java/lua/value/LInteger.java b/src/main/java/lua/value/LInteger.java index d88a7e75..03ec4174 100644 --- a/src/main/java/lua/value/LInteger.java +++ b/src/main/java/lua/value/LInteger.java @@ -10,6 +10,14 @@ public class LInteger extends LNumber { this.m_value = value; } + public boolean equals(Object o) { + return o != null && o instanceof LInteger && m_value == ((LInteger)o).m_value; + } + + public int hashCode() { + return m_value; + } + public int luaAsInt() { return m_value; } diff --git a/src/main/java/lua/value/LString.java b/src/main/java/lua/value/LString.java index 94b0aeb5..4c085048 100644 --- a/src/main/java/lua/value/LString.java +++ b/src/main/java/lua/value/LString.java @@ -10,6 +10,14 @@ public class LString extends LValue { this.m_string = string; } + public boolean equals(Object o) { + return o != null && o instanceof LString && m_string.equals(((LString)o).m_string); + } + + public int hashCode() { + return m_string.hashCode(); + } + // TODO: what to do with LuaState? public LString(StackState l, String string) { this(string); diff --git a/src/main/java/lua/value/LTable.java b/src/main/java/lua/value/LTable.java index d2e8ec24..09d649b8 100644 --- a/src/main/java/lua/value/LTable.java +++ b/src/main/java/lua/value/LTable.java @@ -1,12 +1,14 @@ package lua.value; +import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; +import lua.StackState; + public class LTable extends LValue { private Hashtable m_hash = new Hashtable(); - private Vector m_array = new Vector(); public LTable() { } @@ -15,12 +17,11 @@ public class LTable extends LValue { } public void luaSetTable(LValue key, LValue val) { - m_hash.put( key.luaAsString(), val ); - m_array.add( val ); + m_hash.put( key, val ); } public LValue luaGetTable(LValue key) { - return (LValue) m_hash.get( key.luaAsString() ); + return (LValue) m_hash.get( key ); } public String luaAsString() { @@ -29,6 +30,36 @@ public class LTable extends LValue { /** Built-in opcode LEN, for Strings and Tables */ public LValue luaLength() { - return new LInteger( m_array.size() ); + return new LInteger( m_hash.size() ); } + + /** Valid for tables */ + public LValue luaPairs() { + Enumeration e = m_hash.keys(); + return new LTableIterator(this,e); + } + + /** Iterator for tables */ + private static final class LTableIterator extends LValue { + private final LTable t; + private final Enumeration e; + + private LTableIterator(LTable t, Enumeration e) { + this.t = t; + this.e = e; + } + + // perform a lua call + public void luaStackCall(StackState state, int base, int nresults) { + if ( e.hasMoreElements() ) { + LValue key = (LValue) e.nextElement(); + state.stack[base] = key; + state.stack[base+1] = t.luaGetTable(key); + } else { + state.stack[base] = LNil.NIL; + } + + } + } + } diff --git a/src/main/java/lua/value/LValue.java b/src/main/java/lua/value/LValue.java index 0b1f33e4..1440d4aa 100644 --- a/src/main/java/lua/value/LValue.java +++ b/src/main/java/lua/value/LValue.java @@ -15,7 +15,7 @@ public class LValue { } // perform a lua call - public void luaStackCall(StackState state, int base, int nresults) { + public void luaStackCall(StackState state, int base, int nargs) { luaUnsupportedOperation(); } @@ -91,5 +91,10 @@ public class LValue { return luaUnsupportedOperation(); } + /** Valid for tables */ + public LValue luaPairs() { + return luaUnsupportedOperation(); + } + } diff --git a/src/test/res/test5.lua b/src/test/res/test5.lua index 48fae4a3..ebd0c6f1 100644 --- a/src/test/res/test5.lua +++ b/src/test/res/test5.lua @@ -1,17 +1,16 @@ - - i = 777 - while i<780 do +i = 777 +while i<780 do print(i) i = i+1 - end +end - a,b = 0,1 - while true do -- infinite loop +a,b = 0,1 +while true do -- infinite loop print( b ) a,b = b,a+b if a>10 then break end -- exit the loop if the condition is true - end +end - for count = 336,330,-2 do print(count) end -- numerical iteration - --- for key,value in pairs({a=10, 3.14159265358, c="banana"}) do print(key, value) end \ No newline at end of file +for count = 336,330,-2 do print(count) end -- numerical iteration + +for key,value in pairs({a=10, 3.14159265358, c="banana" }) do print(key, value) end diff --git a/src/test/res/test5.luac b/src/test/res/test5.luac index 240a9c5c..14f39f89 100644 Binary files a/src/test/res/test5.luac and b/src/test/res/test5.luac differ