From 180b6c6b5885e6a6e62d39221d5bccc12daaf485 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 10 Apr 2024 23:42:47 -0500 Subject: Add -> functions to core --- core.lua | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 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 @@ local dump = require("dump") local type = require("type") +local utf8 = require("utf8") local null = type.null -local assert_arity = type.assert_arity +local assert_arity, assert_type = type.assert_arity, type.assert_type +local util = require("util") +local error = util.error local function fold (kons, knil, r) if r == null then @@ -53,6 +56,80 @@ for _, t in ipairs { end end +---[[ CONVERTING BETWEEN TYPES ]]--- + +env["symbol->string"] = function (r) + assert_arity(r,1,1) + assert_type(r[1], "symbol") + return type.string(r[1]) +end + +env["string->symbol"] = function (r) + assert_arity(r,1,1) + assert_type(r[1], "string") + return r[1].v +end + +env["number->string"] = function (r) -- (z . radix) + assert_arity(r,1,2) + assert_type(r[1], "number") + -- todo: assert radix type + local z, radix = r[1], r[2][1] or 10 + local n = {} + if radix == 10 then return type.string(z) end + repeat + local mod = z % radix + if mod > 9 then + mod = string.sub("abcdefghijklmnopqrstuvwxyz", mod-9, 1) + end + table.insert(n, 1, z % radix) + z = math.floor(z/radix) + until z == 0 + return type.string(n) +end + +env["string->number"] = function (r) -- (z . radix) + assert_arity(r,1,2) + assert_type(r[1], "string") + -- todo: assert radix type + print(require('dump').dump(r)) + return tonumber(r[1].v, r[2][1]) or false +end + +env["char->integer"] = function (r) + assert_arity(r,1,1) + assert_type(r[1], "char") + return r[1].u +end + +env["integer->char"] = function (r) + assert_arity(r,1,1) + assert_type(r[1], "number") -- todo: check integer + return type.character(utf8.char(r[1])) +end + +env["string->list"] = function (r) + assert_arity(r,1,1) + assert_type(r[1], "string") + local t = {} + for _, c in ipairs(r[1]) do + table.insert(t, type.character(c)) + end + return type.list(t) +end + +env["list->string"] = function (r) + assert_arity(r,1,1) + if not type.listp(r[1]) then + error("not a list", r[1]) + end + local t = {} + for _, c in ipairs(type.totable(r[1])) do + table.insert(t, c.v) + end + return type.string(t) +end + ---[[ NUMBERS ]]--- 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 -- WHERE is where in the process; DESC is a description of the error; the rest -- are "irritants" function m.error (desc, ...) - m.luaerror(string.format("%s: %s", desc, table.concat({...}, " ") + local irritants = {} + for _, i in ipairs({...}) do + table.insert(irritants, tostring(i)) + end + m.luaerror(string.format("%s: %s", desc, table.concat(irritants, ", ") )) end -- cgit 1.4.1-21-gabe81