Lua运算符,为什么未定义+ =,-=等? [英] Lua operators, why isn't +=, -= and so on defined?

查看:122
本文介绍了Lua运算符,为什么未定义+ =,-=等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题我已经被轻度激怒了一段时间,却一直没有找到答案.

This is a question I've been mildly irritated about for some time and just never got around to search the answer to.

但是我认为我至少可以问这个问题,也许有人可以解释.

However I thought I might at least ask the question and perhaps someone can explain.

我工作过的许多语言都使用语法糖来编写(使用C ++的语法):

Basically many languages I've worked in utilize syntactic sugar to write (using syntax from C++):

int main() {
    int a = 2;
    a += 3; // a=a+3
}

在lua中+=没有定义,所以我不得不写a=a+3,这再次是关于语法糖的.当使用更有意义的"变量名称时,例如:bleed_damage_over_time或类似的东西,开始写起来会变得很乏味:

while in lua the += is not defined, so I would have to write a=a+3, which again is all about syntactical sugar. when using a more "meaningful" variable name such as: bleed_damage_over_time or something it starts getting tedious to write:

bleed_damage_over_time = bleed_damage_over_time + added_bleed_damage_over_time 

代替:

bleed_damage_over_time += added_bleed_damage_over_time

因此,如果您没有很好的解决方案,那么我不希望不解决该问题,在这种情况下,我当然会对听到它感兴趣.而是为什么lua没有实现这种语法糖.

So I would like to know not how to solve this if you don't have a nice solution, in that case I would of course be interested in hearing it; but rather why lua doesn't implement this syntactical sugar.

推荐答案

这只是我的猜测,但是:

This is just guesswork on my part, but:

1.在单遍编译器中很难实现这一点

Lua的字节码编译器实现为可立即生成代码的单遍递归下降解析器.它不会解析为单独的AST结构,然后在第二遍将其转换为字节码.

Lua's bytecode compiler is implemented as a single-pass recursive descent parser that immediately generates code. It does not parse to a separate AST structure and then in a second pass convert that to bytecode.

这在语法和语义上施加了一些限制.特别是,任何需要任意提前或向前引用的内容在此模型中都很难得到支持.这意味着分配已经很难解析.给出类似的东西:

This forces some limitations on the grammar and semantics. In particular, anything that requires arbitrary lookahead or forward references is really hard to support in this model. This means assignments are already hard to parse. Given something like:

foo.bar.baz = "value"

解析foo.bar.baz时,直到解析并生成代码后才按下=,您才意识到自己实际上是在解析分配.因此,Lua的编译器在处理分配方面有很多复杂性.

When you're parsing foo.bar.baz, you don't realize you're actually parsing an assignment until you hit the = after you've already parsed and generated code for that. Lua's compiler has a good bit of complexity just for handling assignments because of this.

支持自我分配将使这一工作变得更加困难.像这样:

Supporting self-assignment would make that even harder. Something like:

foo.bar.baz += "value"

需要翻译为:

foo.bar.baz = foo.bar.baz + "value"

但是在编译器点击=的时候,已经忘记了foo.bar.baz.有可能,但并不容易.

But at the point that the compiler hits the =, it's already forgotten about foo.bar.baz. It's possible, but not easy.

2.语法可能效果不佳

Lua在语法中实际上没有任何语句或行分隔符.空格将被忽略,并且没有强制分号.您可以这样做:

Lua doesn't actually have any statement or line separators in the grammar. Whitespace is ignored and there are no mandatory semicolons. You can do:

io.write("one")
io.write("two")

或者:

io.write("one") io.write("two")

Lua对这两者同样感到满意.保持这种模棱两可的语法是很棘手的.我不确定,但是自赋值运算符 可能会更难.

And Lua is equally happy with both. Keeping a grammar like that unambiguous is tricky. I'm not sure, but self-assignment operators may make that harder.

3.多次分配不能很好地发挥作用

Lua支持多种分配,例如:

Lua supports multiple assignment, like:

a, b, c = someFnThatReturnsThreeValues()

我什至不知道如果您尝试这样做会意味着什么:

It's not even clear to me what it would mean if you tried to do:

a, b, c += someFnThatReturnsThreeValues()

您可以将自赋值运算符限制为单个赋值,但是您刚刚添加了一个人们必须了解的怪异案例.

You could limit self-assignment operators to single assignment, but then you've just added a weird corner case people have to know about.

所有这些,还不清楚自赋值运算符是否足够有用以值得处理上述问题.

With all of this, it's not at all clear that self-assignment operators are useful enough to be worth dealing with the above issues.

这篇关于Lua运算符,为什么未定义+ =,-=等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