Skip to content

Instantly share code, notes, and snippets.

@wernsey
wernsey / pratt.c
Created January 8, 2024 13:21
Example of a Pratt parser for mathematical expressions in C99
/*
Example of a Pratt parser for mathematical expressions in C99.
Done mostly to get my head around the concepts
* [Pratt Parsers: Expression Parsing Made Easy ](https://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/) by Munificent
* He also talks about it in [chapter 17 of Crafting Interpreters](https://craftinginterpreters.com/compiling-expressions.html#a-pratt-parser)
* <https://dev.to/jrop/pratt-parsing> - has a lot of pseudo code and uses
the same terminology that Pratt used in his original paper.
* <https://langdev.stackexchange.com/a/3275> - very thorough explanation.
Would upvote if I was signed in
@wernsey
wernsey / dungeon.c
Created December 6, 2022 17:36
Random Dungeon Generator for a Roguelike
/**
* **Random Dungeon Generator**
*
* A random dungeon generator for a Roguelike game.
*
* It is based on the "Rogue" algorithm, as described by Mark Damon Hughes
* [Game Design: Article 07: Roguelike Dungeon Generation][algorithm], but with
* some modifications.
*
* The basic algorithm is this:
/***************************************************************************
*
*
* chase appeared in Creative Computing
* magazine way back in 1976. According
* to the book "More BASIC Computer Games"
* it was written by Mac Oglesby, with
* improvements by Bill Cotter and Arnold
* Loveridge.
* This version, in C, was written by
javascript:(function(){
/* Ad-B-Gone: Bookmarklet that removes obnoxious ads from pages */
const removeAds = () => {
let selectors = [
/* By ID: */
'#sidebar-wrap', '#advert', '#xrail', '#middle-article-advert-container',
'#sponsored-recommendations', '#around-the-web', '#sponsored-recommendations',
'#taboola-content', '#taboola-below-taboola-native-thumbnails', '#inarticle_wrapper_div',
'#rc-row-container', '#ads', '#at-share-dock', '#at4-share', '#at4-follow', '#right-ads-rail',
'div#ad-interstitial', 'div#advert-article', 'div#ac-lre-player-ph', 'div#videoAdSlot',
@wernsey
wernsey / csvstrm.c
Last active July 3, 2022 15:09
CSV stream reader - single header library
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define CSV_STATIC
#define CSV_IMPLEMENTATION
#include "csvstrm.h"
int main(int argc, char *argv[]) {
@wernsey
wernsey / mdeep.awk
Created July 2, 2022 16:17
Awk script to generate Markdeep documentation from source code
#! /usr/bin/awk -f
BEGIN { print "<meta charset=\"utf-8\">" }
/\/\*\*/ {
sub(/^.*\/\*/,"");
incomment=1;
}
incomment && /\*\// {
incomment=0;
sub(/[[:space:]]*\*\/.*/,"");
sub(/^[[:space:]]*\*[[:space:]]?/,"");
@wernsey
wernsey / README.md
Created June 23, 2022 09:37
Plays a tune using the PC speaker in DOS

Notation

The letters C, D, E, F, G, A and B are used to indicate notes.

Notes can be [sharpened][sharp] by appending a hash symbol (#) or [flattened][flat] by appending an exclaimation mark (!). For example "C#" is used to indicate a C♯, and "D!" is used to indicate a D♭.

Octaves: By default, the notes are in the 4th octave. A numeric value can be appended after the note to indicate a different octave. For example, "A5" indicates an A in the 5th octave, and "G#3" indicates a G♯ in the third octave.

/*
Playing around with the text-to-speech synthesizer single header
library I found here:
https://github.com/mattiasgustavsson/libs/blob/main/speech.h
wav.c and wav.h can be found here:
https://gist.github.com/wernsey/fa8f8b7a145e4cf06df736e3e5659eb1
*/
#include <stdio.h>
@wernsey
wernsey / wayback.js
Created December 22, 2021 13:39
Bookmarklet to go search for a page on the Wayback machine
javascript:(
/*
Bookmarklet to go search for a page on the Wayback machine
https://en.wikipedia.org/wiki/Help:Using_the_Wayback_Machine
The /1if_/ trick comes from here https://news.ycombinator.com/item?id=29646312
*/
function (){
window.location=`https://web.archive.org/web/1if_/${window.location}`;
}
)()
@wernsey
wernsey / lzss.c
Last active August 20, 2022 10:25
LZSS.C -- A Data Compression Program
/**************************************************************
LZSS.C -- A Data Compression Program
(tab = 4 spaces)
***************************************************************
4/6/1989 Haruhiko Okumura
Use, distribute, and modify this program freely.
Please send me your improved versions.
PC-VAN SCIENCE
NIFTY-Serve PAF01022
CompuServe 74050,1022