Lua相关问题整理(4) – 让Lua的eval函数支持赋值语句
上一篇文章提到了在Lua中实现类似于JavaScript中的eval函数,遗憾是该eval函数不支持赋值语句,原因是Lua的赋值运算符是不支持返回值。所以如果要让该eval函数也支持赋值语句,就需要一个额外的工作,让它鉴别一个语句是不是赋值语句,如果是,则return的是被赋值后变量的值。为此,我写了一个isAssignmentExpression函数,比较粗糙,不过够用了,基本思想是检测语句中第一个出现的”=“操作符,且该”=“不能在一对引号当中。
-- Lua code function isAssignmentExpression( str ) local i = 1; local curChar; local quotesType = "none" -- none, single or double local isEscaping = false curChar = string.sub( str, 1, 1 ) while ( curChar ~= "" ) do if ( curChar == "'" and isEscaping == false and quotesType ~= "double" ) then if ( quotesType == "single" ) then quotesType = "none" elseif ( quotesType == "none" ) then quotesType = "single" end end if ( curChar == "\"" and isEscaping == false and quotesType ~= "single" ) then if ( quotesType == "double" ) then quotesType = "none" elseif ( quotesType == "none" ) then quotesType = "double" end end if ( curChar == "\\" and isEscaping == false ) then isEscaping = true else isEscaping = false end if ( curChar == "=" and quotesType == "none" ) then if ( string.sub( str, i+1, i+1 ) ~= "=" ) then return true, string.sub( str, 1, i - 1 ) else return false end end i = i + 1 curChar = string.sub( str, i, i ) end return false end function eval( str ) local bAssign local var bAssign, var = isAssignmentExpression( str ) if ( bAssign ) then print( "Assignment, var=" .. var ) loadstring( str )() return loadstring( "return " .. var )() else return loadstring( "return " .. str )() end end -- 以下是一组测试 print( eval( "3+4" ) ) -- 7 function Multiply( a, b ) return a*b end print( eval( "Multiply( 3, 4 )" ) ) -- 12 print( eval( "i" ) ) -- nil print( eval( "i = 1" ) ) -- Assignment, var=i -- 1 print( eval( "i = i + 1" ) ) -- Assignment, var=i -- 2 print( eval( "i" ) ) -- 2 print( eval( "i+1" ) ) -- 3 print( eval( "i, j = 4, 5" ) ) -- Assignment, var=i, j -- 4 5 print( eval( "i = {}" ) ) -- Assignment, var=i -- table: 003CD818 print( eval( "i[ \"0\" ] = 0" ) ) -- Assignment, var=i[ "0" ] -- 0 print( eval( "i[ \"\\\"0=\" ] = 1" ) ) -- Assignment, var=i[ "\"0=" ] -- 1 print( eval( "i.name=\"hello\"" ) ) -- Assignment, var=i.name -- hello print( eval( "i[0], i.name = 4" ) ) -- Assignment, var=i[0], i.name -- 4 nil print( eval( "i == 10" ) ) -- false
11 Responses to “Lua相关问题整理(4) – 让Lua的eval函数支持赋值语句”
逆向直销,震撼来袭:
优势①:逆向网赚,真正实现就算什么不干都赚钱
优势②:十级提成,下线年年续费,上线年年收钱
优势③:静态分红,每日签到就有钱,封顶一百元
优势④:百万资源,每日更新,一键转存无限下载
优势⑤:强大网站,八个栏目,无限发布产品广告
免费注册地址:
http://www.629494567.9489988.com/
我来看看,欢迎不欢迎?
不错,不错,看看了!
看看!
年中快乐!
博客不错,嘎嘎!
受教了!呵呵!
thank you for clarifying this topic, there was no understanding what exactly to do with it
不赖!真的不赖!
Can You Buy Bitcoin With Paypal…
Nutty Coder » Luaç¸å ³é®é¢æ´çï¼4ï¼ – 让Luaçeval彿°æ¯æèµå¼è¯å¥…
test