This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* Pretty-printing in OCaml: A Format Primer | |
https://keleshev.com/pretty-printing-in-ocaml-a-format-primer *) | |
let printf = Format.printf | |
let fprintf = Format.fprintf | |
let sprintf = Format.asprintf | |
let pp_string ppf string = fprintf ppf "%S" string | |
let pp_print_list ~sep pp_item = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let distance' ~bound a b = | |
let n = String.length a and m = String.length b in | |
assert (n >= m); | |
if n - m > bound then None else | |
let fill_bound = min m bound in | |
let previous = Array.make (m + 1) max_int in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let printf = Format.printf | |
let fprintf = Format.fprintf | |
let dprintf = Format.dprintf | |
let pp_list ~sep pp = | |
Format.pp_print_list ~pp_sep:(fun ppf () -> Format.fprintf ppf sep) pp | |
let pp_string ppf string = fprintf ppf "%S" string | |
let many = [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ast.exe: ast.c | |
gcc -Werror -Wswitch -Wimplicit-fallthrough ast.c -o ast.exe | |
clean: | |
rm -f *.o *.exe *.s a.out | |
test.s: ast.exe | |
./ast.exe > test.s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* https://keleshev.com/advanced-error-handling-in-ocaml *) | |
let failwithf f = Printf.ksprintf failwith f | |
(* | |
t -> bool | int | |
e -> true | false | |
| 0 | 1 | 2 | … |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* https://keleshev.com/composable-error-handling-in-ocaml *) | |
open Printf | |
let (>>=) = Result.bind | |
let (let*) = Result.bind | |
module Exceptions_example (X: sig | |
type tree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Printf | |
module CLI = struct | |
type t = { | |
verbose: bool; | |
max_files: int; | |
dir_to_list: string; | |
sort_order: [`alpha | `chrono | `owner] option; | |
args: string list; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Make (X: sig | |
val block_size: int | |
val hash: string -> string (* binary, not digest *) | |
end) = struct | |
let o_pad = String.make X.block_size '\x5C' | |
let i_pad = String.make X.block_size '\x36' | |
let block_xor left right = | |
let result = Bytes.create X.block_size in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include <stdio.h> | |
#include "parser.h" | |
%} | |
%% | |
[ \r\n\t]* { continue; /* Skip blanks. */ } | |
[0-9]+ { sscanf(yytext, "%d", &yylval->value); | |
return TOKEN_NUMBER; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function verboseRegExp(input) { | |
if (input.raw.length !== 1) { | |
throw Error('verboseRegExp: interpolation is not supported'); | |
} | |
let source = input.raw[0]; | |
let regexp = /(?<!\\)\s|[/][/].*|[/][*][\s\S]*[*][/]/g; | |
let result = source.replace(regexp, ''); |
NewerOlder