diff options
author | Case Duckworth | 2024-03-10 21:39:53 -0500 |
---|---|---|
committer | Case Duckworth | 2024-03-10 21:39:53 -0500 |
commit | a72ff678da253fce46e8e4648f6e4cf5ce1ea9b4 (patch) | |
tree | a82501fca97f4adc272d05145548d10cabe3ea2a /global.lua | |
parent | Ugghhhh totally not working (diff) | |
download | lam-a72ff678da253fce46e8e4648f6e4cf5ce1ea9b4.tar.gz lam-a72ff678da253fce46e8e4648f6e4cf5ce1ea9b4.zip |
uh new start
Diffstat (limited to 'global.lua')
-rw-r--r-- | global.lua | 162 |
1 files changed, 0 insertions, 162 deletions
diff --git a/global.lua b/global.lua deleted file mode 100644 index 1dea773..0000000 --- a/global.lua +++ /dev/null | |||
@@ -1,162 +0,0 @@ | |||
1 | --- lam.environment | ||
2 | |||
3 | local util = require "util" | ||
4 | local types = require "types" | ||
5 | table.unpack = table.unpack or unpack | ||
6 | |||
7 | local global = { | ||
8 | -- constants ---- TODO this should be at the reader level | ||
9 | ["#t"] = true, | ||
10 | ["#f"] = false, | ||
11 | } | ||
12 | |||
13 | --- Types --- | ||
14 | |||
15 | global.luatype = type | ||
16 | global.type = types.lamtype | ||
17 | |||
18 | global["number?"] = function (x) types.isa(x, "Number") end | ||
19 | global["string?"] = function (x) types.isa(x, "String") end | ||
20 | global["symbol?"] = function (x) types.isa(x, "Symbol") end | ||
21 | global["pair?"] = function (x) types.isa(x, "Pair") end | ||
22 | global["is-a?"] = function (x, t) types.isa(x, t) end | ||
23 | |||
24 | --- Basic functions --- | ||
25 | |||
26 | global.car = function (pair) return pair[1] end | ||
27 | global.cdr = function (pair) return pair[2] end | ||
28 | |||
29 | -- global.list = types.List | ||
30 | |||
31 | global["list?"] = | ||
32 | function (x) | ||
33 | -- TODO : detect circular lists | ||
34 | if type(x) == "table" then | ||
35 | if #x == 0 then return true end | ||
36 | if type(x[2]) ~= "table" then return false end | ||
37 | end | ||
38 | return global["list?"](x[2]) | ||
39 | end | ||
40 | |||
41 | global["null?"] = function (x) return type(x) == "table" and #x == 0 end | ||
42 | |||
43 | --- Higher-order functions --- | ||
44 | --[[ | ||
45 | global.apply = function(fn, ...) | ||
46 | local args = {...} | ||
47 | local last = args[#args] | ||
48 | assert(types.luatype(last) == "table", "Bad apply") | ||
49 | table.remove(args) | ||
50 | for _, v in ipairs(last) do | ||
51 | table.insert(args, v) | ||
52 | end | ||
53 | return fn(table.unpack(args)) | ||
54 | end | ||
55 | |||
56 | global.map = function(fn, list) | ||
57 | return util.map(fn, list) | ||
58 | end | ||
59 | --]] | ||
60 | --- Math --- | ||
61 | -- NOTE: we do not have the full numeric tower yet! | ||
62 | |||
63 | for name, func in pairs(math) do | ||
64 | global[name] = func | ||
65 | end | ||
66 | |||
67 | global.fold = | ||
68 | function (fn, lis) | ||
69 | local out = {} | ||
70 | |||
71 | return types.List(out) | ||
72 | end | ||
73 | |||
74 | global["+"] = function (lis) | ||
75 | return | ||
76 | return util.reduce({...}, 0, function (a, b) return a + b end) | ||
77 | end | ||
78 | |||
79 | global["-"] = function (...) | ||
80 | local args = {...} | ||
81 | if #args == 0 then | ||
82 | error("Too few arguments: need at least 1") | ||
83 | elseif #args == 1 then | ||
84 | return (-args[1]) | ||
85 | else | ||
86 | local result = args[1] | ||
87 | for v = 2, #args do | ||
88 | result = result - args[v] | ||
89 | end | ||
90 | return result | ||
91 | end | ||
92 | end | ||
93 | |||
94 | global["*"] = function (...) | ||
95 | local result = 1 | ||
96 | for _, v in ipairs({...}) do | ||
97 | if v == 0 then return 0 end | ||
98 | result = result * v | ||
99 | end | ||
100 | return result | ||
101 | end | ||
102 | |||
103 | global["/"] = function (...) | ||
104 | local args = {...} | ||
105 | if #args == 0 then | ||
106 | error("Too few arguments: need at least 1") | ||
107 | elseif #args == 1 then | ||
108 | if args[1] == 0 then error("Division by zero") end | ||
109 | return (1/args[1]) | ||
110 | else | ||
111 | local result = args[1] | ||
112 | for v = 2, #args do | ||
113 | if args[v] == 0 then error("Division by zero") end | ||
114 | result = result / args[v] | ||
115 | end | ||
116 | return result | ||
117 | end | ||
118 | end | ||
119 | |||
120 | --[[ | ||
121 | global["="] = | ||
122 | function (...) | ||
123 | for _, v in ipairs({...}) do | ||
124 | if not a == b then return false end | ||
125 | end | ||
126 | return true | ||
127 | end | ||
128 | |||
129 | global["<"] = | ||
130 | function (...) | ||
131 | for _, v in ipairs({...}) do | ||
132 | if not a < b then return false end | ||
133 | end | ||
134 | return true | ||
135 | end | ||
136 | |||
137 | global["<="] = | ||
138 | function (...) | ||
139 | for _, v in ipairs({...}) do | ||
140 | if not a <= b then return false end | ||
141 | end | ||
142 | return true | ||
143 | end | ||
144 | |||
145 | global[">"] = | ||
146 | function (...) | ||
147 | for _, v in ipairs({...}) do | ||
148 | if not a > b then return false end | ||
149 | end | ||
150 | return true | ||
151 | end | ||
152 | |||
153 | global[">="] = | ||
154 | function (...) | ||
155 | for _, v in ipairs({...}) do | ||
156 | if not a >= b then return false end | ||
157 | end | ||
158 | return true | ||
159 | end | ||
160 | --]] | ||
161 | --- | ||
162 | return global | ||