From c15089ee1c0a186bf99cab57f8c2700b523c9f35 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 1 Oct 2011 13:05:57 -0700 Subject: [PATCH] updated and rebuilt docs --- docs/reference.md | 36 ++++++++------- docs/reference_manual.html | 94 ++++++++++++++++++++++++++++++-------- 2 files changed, 93 insertions(+), 37 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index 35cd4fa..d32133b 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -28,8 +28,8 @@ a certain amount. They are aliases for their expanded equivalents. x = 0 x += 10 - s = "hello " - s ..= "world" + s = "hello " + s ..= "world" ## Comments @@ -81,9 +81,9 @@ of the variable where the function is stored. When chaining together function calls, the arguments are applied to the closest function to the left. sum 10, 20 - print sum 10, 20 + print sum 10, 20 - a b c "a", "b", "c" + a b c "a", "b", "c" In order to avoid ambiguity in when calling functions, parentheses can also be used to surround the arguments. This is required here in order to make sure the @@ -412,19 +412,19 @@ must be explicitly returned. have_coins = false if have_coins print "Got coins" - else + else print "No coins" A short syntax for single statements can also be used: - have_coins = false - if have_coins then print "Got coins" else print "No coins" + have_coins = false + if have_coins then print "Got coins" else print "No coins" Because if statements can be used as expressions, this can able be written as: - have_coins = false - print if have_coins then "Got coins" else "No coins" + have_coins = false + print if have_coins then "Got coins" else "No coins" Conditionals can also be used in return statements and assignments: @@ -558,8 +558,8 @@ be declared as local, special syntax is required to declare a variable globally. The export keyword makes it so any following assignments to the specified names will not be assigned locally. - export var_name, var_name2 - var_name, var_name3 = "hello", "world" + export var_name, var_name2 + var_name, var_name3 = "hello", "world" This is especially useful when declaring what will be externally visible in a @@ -655,23 +655,25 @@ The function stub syntax is a shorthand for creating a new closure function that bundles both the object and function. This new function calls the wrapped function in the correct context of the object. -Its syntax is the same as calling an instance method with the `\` operator but +Its syntax is the same as calling an instance method with the \\ operator but with no argument list provided. my_object = { value: 1000 - print: => print "the value:", @value + write: => print "the value:", @value } run_callback (func) -> print "running callback..." func! - -- this will not work, the function has to no reference to my_object - run_callback my_object.print + -- this will not work: + -- the function has to no reference to my_object + run_callback my_object.write - -- function stub syntax lets us bundle the object into a new function - run_callback my_object\print + -- function stub syntax + -- lets us bundle the object into a new function + run_callback my_object\write ## The Using Clause; Controlling Destructive Assignment diff --git a/docs/reference_manual.html b/docs/reference_manual.html index f83d279..22654bb 100644 --- a/docs/reference_manual.html +++ b/docs/reference_manual.html @@ -1,7 +1,7 @@ - + MoonScript v0.2.0 @@ -212,6 +212,8 @@ p > code , li > code {
  • With Statement
  • +
  • Function Stubs
  • +
  • The Using Clause; Controlling Destructive Assignment
  • MoonScript API
  • @@ -529,7 +531,7 @@ arguments if we know we will be using a lower indentation futher on.

    }

    The same thing can be done with other block level statements like -conditionals. We can use indentation level to determine what +conditionals. We can use indentation level to determine what statement a value belongs to:

    moonscriptlua
    if func 1,2,3,
    @@ -991,11 +993,11 @@ need to return the results of the loop.

    while running == true do my_function!
    local i = 10
    -while i > 0 do
    +while i > 0 do
       print(i)
       i = i - 1
     end
    -while running == true do
    +while running == true do
       my_function()
     end
    @@ -1264,7 +1266,7 @@ properties and methods from another class.

    size: 10 add_item: (name) => if #@items > size then error "backpack is full" - super name + super name
    local BackPack
     BackPack = (function(_parent_0)
       local _base_0 = {
    @@ -1336,7 +1338,7 @@ will not be assigned locally.

    module:

    moonscriptlua
    -- my_module.moon
    -module "my_module", package.seeall
    +module "my_module", package.seeall
     export print_result
     
     length = (x, y) -> math.sqrt x*x + y*y
    @@ -1345,12 +1347,12 @@ module:

    print "Length is ", length x, y -- main.moon -require "my_module" +require "my_module" my_module.print_result 4, 5 -- prints the result print my_module.length 6, 7 -- errors, `length` not visible -
    module("my_module", package.seeall)
    +
    module("my_module", package.seeall)
     local length
     length = function(x, y)
       return math.sqrt(x * x + y * y)
    @@ -1358,7 +1360,7 @@ module:

    print_result = function(x, y) return print("Length is ", length(x, y)) end -require("my_module") +require("my_module") my_module.print_result(4, 5) print(my_module.length(6, 7))
    @@ -1387,7 +1389,7 @@ to bind it to that table:

    import \add from my_module -print add(22) -- equivalent to calling my_module:get(22) +print add(22) -- equivalent to calling my_module\get 22
    local my_module = {
       state = 100,
       add = function(self, value)
    @@ -1407,8 +1409,11 @@ unnecessary noise. A common solution to this is to pass a table in as an
     argument which contains a collection of keys and values to overwrite. The
     downside to this is that the constructor of this object must support this form.

    -

    The with block helps to alleviate this. It lets us use a bare function and -index syntax in order to work with the object:

    +

    The with block helps to alleviate this. Within a with block we can use a +special statements that begin with either . or \` which represent +those operations applied to the object we are usingwithon.

    + +

    For example, we work with a newly created object:

    moonscriptlua
    with Person!
       .name = "Oswald"
    @@ -1464,6 +1469,55 @@ it has been giving access to.

    sister })
    +

    Function Stubs

    + +

    It is common to pass a function from an object around as a value, for example, +passing an instance method into a function as a callback. If the function +expects the object it is operating on as the first argument then you must +somehow bundle that object with the function so it can be called properly.

    + +

    The function stub syntax is a shorthand for creating a new closure function +that bundles both the object and function. This new function calls the wrapped +function in the correct context of the object.

    + +

    Its syntax is the same as calling an instance method with the \ operator but +with no argument list provided.

    + +
    moonscriptlua
    my_object = {
    +  value: 1000
    +  write: => print "the value:", @value
    +}
    +
    +run_callback (func) ->
    +  print "running callback..."
    +  func!
    +
    +-- this will not work:
    +-- the function has to no reference to my_object
    +run_callback my_object.write
    +
    +-- function stub syntax
    +-- lets us bundle the object into a new function
    +run_callback my_object\write
    +
    local my_object = {
    +  value = 1000,
    +  write = function(self)
    +    return print("the value:", self.value)
    +  end
    +}
    +run_callback(function(func)
    +  print("running callback...")
    +  return func()
    +end)
    +run_callback(my_object.write)
    +run_callback((function()
    +  local _base_0 = my_object
    +  local _fn_0 = _base_0.write
    +  return function(...)
    +    return _fn_0(_base_0, ...)
    +  end
    +end)())
    +

    The Using Clause; Controlling Destructive Assignment

    While lexical scoping can be a great help in reducing the complexity of the @@ -1487,7 +1541,7 @@ the following snippet:

    local my_func my_func = function() i = 10 - while i > 0 do + while i > 0 do print(i) i = i - 1 end @@ -1553,8 +1607,8 @@ accessed, they just cant be modified:

    Upon installing MoonScript, a moonscript module is made available. The best use of this module is making your Lua’s require function MoonScript aware.

    -
    moonscriptlua
    require "moonscript"
    -
    require("moonscript")
    +
    moonscriptlua
    require "moonscript"
    +
    require("moonscript")

    After moonscript is required, Lua’s package loader is updated to search for .moon files on any subsequent calls to require. The search path for .moon @@ -1615,8 +1669,8 @@ Lua code from MoonScript code.

    Here is a quick example of how you would compile a MoonScript string to a Lua String:

    -
    moonscriptlua
    require "moonscript.parse"
    -require "moonscript.compile"
    +
    moonscriptlua
    require "moonscript.parse"
    +require "moonscript.compile"
     
     import parse, compile from moonscript
     
    @@ -1632,8 +1686,8 @@ String:

    -- our code is ready print lua_code -
    require("moonscript.parse")
    -require("moonscript.compile")
    +
    require("moonscript.parse")
    +require("moonscript.compile")
     local parse, compile = moonscript.parse, moonscript.compile
     local moon_code = [[(-> print "hello world")!]]
     local tree, err = parse.string(moon_code)
    @@ -1720,7 +1774,7 @@ THE SOFTWARE.

    - +