Giter Site home page Giter Site logo

Comments (7)

hnes avatar hnes commented on June 27, 2024

Sorry for that I had make a mistake. The 2nd solution is not right at all because for floating-point comparisons (x < y) is not the same as not (x >= y) in the presence of NaNs.

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 :(

from lua-parser.

andremm avatar andremm commented on June 27, 2024

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.

from lua-parser.

hnes avatar hnes commented on June 27, 2024

Hi Andre, thanks for your reply, but I still insist on my idea.

A comparison a > b is translated to b < a and a >= b is translated to b <= a.>

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 lt transformation is fatally wrong in semantic which will lead the wrong result of computation later based on the AST tree.

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 lt transformation had changed the sub-AST tree's execution order mistakenly.

from lua-parser.

hnes avatar hnes commented on June 27, 2024
    [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 lt transformation, you will never know which function should execute first without making some mistake in semantic.

from lua-parser.

andremm avatar andremm commented on June 27, 2024

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?

from lua-parser.

hnes avatar hnes commented on June 27, 2024

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 )

from lua-parser.

andremm avatar andremm commented on June 27, 2024

Thanks Andre for your time and good patience :)

Thank you for the bug report!

( I have checked and tested the commit and it works just as fine as it could > possible being :D )

Sounds great! I'm closing this issue then.

from lua-parser.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.