An Interest In:
Web News this Week
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
- April 12, 2024
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 moonprint "Hello, World!"
$ ./hello.moonHello, World!
FizzBuzz
MoonScript uses indentation base syntax. For loops use Lua syntax instead of ..
ranges.
#!/usr/bin/env moonfor 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 moonfib = (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 moonprint "Hello"\len()print "w"\len()print ""\len()print "w"\upper()print "w"\lower()
$ ./unicode.moon574Ww
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 moonclass Person new: (@name, @surname, @age) => __tostring: => "#{@name} #{@surname}"maria = Person("Maria", "Ivanova", 25)print "#{maria} is #{maria.age} years old"
$ ./person.moonMaria 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 moonclass 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.ya = Vector(20, 60)b = Vector(400, 9)c = a + bprint aprint bprint cprint 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 moonimport split from require "moonscript.util"readlines = (path) -> file = io.open("wordle-answers-alphabetical.txt") text = file\read("*a") file\close! lines = split text, "
" table.remove(lines) -- remove empty "" at the end linesrandom_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 "
" __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.moonGuess: audioGuess: stoneGuess: loverGuess: 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.
Original Link: https://dev.to/taw/100-languages-speedrun-episode-89-moonscript-3bki
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To