Initial commit

This commit is contained in:
Tony Bark 2025-02-07 14:39:37 -05:00
commit df8714aa1d
171 changed files with 1686 additions and 0 deletions

View file

@ -0,0 +1,126 @@
--[[
AND (&): Performs a bitwise AND operation.
OR (|): Performs a bitwise OR operation.
XOR (~): Performs a bitwise exclusive OR operation.
NOT (~): Performs a unary bitwise NOT operation.
Left Shift (<<): Shifts bits to the left.
Right Shift (>>): Shifts bits to the right12.
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101,
103, 107, 109, 113, 127, 131,
137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191,
193, 197, 199, 211, 223, 227, 229, 233,
239, 241, 251, 257, 263, 269, 271, 277,
281, 283, 293, 307, 311, 313,
317, 331, 337, 347, 349, 353,
359, 367, 373, 379, 383,
389, 397, 401, 409, 419, 421, 431, 433, 439,
443, 449, 457, 461, 463, 467, 479, 487, 491, 499,
503, 509, 521, 523, 541, 547, 557, 563, 569, 571,
577, 587, 593, 599, 601, 607, 613, 617, 619, 631,
641, 643, 647, 653, 659, 661, 673, 677, 683, 691,
701, 709, 719, 727, 733, 739, 743, 751, 757, 761,
769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
839, 853, 857, 859, 863, 877, 881, 883, 887, 907,
911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
983, 991, 997
--]]
-- Bitwise AND
function band(a, b)
local result = 0
local bitval = 1
while a > 0 and b > 0 do
local abit = a % 2
local bbit = b % 2
if abit == 1 and bbit == 1 then
result = result + bitval
end
a = math.floor(a / 2)
b = math.floor(b / 2)
bitval = bitval * 2
end
return result
end
-- Bitwise OR
function bor(a, b)
local result = 0
local bitval = 1
while a > 0 or b > 0 do
local abit = a % 2
local bbit = b % 2
if abit == 1 or bbit == 1 then
result = result + bitval
end
a = math.floor(a / 2)
b = math.floor(b / 2)
bitval = bitval * 2
end
return result
end
-- Bitwise XOR
function bxor(a, b)
local result = 0
local bitval = 1
while a > 0 or b > 0 do
local abit = a % 2
local bbit = b % 2
if abit ~= bbit then
result = result + bitval
end
a = math.floor(a / 2)
b = math.floor(b / 2)
bitval = bitval * 2
end
return result
end
-- Bitwise NOT
function bnot(a)
local result = 0
local bitval = 1
while a > 0 do
local abit = a % 2
if abit == 0 then
result = result + bitval
end
a = math.floor(a / 2)
bitval = bitval * 2
end
return result
end
-- Left shift
function lshift(x, by)
return x * 2 ^ by
end
-- Right shift
function rshift(x, by)
return math.floor(x / 2 ^ by)
end
local gen_bitwise_assert = function(a,b)
print("AND: ", band(a, b))
print("OR: ", bor(a, b))
print("XOR: ", bxor(a, b))
print("NOT: ", bnot(271))
print("Left Shift: ", lshift(a, 1))
print("Right Shift: ", rshift(a, 1))
end
local a, b = 127, 271
--gen_bitwise_assert(a,b)
assert(band(a, b) == 15)
assert(bor(a, b) == 383)
assert(bxor(a, b) == 368)
assert(bnot(b) == 240)
assert(lshift(a, 1) == 254)
assert(rshift(a, 1) == 63)

View file

125
mods/wislib/lua5_1/io.lua Normal file
View file

