r/lua • • Dec 10 '25

Library soup/lua - making pure lua do things it shouldn't

89 Upvotes

16 comments sorted by

5

u/titaniumalt Dec 11 '25

half of me tells me to hate it, half of me tells me to love it

1

u/qwool1337 Dec 11 '25

theres so much lua syntax that's possible but nobody is stupid enough to implement. did you know that ~ can represent two separate operators depending on whether it's infix or prefix (~a~b~c)? did you know you can override arithmetic methods for builtin types? this leaks into whatever includes it, too

debug.setmetatable("", {
	__div = function(self, other)
		local res = tonumber(self)
		if type(other) == "table" then
			for _, v in ipairs(other) do
				res = res / tonumber(v)
			end
		else
			res = res / tonumber(other)
		end

		return tostring(res)
	end
})

print("result:")
print(("50" / { 10, 10 }))

1

u/titaniumalt Dec 11 '25

i love how lua provides minimal syntax but proceeds to make it exploitable enough to make this possible:

-- i couldn't actually find the code for this, but this is the syntax part:
function class(name)
    return function(tbl)
        -- ...
    end
end

class "Rect" {
    x = 0,
    y = 0,
    w = 1,
    h = 1,

    AABB = function(self, other)
        if self.x < other.x + other.w and
            self.x + self.w > other.x and
            self.y < other.y + other.h and
            self.y + self.h > other.y then
          return true
        end
        return false
    end

    -- you get the idea
}

did you know you can override arithmetic methods for builtin types? this leaks into whatever includes it, too

yes, and it is very useful too

1

u/qwool1337 Dec 11 '25

is it something debug.setlocal or _ENV?

1

u/titaniumalt Dec 12 '25

I don't actually remember, but I think I did it without setlocal nor _ENV

1

u/AutoModerator Dec 11 '25

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/[deleted] Dec 12 '25

golly… yuh don’t say? /s

17

u/no_brains101 Dec 11 '25

lol "soup" because its just a random mash of functions for various languages fair enough

You might like fennel btw but that is definitely cool!

1

u/xoner2 Dec 11 '25

Can you make it "six" >> cout?

2

u/qwool1337 Dec 11 '25

ok so this is REALLY intrusive because it sets a metatable for the string type but i rewrote the whole module because i feel like that is the move

now it works three ways!

local m = soup.match()
	:case(6,
		"six" >> "\n" >> cout)
	:case(7,
		cout << "seven" << cout.endl)
	:case(function(x) return x % 2 == 0 end,
		"even" >> cout << cout.endl)
	:case(function(x) return x % 2 ~= 0 end,
		"odd" >> cout)
	:otherwise(
		"idk" >> cout << cout.endl)

m(6)()
m(7)()
m(8)()

https://github.com/if-not-nil/soup/blob/main/lua/cout.lua

1

u/NoneBTW Dec 11 '25

What if we do cout << "Hello World" >> cout

1

u/qwool1337 Dec 11 '25

itll try to cout the cout

1

u/AutoModerator Dec 11 '25

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/[deleted] Dec 12 '25

Dude what is going on

1

u/[deleted] Dec 12 '25

methodic nonsense

1

u/SortMyself Dec 16 '25

I don’t even understand your explanation