Top 1K Features Creators Events Podcasts Books Extensions Interviews Blog Explorer CSV

MoonScript

< >

MoonScript is an open source programming language created in 2011.

#185on PLDB 13Years Old 818Repos
Download source code:
git clone https://github.com/leafo/moonscript
Homepage · REPL · Try It Online · Source Code · Docs

:crescent_moon: A language that compiles to Lua


Example from the web:
class Thing name: "unknown" class Person extends Thing say_name: => print "Hello, I am #{@name}!" with Person! .name = "MoonScript" \say_name!
Example from hello-world:
print 'Hello World'
Example from Linguist:
types = require "moonscript.types" util = require "moonscript.util" data = require "moonscript.data" import reversed, unpack from util import ntype, mtype, build, smart_node, is_slice, value_is_singular from types import insert from table import NameProxy, LocalName from require "moonscript.transform.names" destructure = require "moonscript.transform.destructure" local implicitly_return class Run new: (@fn) => self[1] = "run" call: (state) => self.fn state -- transform the last stm is a list of stms -- will puke on group apply_to_last = (stms, fn) -> -- find last (real) exp last_exp_id = 0 for i = #stms, 1, -1 stm = stms[i] if stm and mtype(stm) != Run last_exp_id = i break return for i, stm in ipairs stms if i == last_exp_id fn stm else stm -- is a body a sindle expression/statement is_singular = (body) -> return false if #body != 1 if "group" == ntype body is_singular body[2] else true find_assigns = (body, out={}) -> for thing in *body switch thing[1] when "group" find_assigns thing[2], out when "assign" table.insert out, thing[2] -- extract names out hoist_declarations = (body) -> assigns = {} -- hoist the plain old assigns for names in *find_assigns body for name in *names table.insert assigns, name if type(name) == "string" -- insert after runs idx = 1 while mtype(body[idx]) == Run do idx += 1 table.insert body, idx, {"declare", assigns} expand_elseif_assign = (ifstm) -> for i = 4, #ifstm case = ifstm[i] if ntype(case) == "elseif" and ntype(case[2]) == "assign" split = { unpack ifstm, 1, i - 1 } insert split, { "else", { {"if", case[2], case[3], unpack ifstm, i + 1} } } return split ifstm constructor_name = "new" with_continue_listener = (body) -> continue_name = nil { Run => @listen "continue", -> unless continue_name

Language features

Feature Supported Example Token
Booleans ✓ true false
Strings ✓ 'Hello world' '
Print() Debugging ✓ print
Comments ✓ -- A comment
Line Comments ✓ -- A comment --
Semantic Indentation X

- Build the next great programming language · Add · Issues · About · Search · Keywords · Livestreams · Labs · Resources · Acknowledgements

Built with Scroll v154.3.0