Skip to content

Instantly share code, notes, and snippets.

View wavebeem's full-sized avatar

Sage Fennel wavebeem

View GitHub Profile
@wavebeem
wavebeem / OUTPUT
Created July 28, 2015 20:13
Math parser using Parsimmon
####### AST ###############
[ 'Let',
[ 'Ident', 'x' ],
[ 'Number', 1 ],
[ 'Let',
[ 'Ident', 'y' ],
[ 'Number', 2 ],
[ 'BinOp',
'+',
@wavebeem
wavebeem / main.js
Created July 30, 2015 20:22
TC.js example translated into Squiggle
var TC = require('tc');
var add = TC
.takes([TC.Number, TC.Number])
.returns(TC.Number)
.by(function(a, b) { return a + b; });
var getName = TC
.takes([TC.Object(TC.Any)])
.returns(TC.String)
.by(function(obj) { return obj.name; });
var max = TC([TC.Number, TC.Number], TC.Number, Math.max);
@wavebeem
wavebeem / main.js
Last active August 29, 2015 14:26
RPN calculator in JS
/// Consider that the JavaScript version contains far fewer assertions than the
/// Squiggle version. Squiggle automatically asserts that a key is an own
/// property of the object before retreiving it (e.g. x[0], foo.bar, keys must
/// exist or it throws). Also, functions will throw unless called with the
/// correct number of arguments. Arrays and objects created through Squiggle are
/// also implicitly frozen, so you can't accidentally mutate them.
var _ = require("lodash");
var text = "2 3 4 * 3 - +";
@wavebeem
wavebeem / compilation-idea.js
Last active August 29, 2015 14:26
Squiggle pattern matching idea
// map = fn(f, xs) match (xs) {
// case [] => []
// case [x, ...xs] => [f(x)] ++ map(f, xs)
// }
// This approach is going to be the best, but it requires the compiler to become
// aware of being inside functions, so it can expand things into statements,
// rather than being purely expression oriented. It's sort of amazing how far
// you can get only using expressions in JS, though. It will also need to
// rewrite variable usage, which might be a little tricky.
-- Also note, it seems people generally reach for positional types unless the data has many fields.
-- data BTNode t = BTNode t (BTNode t) (BTNode t) | BTNil
-- as opposed to:
-- data BTNode t = BTNode { data :: t, left :: BTNode, right :: BTnode } | BTNil
data Car2 = Car2 { make :: Maybe String } deriving (Show)
-- Haskell automatically makes a function in this scope:
-- make :: Car2 -> Maybe String
-- So you can just say `make c1` to get the make of the Car2 called `c1`.
@wavebeem
wavebeem / boring.js
Last active August 29, 2015 14:27
a boring browser global module
(function(global) {
var foo = global.foo;
function bar1() { return 1; }
function bar2() { return 2; }
function bar3() { return 3; }
var api = {
bar1: bar1,
bar2: bar2,
bar3: bar3
};
// squiggle> :set f = fn(n) match (n) { case 0 => 1 case x => x * f(x - 1) }
(function () {
'use strict';
return (false || eval)('this')['f'] = sqgl$$freeze(function (n) {
if (arguments.length !== 1) {
throw new sqgl$$Error('expected 1 argument(s), got ' + arguments.length);
}
return function ($match) {
if (sqgl$$is($match, 0)) {
return function () {
@wavebeem
wavebeem / parsefun.js
Created October 6, 2015 00:07
This does not actually generate the correct AST because I was using arrays and dang does that get confusing over time, but this helped me figure out how to parse my binary operators correctly while also including unary operators at the right precedence level and make foo.bar foo[bar] and foo(bar) all chain together correctly.
"use strict";
const util = require("util");
const P = require("parsimmon");
console.log(Date() + "\n");
const _ = P.optWhitespace;
const tag = name => rest =>
def start() =
let body = document.body
let root = React.createElement(GameView)
let container = document.getElementById("react-container")
let thing = React.render(root, container)
in do
set(app, "root", thing);
body.addEventListener("click", focusClickhole);
tick();
end
# Good use of match
def compile(node) =
match node
case {type: "Add", a, b} =>
["+", compile(a), compile(b)]
case {type: "If", condition, result, alternative} =>
["if", compile(condition), compile(result), compile(alternative)]
# Bad use of match
def greet(person) =