Support for iterator-style for loops, and "pairs" builting function
This commit is contained in:
@@ -6,24 +6,43 @@ package lua;
|
|||||||
import lua.value.LFunction;
|
import lua.value.LFunction;
|
||||||
import lua.value.LString;
|
import lua.value.LString;
|
||||||
import lua.value.LTable;
|
import lua.value.LTable;
|
||||||
|
import lua.value.LValue;
|
||||||
|
|
||||||
final class Builtin extends LFunction {
|
final class Builtin extends LFunction {
|
||||||
|
|
||||||
static void addBuiltins(LTable table) {
|
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 PRINT = 0;
|
||||||
|
private static final int PAIRS = 1;
|
||||||
|
|
||||||
|
private static final String[] NAMES = { "print", "pairs" };
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
Builtin( int id ) {
|
private Builtin( int id ) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "Builtin('"+NAMES[id]+"')";
|
||||||
|
}
|
||||||
|
|
||||||
// perform a lua call
|
// perform a lua call
|
||||||
public void luaStackCall(StackState state, int base, int nresults) {
|
public void luaStackCall(StackState state, int base, int nargs) {
|
||||||
switch ( id ) {
|
switch ( id ) {
|
||||||
case PRINT:
|
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;
|
return;
|
||||||
default:
|
default:
|
||||||
luaUnsupportedOperation();
|
luaUnsupportedOperation();
|
||||||
|
|||||||
@@ -10,6 +10,14 @@ public class LDouble extends LNumber {
|
|||||||
this.m_value = value;
|
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() {
|
public String luaAsString() {
|
||||||
return String.valueOf(m_value);
|
return String.valueOf(m_value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,14 @@ public class LInteger extends LNumber {
|
|||||||
this.m_value = value;
|
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() {
|
public int luaAsInt() {
|
||||||
return m_value;
|
return m_value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,14 @@ public class LString extends LValue {
|
|||||||
this.m_string = string;
|
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?
|
// TODO: what to do with LuaState?
|
||||||
public LString(StackState l, String string) {
|
public LString(StackState l, String string) {
|
||||||
this(string);
|
this(string);
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
package lua.value;
|
package lua.value;
|
||||||
|
|
||||||
|
import java.util.Enumeration;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import lua.StackState;
|
||||||
|
|
||||||
public class LTable extends LValue {
|
public class LTable extends LValue {
|
||||||
|
|
||||||
private Hashtable m_hash = new Hashtable();
|
private Hashtable m_hash = new Hashtable();
|
||||||
private Vector m_array = new Vector();
|
|
||||||
|
|
||||||
public LTable() {
|
public LTable() {
|
||||||
}
|
}
|
||||||
@@ -15,12 +17,11 @@ public class LTable extends LValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void luaSetTable(LValue key, LValue val) {
|
public void luaSetTable(LValue key, LValue val) {
|
||||||
m_hash.put( key.luaAsString(), val );
|
m_hash.put( key, val );
|
||||||
m_array.add( val );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LValue luaGetTable(LValue key) {
|
public LValue luaGetTable(LValue key) {
|
||||||
return (LValue) m_hash.get( key.luaAsString() );
|
return (LValue) m_hash.get( key );
|
||||||
}
|
}
|
||||||
|
|
||||||
public String luaAsString() {
|
public String luaAsString() {
|
||||||
@@ -29,6 +30,36 @@ public class LTable extends LValue {
|
|||||||
|
|
||||||
/** Built-in opcode LEN, for Strings and Tables */
|
/** Built-in opcode LEN, for Strings and Tables */
|
||||||
public LValue luaLength() {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public class LValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// perform a lua call
|
// perform a lua call
|
||||||
public void luaStackCall(StackState state, int base, int nresults) {
|
public void luaStackCall(StackState state, int base, int nargs) {
|
||||||
luaUnsupportedOperation();
|
luaUnsupportedOperation();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,5 +91,10 @@ public class LValue {
|
|||||||
return luaUnsupportedOperation();
|
return luaUnsupportedOperation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Valid for tables */
|
||||||
|
public LValue luaPairs() {
|
||||||
|
return luaUnsupportedOperation();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
|
i = 777
|
||||||
i = 777
|
while i<780 do
|
||||||
while i<780 do
|
|
||||||
print(i)
|
print(i)
|
||||||
i = i+1
|
i = i+1
|
||||||
end
|
end
|
||||||
|
|
||||||
a,b = 0,1
|
a,b = 0,1
|
||||||
while true do -- infinite loop
|
while true do -- infinite loop
|
||||||
print( b )
|
print( b )
|
||||||
a,b = b,a+b
|
a,b = b,a+b
|
||||||
if a>10 then break end -- exit the loop if the condition is true
|
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 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 key,value in pairs({a=10, 3.14159265358, c="banana" }) do print(key, value) end
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user