Skip to content

Instantly share code, notes, and snippets.

View jvranish's full-sized avatar

Job Vranish jvranish

  • Grand Rapids, MI
View GitHub Profile
@jvranish
jvranish / dynamic_alloc_reverse.c
Created September 22, 2012 21:07
Dynamic Allocation without malloc in C (with no mutation!)
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
/*
This is our integer linked list type (Null is an empty list)
(it is immutable, note the consts)
*/
typedef struct IntList_s const * const IntList;
@jvranish
jvranish / stack_traces.c
Last active April 25, 2024 15:51
An example of catching exceptions and printing stack traces in C on Windows, Linux and OS X
/* compile with:
on linux: gcc -g stack_traces.c
on OS X: gcc -g -fno-pie stack_traces.c
on windows: gcc -g stack_traces.c -limagehlp
*/
#include <signal.h>
#include <stdio.h>
#include <assert.h>
@jvranish
jvranish / c_option_type.c
Last active May 2, 2022 19:17
Simple option type in C
//usr/bin/make -s "${0%.*}" CFLAGS="-O2 -Wall -Werror" && ./"${0%.*}" "$@"; s=$?; rm ./"${0%.*}"; exit $s
#include <stdio.h>
#include <stdbool.h>
enum option_tag { OPTION_SOME, OPTION_NONE };
#define option_type(T) { enum option_tag tag; T some; }
#define SOME(S) { .tag = OPTION_SOME, .some = S }
#define NONE() { .tag = OPTION_NONE }
@jvranish
jvranish / LICENSE
Created September 13, 2016 21:23
Simple makefiles for C/C++ projest with automatic determination of header dependencies, source files, and include paths
ISC License
Copyright (c) 2016, Job Vranish
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@jvranish
jvranish / either.cpp
Created January 18, 2011 15:45
Example Either type in C++
#include <stdio.h>
template <class T1, class T2>
class Either
{
bool isLeft;
union
{
T1 left;
T2 right;
@jvranish
jvranish / adcpi18.py
Created May 20, 2015 23:51
python script to continuously sample ADC connected to raspberry pi via I2C
#!/usr/bin/env python
# read abelectronics ADC Pi V2 board inputs with repeating reading from each channel.
# # Requries Python 2.7
# Requires SMBus
# I2C API depends on I2C support in the kernel
# Version 1.0 - 06/02/2013
# Version History:
# 1.0 - Initial Release
@jvranish
jvranish / valueLevelClasses.hs
Created May 2, 2012 17:28
An experiment with mixing value level typeclasses and implicit parameters
{-# LANGUAGE Rank2Types
, RebindableSyntax
, ImplicitParams
, NoMonomorphismRestriction #-}
import Data.Maybe
import Data.Function
import Data.String
import Prelude (undefined, error, String, (++))
@jvranish
jvranish / thumbv7em-none-eabi
Created May 16, 2016 13:47
Rust Cortex-M4 target spec
{
"arch": "arm",
"cpu": "cortex-m4",
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64",
"disable-redzone": true,
"executables": true,
"llvm-target": "thumbv7em-none-eabi",
"morestack": false,
"os": "none",
"relocation-model": "static",
@jvranish
jvranish / lift.hs
Created March 29, 2011 19:47
Example of how to automatically lift a haskell function into a function on some AST
import Control.Monad
import Control.Monad.Error
instance Error (RuntimeError a) where
data Expr = Number Int
| SomethingElse
deriving (Show, Eq, Ord)
data Type = Func Type Type
@jvranish
jvranish / logict_vs_fbacktrack.hs
Created March 9, 2013 15:30
A comparison of LogicT and Stream (from Olegs FBackTrack.hs) Stream has better termination criteria!
{-
To install dependencies:
cabal install logict
cabal install stream-monad
-}
import Control.Monad.Logic