This commit is contained in:
Paul Liverman III
2018-04-06 15:30:00 -07:00
parent f0a9da387a
commit a45aa2624e
101 changed files with 12392 additions and 77 deletions

View File

@@ -0,0 +1,5 @@
local moon = require("moon")
for k, v in pairs(moon) do
_G[k] = v
end
return moon

View File

@@ -0,0 +1,6 @@
-- install moon into global scope
moon = require "moon"
for k,v in pairs moon
_G[k] = v
moon

View File

@@ -0,0 +1,175 @@
local lua = {
debug = debug,
type = type
}
local getfenv, setfenv, dump
do
local _obj_0 = require("moonscript.util")
getfenv, setfenv, dump = _obj_0.getfenv, _obj_0.setfenv, _obj_0.dump
end
local p, is_object, type, debug, run_with_scope, bind_methods, defaultbl, extend, copy, mixin, mixin_object, mixin_table, fold
p = function(...)
return print(dump(...))
end
is_object = function(value)
return lua.type(value) == "table" and value.__class
end
type = function(value)
local base_type = lua.type(value)
if base_type == "table" then
local cls = value.__class
if cls then
return cls
end
end
return base_type
end
debug = setmetatable({
upvalue = function(fn, k, v)
local upvalues = { }
local i = 1
while true do
local name = lua.debug.getupvalue(fn, i)
if name == nil then
break
end
upvalues[name] = i
i = i + 1
end
if not upvalues[k] then
error("Failed to find upvalue: " .. tostring(k))
end
if not v then
local _, value = lua.debug.getupvalue(fn, upvalues[k])
return value
else
return lua.debug.setupvalue(fn, upvalues[k], v)
end
end
}, {
__index = lua.debug
})
run_with_scope = function(fn, scope, ...)
local old_env = getfenv(fn)
local env = setmetatable({ }, {
__index = function(self, name)
local val = scope[name]
if val ~= nil then
return val
else
return old_env[name]
end
end
})
setfenv(fn, env)
return fn(...)
end
bind_methods = function(obj)
return setmetatable({ }, {
__index = function(self, name)
local val = obj[name]
if val and lua.type(val) == "function" then
local bound
bound = function(...)
return val(obj, ...)
end
self[name] = bound
return bound
else
return val
end
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 _tbl_0 = { }
for key, val in pairs(self) do
_tbl_0[key] = val
end
return _tbl_0
end
mixin = function(self, cls, ...)
for key, val in pairs(cls.__base) do
if not key:match("^__") then
self[key] = val
end
end
return cls.__init(self, ...)
end
mixin_object = function(self, object, methods)
for _index_0 = 1, #methods do
local name = methods[_index_0]
self[name] = function(parent, ...)
return object[name](object, ...)
end
end
end
mixin_table = function(self, tbl, keys)
if keys then
for _index_0 = 1, #keys do
local key = keys[_index_0]
self[key] = tbl[key]
end
else
for key, val in pairs(tbl) do
self[key] = val
end
end
end
fold = function(items, fn)
local len = #items
if len > 1 then
local accum = fn(items[1], items[2])
for i = 3, len do
accum = fn(accum, items[i])
end
return accum
else
return items[1]
end
end
return {
dump = dump,
p = p,
is_object = is_object,
type = type,
debug = debug,
run_with_scope = run_with_scope,
bind_methods = bind_methods,
defaultbl = defaultbl,
extend = extend,
copy = copy,
mixin = mixin,
mixin_object = mixin_object,
mixin_table = mixin_table,
fold = fold
}

View File

@@ -0,0 +1,133 @@
lua = { :debug, :type }
import getfenv, setfenv, dump from require "moonscript.util"
local *
p = (...) ->
print dump ...
is_object = (value) -> -- is a moonscript object
lua.type(value) == "table" and value.__class
type = (value) -> -- class aware type
base_type = lua.type value
if base_type == "table"
cls = value.__class
return cls if cls
base_type
debug = setmetatable {
upvalue: (fn, k, v) ->
upvalues = {}
i = 1
while true
name = lua.debug.getupvalue(fn, i)
break if name == nil
upvalues[name] = i
i += 1
if not upvalues[k]
error "Failed to find upvalue: " .. tostring k
if not v
_, value = lua.debug.getupvalue fn, upvalues[k]
value
else
lua.debug.setupvalue fn, upvalues[k], v
}, __index: lua.debug
-- run a function with scope injected before its function environment
run_with_scope = (fn, scope, ...) ->
old_env = getfenv fn
env = setmetatable {}, {
__index: (name) =>
val = scope[name]
if val != nil
val
else
old_env[name]
}
setfenv fn, env
fn ...
-- wrap obj such that calls to methods do not need a reference to self
bind_methods = (obj) ->
setmetatable {}, {
__index: (name) =>
val = obj[name]
if val and lua.type(val) == "function"
bound = (...) -> val obj, ...
self[name] = bound
bound
else
val
}
-- use a function to provide default values to table
-- optionally specify a starting table
-- fibanocci table:
-- t = defaultbl {[0]: 0, [1]: 1}, (i) -> self[i - 1] + self[i - 2]
defaultbl = (t, fn) ->
if not fn
fn = t
t = {}
setmetatable t, {
__index: (name) =>
val = fn self, name
rawset self, name, val
val
}
-- 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 = =>
{key,val for key,val in pairs self}
-- mixin class properties into self, call new
mixin = (cls, ...) =>
for key, val in pairs cls.__base
self[key] = val if not key\match"^__"
cls.__init self, ...
-- mixin methods from an object into self
mixin_object = (object, methods) =>
for name in *methods
self[name] = (parent, ...) ->
object[name](object, ...)
-- mixin table values into self
mixin_table = (tbl, keys) =>
if keys
for key in *keys
self[key] = tbl[key]
else
for key, val in pairs tbl
self[key] = val
fold = (items, fn)->
len = #items
if len > 1
accum = fn items[1], items[2]
for i=3,len
accum = fn accum, items[i]
accum
else
items[1]
{
:dump, :p, :is_object, :type, :debug, :run_with_scope, :bind_methods,
:defaultbl, :extend, :copy, :mixin, :mixin_object, :mixin_table, :fold
}