monkey
所属分类:编程语言基础
开发工具:GO
文件大小:0KB
下载次数:0
上传日期:2019-05-20 12:55:31
上 传 者:
sh-1993
说明: 一种玩具编程语言
(A Toy Programming Language)
文件列表:
.travis.yml (78, 2019-05-20)
Dockerfile (114, 2019-05-20)
LICENSE (1066, 2019-05-20)
Makefile (257, 2019-05-20)
ast/ (0, 2019-05-20)
ast/ast.go (8099, 2019-05-20)
ast/ast_test.go (611, 2019-05-20)
ast/modify.go (1959, 2019-05-20)
ast/modify_test.go (3296, 2019-05-20)
benchmark/ (0, 2019-05-20)
benchmark/fibonacci_test.go (2235, 2019-05-20)
code/ (0, 2019-05-20)
code/code.go (3671, 2019-05-20)
code/code_test.go (2740, 2019-05-20)
compiler/ (0, 2019-05-20)
compiler/compiler.go (9757, 2019-05-20)
compiler/compiler_test.go (20992, 2019-05-20)
compiler/symbol_table.go (1675, 2019-05-20)
compiler/symbol_table_test.go (6413, 2019-05-20)
docker-compose.yml (175, 2019-05-20)
docs/ (0, 2019-05-20)
docs/index.html (2486, 2019-05-20)
docs/playground.js (1368431, 2019-05-20)
docs/playground.js.map (59586, 2019-05-20)
docs/static/ (0, 2019-05-20)
docs/static/style.css (2414, 2019-05-20)
evaluator/ (0, 2019-05-20)
evaluator/builtins.go (401, 2019-05-20)
evaluator/evaluator.go (9824, 2019-05-20)
evaluator/evaluator_test.go (11767, 2019-05-20)
evaluator/macro_expansion.go (2467, 2019-05-20)
evaluator/macro_expansion_test.go (2723, 2019-05-20)
evaluator/quote_unquote.go (1380, 2019-05-20)
evaluator/quote_unquote_test.go (1899, 2019-05-20)
formatter/ (0, 2019-05-20)
formatter/formatter.go (3523, 2019-05-20)
formatter/formatter_test.go (2507, 2019-05-20)
go.mod (726, 2019-05-20)
... ...
# Writing an interpreter/compiler in Go
This is an implementation of the Monkey from Thorsten Ball's books
* [Writing An interpreter In Go](https://interpreterbook.com).
* [Writing A Compiler In Go](https://compilerbook.com/).
```
monkey > let hello = monkey
let hello = monkey;
```
## Syntax
As of now, these syntaxes are available.
* literal
* integer: "1" -> 1
* true: "true" -> true
* false: "false" -> false
* function: "fn(x) {return x}" -> fn(x) return x;
* statement
* let: "let x = 1" -> let x = 1;
* return: "return x" -> return x;
* expression
* if: "if(x) {return x}" -> if x return x;
* if-else: "if(x) {return x} else{return 0}" -> if x return x; else return 0;
* call: "add(1,2)" -> add(1, 2)
* operator
* "!": "!x" -> (!x)
* "+": "x + y" -> (x + y)
* "-": "x - y" -> (x - y)
* "*": "x * y" -> (x * y)
* "/": "x / y" -> (x / y)
* "==": "x == y" -> (x == y)
* "!=": "x != y" -> (x != y)
* ">": "x > y" -> (x > y)
* "<": "x < y" -> (x < y)
## Examples
* Let statement
```typescript
let x = 200;
let y = 100;
x + y; // -> 300
```
* Return statement
```typescript
return 200;
let x = 100;
3 * x; // -> 200
```
* If-else expression
```typescript
if(1 > 0) {
return 100;
} else {
return 200;
} // -> 100
```
* Function literal & calling function
```typescript
let f = fn(x) {
return x * 100;
};
f(10); // -> 1000
```
* Closure
```typescript
let f = fn(x){
let z = 3 * x;
return fn(y){
z + y;
}
};
f(1)(2); // -> 5
```
* Recursive function
```typescript
let fib = fn(x){
if(x < 2) {
return x;
}
return fib(x-1) + fib(x-2);
};
fib(6); // -> 8
```
近期下载者:
相关文件:
收藏者: