Skip to content

Instantly share code, notes, and snippets.

View MCJack123's full-sized avatar
๐Ÿ’š
Go Green!

JackMacWindows MCJack123

๐Ÿ’š
Go Green!
View GitHub Profile
@MCJack123
MCJack123 / !SwiftCoroutines.md
Last active April 30, 2025 14:16
Lua-style coroutine objects in Swift 5.5+, using Swift Concurrency

Implementing coroutines in Swift using Swift Concurrency

One of my favorite features of Lua is its first-class support for coroutines. Recently, I started writing a new project using Swift, and I wanted to be able to use coroutines natively in my Swift code. In most Lua VMs, coroutines are a complex feature that require a lot of environmental support to be able to save and restore function calls. However, Swift includes the async and await keywords for pausing functions built into the language. Because of this, I decided to take a crack at using them to implement coroutines natively.

What are coroutines?

A coroutine is an object that represents a function which can be paused and resumed. This function may pause (or yield) itself at any time, which will return execution to the code that last resumed the coroutine. The coroutine can then be resumed later, and the code will pick up right where it left off.

A coroutine also represents a call stack, or a thread of execution. T

@MCJack123
MCJack123 / client.lua
Last active December 5, 2024 11:56
PIN-based door lock system for ComputerCraft, using a monitor for PIN entry
-- Save this as startup.lua on each client computer with a 1x1 monitor and modem attached. You can also optionally attach a speaker for audio feedback.
local secret = "" -- Set this to the same secret that was set in the server file.
local redstoneSide = "left" -- Set this to the side the redstone signal should be output on.
local openTime = 5 -- Set this to the number of seconds to keep the door open for.
local defaultOutput = false -- Set this to the default redstone state for the door. If set to true, this means power will be cut when unlocking.
-- This allows you to place a door sideways, and then have it stay closed even when power is applied externally.\
local accessLevel = 0 -- Set this to the minimum access level required by a PIN.
if not secret or secret == "" then error("Please set some keys inside the script before running.") end
@MCJack123
MCJack123 / ans.lua
Last active August 29, 2024 17:41
Tabled Asymmetrical Numeral Systems (aka Finite State Entropy) for Lua 5.2 (PoC)
-- Tabled Asymmetrical Numeral Systems (aka Finite State Entropy) for Lua 5.2
--
-- MIT License
--
-- Copyright (c) 2023 JackMacWindows
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@MCJack123
MCJack123 / format.lua
Created June 22, 2023 15:14
Quick & dirty Lua formatter written in Lua
-- MIT License
--
-- Copyright (c) 2022-2023 JackMacWindows
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
@MCJack123
MCJack123 / typecheck.ts
Created April 6, 2023 05:10
TypeScriptToLua plugin to automatically add type checks to annotated functions
import * as ts from "typescript";
import * as tstl from "@jackmacwindows/typescript-to-lua";
const EXPECT_PLUGIN = "cc.expect";
const EXPECT_METHOD = "expect";
const EXPECT_FUNCTION = "___TS_Expect";
function fillTypeList(args: ts.Expression[], type: ts.TypeNode, context: tstl.TransformationContext): boolean {
if (ts.isUnionTypeNode(type)) {
for (let t of type.types) if (!fillTypeList(args, t, context)) return false;
@MCJack123
MCJack123 / sidplayer.lua
Created March 17, 2023 07:09
Commodore 64 SID player for ComputerCraft (requires some libraries)
local M6502 = require "M6502" -- https://gist.github.com/MCJack123/cf500b62fdbcb135d2db6cf97a771c6e
local SID = require "sid" -- https://gist.github.com/MCJack123/d076bfcab67767cef6fd0292655e03ae
term.clear()
term.setCursorPos(1, 1)
local win = window.create(term.current(), 38, 1, 14, 10)
win.clear()
local status = window.create(term.current(), 1, 1, 37, 19)
local oldterm = term.redirect(status)
local ok, err = pcall(function(...)
@MCJack123
MCJack123 / sapplayer.lua
Created March 17, 2023 07:08
Atari ST SAP player for CraftOS-PC sound plugin (requires https://gist.github.com/MCJack123/cf500b62fdbcb135d2db6cf97a771c6e)
local M6502 = require "M6502"
local nsongs = 1
local defaultsong = 0
local stereo = false
local ntsc = false
local type = 0 -- 0 = B, 1 = C, 2 = D, 3 = S, 4 = R
local fastplay = 312
local initaddr = 0x0000
local musicaddr = 0x0000
@MCJack123
MCJack123 / sid.lua
Created March 17, 2023 07:07
Pure Lua 5.2 port of reSID Commodore 64 SID emulator
-------------------------------------------------------------------------------
-- This file is reSID, a MOS6581 SID emulator engine, ported to Lua.
-- Copyright (C) 2004 Dag Lem <[email protected]>
-- Lua port Copyright (C) 2023 JackMacWindows <[email protected]>
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
@MCJack123
MCJack123 / M6502.lua
Created March 17, 2023 07:05
MOS 6502 emulator written in pure Lua
-- MIT License
--
-- Copyright (c) 2018-2022 JackMacWindows
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
@MCJack123
MCJack123 / struct.lua
Last active February 21, 2025 23:12
Lua 5.3+ C struct parser library for declarative binary file serialization
-- MIT License
--
-- Copyright (c) 2023-2025 JackMacWindows
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions: