self args can be in any function, default value for self args works

This commit is contained in:
leaf corcoran 2011-09-11 23:18:05 -07:00
parent 2c1b3ab00c
commit d0a92fb1db
7 changed files with 99 additions and 96 deletions

View File

@ -434,8 +434,10 @@ line_compile = {
if not constructor then
constructor = {
"fndef",
{
{
"..."
}
},
{ },
"fat",
@ -460,52 +462,7 @@ line_compile = {
}
end
smart_node(constructor)
local self_args = { }
local get_initializers
get_initializers = function(arg)
if ntype(arg) == "self" then
arg = arg[2]
insert(self_args, arg)
end
return arg
end
constructor.args = (function()
local _accum_0 = { }
local _len_0 = 0
do
local _item_0 = constructor.args
for _index_0 = 1, #_item_0 do
local arg = _item_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = get_initializers(arg)
end
end
return _accum_0
end)()
constructor.arrow = "fat"
local dests = (function()
local _accum_0 = { }
local _len_0 = 0
do
local _item_0 = self_args
for _index_0 = 1, #_item_0 do
local name = _item_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = {
"self",
name
}
end
end
return _accum_0
end)()
if #self_args > 0 then
insert(constructor.body, 1, {
"assign",
dests,
self_args
})
end
local def_scope
do
local _with_0 = self:block()
@ -621,8 +578,12 @@ line_compile = {
{
"fndef",
{
"mt",
{
"mt"
},
{
"..."
}
},
{ },
"slim",

View File

@ -223,30 +223,15 @@ line_compile =
-- synthesize constructor if needed
if not constructor
constructor = {"fndef", {"..."}, {}, "fat", {
constructor = {"fndef", {{"..."}}, {}, "fat", {
{"if", parent_loc, {
{"chain", "super", {"call", {"..."}}}
}}
}}
smart_node constructor
-- organize constructor arguments
-- extract self arguments
self_args = {}
get_initializers = (arg) ->
if ntype(arg) == "self"
arg = arg[2]
insert self_args, arg
arg
constructor.args = [get_initializers arg for arg in *constructor.args]
constructor.arrow = "fat"
-- insert self assigning arguments
dests = [{"self", name} for name in *self_args]
insert constructor.body, 1, {"assign", dests, self_args} if #self_args > 0
def_scope = with @block!
parent_val = @value parent_val if parent_val != ""
\put_name parent_loc
@ -281,7 +266,7 @@ line_compile =
-- the class's meta table, gives us call and access to base methods
cls_mt = {"table", {
{"__index", base_name}
{"__call", {"fndef", {"mt", "..."}, {}, "slim", {
{"__call", {"fndef", {{"mt"}, {"..."}}, {}, "slim", {
{"raw", ("local self = setmetatable({}, %s)")\format(base_name)}
{"chain", "mt.__init", {"call", {"self", "..."}}}
"self"

View File

@ -221,38 +221,45 @@ value_compile = {
fndef = function(self, node)
local _, args, whitelist, arrow, block = unpack(node)
local default_args = { }
local format_names
format_names = function(arg)
if type(arg) == "string" then
return arg
else
insert(default_args, arg)
return arg[1]
end
end
args = (function()
local self_args = { }
local arg_names = (function()
local _accum_0 = { }
local _len_0 = 0
do
local _item_0 = args
for _index_0 = 1, #_item_0 do
local arg = _item_0[_index_0]
local name, default_value = unpack(arg)
if type(name) == "string" then
name = name
else
if name[1] == "self" then
insert(self_args, name)
end
name = name[2]
end
if default_value then
insert(default_args, arg)
end
local _value_0 = name
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = format_names(arg)
_accum_0[_len_0] = _value_0
end
end
end
return _accum_0
end)()
if arrow == "fat" then
insert(args, 1, "self")
insert(arg_names, 1, "self")
end
do
local _with_0 = self:block("function(" .. concat(args, ", ") .. ")")
local _with_0 = self:block("function(" .. concat(arg_names, ", ") .. ")")
if #whitelist > 0 then
_with_0:whitelist_names(whitelist)
end
do
local _item_0 = args
local _item_0 = arg_names
for _index_0 = 1, #_item_0 do
local name = _item_0[_index_0]
_with_0:put_name(name)
@ -263,6 +270,9 @@ value_compile = {
for _index_0 = 1, #_item_0 do
local default = _item_0[_index_0]
local name, value = unpack(default)
if type(name) == "table" then
name = name[2]
end
_with_0:stm({
'if',
{
@ -285,6 +295,26 @@ value_compile = {
})
end
end
local self_arg_values = (function()
local _accum_0 = { }
local _len_0 = 0
do
local _item_0 = self_args
for _index_0 = 1, #_item_0 do
local arg = _item_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = arg[2]
end
end
return _accum_0
end)()
if #self_args > 0 then
_with_0:stm({
"assign",
self_args,
self_arg_values
})
end
_with_0:ret_stms(block)
return _with_0
end

View File

@ -132,32 +132,39 @@ value_compile =
_, args, whitelist, arrow, block = unpack node
default_args = {}
format_names = (arg) ->
if type(arg) == "string"
arg
self_args = {}
arg_names = for arg in *args
name, default_value = unpack arg
name = if type(name) == "string"
name
else
insert default_args, arg
arg[1]
args = [format_names arg for arg in *args]
if name[1] == "self"
insert self_args, name
name[2]
insert default_args, arg if default_value
name
if arrow == "fat"
insert args, 1, "self"
insert arg_names, 1, "self"
with @block "function("..concat(args, ", ")..")"
with @block "function("..concat(arg_names, ", ")..")"
if #whitelist > 0
\whitelist_names whitelist
\put_name name for name in *args
\put_name name for name in *arg_names
for default in *default_args
name, value = unpack default
name = name[2] if type(name) == "table"
\stm {
'if', {'exp', name, '==', 'nil'}, {
{'assign', {name}, {value}}
}
}
self_arg_values = [arg[2] for arg in *self_args]
\stm {"assign", self_args, self_arg_values} if #self_args > 0
\ret_stms block
table: (node) =>

View File

@ -242,11 +242,6 @@ local build_grammar = wrap(function()
return stm
end
local function wrap_default_arg(name, default)
if not default then return name end
return {name, default}
end
local function check_lua_string(str, pos, right, left)
return #left == #right
end
@ -416,7 +411,7 @@ local build_grammar = wrap(function()
sym")" + Ct"" * Ct"",
FnArgDefList = FnArgDef * (sym"," * FnArgDef)^0,
FnArgDef = Name * (sym"=" * Exp)^-1 / wrap_default_arg,
FnArgDef = Ct(Name * (sym"=" * Exp)^-1),
FunLit = FnArgsDef *
(sym"->" * Cc"slim" + sym"=>" * Cc"fat") *

View File

@ -45,3 +45,10 @@ what! the! heck!
something = (hello=100, world=(x=[[yeah cool]])-> print "eat rice") ->
print hello
(x, y) =>
(@x, @y) =>
(x=1) =>
(@x=1,y,@z="hello world") =>

View File

@ -76,3 +76,21 @@ something = function(hello, world)
end
return print(hello)
end
_ = function(self, x, y) end
_ = function(self, x, y)
self.x, self.y = x, y
end
_ = function(self, x)
if x == nil then
x = 1
end
end
_ = function(self, x, y, z)
if x == nil then
x = 1
end
if z == nil then
z = "hello world"
end
self.x, self.z = x, z
end