From c0b5de5a4f8f2be30eed62ae6f86e0360e6699b8 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 27 Aug 2025 19:44:15 +0200 Subject: [PATCH] Init --- adjmat.lua | 28 +++++++++++++++++++++++++++ main.lua | 14 ++++++++++++++ objects.lua | 47 ++++++++++++++++++++++++++++++++++++++++++++++ prompt.lua | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test.lua | 18 ++++++++++++++++++ 5 files changed, 161 insertions(+) create mode 100644 adjmat.lua create mode 100644 main.lua create mode 100644 objects.lua create mode 100644 prompt.lua create mode 100644 test.lua diff --git a/adjmat.lua b/adjmat.lua new file mode 100644 index 0000000..8041de6 --- /dev/null +++ b/adjmat.lua @@ -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") diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..b73c285 --- /dev/null +++ b/main.lua @@ -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 diff --git a/objects.lua b/objects.lua new file mode 100644 index 0000000..e447062 --- /dev/null +++ b/objects.lua @@ -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 diff --git a/prompt.lua b/prompt.lua new file mode 100644 index 0000000..241c50b --- /dev/null +++ b/prompt.lua @@ -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...") diff --git a/test.lua b/test.lua new file mode 100644 index 0000000..5f951fd --- /dev/null +++ b/test.lua @@ -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