Skip to content

Instantly share code, notes, and snippets.

View alphaKAI's full-sized avatar

Akihiro Shoji alphaKAI

View GitHub Profile
@alphaKAI
alphaKAI / direct_threaded_vm.c
Created January 14, 2019 12:19
Tiny example of direct threaded vm and a naive switch-case implementation.
#include <stdio.h>
#include <stdlib.h>
// Instruction set
enum {
Push,
Add,
Sub,
Mul,
Div,
@alphaKAI
alphaKAI / get_winsize.fsx
Created January 10, 2019 17:05
Get width and height of terminal for Windows(cmd.exe), macOS, Linux in F#
type window_size = {
x: uint16;
y: uint16;
}
open System.Runtime.InteropServices
[<Struct; StructLayout(LayoutKind.Sequential)>]
type winsize =
struct
val ws_row: uint16
open System.Runtime.InteropServices
open System
[<DllImport("kernel32.dll", SetLastError = true)>]
extern IntPtr GetStdHandle(int nStdHandle)
[<DllImport("kernel32.dll", SetLastError = true)>]
extern bool GetConsoleMode(IntPtr hConsoleHandle, uint32& lpMode)
[<DllImport("Kernel32.dll", SetLastError = true)>]
@alphaKAI
alphaKAI / Program.fs
Last active January 9, 2019 14:11
OCamlとF#でそれぞれeast_asian_widthを計算する (これを移植しました: http://www.shibu.jp/techmemo/zenhan.html)
module Program
module Unicode =
let start_group = [
161; 164; 167; 170; 173; 176; 182; 188; 198; 208; 215; 222; 230; 232; 236; 240; 242; 247; 252; 254; 257; 273; 275;
283; 294; 299; 305; 312; 319; 324; 328; 333; 338; 358; 363; 462; 464; 466; 468; 470; 472; 474; 476; 593; 609; 708;
711; 713; 717; 720; 728; 733; 735; 768; 913; 945; 963; 1025; 1040; 1105; 4352; 4515; 4602; 8208; 8211; 8216; 8220;
8224; 8228; 8240; 8242; 8245; 8251; 8254; 8308; 8319; 8321; 8364; 8451; 8453; 8457; 8467; 8470; 8481; 8486; 8491;
8531; 8539; 8544; 8560; 8585; 8632; 8658; 8660; 8679; 8704; 8706; 8711; 8715; 8719; 8721; 8725; 8730; 8733; 8739;
8741; 8743; 8750; 8756; 8764; 8776; 8780; 8786; 8800; 8804; 8810; 8814; 8834; 8838; 8853; 8857; 8869; 8895; 8978;
@alphaKAI
alphaKAI / eaw.ml
Created December 23, 2018 09:13
get east_asian_width in OCaml, using Core_kernel and Camomile. Ported from http://www.shibu.jp/techmemo/zenhan.html
open Core_kernel
let start_group = [
161; 164; 167; 170; 173; 176; 182; 188; 198; 208; 215; 222; 230; 232; 236; 240; 242; 247; 252; 254; 257; 273; 275;
283; 294; 299; 305; 312; 319; 324; 328; 333; 338; 358; 363; 462; 464; 466; 468; 470; 472; 474; 476; 593; 609; 708;
711; 713; 717; 720; 728; 733; 735; 768; 913; 945; 963; 1025; 1040; 1105; 4352; 4515; 4602; 8208; 8211; 8216; 8220;
8224; 8228; 8240; 8242; 8245; 8251; 8254; 8308; 8319; 8321; 8364; 8451; 8453; 8457; 8467; 8470; 8481; 8486; 8491;
8531; 8539; 8544; 8560; 8585; 8632; 8658; 8660; 8679; 8704; 8706; 8711; 8715; 8719; 8721; 8725; 8730; 8733; 8739;
8741; 8743; 8750; 8756; 8764; 8776; 8780; 8786; 8800; 8804; 8810; 8814; 8834; 8838; 8853; 8857; 8869; 8895; 8978;
9001; 9312; 9451; 9552; 9600; 9618; 9632; 9635; 9650; 9654; 9660; 9664; 9670; 9675; 9678; 9698; 9711; 9733; 9737;
@alphaKAI
alphaKAI / vm.d
Created June 23, 2018 13:25
Toy register machine based VM in D (Translate the OCaml version into D) (OCaml Version : https://gist.github.com/alphaKAI/e916c7c540817c4b3d119d7f095f29b9)
import std.algorithm;
import std.string;
import std.array;
import std.regex;
import std.range;
import std.stdio;
struct Stack(T) {
T[] stack;
@alphaKAI
alphaKAI / vm.ml
Last active June 24, 2018 11:25
Toy register machine based virtual machine in OCaml (build with: ocamlfind ocamlopt -package core -package ppx_deriving.show -package ppx_deriving.enum vm.ml -linkpkg -o vm -thread -O3)
open Core
type registers = {
mutable a: int;
mutable b: int;
mutable c: int;
mutable d: int;
mutable e: int;
mutable f: int
}
@alphaKAI
alphaKAI / otwitter.ml
Last active June 13, 2018 19:53
Twitter API Client in OCaml (Nocrypto, base64, OCurl, ocaml-uri)
#use "topfind";;
#require "nocrypto";;
#require "base64";;
#require "curl";;
#require "uri";;
let debug = true;;
let baseUrl = "https://api.twitter.com/1.1/";;
@alphaKAI
alphaKAI / heap_sort.ml
Created June 6, 2018 13:54
Heap-sort in OCaml
let swap a i j =
let t = a.(i) in
a.(i) <- a.(j);
a.(j) <- t;;
let printArray arr =
Printf.printf "[";
for idx = 0 to Array.length arr - 1 do
if idx > 0 then Printf.printf ", ";
Printf.printf "%d" @@ Array.get arr idx
import std.traits,
std.meta;
class Any {
private {
abstract class _AnyBase {
abstract TypeInfo type();
abstract _AnyBase clone();
}