pigpigyyy / Yuescript

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

Some improvements to MoonPlus that worth to consider? #12

Open ngocgodwar opened 4 years ago

ngocgodwar commented 4 years ago

Just think about some improvements to MoonPlus that worth to consider:

  1. Exist OP + "with" keyword. Let look at this code:

    with io.open(test.txt, "w")?
    \write "hello"
    \close!

    Compile to:

    local _with_0 = (io.open(test.txt, "w") ~= nil)
    _with_0:write("hello")
    _with_0:clode()
    return _with_0

    Is it intended behaviour or maybe it should be:

    local _with_0 = io.open(test.txt, "w")
    if _with_0 ~= nil then
    _with_0:write("hello")
    _with_0:close()
    end
    return _with_0
  2. Support chain function call over new lines. With would be nice to look at and edit code. EX:

    path = chain path
    \strip(" ")
    \strip("\\")
    \strip("/")
    \replace("\\", "/")
    \replace(" +", "_")
    \replace("-", "_")
    \lower()
    \title()
    \toString()
  3. It would be good if MoonPlus have options to keep full blank lines and full line comments. It would make generated lua file more easy to look at, work with, debug and make sense to distribute lua file. More over many Lua IDE like EmmyLua support editor autocomplete with meta data fetch from line comments. This feature would be a great addition to MoonPlus over old MoonScript.

  4. Support inline many statements separated with ";" because sometime keep many things on the same line many reason, especially when work with anonymous function.

  5. Macro feature is really good. And I have been thinking if something similar can do with operations. The idea is that can add other modern operations to lua like: power(**), floor division (//), bit and(&), bit or(|). EX: Instead of write: local res = bit32.band(a, b) We can write: res = a | b Or let symbol . or & or whatever to denote macro operations like: res = a .| b Or use normal macro string res = a $and b

  6. DSL support. Lua support it but MoonScript lost it. I write some dynamic scripts dialogue include game flow framework. DSL is very good at it kind of task but not play nice with MoonScript. It would be great it MoonPlus somehow make it but I know it would be kind of difficult.

  7. Until ... Repeat Not really need it but Lua have it. MoonScript have it for completion mapping to Lua.

What you thinks? Many thanks and regards!

pigpigyyy commented 4 years ago
  1. It's intended behavior for existential op. Expression ends with a ? will be compiled to (expr ~= nil). Maybe we can add existential check syntax to the with statement in this way.

    with? io.open "test.txt", "w"
    \write "hello"
    \close!

    compiles to:

    local _with_0 = io.open("test.txt", "w")
    if _with_0 ~= nil then
    _with_0:write("hello")
    _with_0:close()
    end
    return _with_0
  2. The reason Moonscript does not support putting chain calls over new lines is because it conflicts with the 'with' statement syntax. Consider the case:

    with strA
    str = strB
    \gsub("a", "") -- Is it strA\gsub("a", "") or strB\gsub("a", "") ?
  3. This feature can be added. But modifiying generated Lua codes from Moonscript might not be a good idea, which means there will be two copies of codes to be maintained separately. Maybe we can write a Moonscript IDE with autocomplete features someday and let the users focus on Moonscript side only.

  4. ";" in Moonscript is currently used to separate argument list from expression list, for example:

    a, b = f 1; 2
    return f 123, 456; 123

    compiles to:

    local a, b = f(1), 2
    return f(123, 456), 123

    Maybe we can change it to another symbol and make ";" work for putting statements in a same line.

  5. The infix macro operator is worth to be considered as a syntax sugar, maybe add it in the future.

  6. Can you show me some Lua DSL usages so that I can figure out what MS lacks for it? And I think macro system is ready for implementing many DSL features in MS.

  7. Until ... Repeat can be added to Moonscript with support for continue keyword.

pigpigyyy commented 4 years ago

Improvements 1, 7 were done by commit d80160356d7b7119d164ada51867cecb0a371f09

ngocgodwar commented 4 years ago
  1. It not about modify generated Lua codes but have a good looking Lua code file. It good for follow reasons:

    • We normal write code have blank lines to separated code blocks. Blank lines have great meaning for us, the human. Keep it will increase value of Lua file generated.
    • MoonScript some time can be very ambiguous. If you not know MoonScript well then you must constantly check out Lua file to make sure it work as intended, at least it is for me.
    • Nice to code in MoonScript, but in the end I must run and debug in Lua file. When I hit something unexpected, I must debug in Lua file. There hardly anyway around it. Have a lua file with blank lines to separated code blocks will be a big plus.
    • You want to have a good generated lua file do distribute as module when working in a group with other people. You in response for that module and they not know about MoonScript. But they can use that module as a part in system. Some don't event write Lua code, they just call it from C#. When they look at the module, any comments or blank lines present will be a big help. When they report error or something wrong, it will more easy to spot than a lua code thank just a bulks of lua code glued together without any blank lines.
  2. Maybe we can use double symbol like ";;" or "&&" or "||" or whatever make sense, maybe it will easy to look at and distinct each statements.

  3. I not that good with DSL so it all my speculate. I love Lua DSL feature that we call function without () with literal string or table. Well EX below valid Lua and nice to look at:

    dialogue
    "A: Hi!"
    "B: Hello"
    "..."
    {"ABC", "XYZ"}
    "A: abc"
    "B: xyz"
    "."
    jump "end"

    Now thing about it, this feature really not that much import... and I think it hard to transfer to MoonScript. We don't really need it that much anyway.

Thanks for your time and regards!