Skip to content

Instantly share code, notes, and snippets.

View mjambon's full-sized avatar

Martin Jambon mjambon

View GitHub Profile
@mjambon
mjambon / typed-shell.sh
Last active August 20, 2020 08:16
Daydreaming about a shell syntax that embeds ocaml code and vice-versa
# Guiding principle: a shell is a domain-specific language for handling
# files and processes. These core features shall be served by a dedicated
# and already familiar syntax. Traditional programming control structures
# on the other hand are hard to use and limited in traditional shell
# languages. For this, we shall use a proper programming language, one that
# checks statically for basic programming mistakes, allows the use of rich
# data structures, and integrates well in large applications.
#
# Language properties:
#
@mjambon
mjambon / CST.ml
Created July 13, 2020 17:25
Typescript CST definition with nested alternatives
(* Generated by ocaml-tree-sitter. *)
(*
typescript grammar
entrypoint: program
*)
open! Sexplib.Conv
open Tree_sitter_run
@mjambon
mjambon / Boilerplate.ml
Created July 11, 2020 18:22
Java boilerplate with environment, no fold
(**
Boilerplate to be used as a template when mapping the java CST
to another type of tree.
*)
(* Disable warnings against unused variables *)
[@@@warning "-26-27"]
(* Disable warning against unused 'rec' *)
[@@@warning "-39"]
@mjambon
mjambon / Boilerplate.ml
Created July 11, 2020 17:38
Java boilerplate with extra env being passed around
(**
Boilerplate to be used as a template when mapping the java CST
to another type of tree.
*)
(* Disable warnings against unused variables *)
[@@@warning "-26-27"]
(* Disable warning against unused 'rec' *)
[@@@warning "-39"]
@mjambon
mjambon / save.sh
Last active July 6, 2020 22:31
Save all the previous copies of a config file without duplicates
# Bash function
#
# Usage: save foo.bar
# - creates a file like foo.bar.d41d8cd,
# - overwrites previous copy if identical,
# - preserves other previous versions with very high probability
#
save() {
file=$1
if [[ -f "$file" ]]; then
@mjambon
mjambon / cmdliner_term_v1.ml
Last active December 7, 2019 02:55
That feeling when you realize there's no magic in Cmdliner
(*
Sample command-line argument specified using Cmdliner's author's style.
*)
open Cmdliner
let seed_term =
let doc = "
Use the number $(docv) to initialize the global pseudo-random number
generator.
@mjambon
mjambon / .bashrc
Last active April 10, 2019 21:38
Bash prompt with git branch and git stash size
git_stash_size() {
n=$( (git stash list 2> /dev/null || :) | wc -l )
if [ $n -gt 0 ]; then
echo -n " +$n"
fi
}
git_branch() {
echo -n "$(git branch 2> /dev/null \
| sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/')"
@mjambon
mjambon / self-extend
Last active September 19, 2018 20:14
Editing a bash script while it's running
#! /bin/bash
#
# Illustrate what happens when editing a script while it's running.
#
# This happens when saving the file in emacs, for example, but not with
# 'sed -i' because the latter deletes the old file before writing a new one.
# See https://unix.stackexchange.com/a/88572/46435 for an explanation.
#
set -eu
@mjambon
mjambon / bash-scoping2
Created July 19, 2018 17:38
Bash scoping. Does this program print 1, 2 or 3?
#! /bin/bash
x=1
f() {
echo "$x"
}
g() {
local x=2
@mjambon
mjambon / bash-scoping
Last active July 19, 2018 18:17
Scoping of bash functions
#! /bin/bash
f() {
echo "$x"
}
main() {
local x=42
f
}