lua: add meta support for builtin function
authorMitsuru Kariya <kariya_mitsuru@hotmail.com>
Wed, 22 Jul 2015 08:59:11 +0000 (17:59 +0900)
committerMitsuru Kariya <kariya_mitsuru@hotmail.com>
Wed, 22 Jul 2015 08:59:11 +0000 (17:59 +0900)
lua/printer.lua
lua/types.lua

index 8c1cdad..f64559c 100644 (file)
@@ -45,7 +45,7 @@ function M._pr_str(obj, print_readably)
         return "(fn* "..M._pr_str(obj.params).." "..M._pr_str(obj.ast)..")"
     elseif types._atom_Q(obj) then
         return "(atom "..M._pr_str(obj.val)..")"
-    elseif type(obj) == 'function' then
+    elseif type(obj) == 'function' or types._functionref_Q(obj) then
         return "#<function>"
     else
         return string.format("%s", obj)
index 23a003b..dfe10d9 100644 (file)
@@ -23,6 +23,9 @@ function M._equal_Q(a,b)
 end
 
 function M.copy(obj)
+    if type(obj) == "function" then
+        return M.FunctionRef:new(obj)
+    end
     if type(obj) ~= "table" then return obj end
 
     -- copy object data
@@ -190,4 +193,18 @@ function M._atom_Q(obj)
     return utils.instanceOf(obj, M.Atom)
 end
 
+-- FunctionRefs
+
+M.FunctionRef = {}
+function M.FunctionRef:new(fn)
+    local newObj = {fn = fn}
+    return setmetatable(newObj, self)
+end
+function M._functionref_Q(obj)
+    return utils.instanceOf(obj, M.FunctionRef)
+end
+function M.FunctionRef:__call(...)
+    return self.fn(...)
+end
+
 return M