100 Languages Speedrun: Episode 89: MoonScript
MoonScript is CoffeeScript for Lua.
It doesn't seem to be maintained much anymore. The most recent release was back in 2015, and it doesn't work with the latest Lua. You can still run it with non-release version, with luarocks install moonscript --dev
.
Hello, World!
Hello World is as simple as it gets. Newline is automatically inserted:
#!/usr/bin/env moon
print "Hello, World!"
$ ./hello.moon
Hello, World!
FizzBuzz
MoonScript uses indentation base syntax. For loops use Lua syntax instead of ..
ranges.
#!/usr/bin/env moon
for i = 1, 100
if i % 15 == 0
print "FizzBuzz"
elseif i % 5 == 0
print "Buzz"
elseif i % 3 == 0
print "Fizz"
else
print i
Fibonacci
MoonScript has nice syntax for defining functions with two kinds of arrows, and implicit return. There's also beautiful string interpolation.
#!/usr/bin/env moon
fib = (n) ->
if n <= 2
1
else
fib(n-1) + fib(n-2)
for n = 1,20
print "fib(#{n})=#{fib(n)}"
Unicode
Unicode is broken in Lua, and MoonScript does not fix it. Method call syntax is also really weird with a backslash, as .
is taken for regular function call (that doesn't pass extra self
as first argument).
#!/usr/bin/env moon
print "Hello"\len()
print "Żółw"\len()
print "💩"\len()
print "Żółw"\upper()
print "Żółw"\lower()
$ ./unicode.moon
5
7
4
ŻółW
Żółw
Person class
It's just an empirical fact that people overwhelmingly hate prototype-based inheritance, and want classes. MoonScript provides the same two main things as CoffeeScript - nicer syntax, and class-based OOP.
#!/usr/bin/env moon
class Person
new: (@name, @surname, @age) =>
__tostring: =>
"#{@name} #{@surname}"
maria = Person("Maria", "Ivanova", 25)
print "#{maria} is #{maria.age} years old"
$ ./person.moon
Maria Ivanova is 25 years old
This is fairly nice. There's no default __tostring
, it's just table: 0xADDRESS
, so we really should provide something. Constructors auto-assign any @arguments
, so you don't need to do the boring thing every time. I'm actually a bit disappointed in Ruby for not doing this obvious thing 80% of the time:
class Person
new: (name, surname, age) =>
@name = name
@surname = surname
@age = age
Vector
We can do enough operator overloading to make vectors work. Which exact operators can be overloaded depends on Lua version (and games often ship with older versions):
#!/usr/bin/env moon
class Vector
new: (@x, @y) =>
__tostring: => "<#{@x},#{@y}>"
__add: (other) => Vector(@x + other.x, @y + other.y)
__eq: (other) =>
@__class == other.__class and @x == other.x and @y == other.y
a = Vector(20, 60)
b = Vector(400, 9)
c = a + b
print a
print b
print c
print c == Vector(420, 69)
$ ./vector.moon
<20,60>
<400,9>
<420,69>
true
This doesn't really help with printing plain Lua tables like arrays, just MoonScript classes.
Wordle
Let's do Wordle.
The first problem we run into is that Lua lacks the most basic functions like string.split
, string.contains
etc. There's one included as import in moonscript.util
, but it leaves extra ""
at the end, so we need to remove it.
This code looks bad because Lua desperately needs a better standard library, and MoonScript does not come with one:
#!/usr/bin/env moon
import split from require "moonscript.util"
readlines = (path) ->
file = io.open("wordle-answers-alphabetical.txt")
text = file\read("*a")
file\close!
lines = split text, "\n"
table.remove(lines) -- remove empty "" at the end
lines
random_element = (array) ->
array[math.random(#array)]
class Wordle
new: =>
@words = readlines("wordle-answers-alphabetical.txt")
report: (word, guess) =>
for i=1,5
letter = guess\sub(i,i)
if word\sub(i,i) == letter
io.write "🟩"
-- non-regexp string.contains?
elseif word\find(letter, 1, true)
io.write "🟨"
else
io.write "🟥"
io.write "\n"
__call: =>
word = random_element(@words)
guess = ""
while guess != word
io.write "Guess: "
guess = io.read!
if #guess == 5
@report(word, guess)
else
print "Guess must be 5 letters long"
game = Wordle()
game()
$ ./wordle.moon
Guess: audio
🟥🟥🟥🟥🟨
Guess: stone
🟥🟥🟨🟥🟨
Guess: lover
🟨🟩🟨🟩🟥
Guess: vowel
🟩🟩🟩🟩🟩
Should you use MoonScript?
It's unfortunately abandoned, and it's hard to recommend abandoned languages.
Otherwise there's a pretty obvious use case. So many games are coded in Lua, and MoonScript compiles into fairly clean Lua, so if you want to mod Lua-based games like Factorio, maybe MoonScript would be a reasonable idea.
I don't really recommend Lua or MoonScript for anything new, but as a game modder you're stuck with whatever game developers decided to use, so having this an as option is nice.
If anyone feels like giving this project a shot, and release a new working version, maybe it would be of some use for modders.
I feel like it would provide a lot more value if such revived MoonScript also came up with decent standard library comparable with what Ruby, JavaScript, and Python have.
Code
All code examples for the series will be in this repository.
Subscribe to my newsletter
Read articles from Tomasz Węgrzanowski directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Tomasz Węgrzanowski
Tomasz Węgrzanowski
I know literally all programming languages.