Hypothetical language that compiles to Lua (akin to Moonscript)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Hypothetical language that compiles to Lua (akin to Moonscript)

Post 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
    end
    return res
end

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

function Person.new(opts)
    -- 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)
end

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
    end,

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

    :say_hello()
        -- Embedded expressions in strings.
        "Hi, my name is ${@name}" |> print
    end,

    -- Longhand function declaration with self parameter manually added
    say_goodbye(self)
        'Goodbye!' |> print
    end,

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

-- TEST

john := Person.new({
    name = 'John Doe',
    age = 24,
    gender = 'male',
})

john:say_hello()

-- with expression.
jane := with Person.new() do
    :set_name('Jane', 'Doe')
    .age = 12 -- assignments are possible
    :set_age(24)
    :set_gender('female')
end

jane:say_hello()

john:say_goodbye()
jane:say_goodbye()

-- 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!'

    [package]
    version = '1.2.3',
    license = 'MIT',

    [abc]
    1, 2, 3,
    content = 'What is love?'

    [abc.def]
    4, 5, 6,
    content = 'Baby don\'t hurt me'

    [abc.def.ghi]
    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.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

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

Post by substitute541 »

So anyway, here's a more fleshed out design:

https://gist.github.com/phoenixenero/91 ... 58b9bd3066

I'm putting waaay too much thought about this (lol)
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
partnano
Prole
Posts: 20
Joined: Sat Jun 11, 2016 4:53 pm
Location: twitter.com/partnano
Contact:

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

Post 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:
User avatar
technomancy
Prole
Posts: 49
Joined: Thu Oct 29, 2015 8:25 am
Location: Thailand
Contact:

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

Post 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.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

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

Post by substitute541 »

Here's a more detailed (but currently incomplete) reference spec with partial grammar, based on Lua 5.3's reference:

https://gist.github.com/phoenixenero/92 ... 978ac7a11d

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

Edit:

Also I ditched the current with statement and used something akin to Python. Yay.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests