This commit is contained in:
Imbus 2025-08-27 19:44:15 +02:00
commit c0b5de5a4f
5 changed files with 161 additions and 0 deletions

28
adjmat.lua Normal file
View file

@ -0,0 +1,28 @@
local N = 4
-- 0 means no edge
local adjMatrix = {
{ 0, 5, 3, 0 }, -- edges from vertex 0
{ 0, 0, 0, 2 }, -- edges from vertex 1
{ 0, 0, 0, 7 }, -- edges from vertex 2
{ 0, 0, 0, 0 }, -- edges from vertex 3
}
for i = 1, N do
for j = 1, N do
io.write(adjMatrix[i][j], " ")
end
print()
end
local function foreach(list, fn)
for _, v in ipairs(list) do
fn(v)
end
end
local numbers = { 10, 20, 30 }
foreach(numbers, print)
print("Hello")

14
main.lua Normal file
View file

@ -0,0 +1,14 @@
local m = require("test")
local f = require("objects")
m.test()
print(m.addtest(1, 2))
local fighter1 = f.Fighter:new({ name = "Fighter1", health = 100, speed = 10 })
local function param(a)
print(a)
end
param("Test")
-- fighter1

47
objects.lua Normal file
View file

@ -0,0 +1,47 @@
local F = {}
----------------------------------------
-- Define the class attributes
----------------------------------------
Fighter = {
name = "",
health = 0,
speed = 0,
}
----------------------------------------
-- Define the class methods
----------------------------------------
function Fighter:light_punch()
print("Figher " .. self.name .. " performed a light punch")
end
----------------------------------------
-- Define the class constructor
----------------------------------------
--- @return Fighter
function Fighter:new(t)
-- In Lua, a constructor essentially
-- binds the object to the class
-- and returns the object
t = t or {}
setmetatable(t, self)
self.__index = self
return t
end
----------------------------------------
-- Instantiate objects
----------------------------------------
-- local fighter1 = Fighter:new({ name = "Fighter1", health = 100, speed = 10 })
-- local fighter2 = Fighter:new({ name = "Fighter2", health = 100, speed = 10 })
----------------------------------------
-- Call the class methods
----------------------------------------
-- fighter1:light_punch()
-- fighter2:light_punch()
F.Fighter = Fighter
return F

54
prompt.lua Normal file
View file

@ -0,0 +1,54 @@
local function prompt_yn(question)
while true do
io.write(question .. " [y/N]: ")
local input = io.read()
if input == "y" or input == "Y" then
return true
end
io.write("Exiting...\n")
os.exit(1)
return false
end
end
-- Run a command using the OS shell and capture the stdout
-- Strips exactly one trailing newline if present, does not strip any other whitespace.
-- Asserts that the command exits cleanly
local function capture(command)
local p = io.popen(command)
local output = p:read("*a")
assert(p:close())
-- Strip exactly one trailing newline from the output, if there is one
return output:match("(.-)\n$") or output
end
local funct
local function append_list(list, other)
for _, item in ipairs(other) do
table.insert(list, item)
end
end
local function err(msg)
io.stderr:write("Error: " .. msg .. "\n")
end
local function fatal(msg)
err(msg)
os.exit(1)
end
local demolist = { 1, 2, 3 }
-- append_list(demolist, 4)
demolist[#demolist + 1] = 4
for _, value in ipairs(demolist) do
print(value)
end
print(capture("ls"))
prompt_yn("Testing")
err("Something bad happened")
fatal("Crashing...")

18
test.lua Normal file
View file

@ -0,0 +1,18 @@
M = {}
-- @return nil
local function test()
print("Hello from test")
end
-- Returns the sum of two numbers.
--
-- @param x1 number
-- @param x2 number
function M.addtest(x1, x2)
return x1 + x2
end
M.test = test
return M