Lua – 为什么允许函数调用后的字符串?
发布时间:2020-12-15 00:16:14 所属栏目:大数据 来源:网络整理
导读:我正在尝试实现一个简单的C函数,它检查Lua脚本的语法.为此,我使用Lua的编译器函数luaL_loadbufferx()并在之后检查其返回值. 最近,我遇到了一个问题,因为我认为应该被标记为无效的代码未被检测到,而是脚本在运行时稍后失败(例如,在lua_pcall()中). 示例Lua代
我正在尝试实现一个简单的C函数,它检查Lua脚本的语法.为此,我使用Lua的编译器函数luaL_loadbufferx()并在之后检查其返回值.
最近,我遇到了一个问题,因为我认为应该被标记为无效的代码未被检测到,而是脚本在运行时稍后失败(例如,在lua_pcall()中). 示例Lua代码(可在official Lua demo上测试): function myfunc() return "everyone" end -- Examples of unexpected behaviour: -- The following lines pass the compile time check without errors. print("Hello " .. myfunc() "!") -- Runtime error: attempt to call a string value print("Hello " .. myfunc() {1,2,3}) -- Runtime error: attempt to call a string value -- Other examples: -- The following lines contain examples of invalid syntax,which IS detected by compiler. print("Hello " myfunc() .. "!") -- Compile error: ')' expected near 'myfunc' print("Hello " .. myfunc() 5) -- Compile error: ')' expected near '5' print("Hello " .. myfunc() .. ) -- Compile error: unexpected symbol near ')' 显然,目标是在编译时捕获所有语法错误.所以我的问题是: >调用字符串值究竟是什么意思? 注意:我使用的是Lua版本5.3.4(manual here). 非常感谢您的帮助. 解决方法
两个myfunc()“!”和myfunc(){1,3}是有效的Lua表达式.
Lua允许调用表单exp字符串.请参阅Syntax of Lua中的functioncall和prefixexp. 所以myfunc()“!”是一个有效的函数调用,它调用myfunc返回的任何内容,并使用字符串“!”调用它. 对于表格exp-literal的调用,也会发生同样的事情. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |