This commit is contained in:
leaf corcoran 2013-01-10 20:20:05 -08:00
parent fa06e6103d
commit 54dc8efa0b
7 changed files with 72 additions and 7 deletions

View File

@ -749,6 +749,17 @@ tree = function(tree, options)
return lua_code, posmap
end
end
do
local _with_0 = require("moonscript.data")
local data = _with_0
for name, cls in pairs({
Line = Line,
Lines = Lines,
DelayedLine = DelayedLine
}) do
data[name] = cls
end
end
return {
tree = tree,
value = value,

View File

@ -411,4 +411,9 @@ tree = (tree, options={}) ->
posmap = scope._lines\flatten_posmap!
lua_code, posmap
-- mmmm
with data = require "moonscript.data"
for name, cls in pairs {:Line, :Lines, :DelayedLine}
data[name] = cls
{ :tree, :value, :format_error, :Block, :RootBlock }

View File

@ -1,8 +1,6 @@
local reversed, unpack
do
local _table_0 = require("moonscript.util")
reversed, unpack = _table_0.reversed, _table_0.unpack
end
local util = require("moonscript.util")
local data = require("moonscript.data")
local reversed, unpack = util.reversed, util.unpack
local ntype
do
local _table_0 = require("moonscript.types")
@ -41,6 +39,23 @@ local statement_compilers = {
end
end
end,
declare_glob = function(self, node)
local names = { }
self:set("name_glob", function(name)
insert(names, name)
return true
end)
return data.DelayedLine(function(buff)
insert(buff, "local ")
local _list_0 = names
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
insert(buff, self:name(name))
insert(buff, ", ")
end
buff[#buff] = nil
end)
end,
declare_with_shadows = function(self, node)
local names = node[2]
self:declare(names)

View File

@ -1,5 +1,8 @@
import reversed, unpack from require "moonscript.util"
util = require "moonscript.util"
data = require "moonscript.data"
import reversed, unpack from util
import ntype from require "moonscript.types"
import concat, insert from table
@ -17,6 +20,19 @@ statement_compilers =
with @line "local "
\append_list [@name name for name in *undeclared], ", "
declare_glob: (node) =>
names = {}
@set "name_glob", (name) ->
insert names, name
true
data.DelayedLine (buff) ->
insert buff, "local "
for name in *names
insert buff, @name name
insert buff, ", "
buff[#buff] = nil -- strips local if no names
-- this overrides the existing names with new locals, used for local keyword
declare_with_shadows: (node) =>
names = node[2]

View File

@ -404,7 +404,7 @@ local build_grammar = wrap_env(function()
PopIndent = Cmt("", pop_indent),
InBlock = Advance * Block * PopIndent,
Local = key"local" * Ct(NameList) / mark"declare_with_shadows",
Local = key"local" * ((op"*" + op"^") / mark"declare_glob" + Ct(NameList) / mark"declare_with_shadows"),
Import = key"import" * Ct(ImportNameList) * key"from" * Exp / mark"import",
ImportName = (sym"\\" * Ct(Cc"colon_stub" * Name) + Name),

View File

@ -329,6 +329,18 @@ local Statement = Transformer({
end,
assign = function(self, node)
local names, values = unpack(node, 2)
do
local globber = self:get("name_glob")
if globber then
local _list_0 = names
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
if globber(name) then
self:put_name(name)
end
end
end
end
local transformed
if #values == 1 then
local value = values[1]

View File

@ -158,6 +158,12 @@ Statement = Transformer {
assign: (node) =>
names, values = unpack node, 2
if globber = @get "name_glob"
for name in *names
if globber name
@put_name name
-- bubble cascading assigns
transformed = if #values == 1
value = values[1]