moonlib updates, built and added to rockspec

This commit is contained in:
leaf corcoran 2011-10-28 22:12:27 -07:00
parent ee5d9eaf73
commit 74060bccae
3 changed files with 87 additions and 12 deletions

View File

@ -1,16 +1,14 @@
if not moon or not moon.inject then
print("wrapping")
module("moon", package.seeall)
end
local util = require("moonscript.util")
dump = util.dump
run_with_scope = function(fn, scope)
run_with_scope = function(fn, scope, ...)
local old_env = getfenv(fn)
local env = setmetatable({ }, {
__index = function(self, name)
print("indexing ", name)
local val = scope[name]
if val then
if val ~= nil then
return val
else
return old_env[name]
@ -18,9 +16,9 @@ run_with_scope = function(fn, scope)
end
})
setfenv(fn, env)
return fn()
return fn(...)
end
bound_methods = function(obj)
bind_methods = function(obj)
return setmetatable({ }, {
__index = function(self, name)
local val = obj[name]
@ -37,3 +35,70 @@ bound_methods = function(obj)
end
})
end
defaultbl = function(t, fn)
if not fn then
fn = t
t = { }
end
return setmetatable(t, {
__index = function(self, name)
local val = fn(self, name)
rawset(self, name, val)
return val
end
})
end
extend = function(...)
local tbls = {
...
}
if #tbls < 2 then
return
end
for i = 1, #tbls - 1 do
local a = tbls[i]
local b = tbls[i + 1]
setmetatable(a, {
__index = b
})
end
return tbls[1]
end
copy = function(self)
local t = { }
for key, val in pairs(self) do
t[key] = val
end
return t
end
mixin = function(self, cls, ...)
local meta = getmetatable(cls)
for key, val in pairs(meta.__index) do
if not key:match("^__") then
self[key] = val
end
end
return cls.__init(self, ...)
end
mixin_object = function(self, object, methods)
local _list_0 = methods
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
self[name] = function(parent, ...)
return object[name](object, ...)
end
end
end
mixin_table = function(self, tbl, keys)
if keys then
local _list_0 = keys
for _index_0 = 1, #_list_0 do
local key = _list_0[_index_0]
self[key] = tbl[key]
end
else
for key, val in pairs(tbl) do
self[key] = val
end
end
end

View File

@ -13,7 +13,6 @@ run_with_scope = (fn, scope, ...) ->
old_env = getfenv fn
env = setmetatable {}, {
__index: (name) =>
print "indexing ", name
val = scope[name]
if val != nil
val
@ -24,7 +23,7 @@ run_with_scope = (fn, scope, ...) ->
fn ...
-- wrap obj such that calls to methods do not need a reference to self
bound_methods = (obj) ->
bind_methods = (obj) ->
setmetatable {}, {
__index: (name) =>
val = obj[name]
@ -51,9 +50,18 @@ defaultbl = (t, fn) ->
val
}
-- helper function to set metatable with index
extend = (base) =>
setmetatable self, __index: base
-- chain together tables by __index metatables
extend = (...) ->
tbls = {...}
return if #tbls < 2
for i = 1, #tbls - 1
a = tbls[i]
b = tbls[i + 1]
setmetatable a, __index: b
tbls[1]
-- shallow copy
copy = =>

View File

@ -6,7 +6,7 @@ source = {
}
description = {
summary = "A little language that compiles to Lua",
summary = "A programmer friendly language that compiles to Lua",
homepage = "http://moonscript.org",
maintainer = "Leaf Corcoran <leafot@gmail.com>",
license = "MIT"
@ -35,6 +35,8 @@ build = {
["moonscript.util"] = "moonscript/util.lua",
["moonscript.errors"] = "moonscript/errors.lua",
["moonscript.version"] = "moonscript/version.lua",
["moon"] = "moon/init.lua",
["moon.all"] = "moon/all.lua",
},
install = {
bin = { "bin/moon", "bin/moonc" }