Fix basic class processing via metatables. Make print output more closely match that produces by C interpreter

This commit is contained in:
James Roseborough
2007-06-19 04:25:34 +00:00
parent 54927db2fc
commit 99077764ac
9 changed files with 69 additions and 12 deletions

View File

@@ -36,7 +36,7 @@ final class Builtin extends LFunction {
switch ( id ) { switch ( id ) {
case PRINT: case PRINT:
for ( int i=base+1; i<top; i++ ) { for ( int i=base+1; i<top; i++ ) {
System.out.print( String.valueOf(state.stack[i]) ); System.out.print( state.stack[i].luaAsString() );
System.out.print( "\t" ); System.out.print( "\t" );
} }
System.out.println(); System.out.println();
@@ -53,7 +53,8 @@ final class Builtin extends LFunction {
break; break;
case SETMETATABLE: case SETMETATABLE:
state.stack[base+1].luaSetMetatable(state.stack[base+2]); state.stack[base+1].luaSetMetatable(state.stack[base+2]);
state.top = base; state.stack[base] = state.stack[base+1];
state.top = base+1;
break; break;
default: default:
luaUnsupportedOperation(); luaUnsupportedOperation();

View File

@@ -1,9 +1,10 @@
package lua.io; package lua.io;
import lua.StackState; import lua.StackState;
import lua.value.LFunction;
import lua.value.LValue; import lua.value.LValue;
public class Closure extends LValue { public class Closure extends LFunction {
public LValue env; public LValue env;
public Proto p; public Proto p;
public UpVal[] upVals; public UpVal[] upVals;
@@ -19,8 +20,4 @@ public class Closure extends LValue {
public void luaStackCall(StackState state, int base, int top, int nresults) { public void luaStackCall(StackState state, int base, int top, int nresults) {
state.setupCall( this, base, top ); state.setupCall( this, base, top );
} }
public String toString() {
return "closure: "+hashCode();
}
} }

View File

@@ -38,7 +38,7 @@ public class Proto extends LValue {
public int maxstacksize; public int maxstacksize;
public String toString() { public String luaAsString() {
return "proto: "+hashCode(); return "proto: "+id();
} }
} }

View File

@@ -14,7 +14,7 @@ public class UpVal {
} }
public String toString() { public String toString() {
return "up("+name+")"; return "up."+name;
} }
} }

View File

@@ -4,7 +4,7 @@ import lua.StackState;
public class LFunction extends LValue { public class LFunction extends LValue {
public String toString() { public String luaAsString() {
return "function: "+hashCode(); return "function: "+hashCode();
} }

View File

@@ -50,10 +50,14 @@ public class LTable extends LValue {
state.stack[base] = val; state.stack[base] = val;
} }
public String luaAsString() { public String toString() {
return m_hash.toString(); return m_hash.toString();
} }
public String luaAsString() {
return "table: "+id();
}
/** 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_hash.size() ); return new LInteger( m_hash.size() );

View File

@@ -10,6 +10,10 @@ public class LValue {
throw new java.lang.UnsupportedOperationException(); throw new java.lang.UnsupportedOperationException();
} }
public String id() {
return Integer.toHexString(hashCode());
}
// test if value is true // test if value is true
public boolean luaAsBoolean() { public boolean luaAsBoolean() {
return true; return true;

View File

@@ -1,8 +1,11 @@
t = { 11, 22, 33, you='one', me='two' } t = { 11, 22, 33, you='one', me='two' }
print( "---------" )
for a,b in pairs(t) do for a,b in pairs(t) do
print( a, b ) print( a, b )
end end
print( "----" )
print( "t[2]", t[2] ) print( "t[2]", t[2] )
print( "t.me", t.me ) print( "t.me", t.me )
print( "t.fred", t.fred ) print( "t.fred", t.fred )
@@ -24,9 +27,11 @@ d = t.you
t[4] = 'rat' t[4] = 'rat'
t[1] = 'pipe' t[1] = 'pipe'
print( a, b, c, d ) print( a, b, c, d )
print( "---------" )
for a,b in pairs(t) do for a,b in pairs(t) do
print( a, b ) print( a, b )
end end
print( "----" )
-- delegate to actual metatable -- delegate to actual metatable
s = { } s = { }
@@ -38,6 +43,52 @@ print( t.x )
me = 'here' me = 'here'
print( t.me ) print( t.me )
t[5] = 99 t[5] = 99
print( "---------" )
for a,b in pairs(s) do for a,b in pairs(s) do
print( a, b ) print( a, b )
end end
print( "----" )
Vector = {}
Vector_mt = { __index = Vector }
function Vector:new(x,y)
return setmetatable( {x=x, y=y}, Vector_mt)
end
function Vector:mag()
return math.sqrt(self:dot(self))
end
function Vector:dot(v)
return self.x * v.x + self.y * v.y
end
v1 = Vector:new(3,4)
print( "--------" )
for a,b in pairs(v1) do
print( a, b )
end
print( "----" )
v2 = Vector:new(2,1)
print( v2:dot(v1) )
print( Vector )
print( "---------" )
for a,b in pairs(Vector) do
print( a, b )
end
print( "----" )
print( v1, v2 )
print( Vector_mt, getmetatable(v1), getmetatable(v2) )
print( "---------" )
for a,b in pairs(Vector_mt) do
print( a, b )
end
print( "----" )

Binary file not shown.