diff --git a/src/main/java/lua/value/LDouble.java b/src/main/java/lua/value/LDouble.java index 453453d9..be9a26d0 100644 --- a/src/main/java/lua/value/LDouble.java +++ b/src/main/java/lua/value/LDouble.java @@ -44,5 +44,36 @@ public class LDouble extends LNumber { public int luaAsInt() { return (int) m_value; } + + // binary compares on integers, first dispatch + public boolean luaBinCmpUnknown(int opcode, LValue lhs) { + return lhs.luaBinCmpDouble( opcode, this.m_value ); + } + + // binary compares on mixtures of doubles and integers + public boolean luaBinCmpInteger(int opcode, int rhs) { + return luaBinCmpDoubleDouble( opcode, m_value, (double) rhs ); + } + + // binary compares on doubles + public boolean luaBinCmpDouble(int opcode, double rhs) { + return luaBinCmpDoubleDouble( opcode, m_value, rhs ); + } + + // compare two doubles + public static boolean luaBinCmpDoubleDouble( int opcode, double lhs, double rhs ) { + switch ( opcode ) { + case Lua.OP_EQ: return lhs == rhs; + case Lua.OP_LT: return lhs < rhs; + case Lua.OP_LE: return lhs <= rhs; + } + luaUnsupportedOperation(); + return false; + } + + /** Arithmetic negative */ + public LValue luaUnaryMinus() { + return new LDouble( -m_value ); + } } diff --git a/src/main/java/lua/value/LInteger.java b/src/main/java/lua/value/LInteger.java index a71972bc..d88a7e75 100644 --- a/src/main/java/lua/value/LInteger.java +++ b/src/main/java/lua/value/LInteger.java @@ -40,4 +40,32 @@ public class LInteger extends LNumber { public LValue luaBinOpDouble(int opcode, double rhs) { return LDouble.luaBinOpDoubleDouble(opcode, (double) m_value, rhs ); } + + // binary compare for integers, first dispatch + public boolean luaBinCmpUnknown(int opcode, LValue lhs) { + return lhs.luaBinCmpInteger( opcode, this.m_value ); + } + + // unsupported except for numbers + public boolean luaBinCmpInteger(int opcode, int rhs) { + switch ( opcode ) { + case Lua.OP_EQ: return m_value == rhs; + case Lua.OP_LT: return m_value < rhs; + case Lua.OP_LE: return m_value <= rhs; + } + luaUnsupportedOperation(); + return false; + } + + // unsupported except for numbers + public boolean luaBinCmpDouble(int opcode, double rhs) { + return LDouble.luaBinCmpDoubleDouble(opcode, (double) m_value, rhs ); + } + + /** Arithmetic negative */ + public LValue luaUnaryMinus() { + return new LInteger( -m_value ); + } + + } diff --git a/src/main/java/lua/value/LString.java b/src/main/java/lua/value/LString.java index 0f0b2dd3..94b0aeb5 100644 --- a/src/main/java/lua/value/LString.java +++ b/src/main/java/lua/value/LString.java @@ -18,4 +18,10 @@ public class LString extends LValue { public String luaAsString() { return m_string; } + + /** Built-in opcode LEN, for Strings and Tables */ + public LValue luaLength() { + return new LInteger( m_string.length() ); + } + } diff --git a/src/main/java/lua/value/LTable.java b/src/main/java/lua/value/LTable.java index f2b41457..d2e8ec24 100644 --- a/src/main/java/lua/value/LTable.java +++ b/src/main/java/lua/value/LTable.java @@ -26,4 +26,9 @@ public class LTable extends LValue { public String luaAsString() { return m_hash.toString(); } + + /** Built-in opcode LEN, for Strings and Tables */ + public LValue luaLength() { + return new LInteger( m_array.size() ); + } } diff --git a/src/main/java/lua/value/LValue.java b/src/main/java/lua/value/LValue.java index a52bd0ad..0b1f33e4 100644 --- a/src/main/java/lua/value/LValue.java +++ b/src/main/java/lua/value/LValue.java @@ -34,6 +34,24 @@ public class LValue { return luaUnsupportedOperation(); } + // unsupported except for numbers + public boolean luaBinCmpUnknown(int opcode, LValue lhs) { + luaUnsupportedOperation(); + return false; + } + + // unsupported except for numbers + public boolean luaBinCmpInteger(int opcode, int rhs) { + luaUnsupportedOperation(); + return false; + } + + // unsupported except for numbers + public boolean luaBinCmpDouble(int opcode, double rhs) { + luaUnsupportedOperation(); + return false; + } + /** set a value in a table */ public void luaSetTable(LValue key, LValue value) { @@ -61,5 +79,17 @@ public class LValue { luaUnsupportedOperation(); return 0; } + + /** Arithmetic negative */ + public LValue luaUnaryMinus() { + return luaUnsupportedOperation(); + } + + /** Built-in opcode LEN, for Strings and Tables */ + public LValue luaLength() { + // TODO: call meta-method TM_LEN here + return luaUnsupportedOperation(); + } + } diff --git a/src/test/res/compile.sh b/src/test/res/compile.sh index 76810893..febb2b50 100644 --- a/src/test/res/compile.sh +++ b/src/test/res/compile.sh @@ -1,7 +1,10 @@ #!/bin/bash LUA_HOME=/cygdrive/c/programs/lua5.1 -for x in test1 test2 test3 test4 +TESTS="test1 test2 test3 test4 test5" +#TESTS="test5" +for x in $TESTS do echo compiling $x ${LUA_HOME}/luac5.1.exe -l -o ${x}.luac ${x}.lua + ${LUA_HOME}/lua5.1.exe ${x}.luac done diff --git a/src/test/res/test5.lua b/src/test/res/test5.lua new file mode 100644 index 00000000..48fae4a3 --- /dev/null +++ b/src/test/res/test5.lua @@ -0,0 +1,17 @@ + + i = 777 + while i<780 do + print(i) + i = i+1 + end + + a,b = 0,1 + while true do -- infinite loop + print( b ) + a,b = b,a+b + if a>10 then break end -- exit the loop if the condition is true + end + + 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 \ No newline at end of file diff --git a/src/test/res/test5.luac b/src/test/res/test5.luac new file mode 100644 index 00000000..240a9c5c Binary files /dev/null and b/src/test/res/test5.luac differ