• About
    • Resume
A Game Developer also plays some guitar

Lua相关问题整理(4) – 让Lua的eval函数支持赋值语句

November 28, 2010 12:22 pm / Benny Chen

上一篇文章提到了在Lua中实现类似于JavaScript中的eval函数,遗憾是该eval函数不支持赋值语句,原因是Lua的赋值运算符是不支持返回值。所以如果要让该eval函数也支持赋值语句,就需要一个额外的工作,让它鉴别一个语句是不是赋值语句,如果是,则return的是被赋值后变量的值。为此,我写了一个isAssignmentExpression函数,比较粗糙,不过够用了,基本思想是检测语句中第一个出现的”=“操作符,且该”=“不能在一对引号当中。

[js collapse=”true”]
— 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
[/js]

Posted in: Game Programming, Lua / Tagged: eval, lua, 赋值语句

About Benny Chen

A game developer, a music lover, and a partisan fan of F.C. Barcelona

View all posts by Benny Chen →

Comments are closed.

Post Navigation

← Previous Post
Next Post →

Categories

  • In My Life (25)
    • A Day in the Life (8)
    • English Learning (2)
    • Learn a Word (7)
    • Something In The Way (8)
  • Music Heaven (8)
    • Guitar (1)
    • In Concert (1)
    • Lyrics (3)
  • OK Computer (54)
    • 3D (3)
    • C++ (10)
    • Computer Graphics (15)
    • Game Programming (23)
    • iOS (6)
    • Linux (1)
    • Lua (9)
    • My Projects (3)
    • Some Experiences (9)
    • Talking in Code (2)
    • Unity (2)
  • Quotations (2)
  • Uncategorized (1)
  • Visca Barça (24)
    • FCB BJ (5)

Recent Posts

  • [译]优化你的手机游戏(没有延迟的,才是健康的)- 一篇给游戏美术设计师读的文章
  • 新浪微博API for MOAI
  • 稍后继续
  • Unity Developer ++
  • Another Thread @ Moai Forum
  • 1st Day of Golden Week
  • 为SyntaxHighlighter添加新语言
  • 基于Lua的State Pattern
  • Class Diagram of Pacman
  • 基于Moai的Pacman

Recent Comments

  • 约修亚_RK on 为SyntaxHighlighter添加新语言
  • 爱装的小男孩 on 小心DLL链接静态库时的内存错误
  • happyfire on Game Loop的几种实现方式
  • William on 新浪微博API for MOAI
  • Benny Chen on 新浪微博API for MOAI
  • your man on 新浪微博API for MOAI
  • 你家男人 on 稍后继续
  • 逍遥 on 关于对std::vector的遍历
  • papa on Unity Developer ++
  • T客网 ︱ Techpot » Blog Archive » iOS开发与OpenGL ES相关问题整理(1) on iOS开发与OpenGL ES相关问题整理(1)

Tags

2d 3D 3dsmax 3ds max air Apply architecture Asia tour barca Beijing bilbao binary search blocked bob boost bruce springsteen C++ capo CGContextDrawImage Champions League Change DLL DX10 eval exporter flash framework frustum culling game game engine iniesta ios linux lua Moai opengles pacman plug-in plugin 北京 导出插件 崩溃 巴萨 游戏引擎 踢球
© Copyright 2026 - A Game Developer
Infinity Theme by DesignCoral / WordPress