pigpigyyy / Yuescript

A Moonscript dialect compiles to Lua.
http://yuescript.org
MIT License
443 stars 38 forks source link

Backward (in)compatibility with Moonscript #166

Closed johnd0e closed 6 months ago

johnd0e commented 6 months ago

Just playing around with yue, trying to switch from moonscript, and found some incompatibilities.

Actually I do not expect yue to be really fully compatible with existing moon code. But still want to make sure that observed differences are intentional.

Here is moon code:

for folder in *init
  switch type folder
    when 'string'
      folder=
        Name:folder
        PluginId:string.rep '\0',16
    when 'table'
      nil
    else
      folder={}

Errors when run with yue:

4: attempt to assign to const variable 'folder'
      folder=
      ^
8: unexpected expression
      nil
      ^
pigpigyyy commented 6 months ago

1.

4: attempt to assign to const variable 'folder' folder= ^

Just confirmed this is a bug and got it fix by 6b204d11ad8426bb179ae3be1e71d30f08e3b375. But some similar cases will still report errors:

for k ,v in pairs tb
  k = 123 -- attempt to assign to const variable 'k'
for i = 1, 10
  i = 123 -- attempt to assign to const variable 'i'

It's intentional because Lua 5.5 is marking these 'key' variables constant in for-loops.

In your case

for folder in *init
  folder = x

Should not report error by now because it compiles to:

local _list_0 = init
for _index_0 = 1, #_list_0 do
  local folder = _list_0[_index_0]
  folder = x
end

And it is not against any Lua rule.

2.

8: unexpected expression nil ^

This is also intentional. Because Yuescript is disallowing putting an simple expression (not a function call) as a statement. If you need a place holder for a block, you can put a line of comment with an indent.

for folder in *init
  switch type folder
    when 'string'
      folder =
        Name:folder
        PluginId:string.rep '\0',16
    when 'table'
      --
    else
      folder = {}
johnd0e commented 6 months ago

Thank you!