Skip to content

Instantly share code, notes, and snippets.

View mlabbe's full-sized avatar

Michael Labbe mlabbe

View GitHub Profile
@mlabbe
mlabbe / calc.rs
Created March 12, 2019 04:44
A simple calculator in rust (my first rust program)
/*
simple rust calculator
add_op := '+' | '-'
mul_op := '*' | '/'
digits := {'+' | '-'} [0..9] {[0..9]}
expr := term {add_op term}
term := factor {mul_op factor}
factor := digits | '(' expr ')'
@mlabbe
mlabbe / build.sh
Created December 11, 2020 23:08
MacOS M1 cross compile and build a fat binary with aarch64/arm64 and x86_64/amd64
#!/bin/bash
clang hello.c -target arm64-apple-darwin20.1.0 -o hello.arm64
echo -n "hello.arm64 is architecture " && lipo -archs ./hello.arm64
clang hello.c -target x86_64-apple-darwin-macho -o hello.x86_64
echo -n "hello.x86_64 is architecture " && lipo -archs ./hello.x86_64
lipo hello.arm64 hello.x86_64 -create -output hello
echo -n "final output binary has archs " && lipo -archs ./hello
@mlabbe
mlabbe / build.sh
Last active March 19, 2024 10:34
Use clang linker --wrap to wrap a function in the same compilation unit
#!/bin/sh -x
CC=clang-9
rm -f *.o wrap nowrap
# build without wrapping
$CC -c wrap.c -o wrap.o
$CC -c wrap_b.c -o wrap_b.o
$CC wrap.o wrap_b.o -o nowrap
@mlabbe
mlabbe / remedybg-mode.el
Created August 17, 2022 03:13
remedybg emacs minor mode (not ready for primetime)
;;
;; remedybg minor mode
;;
;; 0.1 by Michael Labbe
;;
(setq remedybg-path "c:/remedydbg/remedybg.exe")
(defun remedybg-add-breakpoint-at-file()
(interactive)
@mlabbe
mlabbe / log.h
Created December 28, 2021 19:49
Barebones code for a structured logger in C
#pragma once
// config
#define LOG_USE_STDIO 1
#define LOG_USE_FILE 0
#define LOG_USE_STRUCTURED 1
#define LOG_LEVEL_TRACE 0
#define LOG_LEVEL_WARN 1
#define LOG_LEVEL_ERROR 2
@mlabbe
mlabbe / sizeof.c
Created December 11, 2023 17:53
C Print sizeof all types
#include <stdio.h>
#include <stdint.h> // uintptr_t
#include <stddef.h> // size_t
// enable printing sizes as ints to avoid faliing on compilers that don't support %zu
#define SIZEOF(expr) (int)sizeof(expr)
#define PRINT_SIZEOF(expr) printf("sizeof(%-11s) = %1d\n", #expr, SIZEOF(expr))
@mlabbe
mlabbe / calc.c
Last active July 29, 2023 17:34
A simple calculator written as an ll1 recursive descent parser
/*
simple calculator implemented via recursive descent
add_op := + | -
mul_op := * | /
digits := {+|-} [0..9] {[0..9]}
expr := term {add_op term}
term := factor {mul_op factor}
@mlabbe
mlabbe / ftg_bitbuffer.h
Last active January 12, 2023 22:22
bitbuffer
/* ftg_bitbuffer - public domain library
no warranty implied; use at your own risk
Tightly pack values by bits into a stream of bytes.
For example, a 1-bit bool and a 32-bit integer are packed into 33
bits.
Bitbuffers are intended for small amounts of data, like a few
hundred network packets where size is important enough to remove
@mlabbe
mlabbe / i3_config
Created April 30, 2020 18:12
Run Emacsclient on i3wm scratchpad workspace
# launch scratchpad frame in existing emacs daemon
exec --no-startup-id /usr/local/bin/pad.sh
# when scratchpad frame is launched, move it to the scratchpad workspace
for_window [title="_emacs scratchpad_" class="Emacs"] move scratchpad
@mlabbe
mlabbe / log2json.py
Created December 27, 2021 20:58
Process structured logs to JSON
#!/usr/bin/env python3
# parse log.h's structured format:
#
# - each line ends with sequence 0x20 0x1a 0x0a
# - must parse through possible ascii escape sequences
# - 0x1b (esc) until 'm'
# - structure starts with line containing 3 upcase alpha chars
# - message ends with 0x1a 0x1a 0x0a
# - each line after the first is 0x09