From 2a5c0883ca907e97110ea0050080f74ccbb143e2 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Tue, 2 Apr 2024 21:04:32 -0500 Subject: Change arity assertion code --- core.lua | 117 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 60 insertions(+), 57 deletions(-) (limited to 'core.lua') diff --git a/core.lua b/core.lua index dec4a39..7891f3a 100644 --- a/core.lua +++ b/core.lua @@ -4,7 +4,8 @@ local m = {} local type = require "type" local isa, null = type.isa, type.null local math = math -local proc = require("util").proc +local util = require "util" +local assert_arity = util.assert_arity local function fold (kons, knil, r) if r == null then @@ -18,71 +19,73 @@ end m.env = { -- all functions here take R, which is the list of arguments ------- numbers - ["number?"] = proc(1, function (r) return isa(r[1], "number") end), + ["number?"] = + function (r) + assert_arity(r, 1, 1) + return isa(r[1], "number") + end, ["="] = - proc({0}, function (r) - if r[1] == nil then return true end - if r[2] == nil then return true end - while r[2] ~= null do - if r[1] ~= r[2][1] then return false end - r = r[2] - end - return true - end), + function (r) + if r[1] == nil then return true end + if r[2] == nil then return true end + while r[2] ~= null do + if r[1] ~= r[2][1] then return false end + r = r[2] + end + return true + end, ["<"] = - proc({0}, function (r) - if r[1] == nil then return true end - if r[2] == nil then return true end - while r[2] ~= null do - if r[1] >= r[2][1] then return false end - r = r[2] - end - return true - end), + function (r) + if r[1] == nil then return true end + if r[2] == nil then return true end + while r[2] ~= null do + if r[1] >= r[2][1] then return false end + r = r[2] + end + return true + end, [">"] = - proc({0}, function (r) - if r[1] == nil then return true end - if r[2] == nil then return true end - while r[2] ~= null do - if r[1] <= r[2][1] then return false end - r = r[2] - end - return true - end), - ["<="] = proc({0}, function (r) return not m.env[">"](r) end), - [">="] = proc({0}, function (r) return not m.env["<"](r) end), + function (r) + if r[1] == nil then return true end + if r[2] == nil then return true end + while r[2] ~= null do + if r[1] <= r[2][1] then return false end + r = r[2] + end + return true + end, + ["<="] = function (r) return not m.env[">"](r) end, + [">="] = function (r) return not m.env["<"](r) end, ------- math ["+"] = - proc({0}, function (r) - return fold(function (a, b) - return a + b - end, 0, r) - end), + function (r) + return fold(function (a, b) return a + b end, 0, r) + end, ["-"] = - proc({0}, function (r) - if r == null then return -1 end - if r[2] == null then return (- r[1]) end - return fold(function (a, b) - return a - b - end, r[1], r[2]) - end), + function (r) + if r == null then return -1 end + if r[2] == null then return (- r[1]) end + return fold(function (a, b) + return a - b + end, r[1], r[2]) + end, ["*"] = - proc({0}, function (r) - local function go (a, b) - if a == 0 or b == 0 then - return 0, 1 - end - return a * b + function (r) + local function go (a, b) + if a == 0 or b == 0 then + return 0, 1 end - return fold(go, 1, r) - end), + return a * b + end + return fold(go, 1, r) + end, ["/"] = - proc({1}, function (r) - if r[2] == null then return (1 / r[1]) end - return fold(function (a, b) - return a / b - end, r[1], r[2]) - end), + function (r) + assert_arity(r, 1) + if r[2] == null then return (1 / r[1]) end + return fold(function (a, b) return a / b end, + r[1], r[2]) + end, } -------- -- cgit 1.4.1-21-gabe81