aboutsummaryrefslogtreecommitdiff
path: root/lua/types.lua
blob: 23a003bec65985e4dd2612743942ce3fe9b82459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
local utils = require('utils')

local M = {}

-- type functions

function M._sequential_Q(obj)
    return M._list_Q(obj) or M._vector_Q(obj)
end

function M._equal_Q(a,b)
    if M._symbol_Q(a) and M._symbol_Q(b) then
        return a.val == b.val
    elseif M._sequential_Q(a) and M._sequential_Q(b) then
        if #a ~= #b then return false end
        for i, v in ipairs(a) do
            if not M._equal_Q(v,b[i]) then return false end
        end
        return true
    else
        return a == b
    end
end

function M.copy(obj)
    if type(obj) ~= "table" then return obj end

    -- copy object data
    local new_obj = {}
    for k,v in pairs(obj) do
        new_obj[k] = v
    end

    -- copy metatable and link to original
    local old_mt = getmetatable(obj)
    if old_mt ~= nil then
        local new_mt = {}
        for k,v in pairs(old_mt) do
            new_mt[k] = v
        end
        setmetatable(new_mt, old_mt)
        setmetatable(new_obj, new_mt)
    end

    return new_obj
end

function M.slice(lst, start, last)
    if last == nil then last = #lst end
    local new_lst = {}
    if start <= last then
        for i = start, last do
            new_lst[#new_lst+1] = lst[i]
        end
    end
    return new_lst
end

-- Error/exceptions

M.MalException = {}
function M.MalException:new(val)
    local newObj = {val = val}
    self.__index = self
    return setmetatable(newObj, self)
end
function M._malexception_Q(obj)
    return utils.instanceOf(obj, M.MalException)
end

function M.throw(val)
    error(M.MalException:new(val))
end

-- Nil

local NilType = {}
function NilType:new(val)
    local newObj = {}
    self.__index = self
    return setmetatable(newObj, self)
end
M.Nil = NilType:new()
function M._nil_Q(obj)
    return obj == Nil
end

-- Strings
function M._string_Q(obj)
    return type(obj) == "string"
end

-- Symbols

M.Symbol = {}
function M.Symbol:new(val)
    local newObj = {val = val}
    self.__index = self
    return setmetatable(newObj, self)
end
function M._symbol_Q(obj)
    return utils.instanceOf(obj, M.Symbol)
end

-- Keywords
function M._keyword_Q(obj)
    return M._string_Q(obj) and "\177" == string.sub(obj,1,1)
end


-- Lists

M.List = {}
function M.List:new(lst)
    local newObj = lst and lst or {}
    self.__index = self
    return setmetatable(newObj, self)
end
function M._list_Q(obj)
    return utils.instanceOf(obj, M.List)
end
function M.List:slice(start,last)
    return M.List:new(M.slice(self,start,last))
end

-- Vectors

M.Vector = {}
function M.Vector:new(lst)
    local newObj = lst and lst or {}
    self.__index = self
    return setmetatable(newObj, self)
end
function M._vector_Q(obj)
    return utils.instanceOf(obj, M.Vector)
end
function M.Vector:slice(start,last)
    return M.Vector:new(M.slice(self,start,last))
end

-- Hash Maps
--
M.HashMap = {}
function M.HashMap:new(val)
    local newObj = val and val or {}
    self.__index = self
    return setmetatable(newObj, self)
end
function M.hash_map(...)
    return M._assoc_BANG(M.HashMap:new(), unpack(arg))
end
function M._hash_map_Q(obj)
    return utils.instanceOf(obj, M.HashMap)
end
function M._assoc_BANG(hm, ...)
    for i = 1, #arg, 2 do
        hm[arg[i]] = arg[i+1]
    end
    return hm
end
function M._dissoc_BANG(hm, ...)
    for i = 1, #arg do
        hm[arg[i]] = nil
    end
    return hm
end

-- Functions

M.MalFunc = {}
function M.MalFunc:new(fn, ast, env, params)
    local newObj = {fn = fn, ast = ast, env = env,
                    params = params, ismacro = false}
    self.__index = self
    return setmetatable(newObj, self)
end
function M._malfunc_Q(obj)
    return utils.instanceOf(obj, M.MalFunc)
end

-- Atoms

M.Atom = {}
function M.Atom:new(val)
    local newObj = {val = val}
    self.__index = self
    return setmetatable(newObj, self)
end
function M._atom_Q(obj)
    return utils.instanceOf(obj, M.Atom)
end

return M