Hello World in MoonScript

Hello World in MoonScript Featured Image

Welcome to another article in the Hello World series! This time we are looking to implement Hello World in MoonScript, a language that compiles to Lua.

Table of Contents

What is MoonScript?

According to MoonScript.org, MoonScript is a dynamic scripting language that compiles into Lua. What is Lua? Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.

Back to MoonScript, it gives you the full power of Lua while providing a clean easy syntax without the keyword noise typically seen in a Lua script. MoonScript can either be compiled into Lua, or it can be dynamically compiled and run using the moonloader.

How did MoonScript start you may ask? Well, its author Leaf Corcoran built MoonScript, started building games with MoonScript, added them to itch.io—an online platform for indie games which is also built with MoonScript—then generalized the code base to a general purpose web framework with both MoonScript and Lua APIs called Lapis.

At any rate, let’s dig into the implementation.

Hello World In MoonScript.

As you can see here, Hello World in MoonScript has a relatively simple implementation:

print "Hello, World!"

All we have to do is call the built-in Lua function print, and that’s it. Behind the scenes, the code is compiled into Lua which is, in this case, exactly the same.

In other cases, it could be different. For instance, we could have an implementation of some arithmetic which we print to the user:

x = 10
y = 15
z = x + y
print y

The code is then compiled into Lua like this:

local x = 10
local y = 15
local z = x + y
return print(y)

How cool is that?

How to Run the Solution

If your feeling adventurous today, You can quickly install MoonScript using one of the following methods:

For Windows users, you can try installing the windows binaries.

For Linux users, install LuaRocks which is a package manager for Lua modules. Then run the following command:

luarocks install moonscript

With the moon executable and Lua modules on your device, run your .moon file with this command:

moon ./YOURFILE.moon

Also, you can compile your .moon file into Lua by using this command:

moonc ./YOURFILE.moon

Alternatively, you can always run MoonScript using an online compiler.

Sample Programs in Every Language

Viola! There you go. Hello World in MoonScript is done. You can probably tell that this article is a part of a series. Still, there are plenty of programming languages to add. If you like the idea, you’re welcome to contribute, share, or comment.

Series Navigation← Hello World in BrainfuckHello World in MATLAB →

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.