converted some lua to moonscript

This commit is contained in:
leaf corcoran 2011-08-28 22:50:12 -07:00
parent 19211b356d
commit ec8cc9ae66
12 changed files with 359 additions and 214 deletions

View File

@ -1,7 +1,6 @@
module("moonscript.compile", package.seeall)
local util = require("moonscript.util")
local data = require("moonscript.data")
local itwos = util.itwos
local Set, ntype = data.Set, data.ntype
local concat, insert = table.concat, table.insert
indent_char = " "

View File

@ -3,7 +3,6 @@ module "moonscript.compile", package.seeall
util = require "moonscript.util"
data = require "moonscript.data"
import itwos from util
import Set, ntype from data
import concat, insert from table

View File

@ -1,7 +1,6 @@
module("moonscript.compile", package.seeall)
local util = require("moonscript.util")
local data = require("moonscript.data")
local dump = require("moonscript.dump")
require("moonscript.compile.format")
require("moonscript.compile.types")
local reversed = util.reversed

View File

@ -2,7 +2,6 @@ module "moonscript.compile", package.seeall
util = require "moonscript.util"
data = require "moonscript.data"
dump = require "moonscript.dump"
require "moonscript.compile.format"
require "moonscript.compile.types"

View File

@ -1,7 +1,6 @@
module("moonscript.compile", package.seeall)
local util = require("moonscript.util")
local data = require("moonscript.data")
local dump = require("moonscript.dump")
require("moonscript.compile.format")
local ntype = data.ntype
local concat, insert = table.concat, table.insert

View File

@ -3,7 +3,6 @@ module "moonscript.compile", package.seeall
util = require "moonscript.util"
data = require "moonscript.data"
dump = require "moonscript.dump"
require "moonscript.compile.format"

View File

