Movatterモバイル変換


[0]ホーム

URL:


leafo.net

My projects

View more →

Recent guides

View all →

Recent posts

Getting started with MoonScript

PostedFebruary 19, 2012 by leafo (@moonscript)
Tweet

Last updated Fri May 2 2014

MoonScript is an open source prgramming language which compiles intoLua. Lua is a very powerful dynamically typed language. It supportsfeatures like functions as first class objects, closures, tail-recursion and ahigh performance.

Lua has made its way into a variety of locations, likegames,texteditors, andwindow managers, due to its simplicity to embed inaddition to its ability to be run from the command line like any otherscripting language.

MoonScript is built on top of Lua to take advantage of the same excellentplatform in addition to providing a collection of syntactic features which aidin rapid development.

You can think of MoonScript as an easier and faster way to write Lua.MoonScript is compatible with all existing Lua code, and Lua code can nativelywork with MoonScript code. MoonScript is just another way to write Lua.

This is a guide to get your system ready for MoonScript development.

If you have any trouble, or find something outdated in the guide pleaseleavea comment at the bottom.

Installing

Installation of MoonScript is slightly different depending on the platform.MoonScript is written in pure Lua but some of its dependencies are written inC. Chose your platform to begin:

Windows

Because compiling the dependencies can be challenging, pre-built Windowsbinaries have been created.

Downloadmoonscript-0.2.5.zip, thelatest version as of this guide.

All versions can be found here:http://moonscript.org/bin/

Within the zip file,moon.exe andmoonc.exe are the MoonScript executables.We want to be able to run these executables from the command line easily sowe'll need to put them in one of the directories listed by thePATHenvironment variable.

Create a directory somewhere on your computer where you want your MoonScriptinstallation to be. For example, I will useC:\moonscript.

Extract all the files from the zip into the new directory.moon.exe,moonc.exe andlua5.1.dll must be present for MoonScript to work.

Next, from the control panel gotoSystem >Advanced Tab. Click on theEnvironment Variables button.

UnderUser variables, see if there is an existingPATH entry. If there isn’t,clickNew, else selectPATH and clickEdit.

If the variable value is empty, then just paste the MoonScript installationdirectory in. If it isn’t empty, then the new directory can be added at theend but itmust be separated by a; character.

Below is an image of myPATH, I happened to have another directory in it so Iseparated the new entry with a;.

moonscriptwindows installation

Now we can test our installation, open up a new command prompt and type in:

moon --version

If the installation was successful, then the version of MoonScript is printed.

If you see'moon' is not recognized... then the installation was not donesuccessfully. Go through the directions again and make sure nothing was leftout.

Although not required, it is recommended to also install bothLua andLuaRocks. This will give you access to the rich collection of packagesavailable to Lua.

Once you're ready, head toSetting Up Your Editor.

OSX

We'll useHomebrew to installLuaRocks, the Lua package manager.

Install Homebrew

If you don’t Hombrew installed already follow theHombrew installationinstructions, then return here.

If you do Homebrew installed, update it:

$brew update

It’s best to have the latest version of LuaRocks that Hombrew provides, andthis will ensure we get it.

Install LuaRocks and MoonScript

With Hombrew installed we can easily install LuaRocks:

$brew install luarocks

This will also install Lua.

Now we can install the latest version of moonscript:

$luarocks install moonscript

Themoonscript package is now visible to Lua, but the MoonScript binariesaren’t in out path yet. By default LuaRocks will install binaries to/usr/local/lib/luarocks/bin/.

Edit your~/.profile and add:

exportPATH=$PATH:/usr/local/lib/luarocks/bin/

Test your installation by running in a new terminal:

$moon --version

Test that we can requiremoonscript by running this command, it shouldproduce no output:

$lua -e'require "moonscript"'

Once you are ready, head toSetting Up Your Editor.

Linux

We're going to useLuaRocks, the Lua package manager. It will handlebuilding all the dependencies for you in addition to installing MoonScript tothe right spot. This method depends on having a build tools installed.

