From ee5007e7184a15d2a5810b58d87ce9262c9c09ad Mon Sep 17 00:00:00 2001 From: Ian Farmer Date: Sun, 22 Jul 2007 06:20:14 +0000 Subject: [PATCH] Add implicit LString to number coercions. --- .../java/lua/addon/luacompat/LuaCompat.java | 14 +------ src/main/java/lua/value/LString.java | 38 ++++++++++++++++++ src/test/java/lua/LuaJTest.java | 4 ++ src/test/res/coercions.lua | 5 +++ src/test/res/coercions.luac | Bin 0 -> 296 bytes 5 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 src/test/res/coercions.lua create mode 100644 src/test/res/coercions.luac diff --git a/src/addon/java/lua/addon/luacompat/LuaCompat.java b/src/addon/java/lua/addon/luacompat/LuaCompat.java index 3c956e59..06863509 100644 --- a/src/addon/java/lua/addon/luacompat/LuaCompat.java +++ b/src/addon/java/lua/addon/luacompat/LuaCompat.java @@ -109,19 +109,7 @@ public class LuaCompat extends LFunction { if ( first+1 < top ) { base = stack[first+1].luaAsInt(); } - if ( base >= 2 && base <= 36 ) { - String str = input.luaAsString().trim(); - try { - result = new LInteger( Integer.parseInt( str, base ) ); - } catch ( NumberFormatException nfe ) { - if ( base == 10 ) { - try { - result = new LDouble( Double.parseDouble( str ) ); - } catch ( NumberFormatException nfe2 ) { - } - } - } - } + return ( (LString) input ).luaToNumber( base ); } } return result; diff --git a/src/main/java/lua/value/LString.java b/src/main/java/lua/value/LString.java index 675258f8..1de543b3 100644 --- a/src/main/java/lua/value/LString.java +++ b/src/main/java/lua/value/LString.java @@ -46,6 +46,44 @@ public class LString extends LValue { return false; } + public LValue luaBinOpDouble( int opcode, double m_value ) { + return luaToNumber().luaBinOpDouble( opcode, m_value ); + } + + public LValue luaBinOpInteger( int opcode, int m_value ) { + return luaToNumber().luaBinOpInteger( opcode, m_value ); + } + + public LValue luaBinOpUnknown( int opcode, LValue lhs ) { + return luaToNumber().luaBinOpUnknown( opcode, lhs ); + } + + public LValue luaUnaryMinus() { + return luaToNumber().luaUnaryMinus(); + } + + public LValue luaToNumber() { + return luaToNumber( 10 ); + } + + public LValue luaToNumber( int base ) { + if ( base >= 2 && base <= 36 ) { + String str = m_string.trim(); + try { + return new LInteger( Integer.parseInt( str, base ) ); + } catch ( NumberFormatException nfe ) { + if ( base == 10 ) { + try { + return new LDouble( Double.parseDouble( str ) ); + } catch ( NumberFormatException nfe2 ) { + } + } + } + } + + return LNil.NIL; + } + public String luaAsString() { return m_string; } diff --git a/src/test/java/lua/LuaJTest.java b/src/test/java/lua/LuaJTest.java index fd566be2..21bd4b19 100644 --- a/src/test/java/lua/LuaJTest.java +++ b/src/test/java/lua/LuaJTest.java @@ -51,6 +51,10 @@ public class LuaJTest extends TestCase { runTest( "boolean" ); } + public void testCoercions() throws IOException, InterruptedException { + runTest( "coercions" ); + } + public void testCompare() throws IOException, InterruptedException { runTest( "compare" ); } diff --git a/src/test/res/coercions.lua b/src/test/res/coercions.lua new file mode 100644 index 00000000..9db03f32 --- /dev/null +++ b/src/test/res/coercions.lua @@ -0,0 +1,5 @@ +print("5.0"+3.1) +print(4+"7.5") +print(3.14*" 2.75") +print("-33"/"11") +print(-"-5") diff --git a/src/test/res/coercions.luac b/src/test/res/coercions.luac new file mode 100644 index 0000000000000000000000000000000000000000..f71918d4a68e102fdb52c4b0a3d09eed85b2cd24 GIT binary patch literal 296 zcmb34DNPJyU}Rxo;b7orU|?`a&QC2$&dkp%*2^hP1PZ_a6O$m2$qK|i4F?)z92gkE z>;n!>Ft(qAV>68H&u|FNc64B32x4ep0LjS#X)d4{ENnocpeQr1gn@+x$TZb6U|>Fb z1_U@9z#`^)rVPwL86XgFU||PR3JOMg=0HLD*4KeM