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

MoonScript Background

According to MoonScript.orgOpens in a new tab., 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 CorcoranOpens in a new tab. built MoonScript, started building games with MoonScript, added them to itch.ioOpens in a new tab.—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 binariesOpens in a new tab..

For Linux users, install LuaRocksOpens in a new tab. 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 compilerOpens in a new tab..

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.

Bassem Mohamed

Bassem is a developer, tech enthusiast. Started his career as a web developer in a digital marketing agency. After one year of hard work, it got him enough experience to land a job as a core system engineer in one of the biggest banks in Egypt. Being a junior developer and 24 years old, he still has a long way to go. In his spare time, You can find him contributing to Github, Attending a Ted talk or maybe writing a tech article.

Recent Posts