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;
}
public boolean equals(Object o) {
return o != null && o instanceof LDouble && m_value == ((LDouble)o).m_value;
}
public int hashCode() {
return (int) m_value;
}

View File

@@ -10,10 +10,6 @@ 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;
}

View File

@@ -1,6 +1,16 @@
package lua.value;
import lua.Lua;
abstract
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;
import lua.Lua;
import lua.StackState;
public class LString extends LValue {
@@ -23,6 +24,20 @@ public class LString extends LValue {
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() {
return m_string;
}

View File

@@ -1,5 +1,6 @@
package lua.value;
import lua.Lua;
import lua.StackState;
abstract
@@ -34,20 +35,34 @@ public class LValue {
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) {
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();
return false;
}
// unsupported except for numbers
public boolean luaBinCmpInteger(int opcode, int rhs) {
if ( opcode == Lua.OP_EQ )
return false;
luaUnsupportedOperation();
return false;
}
// unsupported except for numbers
public boolean luaBinCmpDouble(int opcode, double rhs) {
if ( opcode == Lua.OP_EQ )
return false;
luaUnsupportedOperation();
return false;
}