From 6b097d337444a0e02efc6cebed08a860d2e435ed Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sat, 4 Feb 2012 22:47:37 +1000 Subject: Implement typecasts when needed. Actually, doing some extras for the moment. --- LuaSL/src/LSL.lua | 32 ++++++++++++++++++++++++++++++++ LuaSL/src/LuaSL_LSL_tree.h | 3 ++- LuaSL/src/LuaSL_compile.c | 13 +++++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) (limited to 'LuaSL') diff --git a/LuaSL/src/LSL.lua b/LuaSL/src/LSL.lua index cc9b454..1e7cc13 100644 --- a/LuaSL/src/LSL.lua +++ b/LuaSL/src/LSL.lua @@ -311,6 +311,38 @@ function LSL.stateChange(x) end end; +function LSL.floatTypecast(x) + local temp = tonumber(x) + if nil == temp then temp = 0 end + return temp; +end + +function LSL.integerTypecast(x) + local temp = tonumber(x) + if nil == temp then temp = 0 end + return temp; +end + +function LSL.keyTypecast(x) + return "" .. x; +end + +function LSL.listTypecast(x) + return {x}; +end + +function LSL.rotationTypecast(x) + return x; +end + +function LSL.stringTypecast(x) + return "" .. x; +end + +function LSL.vectorTypecast(x) + return x; +end + return LSL; diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 6709236..3c30129 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -157,7 +157,8 @@ typedef enum MF_PREINC = 32, MF_POSTDEC = 64, MF_POSTINC = 128, - MF_LSLCONST = 256 + MF_LSLCONST = 256, + MF_TYPECAST = 512 } miscFlags; struct _allowedTypes diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c index f3fddef..cd8494a 100644 --- a/LuaSL/src/LuaSL_compile.c +++ b/LuaSL/src/LuaSL_compile.c @@ -1217,7 +1217,11 @@ LSL_Leaf *addTypecast(LSL_Leaf *lval, LSL_Leaf *type, LSL_Leaf *rval, LSL_Leaf * if (lval) { if (type) + { lval->basicType = type->basicType; + if ((expr) && (OT_integer == type->basicType)) // TODO - Should be from string, but I guess I'm not propagating basic types up from function calls and parenthesis? + lval->value.parenthesis->flags |= MF_TYPECAST; + } // Actualy, at this point, type is no longer needed. lval->toKen = tokens[LSL_TYPECAST_OPEN - lowestToken]; } @@ -1401,7 +1405,7 @@ Explicit type casting - Leading spaces are ignored, as are any characters after the run of digits. All other strings convert to 0. Which means "" and " " convert to 0. - Strings in hexadecimal format will work. + Strings in hexadecimal format will work, same in Lua (though Lua can't handle "0x", but "0x0" is fine). keys <-> string No other typecasting can be done with keys. float -> string @@ -1770,8 +1774,13 @@ static void outputRawParenthesisToken(FILE *file, outputMode mode, LSL_Parenthes { if ((OM_LUA == mode) && (LSL_TYPECAST_OPEN == parenthesis->type)) { - fprintf(file, " --[[(%s)]] ", typeName); + if (MF_TYPECAST & parenthesis->flags) + fprintf(file, " _LSL.%sTypecast(", typeName); + else + fprintf(file, " --[[%s]] ", typeName); outputLeaf(file, mode, parenthesis->contents); + if (MF_TYPECAST & parenthesis->flags) + fprintf(file, ") "); return; } -- cgit v1.1