diff options
author | Case Duckworth | 2024-04-10 23:42:47 -0500 |
---|---|---|
committer | Case Duckworth | 2024-04-10 23:42:47 -0500 |
commit | 180b6c6b5885e6a6e62d39221d5bccc12daaf485 (patch) | |
tree | dbfee8455a9875f46d60339438da99be8cc09930 | |
parent | Change tostring to type.string (diff) | |
download | lam-180b6c6b5885e6a6e62d39221d5bccc12daaf485.tar.gz lam-180b6c6b5885e6a6e62d39221d5bccc12daaf485.zip |
Add -> functions to core
-rw-r--r-- | core.lua | 79 | ||||
-rw-r--r-- | util.lua | 6 |
2 files changed, 83 insertions, 2 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 | ||
3 | local dump = require("dump") | 3 | local dump = require("dump") |
4 | local type = require("type") | 4 | local type = require("type") |
5 | local utf8 = require("utf8") | ||
5 | local null = type.null | 6 | local null = type.null |
6 | local assert_arity = type.assert_arity | 7 | local assert_arity, assert_type = type.assert_arity, type.assert_type |
8 | local util = require("util") | ||
9 | local error = util.error | ||
7 | 10 | ||
8 | local function fold (kons, knil, r) | 11 | local 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 |
54 | end | 57 | end |
55 | 58 | ||
59 | ---[[ CONVERTING BETWEEN TYPES ]]--- | ||
60 | |||
61 | env["symbol->string"] = function (r) | ||
62 | assert_arity(r,1,1) | ||
63 | assert_type(r[1], "symbol") | ||
64 | return type.string(r[1]) | ||
65 | end | ||
66 | |||
67 | env["string->symbol"] = function (r) | ||
68 | assert_arity(r,1,1) | ||
69 | assert_type(r[1], "string") | ||
70 | return r[1].v | ||
71 | end | ||
72 | |||
73 | env["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) | ||
89 | end | ||
90 | |||
91 | env["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 | ||
97 | end | ||
98 | |||
99 | env["char->integer"] = function (r) | ||
100 | assert_arity(r,1,1) | ||
101 | assert_type(r[1], "char") | ||
102 | return r[1].u | ||
103 | end | ||
104 | |||
105 | env["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])) | ||
109 | end | ||
110 | |||
111 | env["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) | ||
119 | end | ||
120 | |||
121 | env["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) | ||
131 | end | ||
132 | |||
56 | ---[[ NUMBERS ]]--- | 133 | ---[[ NUMBERS ]]--- |
57 | 134 | ||
58 | env["="] = function (r) | 135 | env["="] = function (r) |
diff --git a/util.lua b/util.lua index 10460a2..a13912c 100644 --- a/util.lua +++ b/util.lua | |||
@@ -10,7 +10,11 @@ m.luaerror = error | |||
10 | -- WHERE is where in the process; DESC is a description of the error; the rest | 10 | -- WHERE is where in the process; DESC is a description of the error; the rest |
11 | -- are "irritants" | 11 | -- are "irritants" |
12 | function m.error (desc, ...) | 12 | function m.error (desc, ...) |
13 | m.luaerror(string.format("%s: %s", desc, table.concat({...}, " ") | 13 | local irritants = {} |
14 | for _, i in ipairs({...}) do | ||
15 | table.insert(irritants, tostring(i)) | ||
16 | end | ||
17 | m.luaerror(string.format("%s: %s", desc, table.concat(irritants, ", ") | ||
14 | )) | 18 | )) |
15 | end | 19 | end |
16 | 20 | ||