|
| 1 | +# Monkey |
| 2 | + |
| 3 | +[](https://travis-ci.org/Dentrax/Monkey) |
| 4 | + |
| 5 | +**Yet another Monkey interpreter implementation in Rust, based on Thorsten Ball's [Writing An Interpreter In Go](https://interpreterbook.com/) book.** |
| 6 | + |
| 7 | +## Features |
| 8 | +* AST |
| 9 | +* Tokenizer |
| 10 | +* Parser |
| 11 | +* Lexer |
| 12 | +* Evaluator |
| 13 | +* Builtin Functions |
| 14 | +* Read–Eval–Print Loop |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Test |
| 19 | + |
| 20 | +```rust |
| 21 | +cargo test --all |
| 22 | +``` |
| 23 | + |
| 24 | +### Run |
| 25 | + |
| 26 | +```rust |
| 27 | +cargo run |
| 28 | +``` |
| 29 | + |
| 30 | +```bash |
| 31 | +Monkey Interpreter v0.1.0 |
| 32 | +>>> "Hello, " + "World!" |
| 33 | +Hello, World! |
| 34 | +>>> |
| 35 | +``` |
| 36 | + |
| 37 | +## Examples |
| 38 | + |
| 39 | +### Functions |
| 40 | + |
| 41 | +```rust |
| 42 | +[1, 2 * 2, 3 + 3] |
| 43 | +``` |
| 44 | + |
| 45 | + |
| 46 | +### Function Call |
| 47 | + |
| 48 | +```rust |
| 49 | +let identity = fn(x) { x; }; identity(7); //7 |
| 50 | +``` |
| 51 | + |
| 52 | +```rust |
| 53 | +let add = fn(a, b) { a + b }; |
| 54 | +let sub = fn(a, b) { a - b }; |
| 55 | +let applyFunc = fn(a, b, func) { |
| 56 | + func(a, b) |
| 57 | +}; |
| 58 | +applyFunc(10, 2, sub); //8 |
| 59 | +``` |
| 60 | + |
| 61 | +### If |
| 62 | + |
| 63 | + |
| 64 | +```rust |
| 65 | +if (x < y) { z } else { w } |
| 66 | +``` |
| 67 | + |
| 68 | +### Nested If |
| 69 | + |
| 70 | +```rust |
| 71 | +if (2 > 1) { if (3 > 1) { return 7; } return 0; } |
| 72 | +``` |
| 73 | + |
| 74 | +### HashMap |
| 75 | + |
| 76 | +```rust |
| 77 | +{"one": 0 + 1, "two": 10 - 8, "ten": 50 / 5} |
| 78 | +``` |
| 79 | + |
| 80 | +### Arrays |
| 81 | + |
| 82 | +```rust |
| 83 | +let arr = [1, 2 * 2, 3 + 3] //arr[1 + 1] => returns 6 |
| 84 | +``` |
| 85 | + |
| 86 | +### Operator Precedence |
| 87 | + |
| 88 | +```rust |
| 89 | +3 + 4 * 5 == 3 * 1 + 4 * 5 //((3 + (4 * 5)) == ((3 * 1) + (4 * 5))) |
| 90 | +``` |
| 91 | + |
| 92 | +### Error Handlers |
| 93 | + |
| 94 | +```rust |
| 95 | +if (10 > 1) { if (10 > 1) { return true + false; } return 1; } //unknown operator: BOOLEAN + BOOLEAN |
| 96 | +``` |
| 97 | + |
| 98 | +```rust |
| 99 | +let newAdder = fn(x) { fn(y) { x + y } }; let addTwo = newAdder(2); x //identifier not found: x |
| 100 | +``` |
| 101 | + |
| 102 | +## Complete Example |
| 103 | + |
| 104 | +### Builtin |
| 105 | + |
| 106 | +```rust |
| 107 | +let map = fn(arr, f) { |
| 108 | + let iter = fn(arr, accumulated) { |
| 109 | + if (len(arr) == 0) { |
| 110 | + accumulated |
| 111 | + } else { |
| 112 | + iter(rest(arr), push(accumulated, f(first(arr)))); |
| 113 | + } |
| 114 | + }; |
| 115 | + iter(arr, []); |
| 116 | +}; |
| 117 | +let a = [1, 2, 3, 4]; |
| 118 | +let double = fn(x) { x * 2 }; |
| 119 | +map(a, double); |
| 120 | +``` |
| 121 | + |
| 122 | +### HashMap |
| 123 | + |
| 124 | +```rust |
| 125 | +let people = [{"name": "Alice", "age": 24}, {"name": "Anna", "age": 28}]; |
| 126 | +let getAge = fn(person) { person["name"]; }; |
| 127 | +return getAge(people[0]) + getAge(people[1]); |
| 128 | +``` |
| 129 | + |
| 130 | +## TODO |
| 131 | +- [ ] Impl Compiler |
| 132 | +- [ ] Impl VM |
| 133 | + |
| 134 | +## License |
| 135 | + |
| 136 | +MIT License ([LICENSE](LICENSE)). |
0 commit comments