From ca6e4d141484331a25cfa67cf370000b74d7725f Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sun, 9 Jun 2024 13:02:25 -0300 Subject: Add hashmap implementation --- src/cratera.cratera.d/lib.cratera | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'src/cratera.cratera.d/lib.cratera') diff --git a/src/cratera.cratera.d/lib.cratera b/src/cratera.cratera.d/lib.cratera index 69ca476..7e04c83 100644 --- a/src/cratera.cratera.d/lib.cratera +++ b/src/cratera.cratera.d/lib.cratera @@ -16,12 +16,16 @@ along with this program. If not, see . --]] +-- (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 @@ -41,3 +45,52 @@ 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 -- cgit 1.4.1