summary refs log blame commit diff stats
path: root/src/cratera.cratera.d/lib.cratera
blob: 7e04c836dccbf622ac3bb30a46f5280a5ad2f0fc (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

















                                                                               

                        
                      
                   

                                            
                                                            

                                                                              
                     















                                                                      
                                               

                            
















































                                                                            
--[[
    Cratera Library
    Copyright (C) 2024  Soni L.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
--]]

-- (post-)bootstrap bits

local cratera_env = _G
-- the "old" loader
local cratera_loader_base = cratera_env.load

-- only use setfenv if lua load doesn't support env argument
local setfenv = (lua.loadstring or lua.load)("return setfenv", nil, nil, {})()

-- replace the loader
function cratera_env.load(chunk, chunkname, mode, env)
    if env == nil then
        env = cratera_env
    end
    local func, err = cratera_loader_base(chunk, chunkname, mode, env)
    if setfenv and func and env then
        setfenv(func, env)
    end
    -- arity adjustments
    if func then
        return func
    else
        return func, err
    end
end

-- cratera opts not to support dostring/dofile.
cratera_env.dostring = false
cratera_env.dofile = false

-- "proper" stdlib from now on

local setmetatable = setmetatable

-- traits aren't (currently) special, they're just unique tables that can be
-- used as keys
function cratera_env.mktrait()
    return {}
end

-- all structs implement the Struct trait
cratera_env.Struct = mktrait()
-- the Struct trait has no members of its own but just points at the struct
-- type itself.

-- (should be upvalue, not global)
local Struct = cratera_env.Struct
-- structs are a little special
function cratera_env.mkstruct(constructor, meta)
    local struct = {}
    struct[Struct] = struct
    meta = meta or {}
    meta.__index = struct
    setmetatable(struct, {__call=function(...)
        return setmetatable(constructor(...), meta)
    end})
    return struct
end

cratera_env.Iter = mktrait()

function Iter:iter()
    return [[
Returns an iterator function, a state, and an initial value, for use in a
regular generic `for` loop.

# Examples

```cratera
local bucketlib = require "cratera.lib.bucket"

local map = bucketlib.BucketMap()

for k,v in map:[Iter].iter() do
end
```
]]
end