Skip to content

Instantly share code, notes, and snippets.

View jorendorff's full-sized avatar

Jason Orendorff jorendorff

View GitHub Profile
#!/bin/env python3
# jouet.py - Toy JS interpreter
import re
from collections import namedtuple
token_re = r'''(?x)
(?: # whitespace
[ \t\n]
| //.*
// A class template that can't be successfully instantiated.
template <typename X>
struct Foo {
X data;
Foo<X> badIdea; // error: incomplete type
};
struct T { int x; };
typedef Foo<T> FooT;
// push() method for a singly-linked list in Rust?!
use std::rc::Rc;
#[derive(Debug, PartialEq)]
enum List<T> {
Nil,
Cons(Rc<(T, List<T>)>)
}
// head() method for a singly-linked list in Rust
use std::rc::Rc;
enum List<T> {
Nil,
Cons(Rc<(T, List<T>)>)
}
impl<T> List<T> {
// Simple shared-tail singly linked list in Rust
use std::rc::Rc;
pub enum List<T> {
Nil,
Cons(Rc<(T, List<T>)>)
}
fn main() {
// transfering an object from a mutable binding to an immutable one
fn all_pairs(a: &[i32], b: &[i32]) -> Vec<(i32, i32)> {
// Build a vector. Note `result` is mutable throughout.
let mut result = Vec::new();
for &i in a {
for &j in b {
result.push((i, j));
}
}
@jorendorff
jorendorff / mut.rs
Last active November 25, 2015 11:44
// mut variables are mutable
fn main() {
let mut x = 1;
x += 1;
let mut nums = vec![1, 2, 3];
nums.push(4);
println!("x is {:?} and nums is {:?}", x, nums);
// variables are immutable by default
fn main() {
let x = 1;
x = 2; // error: re-assignment of immutable variable `x`
let nums = vec![1, 2, 3];
nums.push(4); // error: cannot borrow immutable local variable `nums` as mutable
}
var line0 = Error().lineNumber;
try { // line0 + 1
switch (3) { // line0 + 2
case 1: // line0 + 3
throw new Error("wat"); // line0 + 4
case BAD: // line0 + 5
throw new Error("fail");
}
} catch (e) {
print((e.lineNumber - line0) + ":" + e);
type Name = String
data Pattern = Nil -- matches the language {""}
| Lit Char -- Lit 'x' matches the language {"x"}
| Seq Exp Exp -- Seq a b matches {aw ++ bw | aw <- language a, bw <- language b}
| Alt Exp Exp -- Alt a b matches the union of language a and language b
| Ref Name -- matches exactly the strings matched by the Pattern with the given Name in the current environment
-- Return true if the given Pattern can match the empty string in the given environment.
isEmpty :: Map Name Pattern -> Pattern -> Bool