aboutsummaryrefslogtreecommitdiff
path: root/nerv.lua
diff options
context:
space:
mode:
Diffstat (limited to 'nerv.lua')
-rw-r--r--nerv.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/nerv.lua b/nerv.lua
index 33b1aff..1c4ba39 100644
--- a/nerv.lua
+++ b/nerv.lua
@@ -1,5 +1,6 @@
require 'libnerv'
require 'matrix.init'
+require 'io.init'
-- nerv.class = require 'pl.class'
nerv.utils = require 'pl.utils'
@@ -35,3 +36,39 @@ function nerv.class(tname, parenttname)
end
return mt, mpt
end
+
+function table.val_to_str(v)
+ if "string" == type(v) then
+ v = string.gsub(v, "\n", "\\n")
+ if string.match(string.gsub(v,"[^'\"]",""), '^"+$') then
+ return "'" .. v .. "'"
+ end
+ return '"' .. string.gsub(v,'"', '\\"') .. '"'
+ else
+ return "table" == type(v) and table.tostring(v) or
+ tostring(v)
+ end
+end
+
+function table.key_to_str (k)
+ if "string" == type(k) and string.match(k, "^[_%a][_%a%d]*$") then
+ return k
+ else
+ return "[" .. table.val_to_str(k) .. "]"
+ end
+end
+
+function table.tostring(tbl)
+ local result, done = {}, {}
+ for k, v in ipairs(tbl) do
+ table.insert(result, table.val_to_str(v))
+ done[k] = true
+ end
+ for k, v in pairs(tbl) do
+ if not done[k] then
+ table.insert(result,
+ table.key_to_str(k) .. "=" .. table.val_to_str(v))
+ end
+ end
+ return "{" .. table.concat(result, ",") .. "}"
+end