diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2024-06-09 13:02:25 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2024-06-09 13:02:25 -0300 |
commit | ca6e4d141484331a25cfa67cf370000b74d7725f (patch) | |
tree | c157a393a202044f08b02a35c5e055f3f1ba6243 /src/cratera.cratera.d/lib.cratera | |
parent | 2751039739e5357beb8e858a25e21e2bc0344fe8 (diff) |
Add hashmap implementation
Diffstat (limited to 'src/cratera.cratera.d/lib.cratera')
-rw-r--r-- | src/cratera.cratera.d/lib.cratera | 53 |
1 files changed, 53 insertions, 0 deletions
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 <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 @@ -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 |