This file contains hidden or 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
| #!/bin/env python3 | |
| # jouet.py - Toy JS interpreter | |
| import re | |
| from collections import namedtuple | |
| token_re = r'''(?x) | |
| (?: # whitespace | |
| [ \t\n] | |
| | //.* |
This file contains hidden or 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
| // 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; |
This file contains hidden or 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
| // 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>)>) | |
| } |
This file contains hidden or 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
| // 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> { |
This file contains hidden or 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
| // Simple shared-tail singly linked list in Rust | |
| use std::rc::Rc; | |
| pub enum List<T> { | |
| Nil, | |
| Cons(Rc<(T, List<T>)>) | |
| } | |
| fn main() { |
This file contains hidden or 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
| // 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)); | |
| } | |
| } |
This file contains hidden or 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
| // 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); |
This file contains hidden or 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
| // 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 | |
| } |
This file contains hidden or 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
| 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); |
This file contains hidden or 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
| 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 |