Skip to content

Instantly share code, notes, and snippets.

View mjambon's full-sized avatar

Martin Jambon mjambon

View GitHub Profile
@mjambon
mjambon / example.ml
Created September 1, 2022 05:40
Deriving language-specific ASTs and conversions to generic AST
module Generic = struct
(* generic expr type used for matching *)
type expr =
| A
| B of expr * expr [@lang a]
| C of expr [@lang b c]
| Unsupported of string [@fallback]
end
(*** Generated from the above ***)
@mjambon
mjambon / everything.atd
Last active February 4, 2022 09:44
Current translation from everything.atd to everything.py done by atdpy
type kind = [
| Root (* class name conflict *)
| Thing of int
| WOW <json name="wow">
| Amaze <json name="!!!"> of string list
]
type root = {
id: string;
await: bool;
@mjambon
mjambon / example.py
Created February 2, 2022 01:38
Python sum types for atdpy
from typing import List
import json
# type t = A | B of int
class T:
pass
class T_A(T):
def __repr__(self):
@mjambon
mjambon / Binary.ml
Created November 2, 2021 09:47
Basic arithmetic operations implemented from scratch on unary and binary representations of natural numbers
(* Arithmetic with Binary numbers (unsigned, arbitrary precision!) *)
type digit = Zero | One
type t = digit list
let rec eq a b =
match a, b with
| [], [] -> true
| da :: a, db :: b when da = db -> eq a b
| _ -> false
@mjambon
mjambon / list_map.ml
Last active July 22, 2021 09:21
List.map safe reimplementation benchmark
(*
Evaluate the performance of a stack-safe implementation of List.map.
Run with:
ocamlopt -o list_map unix.cmxa list_map.ml
./list_map
*)
open Printf
@mjambon
mjambon / Lib_string.mli
Last active January 28, 2021 22:22
What a string library for OCaml might look like
(*
Extra string operations for OCaml
Conventions:
- all operations are failsafe: no exceptions are raised
- negative index i is equivalent to positive index (length - i)
- from/until indicate inclusive ranges like in 'for' loops
(for i = from to until do ... done)
- does not replicate or override the standard String module
@mjambon
mjambon / git-delete
Last active August 26, 2020 17:58
Delete a commit from a git branch
#! /usr/bin/env bash
#
# Remove a single git commit from history.
#
set -eu
usage() {
cat <<EOF
Usage: $0 <commit ID>
@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"]