@ -1,52 +1,87 @@
module("moonscript.data", package.seeall)
local stack_t = {}
local _stack_mt = { __index = stack_t, __tostring = function(self)
return "<Stack {"..table.concat(self, ", ").."}>"
end}
function stack_t:pop()
return table.remove(self)
local concat = table.concat
ntype = function(node)
if type(node) ~= "table" then
return "value"
else
return node[1]
end
end
function stack_t:push(value)
table.insert(self, value)
return value
Set = function(items)
local self = { }
do
local _item_0 = items
for _index_0 = 1, #_item_0 do
local key = _item_0[_index_0]
self[key] = true
end
end
return self
end
function stack_t:top()
return self[#self]
end
function Stack(...)
local self = setmetatable({}, _stack_mt)
for _, v in ipairs{...} do
self:push(v)
end
return self
end
function Set(items)
local self = {}
for _,item in ipairs(items) do
self[item] = true
end
return self
end
-- find out the type of a node
function ntype(node)
if type(node) ~= "table" then return "value" end
return node[1]
end
lua_keywords = Set{
'and', 'break', 'do', 'else', 'elseif',
'end', 'false', 'for', 'function', 'if',
'in', 'local', 'nil', 'not', 'or',
'repeat', 'return', 'then', 'true',
'until', 'while'
}
Stack = (function(_parent_0)
local _base_0 = {
__tostring = function(self)
return "<Stack {" .. concat(self, ", ") .. "}>"
end,
pop = function(self)
return table.remove(self)
end,
push = function(self, value)
table.insert(self, value)
return value
end,
top = function(self)
return self[#self]
end
}
_base_0.__index = _base_0
if _parent_0 then
setmetatable(_base_0, getmetatable(_parent_0).__index)
end
local _class_0 = setmetatable({
__init = function(self, ...)
do
local _item_0 = {
...
}
for _index_0 = 1, #_item_0 do
local v = _item_0[_index_0]
self:push(v)
end
end
return nil
end
}, {
__index = _base_0,
__call = function(mt, ...)
local self = setmetatable({}, _base_0)
mt.__init(self, ...)
return self
end
})
_base_0.__class = _class_0
return _class_0
end)()
lua_keywords = Set({
'and',
'break',
'do',
'else',
'elseif',
'end',
'false',
'for',
'function',
'if',
'in',
'local',
'nil',
'not',
'or',
'repeat',
'return',
'then',
'true',
'until',
'while'
})

47
moonscript/data.moon Normal file
View File

@ -0,0 +1,47 @@
-- data structure utils
module "moonscript.data", package.seeall
export Set, Stack
export ntype, lua_keywords
import concat from table
-- type of node as string
ntype = (node) ->
if type(node) != "table"
"value"
else
node[1]
Set = (items) ->
self = {}
self[key] = true for key in *items
self
class Stack
__tostring: => "<Stack {"..concat(self, ", ").."}>"
new: (...) =>
@push v for v in *{...}
nil
pop: =>
table.remove self
push: (value) =>
table.insert self, value
value
top: =>
self[#self]
lua_keywords = Set{
'and', 'break', 'do', 'else', 'elseif',
'end', 'false', 'for', 'function', 'if',
'in', 'local', 'nil', 'not', 'or',
'repeat', 'return', 'then', 'true',
'until', 'while'
}

View File

@ -1,29 +1,46 @@
module("moonscript.dump", package.seeall)
local function flat_value(op, depth)
depth = depth or 1
if type(op) == "string" then return '"'..op..'"' end
if type(op) ~= "table" then return tostring(op) end
local items = {}
for _, item in ipairs(op) do
table.insert(items, flat_value(item, depth+1))
end
local pos = op[-1]
return "{"..(pos and "["..pos.."] " or "")..table.concat(items, ", ").."}"
local flat_value
flat_value = function(op, depth)
if depth == nil then
depth = 1
end
if type(op) == "string" then
return '"' .. op .. '"'
end
if type(op) ~= "table" then
return tostring(op)
end
local items = (function()
local _accum_0 = { }
local _len_0 = 0
do
local _item_0 = op
for _index_0 = 1, #_item_0 do
local item = _item_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = flat_value(item, depth + 1)
end
end
return _accum_0
end)()
local pos = op[-1]
return "{" .. (pos and "[" .. pos .. "] " or "") .. table.concat(items, ", ") .. "}"
end
function value(op)
return flat_value(op)
value = function(op)
return flat_value(op)
end
function tree(block, depth)
depth = depth or 0
for _, op in ipairs(block) do
print(flat_value(op))
end
tree = function(block)
return (function()
local _accum_0 = { }
local _len_0 = 0
do
local _item_0 = block
for _index_0 = 1, #_item_0 do
local value = _item_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = print(flat_value(value))
end
end
return _accum_0
end)()
end

19
moonscript/dump.moon Normal file
View File

@ -0,0 +1,19 @@
module "moonscript.dump", package.seeall
export value, tree
flat_value = (op, depth=1) ->
return '"'..op..'"' if type(op) == "string"
return tostring(op) if type(op) != "table"
items = [flat_value item, depth + 1 for item in *op]
pos = op[-1]
"{"..(pos and "["..pos.."] " or "")..table.concat(items, ", ").."}"
value = (op) ->
flat_value op
tree = (block) ->
print flat_value value for value in *block

View File

@ -1,144 +1,101 @@
module("moonscript.util", package.seeall)
local concat = table.concat
moon = {
is_object = function(value)
return type(value) == "table" and value.__class
end,
type = function(value)
base_type = type(value)
if base_type == "table" then
cls = value.__class
if cls then return cls end
end
return base_type
end
is_object = function(value)
return type(value) == "table" and value.__class
end,
type = function(value)
local base_type = type(value)
if base_type == "table" then
local cls = value.__class
if cls then
return cls
end
end
return base_type
end
}
function pos_to_line(str, pos)
local line = 1
for _ in str:sub(1, pos):gmatch("\n") do
line = line + 1
end
return line
pos_to_line = function(str, pos)
local line = 1
for _ in str:sub(1, pos):gmatch("\n") do
line = line + 1
end
return line
end
function get_closest_line(str, line_num)
local line = get_line(str, line_num)
if (not line or trim(line) == "") and line_num > 1 then
return get_closest_line(str, line_num - 1)
end
return line, line_num
get_closest_line = function(str, line_num)
local line = get_line(str, line_num)
if (not line or trim(line) == "") and line_num > 1 then
return get_closest_line(str, line_num - 1)
else
return line, line_num
end
end
function get_line(str, line_num)
for line in str:gmatch("(.-)[\n$]") do
if line_num == 1 then
return line
end
line_num = line_num - 1
end
get_line = function(str, line_num)
for line in str:gmatch("(.-)[\n$]") do
if line_num == 1 then
return line
end
line_num = line_num - 1
end
end
-- shallow copy
function clone(tbl)
local out = {}
for k,v in pairs(tbl) do
out[k] = v
end
return out
reversed = function(seq)
return coroutine.wrap(function()
for i = #seq, 1, -1 do
coroutine.yield(i, seq[i])
end
end)
end
function map(tbl, fn)
local out = {}
for i,v in ipairs(tbl) do
out[i] = fn(v)
end
return out
trim = function(str)
return str:match("^%s*(.-)%s*$")
end
function every(tbl, fn)
for i=1,#tbl do
local pass
if fn then
pass = fn(tbl[i])
else
pass = tbl[i]
end
if not pass then return false end
end
return true
split = function(str, delim)
if str == "" then
return { }
end
str = str .. delim
return (function()
local _accum_0 = { }
local _len_0 = 0
for m in str:gmatch("(.-)" .. delim) do
_len_0 = _len_0 + 1
_accum_0[_len_0] = m
end
return _accum_0
end)()
end
function bind(obj, name)
return function(...)
return obj[name](obj, ...)
end
dump = function(what)
local seen = { }
local _dump
_dump = function(what, depth)
if depth == nil then
depth = 0
end
local t = type(what)
if t == "string" then
return '"' .. what .. '"\n'
elseif t == "table" then
if seen[what] then
return "recursion(" .. tostring(what) .. ")...\n"
end
local _ = seen[what] == true
depth = depth + 1
local lines = (function()
local _accum_0 = { }
local _len_0 = 0
for k, v in pairs(what) do
local _value_0 = (" "):rep(depth * 4) .. "[" .. tostring(k) .. "] = " .. _dump(v, depth)
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
seen[what] = false
return "{\n" .. concat(lines) .. (" "):rep((depth - 1) * 4) .. "}\n"
else
return tostring(what) .. "\n"
end
end
return _dump(what)
end
function itwos(seq)
n = 2
return coroutine.wrap(function()
for i = 1, #seq-n+1 do
coroutine.yield(i, seq[i], i+1, seq[i+1])
end
end)
end
function reversed(seq)
return coroutine.wrap(function()
for i=#seq,1,-1 do
coroutine.yield(i, seq[i])
end
end)
end
function dump(what)
local seen = {}
local function _dump(what, depth)
depth = depth or 0
local t = type(what)
if t == "string" then
return '"'..what..'"\n'
elseif t == "table" then
if seen[what] then
return "recursion("..tostring(what)..")...\n"
end
seen[what] = true
depth = depth + 1
out = "{\n"
for k,v in pairs(what) do
out = out..(" "):rep(depth*4).."["..tostring(k).."] = ".._dump(v, depth)
end
seen[what] = false
return out .. (" "):rep((depth-1)*4) .. "}\n"
else
return tostring(what).."\n"
end
end
return _dump(what)
end
function split(str, delim)
if str == "" then return {} end
str = str..delim
local out = {}
for m in str:gmatch("(.-)"..delim) do
table.insert(out, m)
end
return out
end
function trim(str)
return str:match("^%s*(.-)%s*$")
end

76
moonscript/util.moon Normal file
View File

@ -0,0 +1,76 @@
module "moonscript.util", package.seeall
export moon
export pos_to_line, get_closest_line, get_line
export reversed, trim, split
export dump
import concat from table
moon =
is_object: (value) -> -- is a moonscript object
type(value) == "table" and value.__class
type: (value) -> -- the moonscript object class
base_type = type value
if base_type == "table"
cls = value.__class
return cls if cls
base_type
-- convet position in text to line number
pos_to_line = (str, pos) ->
line = 1
for _ in str\sub(1, pos)\gmatch("\n")
line += 1
line
get_closest_line = (str, line_num) ->
line = get_line str, line_num
if (not line or trim(line) == "") and line_num > 1
get_closest_line(str, line_num - 1)
else
line, line_num
get_line = (str, line_num) ->
for line in str\gmatch "(.-)[\n$]"
return line if line_num == 1
line_num -= 1
reversed = (seq) ->
coroutine.wrap ->
for i=#seq,1,-1
coroutine.yield i, seq[i]
trim = (str) ->
str\match "^%s*(.-)%s*$"
split = (str, delim) ->
return {} if str == ""
str ..= delim
[m for m in str\gmatch("(.-)"..delim)]
dump = (what) ->
seen = {}
_dump = (what, depth=0) ->
t = type what
if t == "string"
'"'..what..'"\n'
elseif t == "table"
if seen[what]
return "recursion("..tostring(what) ..")...\n"
seen[what] == true
depth += 1
lines = for k,v in pairs what
(" ")\rep(depth*4).."["..tostring(k).."] = ".._dump(v, depth)
seen[what] = false
"{\n" .. concat(lines) .. (" ")\rep((depth - 1)*4) .. "}\n"
else
tostring(what).."\n"
_dump what