Support for iterator-style for loops, and "pairs" builting function

This commit is contained in:
James Roseborough
2007-06-10 22:53:09 +00:00
parent e1e6625aa1
commit de763e0a1e
8 changed files with 98 additions and 20 deletions

View File

@@ -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<NAMES.length; i++ )
table.luaSetTable( new LString( NAMES[i] ), new Builtin(i) );
}
private static final int PRINT = 0;
private static final int PAIRS = 1;
private static final String[] NAMES = { "print", "pairs" };
private int id;
Builtin( int id ) {
private Builtin( int id ) {
this.id = id;
}
public String toString() {
return "Builtin('"+NAMES[id]+"')";
}
// perform a lua call
public void luaStackCall(StackState state, int base, int nresults) {
public void luaStackCall(StackState state, int base, int nargs) {
switch ( id ) {
case PRINT:
System.out.println( String.valueOf( state.stack[base+1] ) );
for ( int i=0; i<nargs; i++ ) {
if ( 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();

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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;
}
}
}
}

View File

@@ -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();
}
}

View File

@@ -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
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

Binary file not shown.