Skip to content

Commit 7fab23d

Browse files
committed
add more information in docs
1 parent f7d57c3 commit 7fab23d

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

README.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,68 @@ git clone https://github.com/juandspy/monkey-lang.git
1111
go run main.go
1212
```
1313

14-
and start writing Monkey lang code.
14+
and start writing Monkey lang code. The results of the statements will be printed in the stdout. For example:
15+
16+
```
17+
>> 3 * 7
18+
21
19+
>> let x = 3 * 7
20+
>> x
21+
21
22+
```
23+
24+
Note that some statements like variable bindings don't print anything in the stdout.
1525

1626
## Language specs
1727

28+
### Types
29+
30+
There are 5 types supported:
31+
32+
- Booleans: `true` or `false`
33+
- Integers: `1`, `-1`, `12345`...
34+
- Strings: `"Hello World"`
35+
- Arrays: `[1, 2, 3]`. You can access a given position of an array by using indexes: `[1, 2, 3][1]` or `myArray[1]`.
36+
- Hashes: `{"a": 1, 5: "test", true: "bool"}`
37+
1838
### Operators
1939

20-
- Bang (!): it takes any input and returns the opposite. For example `!true = false` and `!5 = false`, as `5` acts as "truthy". However, `!!5` would be `true` as it's the same as `!false`.
40+
#### Prefix expressions
41+
42+
- Bang (`!`): it takes any input and returns the opposite. For example `!true = false` and `!5 = false`, as `5` acts as "truthy". However, `!!5` would be `true` as it's the same as `!false`.
43+
- Minus (`-`): changes the sign of an integer e.g. `-5`.
44+
45+
#### Infix expressions
46+
47+
- Arithmetic expressions: `(1 + 2) * 3 / 4`
48+
- Comparisons: `3 != 2`
49+
- Conditionals: `if (3 == 3) {"equals"} else {"not equals"}`
50+
51+
### Variables
52+
53+
You can define a variable by using `let` statements, e.g. `let x = 3`. You can also bind expressions: `let x = 3 * 7`.
54+
55+
#### Functions
56+
57+
You can bind functions to variables using the `let` statement:
58+
59+
```
60+
let sum = fn(x, y) {return x + y}; sum(1, 2)
61+
```
62+
63+
You can also build recursive functions:
64+
```
65+
>> let fib = fn(n) { if (n < 2) { return n; } else {return fib(n-1) + fib(n-2); } }
66+
>> fib(20)
67+
6765
68+
```
69+
70+
#### Builtin functions
71+
72+
There is a set of builtin functions available which are defined in [builtins.go](evaluator/builtins.go):
73+
- `len`: returns the length of a string or array.
74+
- `first`: returns the first element of an array.
75+
- `last`: returns the last element of an array.
76+
- `rest`: returns all the elements except the first one.
77+
- `push`: appends an item to an array.
78+
- `puts`: output to stdout.

0 commit comments

Comments
 (0)