MoonScript Documentation

repository·master·Indexed 25 days ago

https://github.com/leafo/moonscript

MoonScript is a programmer-friendly language that compiles into Lua, compatible with Lua 5.1 and above, including LuaJIT. This documentation covers the MoonScript toolchain, including the `moon` CLI for execution, `moonc` for compilation and linting, and `moon-tags` for generating ctags. It also details the `moonscript` Lua module for in-memory compilation and the `moonscript.base`, `moonscript.parse`, and `moonscript.compile` modules for low-level AST manipulation.

Tokens
12K
Snippets
47
Records
97
Agent score
83%

What's inside MoonScript

  1. Write MoonScript modules

    master

    MoonScript files implicitly return the value of the last statement. To define a module, place a table containing your exported functions and constants as the final statement in the file. To allow forward declarations (accessing variables regardless of order), add local * to the top of the file.

    MY_CONSTANT = "hello"
    
    my_function = -> print "the function"
    
    { :my_function, :MY_CONSTANT}
  2. Assign Variables in MoonScript

    master

    MoonScript supports dynamic typing and multiple assignments.

    • Local Variables: Assigning to an undeclared name creates a new local variable.
    • Multiple Assignment: You can assign multiple names and values at once (e.g., a, b, c = 1, 2, 3).
    • Global Variables: Use the export keyword to create a global variable.
    • Forward Declaration/Shadowing: Use the local keyword to forward declare a variable or shadow an existing one.
    hello = "world"
    a,b,c = 1, 2, 3
    hello = 123 -- uses the existing variable
    
    export global_var = 10
    local my_local = 5
  3. Use List Comprehensions

    master
    List comprehensions create a new array-like table by iterating over an existing object and applying an expression. You can use a when clause to filter items and multiple for clauses to simulate nested loops. The * operator can be used to iterate over the values of a numerically indexed table directly.
  4. Format Multi-line Function Arguments

    master

    To split a large argument list over multiple lines:

    1. End the current line with a comma.
    2. Indent the following line more than the current line.
    3. All subsequent argument lines must share the same indentation level.

    This indentation-based approach also works for nesting function calls and block-level statements like if.

    my_func 5,4,3,
      8,9,10
    
    cool_func 1,2,
      3,4,
      5,6,
      7,8
    
    -- Nested calls
    my_func 5,6,7,
      6, another_func 6,7,8,
        9,1,2,
      5,4
  5. Use Conditionals (if, unless, and switch)

    master

    MoonScript provides several ways to handle conditional logic:

    • if/else: Standard conditional blocks. Can be used as expressions: print if condition then "a" else "b".
    • unless: The inverse of if. Executes if the condition is false.
    • Assignment in Conditionals: if and elseif can perform an assignment. The assigned variable is only in scope for the body of that conditional block.
    • switch: A shorthand for multiple if checks against the same value. Supports multiple values per when clause (comma-separated) and an else block. Can also be used as an expression.
    • Line Decorators: Single-statement conditionals can be placed at the end of a line: print "hi" if condition.
  6. MoonScript Whitespace and Indentation

    master

    MoonScript is a whitespace-sensitive language. Instead of using delimiters like do/end or {/}, it uses line breaks and indentation to define code blocks.

    • Indentation must be at least 1 space or 1 tab.
    • You must be consistent with your indentation.
    • A tab is treated as equivalent to 4 spaces.
  7. Use with_dev to test local MoonScript development changes

    master

    When developing MoonScript, your system-installed version may take precedence over your local development version during testing. To ensure that require calls within your specs load the MoonScript modules from your current working directory instead of the system install, use the with_dev helper.

    Important: with_dev replaces _G.require with a version that only loads .lua files. You must compile your local .moon files into .lua files before running tests with this helper.

    import with_dev from require "spec.helpers"
    describe "moonscript.base", ->
      with_dev!
    
      it "should load code", ->
        -- the local version is loaded
        moonscript = require "moonscript"
        moonscript.load "print 12"