diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index 45e9a48..bb80e96 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -27,7 +27,7 @@ import concat, insert from table _, names, values = unpack node undeclared = @declare names - declare = "local "..concat(undeclared, ", ") + declare = "local " .. concat(undeclared, ", ") has_fndef = false i = 1 diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 82cc730..af7320e 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -17,6 +17,16 @@ local string_chars = { ["\n"] = "\\n" } return { + scoped = function(self, node) + local _, before, value, after + _, before, value, after = node[1], node[2], node[3], node[4] + _ = before and before:call(self) + do + local _with_0 = self:value(value) + _ = after and after:call(self) + return _with_0 + end + end, exp = function(self, node) local _comp _comp = function(i, value) @@ -248,9 +258,7 @@ return { else assign = self:line("[", _with_0:value(key), "]") end - _with_0:set("current_block", key) local out = self:line(assign, " = ", _with_0:value(value)) - _with_0:set("current_block", nil) return out else return self:line(_with_0:value(tuple[1])) diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index fb5c225..193a4b5 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -15,6 +15,12 @@ string_chars = { } { + scoped: (node) => + {_, before, value, after} = node + before and before\call @ + with @value value + after and after\call @ + -- list of values separated by binary operators exp: (node) => _comp = (i, value) -> @@ -146,9 +152,7 @@ string_chars = { else @line "[", \value(key), "]" - \set "current_block", key out = @line assign, " = ", \value(value) - \set "current_block", nil out else @line \value tuple[1] diff --git a/moonscript/transform/class.lua b/moonscript/transform/class.lua index a8ad78b..1079dcd 100644 --- a/moonscript/transform/class.lua +++ b/moonscript/transform/class.lua @@ -13,6 +13,21 @@ do local _obj_0 = require("moonscript.types") build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP end +local super_scope +super_scope = function(value, key) + local prev_method + return { + "scoped", + Run(function(self) + prev_method = self:get("current_method") + return self:set("current_method", key) + end), + value, + Run(function(self) + return self:set("current_method", prev_method) + end) + } +end return function(self, node, ret, parent_assign) local _, name, parent_val, body = unpack(node) if parent_val == "" then @@ -51,7 +66,12 @@ return function(self, node, ret, parent_assign) _continue_0 = true break else - _value_0 = tuple + local val + key, val = tuple[1], tuple[2] + _value_0 = { + key, + super_scope(val, key) + } end _accum_0[_len_0] = _value_0 _len_0 = _len_0 + 1 @@ -129,7 +149,10 @@ return function(self, node, ret, parent_assign) local cls = build.table({ { "__init", - constructor + super_scope(constructor, { + "key_literal", + "__init" + }) }, { "__base", @@ -304,7 +327,7 @@ return function(self, node, ret, parent_assign) local new_chain = relative_parent local _exp_1 = head[1] if "call" == _exp_1 then - local calling_name = block:get("current_block") + local calling_name = block:get("current_method") assert(calling_name, "missing calling name") chain_tail[1] = { "call", diff --git a/moonscript/transform/class.moon b/moonscript/transform/class.moon index 007bace..f75e4e5 100644 --- a/moonscript/transform/class.moon +++ b/moonscript/transform/class.moon @@ -6,6 +6,19 @@ CONSTRUCTOR_NAME = "new" import insert from table import build, ntype, NOOP from require "moonscript.types" +super_scope = (value, key) -> + local prev_method + + { + "scoped", + Run => + prev_method = @get "current_method" + @set "current_method", key + value + Run => + @set "current_method", prev_method + } + (node, ret, parent_assign) => _, name, parent_val, body = unpack node parent_val = nil if parent_val == "" @@ -32,7 +45,8 @@ import build, ntype, NOOP from require "moonscript.types" constructor = tuple[2] continue else - tuple + {key, val} = tuple + {key, super_scope val, key} parent_cls_name = NameProxy "parent" base_name = NameProxy "base" @@ -77,7 +91,7 @@ import build, ntype, NOOP from require "moonscript.types" {"string", '"', flattened_name} cls = build.table { - {"__init", constructor} + {"__init", super_scope constructor, {"key_literal", "__init"}} {"__base", base_name} {"__name", real_name} -- "quote the string" parent_val and {"__parent", parent_cls_name} or nil @@ -173,7 +187,7 @@ import build, ntype, NOOP from require "moonscript.types" switch head[1] -- calling super, inject calling name and self into chain when "call" - calling_name = block\get"current_block" + calling_name = block\get "current_method" assert calling_name, "missing calling name" chain_tail[1] = {"call", {"self", unpack head[2]}}