@ -0,0 +1,125 @@
--context is always -eq to the highest possible subfolder in the mods dir, this can be ether the mod or modpack root folder.
local DIR_DELIM = debug.getinfo(1, "S").source:match("[\\/]")
local context = debug and debug.getinfo(1, "S").source:match("mods[/\\][/\\]?([^/]*)") or "context"
local cwdpath = minetest and (minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELIM .. "lua5_1") or debug.getinfo(1, "S").source:match("^(.+)[/\\]"):sub(2)
--setup lib env
local print = _G[context]["log"]
--some string function moved here, for more info see: string.lua
function string:trim()
return self:match("^%s*(.-)%s*$")
end
--[[
function string:split (p2Sep, p3Patern)
if p2Sep == nil then
p2Sep = "%s"
end
if p3Patern == nil then
p3Patern = table.concat({"([^", p2Sep, "]+)"}, "")
end
local returns = {}
for _ in string.gmatch(self, p3Patern) do
table.insert(returns, _)
end
return returns
end
--]]
function string:startsWith(p2Needle) -- self == haystack
return self:sub(1, #p2Needle) == p2Needle
end
function string:endsWith(p2Needle) -- self == haystack
return p2Needle == "" or self:sub(-#p2Needle) == p2Needle
end
function string:charCount(p2Char)
local count = 0
for _ in string.gmatch(self, p2Char) do
count = count + 1
end
return count
end
--end string.lua snipet
_G[context].colors ={
reset = "\27[0m",
red = "\27[31m",
green = "\27[32m",
yellow = "\27[33m",
blue = "\27[34m",
magenta = "\27[35m",
cyan = "\27[36m",
white= "\27[37m"
}
_G[context].forFileAsLines=function(filePath, funcHandle, ignoreReturn)
local file = io.open(filePath, "r")
--if file is context:open then
local returns = {}
if not file then error(_G[context].colors.red.."access error: reading non-existant file, @" .._G[context].colors.cyan ..filePath.._G[context].colors.reset) end
if file then
--itarate over each line and pass to funkyFunc
for line in file:lines() do
local linedata=line:trim()--:sub(1,-2) --remove spooky char at the end of a line. If file uses \r\n, it may require (1,-3)
linedata=funcHandle and (funcHandle(linedata)) or linedata --buggy:TODO
--this data subject to brakeages, test for errors here:
--print("<"..linedata..">")
if not ignoreReturn then --used for large files, to avoid memory bloat.
table.insert(returns, linedata)
end
end
file:close()
return returns
else
return {}
end
end
--
_G[context].forFileAsIniObj = function(filePath, ignoreHeaders)
local debug_print=false
local returns = {} --object to store data about ini file as a hashmap table
returns[""]={} --default object to store global values before a header tag('[' or ']') is found in the file stream
local rpointer = "" -- by default pointer set to "" by default
local _ = debug_print and print("<"..filePath..">")
local _ = _G[context].forFileAsLines(filePath, function (line)
--in ini, ';' may be used as a comment by default.
--in the future this may be abstracted into a larger function with more customization
if not line:startsWith(";") and string.charCount(line, " ") ~= #line and #line ~= 0 then --discard comments or empty lines
--if is header "[*]"
local _ = debug_print and print(line)
if line:match("^%[.*%]$") then
if not ignoreHeaders then
rpointer = line:sub(2,-2):trim() --remove [ and ] from the header and use as str:returnPointer
returns[rpointer]= returns[rpointer] or {}
end
--else if is key value pair "*=*"
elseif line:match("^.+=.+$") and line:charCount("=") == 1 then
local spointerarray = line:split("=")
spointerarray[1]=spointerarray[1]:trim()
spointerarray[2]=spointerarray[2]:trim()
--If parser fails due to missing '"', use raw string as a fall back.
--minetest.parse_json create an unmutable error with no way to prevent it,
--just return as string ill deal with this later. :(
--Upon reflection this function should always return a string,
--since it may be used in multiple use case and so should leave parsing decisions to the caller
--_G[context].parse(spointerarray[2]) or
returns[rpointer][spointerarray[1]] = spointerarray[2]
else --else is error
error(table.concat({"error reading: ", filePath, "@^", line, "$"},""))
end
end
end, true)
--if the global array table was never used, delete it.
if #returns[""] == 0 and true then
returns[""]=nil
end
return returns
end
--Why the fuck is "true" true in the context of a conditional, is lua just as bad as javascript?
assert(true == _G[context].parse(_G[context].forFileAsIniObj(cwdpath..DIR_DELIM.."test.ini", false)["testdata"]["success"]))

View file

View file

View file

@ -0,0 +1,30 @@
--[[
-- Original table
original_table = { 1, "hello", true }
-- Metatable to track reads
metatable = {
__index = function(t, key)
local value = rawget(t, key)
print("Key:", key, "Type:", type(value))
return value
end
}
-- Set metatable
setmetatable(original_table, metatable)
-- Access elements
print(original_table[1])
print(original_table[2])
print(original_table[3])
--]]
-- Function to check if a string is in the table
function table.contains(table, element)
for _, value in ipairs(table) do
if value == element then
return true
end
end
return false
end

View file

@ -0,0 +1,9 @@
; This file created to test io.lua's ability to read files
; If you modify this file, please restart your server to see changes take effect.
; Please note: built-in reader reads all data as type:string, then converts them as a json object with a string fallback
; Comments are denoted by a prefacing ; character,
; [headers] are denoted by enclosing a [a-zA-Z0-9%-_] string, with braces
; key value pairs are denoted by placing a key infront of = symbole folowed by a value, Note: spaces are not trimed
[testdata]
success = true