Most mainstream distributions will have LuaRocks in their package repository.If not, you can follow theLuaRocks installationdirections.

To install, just run:

$sudo luarocks install moonscript

We use sudo here to install globally to the system, enabling us to use themoon andmoonc commands from anywhere. If you wish you can installlocally by leaving off the sudo.

Assuming everything builds correctly, the executablesmoon andmoonc willnow be installed, along with themoonscript Lua module.

From Source

The source code can be downloaded from GitHub athttp://github.com/leafo/moonscript.

The latest development version can be installed from source using LuaRocks withthe specialdev rockspec:

$luarocks build http://moonscript.org/rocks/moonscript-dev-1.rockspec

Setting Up Your Editor

The following editors have support for MoonScript:

If you are unsure what to use and are on Windows, download theSciTE Windowsbinary.

SciTE Windows Binary

If you're on Windows then you're in luck! I've packaged SciTE, scintillua andthe MoonScript highlighter together into one package. I've even installed thecustom moon theme.

You can download the latest version athttp://moonscript.org/scite/moonscript-scite.zip.

Just extract, and runscite.exe. You're now ready to start creating programs.

Creating Programs

Compiling & Running

The MoonScript package comes with two executables,moon andmoonc.

Withmoonc we can compile MoonScript code into Lua. Let’s try it out, createa new file calledhello.moon:

-- I'm a comment!print"Hello from MoonScript!"

From your terminal:

$moonc hello.moonBuilt    ./hello.moon

This created a file calledhello.lua. Feel free to run this code usinglua.

Themoon executable lets us run our code without having to manually compileit to Lua first. We can run the script we just created like so:

$moon hello.moonHello from MoonScript!

moon automatically compiles the file in memory and then executes it rightaway inside the Lua runtime.

Watch Mode

moonc can do a couple interesting things worth mentioning. If you runmoonc-h you can see an entire list of all of it’s command line options.

We're going to take a look atwatch mode, which is activated using-w.

Watch mode will watch all of the input files and recompile them if they change.

For example, to automatically rebuild a single moon file if it’s changed:

$moonc -w my_file.moon

If you want to watch all the moon files in the current directory you can simplydo:

$moonc -w *.moon

If you want to watch an entire directory (along with it’s children) for modifiedmoon files, just use the directory’s name:

$moonc -w code_directory

And finally, if you want to watch the current directory and all sub directoriesfor changes you can just use.:

$moonc -w .

Watch mode will tell you every time it compiles a file in the terminal. It willalso tell you if there are any compilation errors.

There are two things to be aware of:

  1. New files aren’t picked up, you have to restartmoonc
  2. moonc doesn’t compile everything on start up. Usemoonc without the-w flag to excplicity compile everything

Compile To Directory

If you want to save your compiled MoonScript’s Lua files elsewhere, use the-t flag to specify a directory. The entire directory structure of yourMoonScript code will be preserved.

It can also be used with watch mode, for example:

The following will recursively watch all moon files from the current directory,and write them into the directorymy_lua.

$moonc -w -t my_lua .

What’s Next?

After you're comfortable with compiling and running code, the next step is tostart learning the language. TheMoonScript Reference Manual is the mostcomprehensive guide to everything provided by MoonScript.

If you're interested in contributing to the language, theMoonScript githubrepository is a good place to start.

If you're interested in using MoonScript as a web scripting language, I'vewritten a nice guide tosetting up Lua on Heroku.

There’s also an online compiler if you want to experiment without installing,or share snippets with other people. It’s located athttp://moonscript.org/compiler/.

If you've created something with MoonScript I'd love to hear about it. Feelfree to leave a comment.

Good Luck!

Related projects

MoonScript

A programming language that compiles to Lua, inspired by CoffeeScript and written in MoonScript.

GitHub Repo
Lapis

A web framework for Lua/MoonScript running inside of Nginx OpenResty.

GitHub Repo

leafo.net · Generated Sun Oct 8 13:02:35 2023 bySitegenmastodon.social/@leafo


[8]ページ先頭

©2009-2025 Movatter.jp