Skip to content

Instantly share code, notes, and snippets.

@zehnpaard
zehnpaard / dune
Created March 1, 2021 02:32
Minimal example using ocamllex/menhir with js_of_ocaml : compile to JS with the command `dune build ./main.bc.js`
(menhir
(modules parser))
(ocamllex lexer)
(executable
(name main)
(modes js))
@zehnpaard
zehnpaard / dune
Created June 10, 2019 09:31
OCaml template for menhir/ocamllex/dune indentation-aware parser
(menhir
(modules parser))
(ocamllex lexer)
(executable
(name ex))
@zehnpaard
zehnpaard / grammar
Last active January 1, 2025 03:26
Python Implementation of Backtracking Recursive Descent Parser based loosely on Language Implementation Patterns
list : '[' elements ']';
elements : element (',' element)*;
element : NAME | assign | list | list_assign;
assign : NAME '=' NAME;
list_assign : list '=' list;
NAME : ('a'..'z' | 'A'..'Z')+;
module D = Effect.Deep
type 'a expr = ..
type _ Effect.t += Extension : 'a expr -> 'a Effect.t
(* Base Interpreter *)
type 'a expr +=
| Int : int -> int expr
| Add : int expr * int expr -> int expr
| Sub : int expr * int expr -> int expr
@zehnpaard
zehnpaard / simple-compojure.core.clj
Created October 30, 2016 06:03
Simple Compojure Demo with GET/POST forms
(ns simple-compojure.core
(require
[ring.adapter.jetty :refer [run-jetty]]
[ring.middleware.params :as p]
[simple-compojure.middleware :as m]
[simple-compojure.routes :as r]
))
(def app
(-> r/routes
@zehnpaard
zehnpaard / evaluate.py
Created August 12, 2021 03:42
Small Lisp Interpreter using Python 3.10 Structural Pattern Matching
import exp as e
import value as v
import globals
def _eval(exp, env):
match exp:
case e.Num(n):
return v.Num(n)
case e.Symbol(s):
return find(s, env)
@zehnpaard
zehnpaard / simple-hiccup.core.clj
Last active March 25, 2023 02:04
Simple Ring/Compojure/Hiccup Demo
(ns simple-hiccup.core
(require
[ring.adapter.jetty :refer [run-jetty]]
[simple-hiccup.middleware :as m]
[simple-hiccup.routes :as r]
))
(def app
(-> r/routes
m/logger
@zehnpaard
zehnpaard / effectful_split_mutual_recursion.ml
Last active January 8, 2023 03:56
Implementation of mutually recursive is_odd and is_even, splitting the definition across modules using OCaml 5.0 effect handlers
module Base = struct
type _ Effect.t +=
| IsOdd: int -> bool Effect.t
| IsEven: int -> bool Effect.t
end
module Odd = struct
let is_odd n =
if n=0 then false
else Effect.perform (Base.IsEven (n-1))
@zehnpaard
zehnpaard / grammar
Last active January 7, 2023 11:16
Python Implementation of LL(1) Non-Recursive Parser based loosely on Language Implementation Patterns
list : '[' elements ']';
elements : element (',' element)*;
element : ('a'..'z' | 'A'..'Z')+;
@zehnpaard
zehnpaard / grammar
Created September 8, 2021 02:53
Python Implementation of LL(2) Recursive Descent Parser based loosely on Language Implementation Patterns
list : '[' elements ']';
elements : element (',' element)*;
element : NAME | assign | list;
assign : NAME '=' NAME;
NAME : ('a'..'z' | 'A'..'Z')+;