Page 1 of 1

Hypothetical language that compiles to Lua (akin to Moonscript)

Posted: Tue Aug 02, 2016 8:47 am
by substitute541
Just a thought I had while doing other stuff. I want to see a language that's a near superset to Lua — existing Lua code can be compiled with no problems — while adding some useful syntax sugar.

Arguably the most notable of compiled-to-Lua languages is Moonscript. However, there are some parts in that language that I don't like.
  • Whitespace significant. Subjective of course, some of you might like it.
  • Parenthesis-less function calls. Makes some code more "DSL-like". Personally I don't care for it; IMHO it makes the code feel "naked" and can complicate the parser. I'm fine with Lua's own rules for omitting parenthesis.
  • Removal of local variable declaration and replacing it with a `using` statement. It's a rather stupid alternative when Lua's own local is fine.
  • Implicit returns in nearly all blocks. I prefer being explicit
So anyway, here's a very rough example of my hypothetical language without the disadvantages:

Code: Select all

-- Local function declaration
local parse_args(defaults, args)
    -- local variable declaration
    res := {}
    for k, v in pairs(defaults) do
        res[k] = if v != nil then v end
    return res

-- Local variable declaration
Person, Person.prototype := {}, nil

    -- Piping operator sends the result of first expression to the next
    'Created a new Person object!' |> print
    tbl := parse_args({
        name = '',
        age = 0,
        gender = nil,
    }, opts)
    return setmetatable(Person.prototype, tbl)

Person.prototype = {
    -- Shorthand function declaration with self and return
    :get_name = @name,

    -- Longhand function declaration with self
    :set_name(first_name, last_name)
        @name = first_name .. last_name

    -- Shortcut for getters and setters. Useful when you don't need additional logic
    get age,
    set age,
    get gender,
    set gender,

        -- Embedded expressions in strings.
        "Hi, my name is ${@name}" |> print

    -- Longhand function declaration with self parameter manually added
        'Goodbye!' |> print

    :__index(key) = Person.prototype[key],


john :={
    name = 'John Doe',
    age = 24,
    gender = 'male',


-- with expression.
jane := with do
    :set_name('Jane', 'Doe')
    .age = 12 -- assignments are possible



-- destructuring assignment
thing := {'hello', 'world'}
{a, b} := thing
print(a, b) -- "hello"    "world"

pos := {x = 1, y = 3}
{.x, .y} := pos
print(x, y) -- 1    3

person := {name = "John Doe", occupation = "Programmer"}
{.name, job = .occupation} := person
print(name, job) -- "John Doe"  "Programmer"

deep := {a = {b = {c = {d = {e = "duckbutt"}}}}}
{.a.b.c.d.e} = deep
print(e) -- "duckbutt"

import 'braces' from '__future__'
-- equivalent to
{.braces} = require '__future__'

-- vvv random additions with questionable usefulness vvv

-- TOML-inspired table syntax (?)
foobar := {
    hello = 'Hello World!'

    version = '1.2.3',
    license = 'MIT',

    1, 2, 3,
    content = 'What is love?'

    4, 5, 6,
    content = 'Baby don\'t hurt me'

    7, 8, 9,
    content = 'Don\'t hurt me, no more'
I'm probably not gonna implement this anytime soon since I'm busy. Just some random thoughts.

Edit: I just realized this looks very similar to my other post "Language that compiles to Lua Bytecode". This post deals with languages that compile to Lua source code instead.

Re: Hypothetical language that compiles to Lua (akin to Moonscript)

Posted: Sat Aug 06, 2016 8:35 am
by substitute541
So anyway, here's a more fleshed out design: ... 58b9bd3066

I'm putting waaay too much thought about this (lol)

Re: Hypothetical language that compiles to Lua (akin to Moonscript)

Posted: Sat Aug 06, 2016 7:50 pm
by partnano
I like some of the ideas you presented, though I have to say I'm one of the lunatics that likes the aesthetics of moonscript (though it's still on my todo list to learn properly)

I guess random thoughts about Lua and stuff are rather fitting in this, so here goes:

I like Lua .. a lot actually, but I'm not the biggest fan of the crowded-ness that makes the full-word enclosures. I'm not a braces-fanatic, not at all, java for example is in my opinion one of the ugliest languages I know, aesthetics wise (Though I guess, here it's not the braces fault).
I like how moonscript solves this, but I can see how it appears naked to some.

Furthermore, even if this feels kinda unnecessary to be annoyed by ... I'd love to have some shorthand math in Lua. x = x +1 is really tiring after a while, and makes for really cluttered code, especially if variable names are a tiny bit longer. I understand the reasoning behind it, but still annoys me oh so often D:

Re: Hypothetical language that compiles to Lua (akin to Moonscript)

Posted: Sun Aug 07, 2016 3:44 pm
by technomancy
By far the biggest shortcoming of Lua is that there is no way to declare functions that get arity-checked. (Though obviously defaulting to setting globals is a no-brainer mistake to fix.) Of course you should make it easy to declare raw Lua functions, but a quick syntax for "these are the args I expect; make sure they are provided" would go a long way towards catching bugs quicker.

I like the use of := for setting and |> in your snippets. I am somewhat baffled by the use of shorthand for getters and setters though; I think it's much better to use closures for private data.

I don't think "with" actually buys you enough to justify the inconsistency it brings with it. I think the positional destructuring is a bit silly on its own (all that just to avoid calling `unpack`?) but when you put it in context with the named arg destructuring and the deeply-nested access it makes a lot more sense.

Anyway, I am glad to see more experimentation in this area. I think it's great that Moonscript exists, but I also have similar hesitations about it, and it's always bummed me out that I discovered Metalua too late after it was abandoned. Always good to have more options, and good luck going forward.

Re: Hypothetical language that compiles to Lua (akin to Moonscript)

Posted: Mon Aug 08, 2016 11:50 am
by substitute541
Here's a more detailed (but currently incomplete) reference spec with partial grammar, based on Lua 5.3's reference: ... 978ac7a11d

I'm currently conflicted about using `:=`. Also, I used braces instead and ditched the macro system.


Also I ditched the current with statement and used something akin to Python. Yay.