-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem may in lua-parser/parser.lua:binaryOp #12
Comments
Sorry for that I had make a mistake. The 2nd solution is not right at all because for floating-point comparisons Proof: #lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> z = 0/0
> =z
-nan
> return z>z
false
> return z<z
false
> return z>=z
false
> return not (z>=z)
true
> So that seems the 1st solution is the only choice after then :( |
The function binaryOp is coded that way because the parser reproduces the same behavior of the Lua parser itself, as it is expressed by the Lua manual: A comparison a > b is translated to b < a and a >= b is translated to b <= a. |
Hi Andre, thanks for your reply, but I still insist on my idea.
I approve this statement but that is not mean you could simply swap them in the AST generating phase. I think you may actually misunderstand it because this kinda AST For example: $ cat t.lua
gl_f_ct = 0
function f()
if gl_f_ct <= 0 then
gl_f_ct=1
return 1000
end
return -1000
end
print( f("1st call") > f("2nd call") ) --> in lua-parser's ast: lt f("2nd call") f("1st call") | wrong
gl_f_ct = 0
print( f("1st call") < f("2nd call") ) --> in lua-parser's ast: lt f("1st call") f("2nd call") | right
$ lua t.lua
true
false But after execution in the AST lua-parser generated, it's result is: $ lua-parser gen_ast_and_execute_it.lua t.lua
fasle
false This kinda of error is due to the |
[2] = {
[tag] = Op
[pos] = 8
[1] = lt
[2] = {
[tag] = Call
[pos] = 24
[1] = {
[tag] = Id
[pos] = 24
[1] = f
}
[2] = {
[tag] = String
[pos] = 26
[1] = 2nd call <-- wrong
}
}
[3] = {
[tag] = Call
[pos] = 8
[1] = {
[tag] = Id
[pos] = 8
[1] = f
}
[2] = {
[tag] = String
[pos] = 10
[1] = 1st calll <-- wrong
}
}
} If you only have this kinda AST tree which may had been with some |
You have a good point: we cannot assume that values are always evaluated or never have collateral effects before applying relational operators. It looks like Metalua has the same issue, as local mlc = require "metalua.compiler".new()
local test = [[
gl_f_ct = 0
function f()
if gl_f_ct <= 0 then
gl_f_ct=1
return 1000
end
return -1000
end
print( f("1st call") > f("2nd call") )
gl_f_ct = 0
print( f("1st call") < f("2nd call") )
]]
local ast = mlc:src_to_ast(test)
local f = mlc:ast_to_function(ast)
f() produces the same error. I just pushed a commit that adds all relational operators to the AST. Can you please check whether this issue is solved? |
Yes, Metalua does have the same issue, but it seems Metalua has no maintenance anymore for a quite long time. Thanks Andre for your time and good patience :) ( I have checked and tested the commit and it works just as fine as it could possible being :D ) |
Thank you for the bug report!
Sounds great! I'm closing this issue then. |
Hi Andre, there may be something not right in lua-parser/parser.lua:binaryOp:
I think the AST shouldn't consider too much about the constraints of latter IR phase. And there maybe two possible solutions:
Add 'gt' & 'ge' to the opid (which is used in PR11).
Use the mathematical transformation below to solve it lazily (but which would cause some more unnecessarily complexity):Thanks a lot and all best :)
The text was updated successfully, but these errors were encountered: