免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2132 | 回复: 3
打印 上一主题 下一主题

[其他] lua 也短路不? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-04 22:47 |只看该作者 |倒序浏览
在看一本 "Lua Cookbook"(https://github.com/lua-cookbook/ ... k/en_US/00-intro.md),发现有这样一处:

  1. a > 0 and b > 0 or a == 0 and b < 0
复制代码
In this expression, > and == evaluate first, followed by and, and then or. So the explicit form would be:
  1. ((a > 0) and (b > 0)) or ((a == 0) and (b < 0))
复制代码
敢问这里是否有问题?

论坛徽章:
5
狮子座
日期:2013-08-20 10:12:24午马
日期:2013-11-23 18:04:102015年辞旧岁徽章
日期:2015-03-03 16:54:152015亚冠之德黑兰石油
日期:2015-06-29 18:11:1115-16赛季CBA联赛之新疆
日期:2024-02-21 10:00:53
2 [报告]
发表于 2012-11-05 03:00 |只看该作者
显然短路。字节码是这样的:
  1. luac -l -l  -- "noname\2012-11-05-1.lua"

  2. main <noname\2012-11-05-1.lua:0,0> (15 instructions at 006C7CF8)
  3. 0+ params, 2 slots, 1 upvalue, 1 local, 3 constants, 0 functions
  4.         1       [1]     GETTABUP        0 0 -1  ; _ENV "a"
  5.         2       [1]     LT              0 -2 0  ; 0 -
  6.         3       [1]     JMP             0 3     ; to 7
  7.         4       [1]     GETTABUP        0 0 -3  ; _ENV "b"
  8.         5       [1]     LT              1 -2 0  ; 0 -
  9.         6       [1]     JMP             0 7     ; to 14
  10.         7       [1]     GETTABUP        0 0 -1  ; _ENV "a"
  11.         8       [1]     EQ              0 0 -2  ; - 0
  12.         9       [1]     JMP             0 3     ; to 13
  13.         10      [1]     GETTABUP        0 0 -3  ; _ENV "b"
  14.         11      [1]     LT              1 0 -2  ; - 0
  15.         12      [1]     JMP             0 1     ; to 14
  16.         13      [1]     LOADBOOL        0 0 1
  17.         14      [1]     LOADBOOL        0 1 0
  18.         15      [1]     RETURN          0 1
复制代码
所以明显这个描述错误了。

另外,manual里面是这么描述的:

The negation operator not always returns false or true. The conjunction operator and returns its first argument if this value is false or nil; otherwise, and returns its second argument. The disjunction operator or returns its first argument if this value is different from nil and false; otherwise, or returns its second argument. Both and and or use short-cut evaluation; that is, the second operand is evaluated only if necessary.

论坛徽章:
0
3 [报告]
发表于 2012-11-05 04:51 |只看该作者
starwing83 发表于 2012-11-05 03:00
显然短路。字节码是这样的:所以明显这个描述错误了。

另外,manual里面是这么描述的:


这条语句有多少执行路径?

  1. a > 0 and b > 0 or a == 0 and b < 0
复制代码

论坛徽章:
5
狮子座
日期:2013-08-20 10:12:24午马
日期:2013-11-23 18:04:102015年辞旧岁徽章
日期:2015-03-03 16:54:152015亚冠之德黑兰石油
日期:2015-06-29 18:11:1115-16赛季CBA联赛之新疆
日期:2024-02-21 10:00:53
4 [报告]
发表于 2012-11-05 15:38 |只看该作者
回复 3# fallening_cu


    都告诉你短路了,自己分析嘛,这可是苦差事……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP