Enhance binary compare operators, especially equals, to more closely match what standard lua does.

This commit is contained in:
James Roseborough
2007-06-14 04:58:09 +00:00
parent 14108aee87
commit 19bd995ba6
6 changed files with 93 additions and 10 deletions

View File

@@ -10,10 +10,6 @@ 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() { public int hashCode() {
return (int) m_value; return (int) m_value;
} }

View File

@@ -10,10 +10,6 @@ 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() { public int hashCode() {
return m_value; return m_value;
} }

View File

@@ -1,6 +1,16 @@
package lua.value; package lua.value;
import lua.Lua;
abstract abstract
public class LNumber extends LValue { public class LNumber extends LValue {
/** Compare for equivalence by using lua op comparator */
public boolean equals(Object o) {
if ( ! ( o instanceof LValue) )
return false;
LValue v = (LValue) o;
return this.luaBinCmpUnknown(Lua.OP_EQ, v );
}
} }

View File

@@ -1,5 +1,6 @@
package lua.value; package lua.value;
import lua.Lua;
import lua.StackState; import lua.StackState;
public class LString extends LValue { public class LString extends LValue {
@@ -23,6 +24,20 @@ public class LString extends LValue {
this(string); this(string);
} }
public boolean luaBinCmpUnknown(int opcode, LValue lhs) {
return lhs.luaBinCmpString(opcode, m_string);
}
public boolean luaBinCmpString(int opcode, String rhs) {
switch ( opcode ) {
case Lua.OP_EQ: return m_string.equals(rhs);
case Lua.OP_LT: return m_string.compareTo(rhs) < 0;
case Lua.OP_LE: return m_string.compareTo(rhs) <= 0;
}
luaUnsupportedOperation();
return false;
}
public String luaAsString() { public String luaAsString() {
return m_string; return m_string;
} }

View File

@@ -1,5 +1,6 @@
package lua.value; package lua.value;
import lua.Lua;
import lua.StackState; import lua.StackState;
abstract abstract
@@ -34,20 +35,34 @@ public class LValue {
return luaUnsupportedOperation(); return luaUnsupportedOperation();
} }
// unsupported except for numbers // unsupported except for numbers, strings, and == with various combinations of Nil, Boolean, etc.
public boolean luaBinCmpUnknown(int opcode, LValue lhs) { public boolean luaBinCmpUnknown(int opcode, LValue lhs) {
if ( opcode == Lua.OP_EQ )
return lhs == this;
luaUnsupportedOperation();
return false;
}
// unsupported except for strings
public boolean luaBinCmpString(int opcode, String rhs) {
if ( opcode == Lua.OP_EQ )
return false;
luaUnsupportedOperation(); luaUnsupportedOperation();
return false; return false;
} }
// unsupported except for numbers // unsupported except for numbers
public boolean luaBinCmpInteger(int opcode, int rhs) { public boolean luaBinCmpInteger(int opcode, int rhs) {
if ( opcode == Lua.OP_EQ )
return false;
luaUnsupportedOperation(); luaUnsupportedOperation();
return false; return false;
} }
// unsupported except for numbers // unsupported except for numbers
public boolean luaBinCmpDouble(int opcode, double rhs) { public boolean luaBinCmpDouble(int opcode, double rhs) {
if ( opcode == Lua.OP_EQ )
return false;
luaUnsupportedOperation(); luaUnsupportedOperation();
return false; return false;
} }

View File

@@ -1,4 +1,3 @@
function sum(a,b,c,d) -- "sum" method function sum(a,b,c,d) -- "sum" method
local d = d or 0 local d = d or 0
return a+b+c+d -- return sum return a+b+c+d -- return sum
@@ -27,3 +26,55 @@ end
print( myfunc(0.1) ) print( myfunc(0.1) )
print( myfunc(0.1) ) print( myfunc(0.1) )
--[[
i = 1
table = { "west", "south", "east", "north" }
function next()
i = (i % 4) + 1
return table[i]
end
print( next() )
print( next() )
print( next() )
print( next() )
print( next() )
function room1 ()
local move = next()
print( "room1 moving", move )
if move == "south" then return room3()
elseif move == "east" then return room2()
else print("invalid move")
return room1() -- stay in the same room
end
end
function room2 ()
local move = next()
print( "room2 moving", move )
if move == "south" then return room4()
elseif move == "west" then return room1()
else print("invalid move")
return room2()
end
end
function room3 ()
local move = next()
print( "room3 moving", move )
if move == "north" then return room1()
elseif move == "east" then return room4()
else print("invalid move")
return room3()
end
end
function room4 ()
print("congratulations!")
end
room1()
--]]