Enhance binary compare operators, especially equals, to more closely match what standard lua does.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
function sum(a,b,c,d) -- "sum" method
|
||||
local d = d or 0
|
||||
return a+b+c+d -- return sum
|
||||
@@ -27,3 +26,55 @@ end
|
||||
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()
|
||||
--]]
|
||||
|
||||
Reference in New Issue
Block a user