about summary refs log tree commit diff stats
path: root/core.lua
diff options
context:
space:
mode:
Diffstat (limited to 'core.lua')
-rw-r--r--core.lua79
1 files changed, 78 insertions, 1 deletions
diff --git a/core.lua b/core.lua index 3b0eab0..556b5d1 100644 --- a/core.lua +++ b/core.lua
@@ -2,8 +2,11 @@
2 2
3local dump = require("dump") 3local dump = require("dump")
4local type = require("type") 4local type = require("type")
5local utf8 = require("utf8")
5local null = type.null 6local null = type.null
6local assert_arity = type.assert_arity 7local assert_arity, assert_type = type.assert_arity, type.assert_type
8local util = require("util")
9local error = util.error
7 10
8local function fold (kons, knil, r) 11local function fold (kons, knil, r)
9 if r == null then 12 if r == null then
@@ -53,6 +56,80 @@ for _, t in ipairs {
53 end 56 end
54end 57end
55 58
59---[[ CONVERTING BETWEEN TYPES ]]---
60
61env["symbol->string"] = function (r)
62 assert_arity(r,1,1)
63 assert_type(r[1], "symbol")
64 return type.string(r[1])
65end
66
67env["string->symbol"] = function (r)
68 assert_arity(r,1,1)
69 assert_type(r[1], "string")
70 return r[1].v
71end
72
73env["number->string"] = function (r) -- (z . radix)
74 assert_arity(r,1,2)
75 assert_type(r[1], "number")
76 -- todo: assert radix type
77 local z, radix = r[1], r[2][1] or 10
78 local n = {}
79 if radix == 10 then return type.string(z) end
80 repeat
81 local mod = z % radix
82 if mod > 9 then
83 mod = string.sub("abcdefghijklmnopqrstuvwxyz", mod-9, 1)
84 end
85 table.insert(n, 1, z % radix)
86 z = math.floor(z/radix)
87 until z == 0
88 return type.string(n)
89end
90
91env["string->number"] = function (r) -- (z . radix)
92 assert_arity(r,1,2)
93 assert_type(r[1], "string")
94 -- todo: assert radix type
95 print(require('dump').dump(r))
96 return tonumber(r[1].v, r[2][1]) or false
97end
98
99env["char->integer"] = function (r)
100 assert_arity(r,1,1)
101 assert_type(r[1], "char")
102 return r[1].u
103end
104
105env["integer->char"] = function (r)
106 assert_arity(r,1,1)
107 assert_type(r[1], "number") -- todo: check integer
108 return type.character(utf8.char(r[1]))
109end
110
111env["string->list"] = function (r)
112 assert_arity(r,1,1)
113 assert_type(r[1], "string")
114 local t = {}
115 for _, c in ipairs(r[1]) do
116 table.insert(t, type.character(c))
117 end
118 return type.list(t)
119end
120
121env["list->string"] = function (r)
122 assert_arity(r,1,1)
123 if not type.listp(r[1]) then
124 error("not a list", r[1])
125 end
126 local t = {}
127 for _, c in ipairs(type.totable(r[1])) do
128 table.insert(t, c.v)
129 end
130 return type.string(t)
131end
132
56---[[ NUMBERS ]]--- 133---[[ NUMBERS ]]---
57 134
58env["="] = function (r) 135env["="] = function (r)