mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
add distinct class and instance super transformers
This commit is contained in:
parent
1bdcf26b08
commit
7eb269b35a
@ -13,8 +13,13 @@ do
|
|||||||
local _obj_0 = require("moonscript.types")
|
local _obj_0 = require("moonscript.types")
|
||||||
build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP
|
build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP
|
||||||
end
|
end
|
||||||
|
local unpack
|
||||||
|
unpack = require("moonscript.util").unpack
|
||||||
local transform_super
|
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 = {
|
local relative_parent = {
|
||||||
"chain",
|
"chain",
|
||||||
cls_name,
|
cls_name,
|
||||||
@ -36,6 +41,12 @@ transform_super = function(cls_name, block, chain)
|
|||||||
local new_chain = relative_parent
|
local new_chain = relative_parent
|
||||||
local _exp_0 = head[1]
|
local _exp_0 = head[1]
|
||||||
if "call" == _exp_0 then
|
if "call" == _exp_0 then
|
||||||
|
if on_base then
|
||||||
|
insert(new_chain, {
|
||||||
|
"dot",
|
||||||
|
"__base"
|
||||||
|
})
|
||||||
|
end
|
||||||
local calling_name = block:get("current_method")
|
local calling_name = block:get("current_method")
|
||||||
assert(calling_name, "missing calling name")
|
assert(calling_name, "missing calling name")
|
||||||
chain_tail[1] = {
|
chain_tail[1] = {
|
||||||
@ -103,9 +114,13 @@ return function(self, node, ret, parent_assign)
|
|||||||
local base_name = NameProxy("base")
|
local base_name = NameProxy("base")
|
||||||
local self_name = NameProxy("self")
|
local self_name = NameProxy("self")
|
||||||
local cls_name = NameProxy("class")
|
local cls_name = NameProxy("class")
|
||||||
|
local cls_instance_super
|
||||||
|
cls_instance_super = function(...)
|
||||||
|
return transform_super(cls_name, true, ...)
|
||||||
|
end
|
||||||
local cls_super
|
local cls_super
|
||||||
cls_super = function(...)
|
cls_super = function(...)
|
||||||
return transform_super(cls_name, ...)
|
return transform_super(cls_name, false, ...)
|
||||||
end
|
end
|
||||||
local statements = { }
|
local statements = { }
|
||||||
local properties = { }
|
local properties = { }
|
||||||
@ -150,7 +165,7 @@ return function(self, node, ret, parent_assign)
|
|||||||
key, val = tuple[1], tuple[2]
|
key, val = tuple[1], tuple[2]
|
||||||
_value_0 = {
|
_value_0 = {
|
||||||
key,
|
key,
|
||||||
super_scope(val, cls_super, key)
|
super_scope(val, cls_instance_super, key)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
_accum_0[_len_0] = _value_0
|
_accum_0[_len_0] = _value_0
|
||||||
|
@ -5,8 +5,9 @@ CONSTRUCTOR_NAME = "new"
|
|||||||
|
|
||||||
import insert from table
|
import insert from table
|
||||||
import build, ntype, NOOP from require "moonscript.types"
|
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 = {
|
relative_parent = {
|
||||||
"chain",
|
"chain",
|
||||||
cls_name
|
cls_name
|
||||||
@ -26,6 +27,9 @@ transform_super = (cls_name, block, chain) ->
|
|||||||
switch head[1]
|
switch head[1]
|
||||||
-- calling super, inject calling name and self into chain
|
-- calling super, inject calling name and self into chain
|
||||||
when "call"
|
when "call"
|
||||||
|
if on_base
|
||||||
|
insert new_chain, {"dot", "__base"}
|
||||||
|
|
||||||
calling_name = block\get "current_method"
|
calling_name = block\get "current_method"
|
||||||
assert calling_name, "missing calling name"
|
assert calling_name, "missing calling name"
|
||||||
chain_tail[1] = {"call", {"self", unpack head[2]}}
|
chain_tail[1] = {"call", {"self", unpack head[2]}}
|
||||||
@ -80,7 +84,11 @@ super_scope = (value, t, key) ->
|
|||||||
self_name = NameProxy "self"
|
self_name = NameProxy "self"
|
||||||
cls_name = NameProxy "class"
|
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
|
-- split apart properties and statements
|
||||||
statements = {}
|
statements = {}
|
||||||
@ -107,7 +115,7 @@ super_scope = (value, t, key) ->
|
|||||||
continue
|
continue
|
||||||
else
|
else
|
||||||
{key, val} = tuple
|
{key, val} = tuple
|
||||||
{key, super_scope val, cls_super, key}
|
{key, super_scope val, cls_instance_super, key}
|
||||||
|
|
||||||
|
|
||||||
unless constructor
|
unless constructor
|
||||||
|
Loading…
Reference in New Issue
Block a user