From 7eb269b35ab55de10be054756dc4709244d07988 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 11:22:40 -0800 Subject: [PATCH] add distinct class and instance super transformers --- moonscript/transform/class.lua | 21 ++++++++++++++++++--- moonscript/transform/class.moon | 14 +++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/moonscript/transform/class.lua b/moonscript/transform/class.lua index 3e480c2..f55dce0 100644 --- a/moonscript/transform/class.lua +++ b/moonscript/transform/class.lua @@ -13,8 +13,13 @@ do local _obj_0 = require("moonscript.types") build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP end +local unpack +unpack = require("moonscript.util").unpack local transform_super -transform_super = function(cls_name, block, chain) +transform_super = function(cls_name, on_base, block, chain) + if on_base == nil then + on_base = true + end local relative_parent = { "chain", cls_name, @@ -36,6 +41,12 @@ transform_super = function(cls_name, block, chain) local new_chain = relative_parent local _exp_0 = head[1] if "call" == _exp_0 then + if on_base then + insert(new_chain, { + "dot", + "__base" + }) + end local calling_name = block:get("current_method") assert(calling_name, "missing calling name") chain_tail[1] = { @@ -103,9 +114,13 @@ return function(self, node, ret, parent_assign) local base_name = NameProxy("base") local self_name = NameProxy("self") local cls_name = NameProxy("class") + local cls_instance_super + cls_instance_super = function(...) + return transform_super(cls_name, true, ...) + end local cls_super cls_super = function(...) - return transform_super(cls_name, ...) + return transform_super(cls_name, false, ...) end local statements = { } local properties = { } @@ -150,7 +165,7 @@ return function(self, node, ret, parent_assign) key, val = tuple[1], tuple[2] _value_0 = { key, - super_scope(val, cls_super, key) + super_scope(val, cls_instance_super, key) } end _accum_0[_len_0] = _value_0 diff --git a/moonscript/transform/class.moon b/moonscript/transform/class.moon index 729c47a..2a95265 100644 --- a/moonscript/transform/class.moon +++ b/moonscript/transform/class.moon @@ -5,8 +5,9 @@ CONSTRUCTOR_NAME = "new" import insert from table import build, ntype, NOOP from require "moonscript.types" +import unpack from require "moonscript.util" -transform_super = (cls_name, block, chain) -> +transform_super = (cls_name, on_base=true, block, chain) -> relative_parent = { "chain", cls_name @@ -26,6 +27,9 @@ transform_super = (cls_name, block, chain) -> switch head[1] -- calling super, inject calling name and self into chain when "call" + if on_base + insert new_chain, {"dot", "__base"} + calling_name = block\get "current_method" assert calling_name, "missing calling name" chain_tail[1] = {"call", {"self", unpack head[2]}} @@ -80,7 +84,11 @@ super_scope = (value, t, key) -> self_name = NameProxy "self" cls_name = NameProxy "class" - cls_super = (...) -> transform_super cls_name, ... + -- super call on instance + cls_instance_super = (...) -> transform_super cls_name, true, ... + + -- super call on parent class + cls_super = (...) -> transform_super cls_name, false, ... -- split apart properties and statements statements = {} @@ -107,7 +115,7 @@ super_scope = (value, t, key) -> continue else {key, val} = tuple - {key, super_scope val, cls_super, key} + {key, super_scope val, cls_instance_super, key} unless constructor